Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(470)

Side by Side Diff: mojo/shell/dynamic_application_loader.cc

Issue 513573002: Mojo: Fix two bugs in content handling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Darin comments, updates for Android toolchain Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // Encapsulates loading and running one individual application.
25 //
26 // Loaders are owned by DynamicApplicationLoader, so it's always safe for a
27 // loader to poke at |delegate_|.
28 //
29 // Async operations are done with WeakPtr to protect against
30 // DynamicApplicationLoader going away (and taking all the Loaders with it)
tim (not reviewing) 2014/08/27 17:32:19 I'm not sure I follow why this isn't enough in ter
Aaron Boodman 2014/08/27 18:33:43 I was worried about the case where DynamicApplicat
tim (not reviewing) 2014/08/27 22:54:14 Yes but I don't see why it matters. In the system
31 // while the async operation is outstanding.
32 class Loader {
33 public:
34 Loader(scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks,
35 internal::LoaderDelegate* delegate)
darin (slow to review) 2014/08/27 06:34:19 what was the purpose of introducing LoaderDelegate
Aaron Boodman 2014/08/27 18:26:13 Yes, I considered using friend, but there sure end
Aaron Boodman 2014/08/27 18:27:37 Sorry s/weak pointers/raw pointers/.
36 : callbacks_(callbacks),
37 delegate_(delegate),
38 weak_ptr_factory_(this) {
39 }
40
41 virtual ~Loader() {
42 }
43
44 protected:
45 void RunLibrary(const base::FilePath& path, bool path_exists) {
46 ScopedMessagePipeHandle shell_handle = callbacks_->RegisterApplication();
47 if (!shell_handle.is_valid()) {
48 AppCompleted();
49 return;
50 }
51
52 if (!path_exists) {
53 DVLOG(1) << "Library not started because library path '"
54 << path.value() << "' does not exist.";
55 AppCompleted();
56 return;
57 }
58
59 runner_ = delegate_->CreateRunner();
60 runner_->Start(path,
61 shell_handle.Pass(),
62 base::Bind(&Loader::AppCompleted,
63 weak_ptr_factory_.GetWeakPtr()));
64 }
65
66 void AppCompleted() {
67 delegate_->LoaderComplete(this);
68 }
69
70 scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks_;
71 internal::LoaderDelegate* delegate_;
72
73 private:
74 scoped_ptr<DynamicServiceRunner> runner_;
75 base::WeakPtrFactory<Loader> weak_ptr_factory_;
76 };
77
78 // A loader for local files.
79 class LocalLoader : public Loader {
80 public:
81 LocalLoader(const GURL& url,
82 scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks,
83 internal::LoaderDelegate* delegate)
84 : Loader(callbacks, delegate),
85 weak_ptr_factory_(this) {
86 base::FilePath path;
87 net::FileURLToFilePath(url, &path);
88
89 // Async for consistency with network case.
90 base::MessageLoop::current()->PostTask(
91 FROM_HERE,
92 base::Bind(&LocalLoader::RunLibrary,
93 weak_ptr_factory_.GetWeakPtr(),
94 path,
95 base::PathExists(path)));
96 }
97
98 virtual ~LocalLoader() {
99 }
100
101 private:
102 base::WeakPtrFactory<LocalLoader> weak_ptr_factory_;
103 };
104
105 // A loader for network files.
106 class NetworkLoader : public Loader {
107 public:
108 NetworkLoader(const GURL& url,
109 scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks,
110 internal::LoaderDelegate* delegate)
111 : Loader(callbacks, delegate),
112 weak_ptr_factory_(this) {
113 URLRequestPtr request(URLRequest::New());
114 request->url = String::From(url);
115 request->auto_follow_redirects = true;
116
117 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
118 switches::kDisableCache)) {
119 request->bypass_cache = true;
120 }
121
122 delegate_->CreateURLLoader(Get(&url_loader_));
123 url_loader_->Start(request.Pass(),
124 base::Bind(&NetworkLoader::OnLoadComplete,
125 weak_ptr_factory_.GetWeakPtr()));
126 }
127
128 virtual ~NetworkLoader() {
129 if (!file_.empty())
130 base::DeleteFile(file_, false);
131 }
132
133 private:
134 void OnLoadComplete(URLResponsePtr response) {
135 if (response->error) {
136 LOG(ERROR) << "Error (" << response->error->code << ": "
137 << response->error->description << ") while fetching "
138 << response->url;
139 AppCompleted();
140 return;
141 }
142
143 const GURL& content_handler_url =
144 delegate_->GetContentHandlerURL(response->mime_type);
145 if (!content_handler_url.is_empty()) {
146 callbacks_->LoadWithContentHandler(content_handler_url,
147 response.Pass(),
148 url_loader_.Pass());
149 AppCompleted();
150 return;
151 }
152
153 base::CreateTemporaryFile(&file_);
154 common::CopyToFile(
155 response->body.Pass(),
156 file_,
157 delegate_->GetBlockingPool(),
158 base::Bind(&NetworkLoader::RunLibrary,
159 weak_ptr_factory_.GetWeakPtr(),
160 file_));
161 }
162
163 URLLoaderPtr url_loader_;
164 base::FilePath file_;
165 base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_;
166 };
167
23 DynamicApplicationLoader::DynamicApplicationLoader( 168 DynamicApplicationLoader::DynamicApplicationLoader(
24 Context* context, 169 Context* context,
25 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) 170 scoped_ptr<DynamicServiceRunnerFactory> runner_factory)
26 : context_(context), 171 : context_(context),
27 runner_factory_(runner_factory.Pass()), 172 runner_factory_(runner_factory.Pass()) {
28 weak_ptr_factory_(this) {
29 } 173 }
30 174
31 DynamicApplicationLoader::~DynamicApplicationLoader() { 175 DynamicApplicationLoader::~DynamicApplicationLoader() {
32 } 176 }
33 177
34 void DynamicApplicationLoader::RegisterContentHandler( 178 void DynamicApplicationLoader::RegisterContentHandler(
35 const std::string& mime_type, 179 const std::string& mime_type,
36 const GURL& content_handler_url) { 180 const GURL& content_handler_url) {
37 mime_type_to_url_[mime_type] = content_handler_url; 181 mime_type_to_url_[mime_type] = content_handler_url;
38 } 182 }
39 183
40 void DynamicApplicationLoader::Load(ApplicationManager* manager, 184 void DynamicApplicationLoader::Load(ApplicationManager* manager,
41 const GURL& url, 185 const GURL& url,
42 scoped_refptr<LoadCallbacks> callbacks) { 186 scoped_refptr<LoadCallbacks> callbacks) {
43 GURL resolved_url; 187 GURL resolved_url;
44 if (url.SchemeIs("mojo")) { 188 if (url.SchemeIs("mojo")) {
45 resolved_url = context_->mojo_url_resolver()->Resolve(url); 189 resolved_url = context_->mojo_url_resolver()->Resolve(url);
46 } else { 190 } else {
47 resolved_url = url; 191 resolved_url = url;
48 } 192 }
49 193
50 if (resolved_url.SchemeIsFile()) 194 if (resolved_url.SchemeIsFile())
51 LoadLocalService(resolved_url, callbacks); 195 loaders_.push_back(new LocalLoader(resolved_url, callbacks, this));
52 else 196 else
53 LoadNetworkService(resolved_url, callbacks); 197 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 } 198 }
169 199
170 void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager, 200 void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager,
171 const GURL& url) { 201 const GURL& url) {
172 // TODO(darin): What should we do about service errors? This implies that 202 // 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? 203 // the app closed its handle to the service manager. Maybe we don't care?
174 } 204 }
175 205
206 void DynamicApplicationLoader::LoaderComplete(Loader* loader) {
207 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader));
208 }
209
210 scoped_ptr<DynamicServiceRunner> DynamicApplicationLoader::CreateRunner() {
211 return runner_factory_->Create(context_);
212 }
213
214 void DynamicApplicationLoader::CreateURLLoader(
215 InterfaceRequest<URLLoader> loader_request) {
216 if (!network_service_) {
217 context_->application_manager()->ConnectToService(
218 GURL("mojo:mojo_network_service"), &network_service_);
219 }
220 network_service_->CreateURLLoader(loader_request.Pass());
221 }
222
223 const GURL& DynamicApplicationLoader::GetContentHandlerURL(
224 const std::string& mime_type) {
225 MimeTypeToURLMap::iterator iter = mime_type_to_url_.find(mime_type);
226 if (iter != mime_type_to_url_.end()) {
227 return iter->second;
228 } else {
darin (slow to review) 2014/08/27 06:34:19 nit: no need for "else" after "return"
229 return GURL::EmptyGURL();
230 }
231 }
232
233 base::SequencedWorkerPool* DynamicApplicationLoader::GetBlockingPool() {
234 return context_->task_runners()->blocking_pool();
235 }
236
176 } // namespace shell 237 } // namespace shell
177 } // namespace mojo 238 } // namespace mojo
OLDNEW
« mojo/shell/dynamic_application_loader.h ('K') | « mojo/shell/dynamic_application_loader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698