| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/shell/background/background_shell.h" | 5 #include "mojo/shell/background/background_shell.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/threading/simple_thread.h" | 13 #include "base/threading/simple_thread.h" |
| 14 #include "mojo/message_pump/message_pump_mojo.h" | 14 #include "mojo/message_pump/message_pump_mojo.h" |
| 15 #include "mojo/services/package_manager/package_manager.h" | 15 #include "mojo/services/package_manager/package_manager.h" |
| 16 #include "mojo/shell/application_loader.h" | 16 #include "mojo/shell/application_loader.h" |
| 17 #include "mojo/shell/application_manager.h" | 17 #include "mojo/shell/application_manager.h" |
| 18 #include "mojo/shell/capability_filter.h" | 18 #include "mojo/shell/capability_filter.h" |
| 19 #include "mojo/shell/connect_params.h" | 19 #include "mojo/shell/connect_params.h" |
| 20 #include "mojo/shell/public/cpp/shell_client.h" | 20 #include "mojo/shell/public/cpp/shell_client.h" |
| 21 #include "mojo/shell/public/cpp/shell_connection.h" | 21 #include "mojo/shell/public/cpp/shell_connection.h" |
| 22 #include "mojo/shell/standalone/context.h" | 22 #include "mojo/shell/standalone/context.h" |
| 23 #include "url/gurl.h" | |
| 24 | 23 |
| 25 namespace mojo { | 24 namespace mojo { |
| 26 namespace shell { | 25 namespace shell { |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 scoped_ptr<base::MessagePump> CreateMessagePumpMojo() { | 28 scoped_ptr<base::MessagePump> CreateMessagePumpMojo() { |
| 30 return make_scoped_ptr(new common::MessagePumpMojo); | 29 return make_scoped_ptr(new common::MessagePumpMojo); |
| 31 } | 30 } |
| 32 | 31 |
| 33 // Used to obtain the ShellClientRequest for an application. When | 32 // Used to obtain the ShellClientRequest for an application. When |
| 34 // ApplicationLoader::Load() is called a callback is run with the | 33 // ApplicationLoader::Load() is called a callback is run with the |
| 35 // ShellClientRequest. | 34 // ShellClientRequest. |
| 36 class BackgroundApplicationLoader : public ApplicationLoader { | 35 class BackgroundApplicationLoader : public ApplicationLoader { |
| 37 public: | 36 public: |
| 38 using Callback = base::Callback<void(mojom::ShellClientRequest)>; | 37 using Callback = base::Callback<void(mojom::ShellClientRequest)>; |
| 39 | 38 |
| 40 explicit BackgroundApplicationLoader(const Callback& callback) | 39 explicit BackgroundApplicationLoader(const Callback& callback) |
| 41 : callback_(callback) {} | 40 : callback_(callback) {} |
| 42 ~BackgroundApplicationLoader() override {} | 41 ~BackgroundApplicationLoader() override {} |
| 43 | 42 |
| 44 // ApplicationLoader: | 43 // ApplicationLoader: |
| 45 void Load(const GURL& url, mojom::ShellClientRequest request) override { | 44 void Load(const std::string& name, |
| 45 mojom::ShellClientRequest request) override { |
| 46 DCHECK(!callback_.is_null()); // Callback should only be run once. | 46 DCHECK(!callback_.is_null()); // Callback should only be run once. |
| 47 Callback callback = callback_; | 47 Callback callback = callback_; |
| 48 callback_.Reset(); | 48 callback_.Reset(); |
| 49 callback.Run(std::move(request)); | 49 callback.Run(std::move(request)); |
| 50 } | 50 } |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 Callback callback_; | 53 Callback callback_; |
| 54 | 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(BackgroundApplicationLoader); | 55 DISALLOW_COPY_AND_ASSIGN(BackgroundApplicationLoader); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 78 init_params_(std::move(init_params)) {} | 78 init_params_(std::move(init_params)) {} |
| 79 ~MojoThread() override {} | 79 ~MojoThread() override {} |
| 80 | 80 |
| 81 void CreateShellClientRequest(base::WaitableEvent* signal, | 81 void CreateShellClientRequest(base::WaitableEvent* signal, |
| 82 scoped_ptr<ConnectParams> params, | 82 scoped_ptr<ConnectParams> params, |
| 83 mojom::ShellClientRequest* request) { | 83 mojom::ShellClientRequest* request) { |
| 84 // Only valid to call this on the background thread. | 84 // Only valid to call this on the background thread. |
| 85 DCHECK_EQ(message_loop_, base::MessageLoop::current()); | 85 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 86 | 86 |
| 87 // Ownership of |loader| passes to ApplicationManager. | 87 // Ownership of |loader| passes to ApplicationManager. |
| 88 const GURL url = params->target().url(); | 88 const std::string name = params->target().name(); |
| 89 BackgroundApplicationLoader* loader = new BackgroundApplicationLoader( | 89 BackgroundApplicationLoader* loader = new BackgroundApplicationLoader( |
| 90 base::Bind(&MojoThread::OnGotApplicationRequest, base::Unretained(this), | 90 base::Bind(&MojoThread::OnGotApplicationRequest, base::Unretained(this), |
| 91 url, signal, request)); | 91 name, signal, request)); |
| 92 context_->application_manager()->SetLoaderForURL(make_scoped_ptr(loader), | 92 context_->application_manager()->SetLoaderForName(make_scoped_ptr(loader), |
| 93 url); | 93 name); |
| 94 context_->application_manager()->Connect(std::move(params)); | 94 context_->application_manager()->Connect(std::move(params)); |
| 95 // The request is asynchronously processed. When processed | 95 // The request is asynchronously processed. When processed |
| 96 // OnGotApplicationRequest() is called and we'll signal |signal|. | 96 // OnGotApplicationRequest() is called and we'll signal |signal|. |
| 97 } | 97 } |
| 98 | 98 |
| 99 base::MessageLoop* message_loop() { return message_loop_; } | 99 base::MessageLoop* message_loop() { return message_loop_; } |
| 100 | 100 |
| 101 // Stops the background thread. | 101 // Stops the background thread. |
| 102 void Stop() { | 102 void Stop() { |
| 103 DCHECK_NE(message_loop_, base::MessageLoop::current()); | 103 DCHECK_NE(message_loop_, base::MessageLoop::current()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 136 |
| 137 // Has to happen after run, but while messageloop still valid. | 137 // Has to happen after run, but while messageloop still valid. |
| 138 context_->Shutdown(); | 138 context_->Shutdown(); |
| 139 | 139 |
| 140 // Context has to be destroyed after the MessageLoop has been destroyed. | 140 // Context has to be destroyed after the MessageLoop has been destroyed. |
| 141 message_loop.reset(); | 141 message_loop.reset(); |
| 142 context_ = nullptr; | 142 context_ = nullptr; |
| 143 } | 143 } |
| 144 | 144 |
| 145 private: | 145 private: |
| 146 void OnGotApplicationRequest(const GURL& url, | 146 void OnGotApplicationRequest(const std::string& name, |
| 147 base::WaitableEvent* signal, | 147 base::WaitableEvent* signal, |
| 148 mojom::ShellClientRequest* request_result, | 148 mojom::ShellClientRequest* request_result, |
| 149 mojom::ShellClientRequest actual_request) { | 149 mojom::ShellClientRequest actual_request) { |
| 150 *request_result = std::move(actual_request); | 150 *request_result = std::move(actual_request); |
| 151 // Trigger destruction of the loader. | 151 // Trigger destruction of the loader. |
| 152 context_->application_manager()->SetLoaderForURL(nullptr, url); | 152 context_->application_manager()->SetLoaderForName(nullptr, name); |
| 153 signal->Signal(); | 153 signal->Signal(); |
| 154 } | 154 } |
| 155 | 155 |
| 156 // We own this. It's created on the main thread, but destroyed on the | 156 // We own this. It's created on the main thread, but destroyed on the |
| 157 // background thread. | 157 // background thread. |
| 158 MojoMessageLoop* message_loop_ = nullptr; | 158 MojoMessageLoop* message_loop_ = nullptr; |
| 159 // Created in Run() on the background thread. | 159 // Created in Run() on the background thread. |
| 160 Context* context_ = nullptr; | 160 Context* context_ = nullptr; |
| 161 | 161 |
| 162 scoped_ptr<BackgroundShell::InitParams> init_params_; | 162 scoped_ptr<BackgroundShell::InitParams> init_params_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 173 thread_->Stop(); | 173 thread_->Stop(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 void BackgroundShell::Init(scoped_ptr<InitParams> init_params) { | 176 void BackgroundShell::Init(scoped_ptr<InitParams> init_params) { |
| 177 DCHECK(!thread_); | 177 DCHECK(!thread_); |
| 178 thread_.reset(new MojoThread(std::move(init_params))); | 178 thread_.reset(new MojoThread(std::move(init_params))); |
| 179 thread_->Start(); | 179 thread_->Start(); |
| 180 } | 180 } |
| 181 | 181 |
| 182 mojom::ShellClientRequest BackgroundShell::CreateShellClientRequest( | 182 mojom::ShellClientRequest BackgroundShell::CreateShellClientRequest( |
| 183 const GURL& url) { | 183 const std::string& name) { |
| 184 scoped_ptr<ConnectParams> params(new ConnectParams); | 184 scoped_ptr<ConnectParams> params(new ConnectParams); |
| 185 params->set_target(Identity(url, std::string(), mojom::Connector::kUserRoot)); | 185 params->set_target(Identity(name, std::string(), |
| 186 mojom::Connector::kUserRoot)); |
| 186 mojom::ShellClientRequest request; | 187 mojom::ShellClientRequest request; |
| 187 base::WaitableEvent signal(true, false); | 188 base::WaitableEvent signal(true, false); |
| 188 thread_->message_loop()->task_runner()->PostTask( | 189 thread_->message_loop()->task_runner()->PostTask( |
| 189 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest, | 190 FROM_HERE, base::Bind(&MojoThread::CreateShellClientRequest, |
| 190 base::Unretained(thread_.get()), &signal, | 191 base::Unretained(thread_.get()), &signal, |
| 191 base::Passed(¶ms), &request)); | 192 base::Passed(¶ms), &request)); |
| 192 signal.Wait(); | 193 signal.Wait(); |
| 193 return request; | 194 return request; |
| 194 } | 195 } |
| 195 | 196 |
| 196 } // namespace shell | 197 } // namespace shell |
| 197 } // namespace mojo | 198 } // namespace mojo |
| OLD | NEW |