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

Side by Side Diff: shell/android/background_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
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/android/background_application_loader.h" 5 #include "shell/android/background_application_loader.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "shell/application_manager/application_manager.h" 9 #include "shell/application_manager/application_manager.h"
10 10
11 namespace mojo { 11 namespace mojo {
12 12
13 BackgroundApplicationLoader::BackgroundApplicationLoader( 13 BackgroundApplicationLoader::BackgroundApplicationLoader(
14 scoped_ptr<ApplicationLoader> real_loader, 14 scoped_ptr<ApplicationLoader> real_loader,
15 const std::string& thread_name, 15 const std::string& thread_name,
16 base::MessageLoop::Type message_loop_type) 16 base::MessageLoop::Type message_loop_type)
17 : loader_(real_loader.Pass()), 17 : loader_(real_loader.Pass()),
18 message_loop_type_(message_loop_type), 18 message_loop_type_(message_loop_type),
19 thread_name_(thread_name), 19 thread_name_(thread_name),
20 message_loop_created_(true, false) { 20 message_loop_created_(true, false) {
21 } 21 }
22 22
23 BackgroundApplicationLoader::~BackgroundApplicationLoader() { 23 BackgroundApplicationLoader::~BackgroundApplicationLoader() {
24 if (thread_) 24 if (thread_)
25 thread_->Join(); 25 thread_->Join();
26 } 26 }
27 27
28 void BackgroundApplicationLoader::Load( 28 void BackgroundApplicationLoader::Load(
29 ApplicationManager* manager,
30 const GURL& url, 29 const GURL& url,
31 InterfaceRequest<Application> application_request, 30 InterfaceRequest<Application> application_request) {
32 LoadCallback callback) {
33 DCHECK(application_request.is_pending()); 31 DCHECK(application_request.is_pending());
34 if (!thread_) { 32 if (!thread_) {
35 // TODO(tim): It'd be nice if we could just have each Load call 33 // TODO(tim): It'd be nice if we could just have each Load call
36 // result in a new thread like DynamicService{Loader, Runner}. But some 34 // result in a new thread like DynamicService{Loader, Runner}. But some
37 // loaders are creating multiple ApplicationImpls (NetworkApplicationLoader) 35 // loaders are creating multiple ApplicationImpls (NetworkApplicationLoader)
38 // sharing a delegate (etc). So we have to keep it single threaded, wait 36 // sharing a delegate (etc). So we have to keep it single threaded, wait
39 // for the thread to initialize, and post to the TaskRunner for subsequent 37 // for the thread to initialize, and post to the TaskRunner for subsequent
40 // Load calls for now. 38 // Load calls for now.
41 thread_.reset(new base::DelegateSimpleThread(this, thread_name_)); 39 thread_.reset(new base::DelegateSimpleThread(this, thread_name_));
42 thread_->Start(); 40 thread_->Start();
43 message_loop_created_.Wait(); 41 message_loop_created_.Wait();
44 DCHECK(task_runner_.get()); 42 DCHECK(task_runner_.get());
45 } 43 }
46 44
47 task_runner_->PostTask( 45 task_runner_->PostTask(
48 FROM_HERE, 46 FROM_HERE,
49 base::Bind(&BackgroundApplicationLoader::LoadOnBackgroundThread, 47 base::Bind(&BackgroundApplicationLoader::LoadOnBackgroundThread,
50 base::Unretained(this), manager, url, 48 base::Unretained(this), url,
51 base::Passed(&application_request))); 49 base::Passed(&application_request)));
52 } 50 }
53 51
54 void BackgroundApplicationLoader::OnApplicationError( 52 void BackgroundApplicationLoader::OnApplicationError(
55 ApplicationManager* manager, 53 ApplicationManager* manager,
56 const GURL& url) { 54 const GURL& url) {
57 task_runner_->PostTask( 55 task_runner_->PostTask(
58 FROM_HERE, 56 FROM_HERE,
59 base::Bind( 57 base::Bind(
60 &BackgroundApplicationLoader::OnApplicationErrorOnBackgroundThread, 58 &BackgroundApplicationLoader::OnApplicationErrorOnBackgroundThread,
61 base::Unretained(this), manager, url)); 59 base::Unretained(this), manager, url));
62 } 60 }
63 61
64 void BackgroundApplicationLoader::Run() { 62 void BackgroundApplicationLoader::Run() {
65 base::MessageLoop message_loop(message_loop_type_); 63 base::MessageLoop message_loop(message_loop_type_);
66 base::RunLoop loop; 64 base::RunLoop loop;
67 task_runner_ = message_loop.task_runner(); 65 task_runner_ = message_loop.task_runner();
68 quit_closure_ = loop.QuitClosure(); 66 quit_closure_ = loop.QuitClosure();
69 message_loop_created_.Signal(); 67 message_loop_created_.Signal();
70 loop.Run(); 68 loop.Run();
71 69
72 // Destroy |loader_| on the thread it's actually used on. 70 // Destroy |loader_| on the thread it's actually used on.
73 loader_.reset(); 71 loader_.reset();
74 } 72 }
75 73
76 void BackgroundApplicationLoader::LoadOnBackgroundThread( 74 void BackgroundApplicationLoader::LoadOnBackgroundThread(
77 ApplicationManager* manager,
78 const GURL& url, 75 const GURL& url,
79 InterfaceRequest<Application> application_request) { 76 InterfaceRequest<Application> application_request) {
80 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 77 DCHECK(task_runner_->RunsTasksOnCurrentThread());
81 loader_->Load(manager, url, application_request.Pass(), SimpleLoadCallback()); 78 loader_->Load(url, application_request.Pass());
82 } 79 }
83 80
84 void BackgroundApplicationLoader::OnApplicationErrorOnBackgroundThread( 81 void BackgroundApplicationLoader::OnApplicationErrorOnBackgroundThread(
85 ApplicationManager* manager, 82 ApplicationManager* manager,
86 const GURL& url) { 83 const GURL& url) {
87 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 84 DCHECK(task_runner_->RunsTasksOnCurrentThread());
88 loader_->OnApplicationError(manager, url); 85 loader_->OnApplicationError(manager, url);
89 } 86 }
90 87
91 } // namespace mojo 88 } // namespace mojo
OLDNEW
« no previous file with comments | « shell/android/background_application_loader.h ('k') | shell/android/background_application_loader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698