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 "services/shell/public/cpp/application_runner.h" | |
19 #include "services/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 std::unique_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(shell::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 shell::Connector* connector_; | |
82 | |
83 DISALLOW_COPY_AND_ASSIGN(Test); | |
84 }; | |
85 | |
86 class TestApp : public shell::ShellClient, | |
87 public shell::InterfaceFactory<mojom::Test> { | |
88 public: | |
89 TestApp() {} | |
90 ~TestApp() override {} | |
91 | |
92 // shell::ShellClient: | |
93 void Initialize(shell::Connector* connector, | |
94 const shell::Identity& identity, | |
95 uint32_t id) override { | |
96 test_.reset(new Test(connector)); | |
97 } | |
98 bool AcceptConnection(shell::Connection* connection) override { | |
99 connection->AddInterface<mojom::Test>(this); | |
100 return true; | |
101 } | |
102 | |
103 // InterfaceFactory<mojom::Test>: | |
104 void Create(shell::Connection* connection, | |
105 mojom::TestRequest request) override { | |
106 printf("test app create\n"); | |
107 bindings_.AddBinding(test_.get(), std::move(request)); | |
108 } | |
109 | |
110 private: | |
111 std::unique_ptr<Test> test_; | |
112 mojo::BindingSet<test::mojom::Test> bindings_; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(TestApp); | |
115 }; | |
116 | |
117 } // namespace test | |
118 } // namespace resource_provider | |
119 | |
120 MojoResult MojoMain(MojoHandle shell_handle) { | |
121 return shell::ApplicationRunner(new resource_provider::test::TestApp) | |
122 .Run(shell_handle); | |
123 } | |
OLD | NEW |