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 "mojo/shell/dynamic_application_loader.h" | 5 #include "mojo/shell/dynamic_application_loader.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "mojo/common/common_type_converters.h" | 14 #include "mojo/common/common_type_converters.h" |
14 #include "mojo/common/data_pipe_utils.h" | 15 #include "mojo/common/data_pipe_utils.h" |
15 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" | 16 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" |
16 #include "mojo/shell/context.h" | 17 #include "mojo/shell/context.h" |
17 #include "mojo/shell/switches.h" | 18 #include "mojo/shell/switches.h" |
18 #include "net/base/filename_util.h" | 19 #include "net/base/filename_util.h" |
19 | 20 |
20 namespace mojo { | 21 namespace mojo { |
21 namespace shell { | 22 namespace shell { |
22 | 23 |
24 namespace { | |
25 | |
26 // Encapsulates loading and running one individual application. | |
27 // | |
28 // Loaders are owned by DynamicApplicationLoader, so they can always poke into | |
29 // DynamicApplicationLoader (via LoaderDelegate). | |
30 // | |
31 // Async operations are done with WeakPtr to protect against | |
32 // DynamicApplicationLoader going away (and taking all the Loaders with it) | |
33 // while the async operation is oustanding. | |
darin (slow to review)
2014/08/27 03:22:04
nit: oustanding -> outstanding
Aaron Boodman
2014/08/27 05:43:51
Done.
| |
34 class Loader { | |
35 public: | |
36 Loader(scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks, | |
37 LoaderDelegate* delegate) | |
38 : callbacks_(callbacks), | |
39 delegate_(delegate), | |
40 weak_ptr_factory_(this) { | |
41 } | |
42 | |
43 virtual ~Loader() { | |
44 } | |
45 | |
46 protected: | |
47 void RunLibrary(const base::FilePath& path, bool path_exists) { | |
48 ScopedMessagePipeHandle shell_handle = callbacks_->RegisterApplication(); | |
49 if (!shell_handle.is_valid()) { | |
50 AppCompleted(); | |
51 return; | |
52 } | |
53 | |
54 if (!path_exists) { | |
55 DVLOG(1) << "Library not started because library path '" | |
56 << path.value() << "' does not exist."; | |
57 AppCompleted(); | |
58 return; | |
59 } | |
60 | |
61 runner_ = delegate_->CreateRunner(); | |
62 runner_->Start(path, | |
63 shell_handle.Pass(), | |
64 base::Bind(&Loader::AppCompleted, | |
65 weak_ptr_factory_.GetWeakPtr())); | |
66 } | |
67 | |
68 void AppCompleted() { | |
69 delegate_->LoaderComplete(this); | |
70 } | |
71 | |
72 scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks_; | |
73 LoaderDelegate* delegate_; | |
74 | |
75 private: | |
76 scoped_ptr<DynamicServiceRunner> runner_; | |
77 base::WeakPtrFactory<Loader> weak_ptr_factory_; | |
78 }; | |
79 | |
80 // A loader for local files. | |
81 class LocalLoader : public Loader { | |
82 public: | |
83 LocalLoader(const GURL& url, | |
84 scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks, | |
85 LoaderDelegate* delegate) | |
86 : Loader(callbacks, delegate), | |
87 weak_ptr_factory_(this) { | |
88 base::FilePath path; | |
89 net::FileURLToFilePath(url, &path); | |
90 | |
91 // Async for consistency with network case. | |
92 base::MessageLoop::current()->PostTask( | |
93 FROM_HERE, | |
94 base::Bind(&LocalLoader::RunLibrary, | |
95 weak_ptr_factory_.GetWeakPtr(), | |
96 path, | |
97 base::PathExists(path))); | |
98 } | |
99 | |
100 virtual ~LocalLoader() { | |
101 } | |
102 | |
103 private: | |
104 base::WeakPtrFactory<LocalLoader> weak_ptr_factory_; | |
105 }; | |
106 | |
107 // A loader for network files. | |
108 class NetworkLoader : public Loader { | |
109 public: | |
110 NetworkLoader(const GURL& url, | |
111 scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks, | |
112 LoaderDelegate* delegate) | |
113 : Loader(callbacks, delegate), | |
114 weak_ptr_factory_(this) { | |
115 URLRequestPtr request(URLRequest::New()); | |
116 request->url = String::From(url); | |
117 request->auto_follow_redirects = true; | |
118 | |
119 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
120 switches::kDisableCache)) { | |
121 request->bypass_cache = true; | |
122 } | |
123 | |
124 delegate_->CreateURLLoader(Get(&url_loader_)); | |
125 url_loader_->Start(request.Pass(), | |
126 base::Bind(&NetworkLoader::OnLoadComplete, | |
127 weak_ptr_factory_.GetWeakPtr())); | |
128 } | |
129 | |
130 virtual ~NetworkLoader() { | |
131 if (!file_.empty()) | |
132 base::DeleteFile(file_, false); | |
133 } | |
134 | |
135 private: | |
136 void OnLoadComplete(URLResponsePtr response) { | |
137 if (response->error) { | |
138 LOG(ERROR) << "Error (" << response->error->code << ": " | |
139 << response->error->description << ") while fetching " | |
140 << response->url; | |
141 AppCompleted(); | |
142 return; | |
143 } | |
144 | |
145 const GURL& content_handler_url = | |
146 delegate_->GetContentHandlerURL(response->mime_type); | |
147 if (!content_handler_url.is_empty()) { | |
148 callbacks_->LoadWithContentHandler(content_handler_url, | |
149 response.Pass(), | |
150 url_loader_.Pass()); | |
151 AppCompleted(); | |
152 return; | |
153 } | |
154 | |
155 base::CreateTemporaryFile(&file_); | |
156 common::CopyToFile( | |
157 response->body.Pass(), | |
158 file_, | |
159 delegate_->GetBlockingPool(), | |
160 base::Bind(&NetworkLoader::RunLibrary, | |
161 weak_ptr_factory_.GetWeakPtr(), | |
162 file_)); | |
163 } | |
164 | |
165 URLLoaderPtr url_loader_; | |
166 base::FilePath file_; | |
167 base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_; | |
168 }; | |
169 | |
170 } // namespace | |
171 | |
23 DynamicApplicationLoader::DynamicApplicationLoader( | 172 DynamicApplicationLoader::DynamicApplicationLoader( |
24 Context* context, | 173 Context* context, |
25 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) | 174 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) |
26 : context_(context), | 175 : context_(context), |
27 runner_factory_(runner_factory.Pass()), | 176 runner_factory_(runner_factory.Pass()) { |
28 weak_ptr_factory_(this) { | |
29 } | 177 } |
30 | 178 |
31 DynamicApplicationLoader::~DynamicApplicationLoader() { | 179 DynamicApplicationLoader::~DynamicApplicationLoader() { |
32 } | 180 } |
33 | 181 |
34 void DynamicApplicationLoader::RegisterContentHandler( | 182 void DynamicApplicationLoader::RegisterContentHandler( |
35 const std::string& mime_type, | 183 const std::string& mime_type, |
36 const GURL& content_handler_url) { | 184 const GURL& content_handler_url) { |
37 mime_type_to_url_[mime_type] = content_handler_url; | 185 mime_type_to_url_[mime_type] = content_handler_url; |
38 } | 186 } |
39 | 187 |
40 void DynamicApplicationLoader::Load(ApplicationManager* manager, | 188 void DynamicApplicationLoader::Load(ApplicationManager* manager, |
41 const GURL& url, | 189 const GURL& url, |
42 scoped_refptr<LoadCallbacks> callbacks) { | 190 scoped_refptr<LoadCallbacks> callbacks) { |
43 GURL resolved_url; | 191 GURL resolved_url; |
44 if (url.SchemeIs("mojo")) { | 192 if (url.SchemeIs("mojo")) { |
45 resolved_url = context_->mojo_url_resolver()->Resolve(url); | 193 resolved_url = context_->mojo_url_resolver()->Resolve(url); |
46 } else { | 194 } else { |
47 resolved_url = url; | 195 resolved_url = url; |
48 } | 196 } |
49 | 197 |
50 if (resolved_url.SchemeIsFile()) | 198 if (resolved_url.SchemeIsFile()) |
51 LoadLocalService(resolved_url, callbacks); | 199 loaders_.push_back(new LocalLoader(resolved_url, callbacks, this)); |
52 else | 200 else |
53 LoadNetworkService(resolved_url, callbacks); | 201 loaders_.push_back(new NetworkLoader(resolved_url, callbacks, this)); |
54 } | |
55 | |
56 void DynamicApplicationLoader::LoadLocalService( | |
57 const GURL& resolved_url, | |
58 scoped_refptr<LoadCallbacks> callbacks) { | |
59 base::FilePath path; | |
60 net::FileURLToFilePath(resolved_url, &path); | |
61 const bool kDeleteFileAfter = false; | |
62 | |
63 // Async for consistency with network case. | |
64 base::MessageLoop::current()->PostTask( | |
65 FROM_HERE, | |
66 base::Bind(&DynamicApplicationLoader::RunLibrary, | |
67 weak_ptr_factory_.GetWeakPtr(), | |
68 path, | |
69 callbacks, | |
70 kDeleteFileAfter, | |
71 base::PathExists(path))); | |
72 } | |
73 | |
74 void DynamicApplicationLoader::LoadNetworkService( | |
75 const GURL& resolved_url, | |
76 scoped_refptr<LoadCallbacks> callbacks) { | |
77 if (!network_service_) { | |
78 context_->application_manager()->ConnectToService( | |
79 GURL("mojo:mojo_network_service"), &network_service_); | |
80 } | |
81 if (!url_loader_) | |
82 network_service_->CreateURLLoader(Get(&url_loader_)); | |
83 | |
84 URLRequestPtr request(URLRequest::New()); | |
85 request->url = String::From(resolved_url); | |
86 request->auto_follow_redirects = true; | |
87 | |
88 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
89 switches::kDisableCache)) { | |
90 request->bypass_cache = true; | |
91 } | |
92 | |
93 url_loader_->Start( | |
94 request.Pass(), | |
95 base::Bind(&DynamicApplicationLoader::OnLoadNetworkServiceComplete, | |
96 weak_ptr_factory_.GetWeakPtr(), | |
97 callbacks)); | |
98 } | |
99 | |
100 void DynamicApplicationLoader::OnLoadNetworkServiceComplete( | |
101 scoped_refptr<LoadCallbacks> callbacks, | |
102 URLResponsePtr response) { | |
103 if (response->error) { | |
104 LOG(ERROR) << "Error (" << response->error->code << ": " | |
105 << response->error->description << ") while fetching " | |
106 << response->url; | |
107 } | |
108 | |
109 MimeTypeToURLMap::iterator iter = mime_type_to_url_.find(response->mime_type); | |
110 if (iter != mime_type_to_url_.end()) { | |
111 callbacks->LoadWithContentHandler(iter->second, response.Pass()); | |
112 return; | |
113 } | |
114 | |
115 base::FilePath file; | |
116 base::CreateTemporaryFile(&file); | |
117 | |
118 const bool kDeleteFileAfter = true; | |
119 common::CopyToFile(response->body.Pass(), | |
120 file, | |
121 context_->task_runners()->blocking_pool(), | |
122 base::Bind(&DynamicApplicationLoader::RunLibrary, | |
123 weak_ptr_factory_.GetWeakPtr(), | |
124 file, | |
125 callbacks, | |
126 kDeleteFileAfter)); | |
127 } | |
128 | |
129 void DynamicApplicationLoader::RunLibrary( | |
130 const base::FilePath& path, | |
131 scoped_refptr<LoadCallbacks> callbacks, | |
132 bool delete_file_after, | |
133 bool path_exists) { | |
134 | |
135 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); | |
136 if (!shell_handle.is_valid()) | |
137 return; | |
138 | |
139 if (!path_exists) { | |
140 DVLOG(1) << "Library not started because library path '" | |
141 << path.value() << "' does not exist."; | |
142 return; | |
143 } | |
144 | |
145 scoped_ptr<DynamicServiceRunner> runner = | |
146 runner_factory_->Create(context_).Pass(); | |
147 runner->Start(path, | |
148 shell_handle.Pass(), | |
149 base::Bind(&DynamicApplicationLoader::OnRunLibraryComplete, | |
150 weak_ptr_factory_.GetWeakPtr(), | |
151 base::Unretained(runner.get()), | |
152 delete_file_after ? path : base::FilePath())); | |
153 runners_.push_back(runner.release()); | |
154 } | |
155 | |
156 void DynamicApplicationLoader::OnRunLibraryComplete( | |
157 DynamicServiceRunner* runner, | |
158 const base::FilePath& temp_file) { | |
159 for (ScopedVector<DynamicServiceRunner>::iterator it = runners_.begin(); | |
160 it != runners_.end(); ++it) { | |
161 if (*it == runner) { | |
162 runners_.erase(it); | |
163 if (!temp_file.empty()) | |
164 base::DeleteFile(temp_file, false); | |
165 return; | |
166 } | |
167 } | |
168 } | 202 } |
169 | 203 |
170 void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager, | 204 void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager, |
171 const GURL& url) { | 205 const GURL& url) { |
172 // TODO(darin): What should we do about service errors? This implies that | 206 // TODO(darin): What should we do about service errors? This implies that |
173 // the app closed its handle to the service manager. Maybe we don't care? | 207 // the app closed its handle to the service manager. Maybe we don't care? |
174 } | 208 } |
175 | 209 |
210 void DynamicApplicationLoader::LoaderComplete(Loader* loader) { | |
211 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); | |
212 } | |
213 | |
214 scoped_ptr<DynamicServiceRunner> DynamicApplicationLoader::CreateRunner() { | |
215 return runner_factory_->Create(context_); | |
216 } | |
217 | |
218 void DynamicApplicationLoader::CreateURLLoader( | |
219 InterfaceRequest<URLLoader> loader_request) { | |
220 if (!network_service_) { | |
221 context_->application_manager()->ConnectToService( | |
222 GURL("mojo:mojo_network_service"), &network_service_); | |
223 } | |
224 network_service_->CreateURLLoader(loader_request.Pass()); | |
225 } | |
226 | |
227 const GURL& DynamicApplicationLoader::GetContentHandlerURL( | |
228 const std::string& mime_type) { | |
229 MimeTypeToURLMap::iterator iter = mime_type_to_url_.find(mime_type); | |
230 if (iter != mime_type_to_url_.end()) { | |
231 return iter->second; | |
232 } else { | |
233 return GURL::EmptyGURL(); | |
234 } | |
235 } | |
236 | |
237 base::SequencedWorkerPool* DynamicApplicationLoader::GetBlockingPool() { | |
238 return context_->task_runners()->blocking_pool(); | |
239 } | |
240 | |
176 } // namespace shell | 241 } // namespace shell |
177 } // namespace mojo | 242 } // namespace mojo |
OLD | NEW |