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 <cstdlib> | 5 #include <cstdlib> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "examples/indirect_service/indirect_service_demo.mojom.h" | 11 #include "examples/indirect_service/indirect_service_demo.mojom.h" |
12 #include "mojo/application/application_runner_chromium.h" | |
13 #include "mojo/message_pump/message_pump_mojo.h" | 12 #include "mojo/message_pump/message_pump_mojo.h" |
14 #include "mojo/public/c/system/main.h" | 13 #include "mojo/public/c/system/main.h" |
15 #include "mojo/public/cpp/application/application_delegate.h" | 14 #include "mojo/public/cpp/application/application_impl_base.h" |
16 #include "mojo/public/cpp/application/application_impl.h" | |
17 #include "mojo/public/cpp/application/connect.h" | 15 #include "mojo/public/cpp/application/connect.h" |
| 16 #include "mojo/public/cpp/application/run_application.h" |
18 | 17 |
19 namespace mojo { | 18 namespace mojo { |
20 namespace examples { | 19 namespace examples { |
21 | 20 |
22 class DemoTask; | 21 class DemoTask; |
23 | 22 |
24 typedef base::Callback<void(DemoTask*, const std::vector<int32_t>&)> | 23 typedef base::Callback<void(DemoTask*, const std::vector<int32_t>&)> |
25 DemoTaskFinishedCallback; | 24 DemoTaskFinishedCallback; |
26 | 25 |
27 // A thread that connects to the IndirectIntegerService, gets a connection | 26 // A thread that connects to the IndirectIntegerService, gets a connection |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 // Increment() method kTaskIterationCount times, collect the results in | 83 // Increment() method kTaskIterationCount times, collect the results in |
85 // a vector and return them to FinishDemoTask. | 84 // a vector and return them to FinishDemoTask. |
86 // | 85 // |
87 // The IntegerService, whose value is initially 0, will be called a total of | 86 // The IntegerService, whose value is initially 0, will be called a total of |
88 // N = |kTaskCount * kTaskIterationCount| times. Each DemoTask's results | 87 // N = |kTaskCount * kTaskIterationCount| times. Each DemoTask's results |
89 // are displayed in array of length N. Digits appear in positions that | 88 // are displayed in array of length N. Digits appear in positions that |
90 // correspond to the results obtained by the DemoTask thread. The results | 89 // correspond to the results obtained by the DemoTask thread. The results |
91 // show that the DemoTask threads are accessing the Integer in parallel. | 90 // show that the DemoTask threads are accessing the Integer in parallel. |
92 // The fact that only one digit appears in each column shows that things | 91 // The fact that only one digit appears in each column shows that things |
93 // are working correctly. | 92 // are working correctly. |
94 class IndirectServiceDemoAppDelegate : public ApplicationDelegate { | 93 class IndirectServiceDemoApp : public ApplicationImplBase { |
95 public: | 94 public: |
96 void Initialize(ApplicationImpl* app) override { | 95 void OnInitialize() override { |
97 IntegerServicePtr indirect_service_delegate; | 96 IntegerServicePtr indirect_service_delegate; |
98 ConnectToService(app->shell(), "mojo:indirect_integer_service", | 97 ConnectToService(shell(), "mojo:indirect_integer_service", |
99 GetProxy(&indirect_integer_service_)); | 98 GetProxy(&indirect_integer_service_)); |
100 ConnectToService(app->shell(), "mojo:integer_service", | 99 ConnectToService(shell(), "mojo:integer_service", |
101 GetProxy(&indirect_service_delegate)); | 100 GetProxy(&indirect_service_delegate)); |
102 indirect_integer_service_->Set( | 101 indirect_integer_service_->Set( |
103 indirect_service_delegate.PassInterfaceHandle()); | 102 indirect_service_delegate.PassInterfaceHandle()); |
104 | 103 |
105 for (unsigned i = 0; i < kTaskCount; i++) { | 104 for (unsigned i = 0; i < kTaskCount; i++) { |
106 IntegerServicePtr integer_service; | 105 IntegerServicePtr integer_service; |
107 indirect_integer_service_->Get(GetProxy(&integer_service)); | 106 indirect_integer_service_->Get(GetProxy(&integer_service)); |
108 DemoTaskFinishedCallback finished_callback = base::Bind( | 107 DemoTaskFinishedCallback finished_callback = base::Bind( |
109 &IndirectServiceDemoAppDelegate::FinishDemoTask, | 108 &IndirectServiceDemoApp::FinishDemoTask, base::Unretained(this), |
110 base::Unretained(this), | |
111 base::Unretained(base::MessageLoop::current())); | 109 base::Unretained(base::MessageLoop::current())); |
112 // We're passing the integer_service_ proxy to another thread, so | 110 // We're passing the integer_service_ proxy to another thread, so |
113 // use its MessagePipe. | 111 // use its MessagePipe. |
114 tasks_.push_back( | 112 tasks_.push_back( |
115 new DemoTask(integer_service.PassInterfaceHandle().PassHandle(), | 113 new DemoTask(integer_service.PassInterfaceHandle().PassHandle(), |
116 finished_callback, kTaskIterationCount)); | 114 finished_callback, kTaskIterationCount)); |
117 } | 115 } |
118 } | 116 } |
119 | 117 |
120 private: | 118 private: |
121 static const unsigned kTaskCount = 10; | 119 static const unsigned kTaskCount = 10; |
122 static const unsigned kTaskIterationCount = 6; | 120 static const unsigned kTaskIterationCount = 6; |
123 | 121 |
124 // This method is called on a DemoTask thread. It just calls DoFinishDemoTask | 122 // This method is called on a DemoTask thread. It just calls DoFinishDemoTask |
125 // on the application's run loop. Doing so serializes the DoFinishDemoTask | 123 // on the application's run loop. Doing so serializes the DoFinishDemoTask |
126 // calls. | 124 // calls. |
127 void FinishDemoTask(base::MessageLoop *run_loop, | 125 void FinishDemoTask(base::MessageLoop *run_loop, |
128 DemoTask* task, | 126 DemoTask* task, |
129 const std::vector<int32_t>& results) { | 127 const std::vector<int32_t>& results) { |
130 run_loop->PostTask(FROM_HERE, base::Bind( | 128 run_loop->PostTask( |
131 &IndirectServiceDemoAppDelegate::DoFinishDemoTask, | 129 FROM_HERE, |
132 base::Unretained(this), | 130 base::Bind(&IndirectServiceDemoApp::DoFinishDemoTask, |
133 base::Unretained(task), | 131 base::Unretained(this), base::Unretained(task), results)); |
134 results)); | |
135 } | 132 } |
136 | 133 |
137 void DoFinishDemoTask(DemoTask* task, const std::vector<int32_t>& results) { | 134 void DoFinishDemoTask(DemoTask* task, const std::vector<int32_t>& results) { |
138 std::string display(kTaskCount * kTaskIterationCount, ' '); | 135 std::string display(kTaskCount * kTaskIterationCount, ' '); |
139 for (unsigned i = 0; i < results.size(); i++) | 136 for (unsigned i = 0; i < results.size(); i++) |
140 display[results[i]] = '0' + (results[i] % 10); | 137 display[results[i]] = '0' + (results[i] % 10); |
141 printf("DemoTask Thread [%s]\n", display.c_str()); | 138 printf("DemoTask Thread [%s]\n", display.c_str()); |
142 tasks_.erase(std::remove(tasks_.begin(), tasks_.end(), task), tasks_.end()); | 139 tasks_.erase(std::remove(tasks_.begin(), tasks_.end(), task), tasks_.end()); |
143 delete task; // Stop the DemoTask's thread etc. | 140 delete task; // Stop the DemoTask's thread etc. |
144 if (tasks_.empty()) | 141 if (tasks_.empty()) |
145 ApplicationImpl::Terminate(); | 142 TerminateMainApplication(MOJO_RESULT_OK); |
146 } | 143 } |
147 | 144 |
148 IndirectIntegerServicePtr indirect_integer_service_; | 145 IndirectIntegerServicePtr indirect_integer_service_; |
149 std::vector<DemoTask*> tasks_; | 146 std::vector<DemoTask*> tasks_; |
150 }; | 147 }; |
151 | 148 |
152 | |
153 } // namespace examples | 149 } // namespace examples |
154 } // namespace mojo | 150 } // namespace mojo |
155 | 151 |
156 MojoResult MojoMain(MojoHandle application_request) { | 152 MojoResult MojoMain(MojoHandle application_request) { |
157 mojo::ApplicationRunnerChromium runner( | 153 mojo::examples::IndirectServiceDemoApp indirect_service_demo_app; |
158 new mojo::examples::IndirectServiceDemoAppDelegate); | 154 return mojo::RunMainApplication(application_request, |
159 return runner.Run(application_request); | 155 &indirect_service_demo_app); |
160 } | 156 } |
OLD | NEW |