| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 <stddef.h> |
| 6 #include <stdint.h> |
| 7 |
| 8 #include "base/files/file.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "components/resource_provider/public/cpp/resource_loader.h" |
| 12 #include "components/resource_provider/public/interfaces/resource_provider.mojom
.h" |
| 13 #include "components/resource_provider/test.mojom.h" |
| 14 #include "mojo/platform_handle/platform_handle_functions.h" |
| 15 #include "mojo/public/c/system/main.h" |
| 16 #include "mojo/public/cpp/bindings/array.h" |
| 17 #include "mojo/public/cpp/bindings/binding_set.h" |
| 18 #include "mojo/shell/public/cpp/application_runner.h" |
| 19 #include "mojo/shell/public/cpp/shell_client.h" |
| 20 |
| 21 namespace resource_provider { |
| 22 namespace test { |
| 23 |
| 24 std::string ReadFile(base::File* file) { |
| 25 const size_t kBufferSize = 1 << 16; |
| 26 scoped_ptr<char[]> buffer(new char[kBufferSize]); |
| 27 const int read = file->ReadAtCurrentPos(buffer.get(), kBufferSize); |
| 28 if (read == -1) |
| 29 return std::string(); |
| 30 return std::string(buffer.get(), read); |
| 31 } |
| 32 |
| 33 std::set<std::string> SetWithString(const std::string& contents) { |
| 34 std::set<std::string> result; |
| 35 result.insert(contents); |
| 36 return result; |
| 37 } |
| 38 |
| 39 std::set<std::string> SetWithStrings(const std::string& contents1, |
| 40 const std::string& contents2) { |
| 41 std::set<std::string> result; |
| 42 result.insert(contents1); |
| 43 result.insert(contents2); |
| 44 return result; |
| 45 } |
| 46 |
| 47 class Test : public mojom::Test { |
| 48 public: |
| 49 explicit Test(mojo::Connector* connector) : connector_(connector) {} |
| 50 ~Test() override {} |
| 51 |
| 52 // mojom::Test: |
| 53 void GetResource1(const GetResource1Callback& callback) override { |
| 54 printf("GetResource1\n"); |
| 55 ResourceContentsMap results(GetResources(SetWithString("sample"))); |
| 56 printf("GetResource1 result %s\n", results["sample"].c_str()); |
| 57 callback.Run(results["sample"]); |
| 58 } |
| 59 void GetBothResources(const GetBothResourcesCallback& callback) override { |
| 60 ResourceContentsMap results( |
| 61 GetResources(SetWithStrings("sample", "dir/sample2"))); |
| 62 callback.Run(results["sample"], results["dir/sample2"]); |
| 63 } |
| 64 |
| 65 private: |
| 66 using ResourceContentsMap = std::map<std::string, std::string>; |
| 67 |
| 68 ResourceContentsMap GetResources(const std::set<std::string>& paths) { |
| 69 ResourceLoader loader(connector_, paths); |
| 70 loader.BlockUntilLoaded(); |
| 71 |
| 72 // Load the contents of each of the handles. |
| 73 ResourceContentsMap results; |
| 74 for (auto& path : paths) { |
| 75 base::File file(loader.ReleaseFile(path)); |
| 76 results[path] = ReadFile(&file); |
| 77 } |
| 78 return results; |
| 79 } |
| 80 |
| 81 mojo::Connector* connector_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(Test); |
| 84 }; |
| 85 |
| 86 class TestApp : public mojo::ShellClient, |
| 87 public mojo::InterfaceFactory<mojom::Test> { |
| 88 public: |
| 89 TestApp() {} |
| 90 ~TestApp() override {} |
| 91 |
| 92 // mojo::ShellClient: |
| 93 void Initialize(mojo::Connector* connector, |
| 94 const mojo::Identity& identity, |
| 95 uint32_t id) override { |
| 96 test_.reset(new Test(connector)); |
| 97 } |
| 98 bool AcceptConnection(mojo::Connection* connection) override { |
| 99 connection->AddInterface<mojom::Test>(this); |
| 100 return true; |
| 101 } |
| 102 void ShellConnectionLost() override { |
| 103 base::MessageLoop::current()->QuitWhenIdle(); |
| 104 } |
| 105 |
| 106 // InterfaceFactory<mojom::Test>: |
| 107 void Create(mojo::Connection* connection, |
| 108 mojom::TestRequest request) override { |
| 109 printf("test app create\n"); |
| 110 bindings_.AddBinding(test_.get(), std::move(request)); |
| 111 } |
| 112 |
| 113 private: |
| 114 scoped_ptr<Test> test_; |
| 115 mojo::BindingSet<test::mojom::Test> bindings_; |
| 116 |
| 117 DISALLOW_COPY_AND_ASSIGN(TestApp); |
| 118 }; |
| 119 |
| 120 } // namespace test |
| 121 } // namespace resource_provider |
| 122 |
| 123 MojoResult MojoMain(MojoHandle shell_handle) { |
| 124 return mojo::ApplicationRunner(new resource_provider::test::TestApp) |
| 125 .Run(shell_handle); |
| 126 } |
| OLD | NEW |