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

Side by Side Diff: shell/native_application_loader.cc

Issue 930243006: Simplify the ApplicationLoader interface in preparation for changes. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: ptal Created 5 years, 10 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
« no previous file with comments | « shell/native_application_loader.h ('k') | shell/native_application_loader_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "shell/dynamic_application_loader.h" 5 #include "shell/native_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/files/file.h" 9 #include "base/files/file.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
(...skipping 23 matching lines...) Expand all
39 static const char kMojoMagic[] = "#!mojo "; 39 static const char kMojoMagic[] = "#!mojo ";
40 static const size_t kMaxShebangLength = 2048; 40 static const size_t kMaxShebangLength = 2048;
41 41
42 void IgnoreResult(bool result) { 42 void IgnoreResult(bool result) {
43 } 43 }
44 44
45 } // namespace 45 } // namespace
46 46
47 // Encapsulates loading and running one individual application. 47 // Encapsulates loading and running one individual application.
48 // 48 //
49 // Loaders are owned by DynamicApplicationLoader. DynamicApplicationLoader must 49 // Loaders are owned by NativeApplicationLoader. NativeApplicationLoader must
50 // ensure that all the parameters passed to Loader subclasses stay valid through 50 // ensure that all the parameters passed to Loader subclasses stay valid through
51 // Loader's lifetime. 51 // Loader's lifetime.
52 // 52 //
53 // Async operations are done with WeakPtr to protect against 53 // Async operations are done with WeakPtr to protect against
54 // DynamicApplicationLoader going away (and taking all the Loaders with it) 54 // NativeApplicationLoader going away (and taking all the Loaders with it)
55 // while the async operation is outstanding. 55 // while the async operation is outstanding.
56 class DynamicApplicationLoader::Loader { 56 class NativeApplicationLoader::Loader {
57 public: 57 public:
58 Loader(DynamicServiceRunner::CleanupBehavior cleanup_behavior, 58 Loader(DynamicServiceRunner::CleanupBehavior cleanup_behavior,
59 MimeTypeToURLMap* mime_type_to_url, 59 MimeTypeToURLMap* mime_type_to_url,
60 Context* context, 60 Context* context,
61 DynamicServiceRunnerFactory* runner_factory, 61 DynamicServiceRunnerFactory* runner_factory,
62 InterfaceRequest<Application> application_request, 62 InterfaceRequest<Application> application_request,
63 ApplicationLoader::LoadCallback load_callback, 63 LoadCallback load_callback,
64 const LoaderCompleteCallback& loader_complete_callback) 64 const LoaderCompleteCallback& loader_complete_callback)
65 : cleanup_behavior_(cleanup_behavior), 65 : cleanup_behavior_(cleanup_behavior),
66 application_request_(application_request.Pass()), 66 application_request_(application_request.Pass()),
67 load_callback_(load_callback), 67 load_callback_(load_callback),
68 loader_complete_callback_(loader_complete_callback), 68 loader_complete_callback_(loader_complete_callback),
69 context_(context), 69 context_(context),
70 mime_type_to_url_(mime_type_to_url), 70 mime_type_to_url_(mime_type_to_url),
71 runner_factory_(runner_factory), 71 runner_factory_(runner_factory),
72 weak_ptr_factory_(this) {} 72 weak_ptr_factory_(this) {}
73 73
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 } 144 }
145 145
146 runner_ = runner_factory_->Create(context_); 146 runner_ = runner_factory_->Create(context_);
147 runner_->Start( 147 runner_->Start(
148 path, cleanup_behavior_, application_request_.Pass(), 148 path, cleanup_behavior_, application_request_.Pass(),
149 base::Bind(&Loader::ReportComplete, weak_ptr_factory_.GetWeakPtr())); 149 base::Bind(&Loader::ReportComplete, weak_ptr_factory_.GetWeakPtr()));
150 } 150 }
151 151
152 DynamicServiceRunner::CleanupBehavior cleanup_behavior_; 152 DynamicServiceRunner::CleanupBehavior cleanup_behavior_;
153 InterfaceRequest<Application> application_request_; 153 InterfaceRequest<Application> application_request_;
154 ApplicationLoader::LoadCallback load_callback_; 154 LoadCallback load_callback_;
155 LoaderCompleteCallback loader_complete_callback_; 155 LoaderCompleteCallback loader_complete_callback_;
156 Context* context_; 156 Context* context_;
157 MimeTypeToURLMap* mime_type_to_url_; 157 MimeTypeToURLMap* mime_type_to_url_;
158 DynamicServiceRunnerFactory* runner_factory_; 158 DynamicServiceRunnerFactory* runner_factory_;
159 scoped_ptr<DynamicServiceRunner> runner_; 159 scoped_ptr<DynamicServiceRunner> runner_;
160 base::WeakPtrFactory<Loader> weak_ptr_factory_; 160 base::WeakPtrFactory<Loader> weak_ptr_factory_;
161 }; 161 };
162 162
163 // A loader for local files. 163 // A loader for local files.
164 class DynamicApplicationLoader::LocalLoader : public Loader { 164 class NativeApplicationLoader::LocalLoader : public Loader {
165 public: 165 public:
166 LocalLoader(const GURL& url, 166 LocalLoader(const GURL& url,
167 MimeTypeToURLMap* mime_type_to_url, 167 MimeTypeToURLMap* mime_type_to_url,
168 Context* context, 168 Context* context,
169 DynamicServiceRunnerFactory* runner_factory, 169 DynamicServiceRunnerFactory* runner_factory,
170 InterfaceRequest<Application> application_request, 170 InterfaceRequest<Application> application_request,
171 ApplicationLoader::LoadCallback load_callback, 171 LoadCallback load_callback,
172 const LoaderCompleteCallback& loader_complete_callback) 172 const LoaderCompleteCallback& loader_complete_callback)
173 : Loader(DynamicServiceRunner::DontDeleteAppPath, 173 : Loader(DynamicServiceRunner::DontDeleteAppPath,
174 mime_type_to_url, 174 mime_type_to_url,
175 context, 175 context,
176 runner_factory, 176 runner_factory,
177 application_request.Pass(), 177 application_request.Pass(),
178 load_callback, 178 load_callback,
179 loader_complete_callback), 179 loader_complete_callback),
180 url_(url), 180 url_(url),
181 path_(UrlToFile(url)) { 181 path_(UrlToFile(url)) {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 return true; 242 return true;
243 } 243 }
244 244
245 GURL url_; 245 GURL url_;
246 base::FilePath path_; 246 base::FilePath path_;
247 247
248 DISALLOW_COPY_AND_ASSIGN(LocalLoader); 248 DISALLOW_COPY_AND_ASSIGN(LocalLoader);
249 }; 249 };
250 250
251 // A loader for network files. 251 // A loader for network files.
252 class DynamicApplicationLoader::NetworkLoader : public Loader { 252 class NativeApplicationLoader::NetworkLoader : public Loader {
253 public: 253 public:
254 NetworkLoader(const GURL& url, 254 NetworkLoader(const GURL& url,
255 NetworkService* network_service, 255 NetworkService* network_service,
256 MimeTypeToURLMap* mime_type_to_url, 256 MimeTypeToURLMap* mime_type_to_url,
257 Context* context, 257 Context* context,
258 DynamicServiceRunnerFactory* runner_factory, 258 DynamicServiceRunnerFactory* runner_factory,
259 InterfaceRequest<Application> application_request, 259 InterfaceRequest<Application> application_request,
260 ApplicationLoader::LoadCallback load_callback, 260 LoadCallback load_callback,
261 const LoaderCompleteCallback& loader_complete_callback) 261 const LoaderCompleteCallback& loader_complete_callback)
262 : Loader(DynamicServiceRunner::DeleteAppPath, 262 : Loader(DynamicServiceRunner::DeleteAppPath,
263 mime_type_to_url, 263 mime_type_to_url,
264 context, 264 context,
265 runner_factory, 265 runner_factory,
266 application_request.Pass(), 266 application_request.Pass(),
267 load_callback, 267 load_callback,
268 loader_complete_callback), 268 loader_complete_callback),
269 url_(url), 269 url_(url),
270 weak_ptr_factory_(this) { 270 weak_ptr_factory_(this) {
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 438
439 const GURL url_; 439 const GURL url_;
440 URLLoaderPtr url_loader_; 440 URLLoaderPtr url_loader_;
441 URLResponsePtr response_; 441 URLResponsePtr response_;
442 base::FilePath path_; 442 base::FilePath path_;
443 base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_; 443 base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_;
444 444
445 DISALLOW_COPY_AND_ASSIGN(NetworkLoader); 445 DISALLOW_COPY_AND_ASSIGN(NetworkLoader);
446 }; 446 };
447 447
448 DynamicApplicationLoader::DynamicApplicationLoader( 448 NativeApplicationLoader::NativeApplicationLoader(
449 Context* context, 449 Context* context,
450 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) 450 scoped_ptr<DynamicServiceRunnerFactory> runner_factory)
451 : context_(context), 451 : context_(context),
452 runner_factory_(runner_factory.Pass()), 452 runner_factory_(runner_factory.Pass()),
453 453
454 // Unretained() is correct here because DynamicApplicationLoader owns the 454 // Unretained() is correct here because NativeApplicationLoader owns the
455 // loaders that we pass this callback to. 455 // loaders that we pass this callback to.
456 loader_complete_callback_( 456 loader_complete_callback_(
457 base::Bind(&DynamicApplicationLoader::LoaderComplete, 457 base::Bind(&NativeApplicationLoader::LoaderComplete,
458 base::Unretained(this))) { 458 base::Unretained(this))) {
459 } 459 }
460 460
461 DynamicApplicationLoader::~DynamicApplicationLoader() { 461 NativeApplicationLoader::~NativeApplicationLoader() {
462 } 462 }
463 463
464 void DynamicApplicationLoader::RegisterContentHandler( 464 void NativeApplicationLoader::RegisterContentHandler(
465 const std::string& mime_type, 465 const std::string& mime_type,
466 const GURL& content_handler_url) { 466 const GURL& content_handler_url) {
467 DCHECK(content_handler_url.is_valid()) 467 DCHECK(content_handler_url.is_valid())
468 << "Content handler URL is invalid for mime type " << mime_type; 468 << "Content handler URL is invalid for mime type " << mime_type;
469 mime_type_to_url_[mime_type] = content_handler_url; 469 mime_type_to_url_[mime_type] = content_handler_url;
470 } 470 }
471 471
472 void DynamicApplicationLoader::Load( 472 void NativeApplicationLoader::Load(
473 ApplicationManager* manager,
474 const GURL& url, 473 const GURL& url,
475 InterfaceRequest<Application> application_request, 474 InterfaceRequest<Application> application_request,
476 LoadCallback load_callback) { 475 LoadCallback load_callback) {
477 if (url.SchemeIsFile()) { 476 if (url.SchemeIsFile()) {
478 loaders_.push_back(new LocalLoader( 477 loaders_.push_back(new LocalLoader(
479 url, &mime_type_to_url_, context_, runner_factory_.get(), 478 url, &mime_type_to_url_, context_, runner_factory_.get(),
480 application_request.Pass(), load_callback, loader_complete_callback_)); 479 application_request.Pass(), load_callback, loader_complete_callback_));
481 return; 480 return;
482 } 481 }
483 482
484 if (!network_service_) { 483 if (!network_service_) {
485 context_->application_manager()->ConnectToService( 484 context_->application_manager()->ConnectToService(
486 GURL("mojo:network_service"), &network_service_); 485 GURL("mojo:network_service"), &network_service_);
487 } 486 }
488 487
489 loaders_.push_back(new NetworkLoader( 488 loaders_.push_back(new NetworkLoader(
490 url, network_service_.get(), &mime_type_to_url_, context_, 489 url, network_service_.get(), &mime_type_to_url_, context_,
491 runner_factory_.get(), application_request.Pass(), load_callback, 490 runner_factory_.get(), application_request.Pass(), load_callback,
492 loader_complete_callback_)); 491 loader_complete_callback_));
493 } 492 }
494 493
495 void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager, 494 void NativeApplicationLoader::LoaderComplete(Loader* loader) {
496 const GURL& url) {
497 // TODO(darin): What should we do about service errors? This implies that
498 // the app closed its handle to the service manager. Maybe we don't care?
499 }
500
501 void DynamicApplicationLoader::LoaderComplete(Loader* loader) {
502 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); 495 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader));
503 } 496 }
504 497
505 } // namespace shell 498 } // namespace shell
506 } // namespace mojo 499 } // namespace mojo
OLDNEW
« no previous file with comments | « shell/native_application_loader.h ('k') | shell/native_application_loader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698