OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/application/application_runner_chromium.h" | 5 #include "services/icu_data/icu_data_impl.h" |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
6 #include "mojo/common/binding_set.h" | 8 #include "mojo/common/binding_set.h" |
7 #include "mojo/public/c/system/main.h" | |
8 #include "mojo/public/cpp/application/application_delegate.h" | 9 #include "mojo/public/cpp/application/application_delegate.h" |
9 #include "mojo/public/cpp/application/service_provider_impl.h" | 10 #include "mojo/public/cpp/application/service_provider_impl.h" |
10 #include "mojo/public/cpp/bindings/interface_ptr.h" | 11 #include "mojo/public/cpp/bindings/interface_ptr.h" |
11 #include "mojo/services/icu_data/interfaces/icu_data.mojom.h" | 12 #include "mojo/services/icu_data/interfaces/icu_data.mojom.h" |
12 #include "services/icu_data/kICUData.h" | 13 #include "services/icu_data/kICUData.h" |
ppi
2016/05/19 15:58:37
nit: no need to repeat the includes present in the
nelly
2016/05/19 16:09:52
Done. Thanks!
| |
13 | 14 |
14 namespace icu_data { | 15 namespace icu_data { |
15 | 16 |
16 class ICUDataImpl : public mojo::ApplicationDelegate, public ICUData { | 17 ICUDataImpl::ICUDataImpl() {} |
17 public: | 18 ICUDataImpl::~ICUDataImpl() {} |
18 ICUDataImpl() {} | |
19 ~ICUDataImpl() override {} | |
20 | 19 |
21 // mojo::ApplicationDelegate implementation. | 20 bool ICUDataImpl::ConfigureIncomingConnection( |
22 bool ConfigureIncomingConnection( | 21 mojo::ServiceProviderImpl* service_provider_impl) { |
23 mojo::ServiceProviderImpl* service_provider_impl) override { | 22 service_provider_impl->AddService<ICUData>( |
24 service_provider_impl->AddService<ICUData>( | 23 [this](const mojo::ConnectionContext& connection_context, |
25 [this](const mojo::ConnectionContext& connection_context, | 24 mojo::InterfaceRequest<ICUData> icu_data_request) { |
26 mojo::InterfaceRequest<ICUData> icu_data_request) { | 25 bindings_.AddBinding(this, icu_data_request.Pass()); |
27 bindings_.AddBinding(this, icu_data_request.Pass()); | 26 }); |
28 }); | |
29 | 27 |
30 return true; | 28 return true; |
31 } | |
32 | |
33 void Map(const mojo::String& sha1hash, | |
34 const mojo::Callback<void(mojo::ScopedSharedBufferHandle)>& callback) | |
35 override { | |
36 if (std::string(sha1hash) != std::string(kICUData.hash)) { | |
37 LOG(WARNING) << "Failed to match sha1sum. Expected " << kICUData.hash; | |
38 callback.Run(mojo::ScopedSharedBufferHandle()); | |
39 return; | |
40 } | |
41 EnsureBuffer(); | |
42 mojo::ScopedSharedBufferHandle handle; | |
43 // FIXME: We should create a read-only duplicate of the handle. | |
44 mojo::DuplicateBuffer(buffer_->handle.get(), nullptr, &handle); | |
45 callback.Run(handle.Pass()); | |
46 } | |
47 | |
48 private: | |
49 void EnsureBuffer() { | |
50 if (buffer_) | |
51 return; | |
52 buffer_.reset(new mojo::SharedBuffer(kICUData.size)); | |
53 void* ptr = nullptr; | |
54 MojoResult rv = mojo::MapBuffer(buffer_->handle.get(), 0, kICUData.size, | |
55 &ptr, MOJO_MAP_BUFFER_FLAG_NONE); | |
56 CHECK_EQ(rv, MOJO_RESULT_OK); | |
57 memcpy(ptr, kICUData.data, kICUData.size); | |
58 rv = mojo::UnmapBuffer(ptr); | |
59 CHECK_EQ(rv, MOJO_RESULT_OK); | |
60 } | |
61 | |
62 scoped_ptr<mojo::SharedBuffer> buffer_; | |
63 mojo::BindingSet<ICUData> bindings_; | |
64 }; | |
65 } | 29 } |
66 | 30 |
67 MojoResult MojoMain(MojoHandle application_request) { | 31 void ICUDataImpl::Map( |
68 mojo::ApplicationRunnerChromium runner(new icu_data::ICUDataImpl); | 32 const mojo::String& sha1hash, |
69 return runner.Run(application_request); | 33 const mojo::Callback<void(mojo::ScopedSharedBufferHandle)>& callback) { |
34 if (std::string(sha1hash) != std::string(kICUData.hash)) { | |
35 LOG(WARNING) << "Failed to match sha1sum. Expected " << kICUData.hash; | |
36 callback.Run(mojo::ScopedSharedBufferHandle()); | |
37 return; | |
38 } | |
39 EnsureBuffer(); | |
40 mojo::ScopedSharedBufferHandle handle; | |
41 // FIXME: We should create a read-only duplicate of the handle. | |
42 mojo::DuplicateBuffer(buffer_->handle.get(), nullptr, &handle); | |
43 callback.Run(handle.Pass()); | |
70 } | 44 } |
45 | |
46 void ICUDataImpl::EnsureBuffer() { | |
47 if (buffer_) | |
48 return; | |
49 buffer_.reset(new mojo::SharedBuffer(kICUData.size)); | |
50 void* ptr = nullptr; | |
51 MojoResult rv = mojo::MapBuffer(buffer_->handle.get(), 0, kICUData.size, &ptr, | |
52 MOJO_MAP_BUFFER_FLAG_NONE); | |
53 CHECK_EQ(rv, MOJO_RESULT_OK); | |
54 memcpy(ptr, kICUData.data, kICUData.size); | |
55 rv = mojo::UnmapBuffer(ptr); | |
56 CHECK_EQ(rv, MOJO_RESULT_OK); | |
57 } | |
58 | |
59 } // namespace icu_data | |
OLD | NEW |