Adam Barth | 3d35910 | 2015-01-08 11:25:50 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "mojo/application/application_runner_chromium.h" |
Mitch Rudominer | baee08c | 2015-07-21 11:12:25 -0700 | [diff] [blame] | 6 | #include "mojo/common/binding_set.h" |
Adam Barth | 3d35910 | 2015-01-08 11:25:50 -0800 | [diff] [blame] | 7 | #include "mojo/public/c/system/main.h" |
| 8 | #include "mojo/public/cpp/application/application_connection.h" |
| 9 | #include "mojo/public/cpp/application/application_delegate.h" |
| 10 | #include "mojo/public/cpp/application/interface_factory.h" |
| 11 | #include "mojo/public/cpp/bindings/interface_ptr.h" |
Viet-Trung Luu | 84765c4 | 2015-10-10 01:07:51 -0700 | [diff] [blame^] | 12 | #include "mojo/services/icu_data/interfaces/icu_data.mojom.h" |
Benjamin Lerman | dc3d193 | 2015-03-09 11:17:39 +0100 | [diff] [blame] | 13 | #include "services/icu_data/kICUData.h" |
Adam Barth | 3d35910 | 2015-01-08 11:25:50 -0800 | [diff] [blame] | 14 | |
| 15 | namespace icu_data { |
| 16 | |
| 17 | class ICUDataImpl : public mojo::ApplicationDelegate, |
| 18 | public ICUData, |
| 19 | public mojo::InterfaceFactory<ICUData> { |
| 20 | public: |
| 21 | ICUDataImpl() {} |
| 22 | ~ICUDataImpl() override {} |
| 23 | |
| 24 | // mojo::ApplicationDelegate implementation. |
| 25 | bool ConfigureIncomingConnection( |
| 26 | mojo::ApplicationConnection* connection) override { |
| 27 | connection->AddService(this); |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | // mojo::InterfaceFactory<mojo::ICUData> implementation. |
| 32 | void Create(mojo::ApplicationConnection* connection, |
| 33 | mojo::InterfaceRequest<ICUData> request) override { |
| 34 | bindings_.AddBinding(this, request.Pass()); |
| 35 | } |
| 36 | |
| 37 | void Map(const mojo::String& sha1hash, |
| 38 | const mojo::Callback<void(mojo::ScopedSharedBufferHandle)>& callback) |
| 39 | override { |
Benjamin Lerman | dc3d193 | 2015-03-09 11:17:39 +0100 | [diff] [blame] | 40 | if (std::string(sha1hash) != std::string(kICUData.hash)) { |
| 41 | LOG(WARNING) << "Failed to match sha1sum. Expected " << kICUData.hash; |
Adam Barth | 3d35910 | 2015-01-08 11:25:50 -0800 | [diff] [blame] | 42 | callback.Run(mojo::ScopedSharedBufferHandle()); |
| 43 | return; |
| 44 | } |
| 45 | EnsureBuffer(); |
| 46 | mojo::ScopedSharedBufferHandle handle; |
| 47 | // FIXME: We should create a read-only duplicate of the handle. |
| 48 | mojo::DuplicateBuffer(buffer_->handle.get(), nullptr, &handle); |
| 49 | callback.Run(handle.Pass()); |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | void EnsureBuffer() { |
| 54 | if (buffer_) |
| 55 | return; |
Benjamin Lerman | dc3d193 | 2015-03-09 11:17:39 +0100 | [diff] [blame] | 56 | buffer_.reset(new mojo::SharedBuffer(kICUData.size)); |
Adam Barth | 3d35910 | 2015-01-08 11:25:50 -0800 | [diff] [blame] | 57 | void* ptr = nullptr; |
Benjamin Lerman | dc3d193 | 2015-03-09 11:17:39 +0100 | [diff] [blame] | 58 | MojoResult rv = mojo::MapBuffer(buffer_->handle.get(), 0, kICUData.size, |
Adam Barth | 3d35910 | 2015-01-08 11:25:50 -0800 | [diff] [blame] | 59 | &ptr, MOJO_MAP_BUFFER_FLAG_NONE); |
| 60 | CHECK_EQ(rv, MOJO_RESULT_OK); |
Benjamin Lerman | dc3d193 | 2015-03-09 11:17:39 +0100 | [diff] [blame] | 61 | memcpy(ptr, kICUData.data, kICUData.size); |
Adam Barth | 3d35910 | 2015-01-08 11:25:50 -0800 | [diff] [blame] | 62 | rv = mojo::UnmapBuffer(ptr); |
| 63 | CHECK_EQ(rv, MOJO_RESULT_OK); |
| 64 | } |
| 65 | |
| 66 | scoped_ptr<mojo::SharedBuffer> buffer_; |
Mitch Rudominer | baee08c | 2015-07-21 11:12:25 -0700 | [diff] [blame] | 67 | mojo::BindingSet<ICUData> bindings_; |
Adam Barth | 3d35910 | 2015-01-08 11:25:50 -0800 | [diff] [blame] | 68 | }; |
| 69 | } |
| 70 | |
Mitch Rudominer | 963aa77 | 2015-04-03 08:22:46 -0700 | [diff] [blame] | 71 | MojoResult MojoMain(MojoHandle application_request) { |
Adam Barth | 3d35910 | 2015-01-08 11:25:50 -0800 | [diff] [blame] | 72 | mojo::ApplicationRunnerChromium runner(new icu_data::ICUDataImpl); |
Mitch Rudominer | 963aa77 | 2015-04-03 08:22:46 -0700 | [diff] [blame] | 73 | return runner.Run(application_request); |
Adam Barth | 3d35910 | 2015-01-08 11:25:50 -0800 | [diff] [blame] | 74 | } |