blob: a3862a6fb9d42fa78799a8d35278bbcf23b77daf [file] [log] [blame]
Adam Barth3d359102015-01-08 11:25:50 -08001// 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 Rudominerbaee08c2015-07-21 11:12:25 -07006#include "mojo/common/binding_set.h"
Adam Barth3d359102015-01-08 11:25:50 -08007#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 Luu84765c42015-10-10 01:07:51 -070012#include "mojo/services/icu_data/interfaces/icu_data.mojom.h"
Benjamin Lermandc3d1932015-03-09 11:17:39 +010013#include "services/icu_data/kICUData.h"
Adam Barth3d359102015-01-08 11:25:50 -080014
15namespace icu_data {
16
17class 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 Lermandc3d1932015-03-09 11:17:39 +010040 if (std::string(sha1hash) != std::string(kICUData.hash)) {
41 LOG(WARNING) << "Failed to match sha1sum. Expected " << kICUData.hash;
Adam Barth3d359102015-01-08 11:25:50 -080042 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 Lermandc3d1932015-03-09 11:17:39 +010056 buffer_.reset(new mojo::SharedBuffer(kICUData.size));
Adam Barth3d359102015-01-08 11:25:50 -080057 void* ptr = nullptr;
Benjamin Lermandc3d1932015-03-09 11:17:39 +010058 MojoResult rv = mojo::MapBuffer(buffer_->handle.get(), 0, kICUData.size,
Adam Barth3d359102015-01-08 11:25:50 -080059 &ptr, MOJO_MAP_BUFFER_FLAG_NONE);
60 CHECK_EQ(rv, MOJO_RESULT_OK);
Benjamin Lermandc3d1932015-03-09 11:17:39 +010061 memcpy(ptr, kICUData.data, kICUData.size);
Adam Barth3d359102015-01-08 11:25:50 -080062 rv = mojo::UnmapBuffer(ptr);
63 CHECK_EQ(rv, MOJO_RESULT_OK);
64 }
65
66 scoped_ptr<mojo::SharedBuffer> buffer_;
Mitch Rudominerbaee08c2015-07-21 11:12:25 -070067 mojo::BindingSet<ICUData> bindings_;
Adam Barth3d359102015-01-08 11:25:50 -080068};
69}
70
Mitch Rudominer963aa772015-04-03 08:22:46 -070071MojoResult MojoMain(MojoHandle application_request) {
Adam Barth3d359102015-01-08 11:25:50 -080072 mojo::ApplicationRunnerChromium runner(new icu_data::ICUDataImpl);
Mitch Rudominer963aa772015-04-03 08:22:46 -070073 return runner.Run(application_request);
Adam Barth3d359102015-01-08 11:25:50 -080074}