OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "mandoline/services/core_services/core_services_application_delegate.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/macros.h" | |
11 #include "base/single_thread_task_runner.h" | |
12 #include "base/threading/platform_thread.h" | |
13 #include "base/threading/simple_thread.h" | |
14 #include "components/clipboard/clipboard_application_delegate.h" | |
15 #include "components/filesystem/file_system_app.h" | |
16 #include "components/web_view/web_view_application_delegate.h" | |
17 #include "mandoline/services/core_services/application_delegate_factory.h" | |
18 #include "mojo/logging/init_logging.h" | |
19 #include "mojo/message_pump/message_pump_mojo.h" | |
20 #include "mojo/services/tracing/public/cpp/tracing_impl.h" | |
21 #include "mojo/services/tracing/tracing_app.h" | |
22 #include "mojo/shell/public/cpp/application_runner.h" | |
23 #include "mojo/shell/public/cpp/connection.h" | |
24 #include "url/gurl.h" | |
25 | |
26 namespace core_services { | |
27 | |
28 // A helper class for hosting a mojo::ApplicationImpl on its own thread. | |
29 class ApplicationThread : public base::SimpleThread { | |
30 public: | |
31 ApplicationThread( | |
32 const base::WeakPtr<CoreServicesApplicationDelegate> | |
33 core_services_application, | |
34 const std::string& url, | |
35 scoped_ptr<mojo::ShellClient> delegate, | |
36 mojo::InterfaceRequest<mojo::shell::mojom::Application> request, | |
37 const mojo::Callback<void()>& destruct_callback) | |
38 : base::SimpleThread(url), | |
39 core_services_application_(core_services_application), | |
40 core_services_application_task_runner_(base::MessageLoop::current() | |
41 ->task_runner()), | |
42 url_(url), | |
43 delegate_(std::move(delegate)), | |
44 request_(std::move(request)), | |
45 destruct_callback_(destruct_callback) {} | |
46 | |
47 ~ApplicationThread() override { | |
48 Join(); | |
49 destruct_callback_.Run(); | |
50 } | |
51 | |
52 // Overridden from base::SimpleThread: | |
53 void Run() override { | |
54 scoped_ptr<mojo::ApplicationRunner> runner( | |
55 new mojo::ApplicationRunner(delegate_.release())); | |
56 if (url_ == "mojo://network_service/") { | |
57 runner->set_message_loop_type(base::MessageLoop::TYPE_IO); | |
58 } else if (url_ == "mojo://mus/") { | |
59 runner->set_message_loop_type(base::MessageLoop::TYPE_UI); | |
60 } | |
61 runner->Run(request_.PassMessagePipe().release().value(), false); | |
62 | |
63 core_services_application_task_runner_->PostTask( | |
64 FROM_HERE, | |
65 base::Bind(&CoreServicesApplicationDelegate::ApplicationThreadDestroyed, | |
66 core_services_application_, | |
67 this)); | |
68 } | |
69 | |
70 private: | |
71 base::WeakPtr<CoreServicesApplicationDelegate> core_services_application_; | |
72 scoped_refptr<base::SingleThreadTaskRunner> | |
73 core_services_application_task_runner_; | |
74 std::string url_; | |
75 scoped_ptr<mojo::ShellClient> delegate_; | |
76 mojo::InterfaceRequest<mojo::shell::mojom::Application> request_; | |
77 mojo::Callback<void()> destruct_callback_; | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(ApplicationThread); | |
80 }; | |
81 | |
82 CoreServicesApplicationDelegate::CoreServicesApplicationDelegate() | |
83 : weak_factory_(this) { | |
84 } | |
85 | |
86 CoreServicesApplicationDelegate::~CoreServicesApplicationDelegate() { | |
87 application_threads_.clear(); | |
88 } | |
89 | |
90 void CoreServicesApplicationDelegate::ApplicationThreadDestroyed( | |
91 ApplicationThread* thread) { | |
92 ScopedVector<ApplicationThread>::iterator iter = | |
93 std::find(application_threads_.begin(), | |
94 application_threads_.end(), | |
95 thread); | |
96 DCHECK(iter != application_threads_.end()); | |
97 application_threads_.erase(iter); | |
98 } | |
99 | |
100 void CoreServicesApplicationDelegate::Initialize(mojo::Shell* shell, | |
101 const std::string& url, | |
102 uint32_t id) { | |
103 mojo::InitLogging(); | |
104 tracing_.Initialize(shell, url); | |
105 } | |
106 | |
107 bool CoreServicesApplicationDelegate::AcceptConnection( | |
108 mojo::Connection* connection) { | |
109 connection->AddService(this); | |
110 return true; | |
111 } | |
112 | |
113 void CoreServicesApplicationDelegate::Quit() { | |
114 // This will delete all threads. This also performs a blocking join, waiting | |
115 // for the threads to end. | |
116 application_threads_.clear(); | |
117 weak_factory_.InvalidateWeakPtrs(); | |
118 } | |
119 | |
120 void CoreServicesApplicationDelegate::Create( | |
121 mojo::Connection* connection, | |
122 mojo::InterfaceRequest<mojo::shell::mojom::ContentHandler> request) { | |
123 handler_bindings_.AddBinding(this, std::move(request)); | |
124 } | |
125 | |
126 void CoreServicesApplicationDelegate::StartApplication( | |
127 mojo::InterfaceRequest<mojo::shell::mojom::Application> request, | |
128 mojo::URLResponsePtr response, | |
129 const mojo::Callback<void()>& destruct_callback) { | |
130 const std::string url = response->url; | |
131 | |
132 scoped_ptr<mojo::ShellClient> delegate; | |
133 if (url == "mojo://clipboard/") { | |
134 delegate.reset(new clipboard::ClipboardApplicationDelegate); | |
135 } else if (url == "mojo://filesystem/") { | |
136 delegate.reset(new filesystem::FileSystemApp); | |
137 } else if (url == "mojo://tracing/") { | |
138 delegate.reset(new tracing::TracingApp); | |
139 } else if (url == "mojo://web_view/") { | |
140 delegate.reset(new web_view::WebViewApplicationDelegate); | |
141 } else { | |
142 #if !defined(OS_ANDROID) | |
143 if (!delegate) | |
144 delegate = CreateApplicationDelegateNotAndroid(url); | |
145 #endif | |
146 if (!delegate) | |
147 delegate = CreatePlatformSpecificApplicationDelegate(url); | |
148 | |
149 if (!delegate) | |
150 NOTREACHED() << "This application package does not support " << url; | |
151 } | |
152 | |
153 scoped_ptr<ApplicationThread> thread(new ApplicationThread( | |
154 weak_factory_.GetWeakPtr(), url, std::move(delegate), std::move(request), | |
155 destruct_callback)); | |
156 thread->Start(); | |
157 application_threads_.push_back(std::move(thread)); | |
158 } | |
159 | |
160 } // namespace core_services | |
OLD | NEW |