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/service_manager/background_shell_service_loader.h" | 5 #include "mojo/service_manager/background_shell_service_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 "mojo/service_manager/service_manager.h" | 9 #include "mojo/service_manager/service_manager.h" |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 | 12 |
13 class BackgroundShellServiceLoader::BackgroundLoader { | 13 class BackgroundShellServiceLoader::BackgroundLoader { |
14 public: | 14 public: |
15 explicit BackgroundLoader(ServiceLoader* loader) : loader_(loader) {} | 15 explicit BackgroundLoader(ServiceLoader* loader) : loader_(loader) {} |
16 ~BackgroundLoader() {} | 16 ~BackgroundLoader() {} |
17 | 17 |
18 void LoadService(ServiceManager* manager, | 18 void Load(ServiceManager* manager, |
19 const GURL& url, | 19 const GURL& url, |
20 ScopedMessagePipeHandle shell_handle) { | 20 ScopedMessagePipeHandle shell_handle) { |
21 loader_->LoadService(manager, url, shell_handle.Pass()); | 21 scoped_refptr<LoadCallbacks> callbacks( |
| 22 new ServiceLoader::SimpleLoadCallbacks(shell_handle.Pass())); |
| 23 loader_->Load(manager, url, callbacks); |
22 } | 24 } |
23 | 25 |
24 void OnServiceError(ServiceManager* manager, const GURL& url) { | 26 void OnServiceError(ServiceManager* manager, const GURL& url) { |
25 loader_->OnServiceError(manager, url); | 27 loader_->OnServiceError(manager, url); |
26 } | 28 } |
27 | 29 |
28 private: | 30 private: |
29 ServiceLoader* loader_; // Owned by BackgroundShellServiceLoader | 31 ServiceLoader* loader_; // Owned by BackgroundShellServiceLoader |
30 | 32 |
31 DISALLOW_COPY_AND_ASSIGN(BackgroundLoader); | 33 DISALLOW_COPY_AND_ASSIGN(BackgroundLoader); |
32 }; | 34 }; |
33 | 35 |
34 BackgroundShellServiceLoader::BackgroundShellServiceLoader( | 36 BackgroundShellServiceLoader::BackgroundShellServiceLoader( |
35 scoped_ptr<ServiceLoader> real_loader, | 37 scoped_ptr<ServiceLoader> real_loader, |
36 const std::string& thread_name, | 38 const std::string& thread_name, |
37 base::MessageLoop::Type message_loop_type) | 39 base::MessageLoop::Type message_loop_type) |
38 : loader_(real_loader.Pass()), | 40 : loader_(real_loader.Pass()), |
39 message_loop_type_(message_loop_type), | 41 message_loop_type_(message_loop_type), |
40 thread_name_(thread_name), | 42 thread_name_(thread_name), |
41 message_loop_created_(true, false), | 43 message_loop_created_(true, false), |
42 background_loader_(NULL) { | 44 background_loader_(NULL) { |
43 } | 45 } |
44 | 46 |
45 BackgroundShellServiceLoader::~BackgroundShellServiceLoader() { | 47 BackgroundShellServiceLoader::~BackgroundShellServiceLoader() { |
46 if (thread_) | 48 if (thread_) |
47 thread_->Join(); | 49 thread_->Join(); |
48 } | 50 } |
49 | 51 |
50 void BackgroundShellServiceLoader::LoadService( | 52 void BackgroundShellServiceLoader::Load( |
51 ServiceManager* manager, | 53 ServiceManager* manager, |
52 const GURL& url, | 54 const GURL& url, |
53 ScopedMessagePipeHandle shell_handle) { | 55 scoped_refptr<LoadCallbacks> callbacks) { |
| 56 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); |
| 57 if (!shell_handle.is_valid()) |
| 58 return; |
| 59 |
54 if (!thread_) { | 60 if (!thread_) { |
55 // TODO(tim): It'd be nice if we could just have each LoadService call | 61 // TODO(tim): It'd be nice if we could just have each Load call |
56 // result in a new thread like DynamicService{Loader, Runner}. But some | 62 // result in a new thread like DynamicService{Loader, Runner}. But some |
57 // loaders are creating multiple ApplicationImpls (NetworkServiceLoader) | 63 // loaders are creating multiple ApplicationImpls (NetworkServiceLoader) |
58 // sharing a delegate (etc). So we have to keep it single threaded, wait | 64 // sharing a delegate (etc). So we have to keep it single threaded, wait |
59 // for the thread to initialize, and post to the TaskRunner for subsequent | 65 // for the thread to initialize, and post to the TaskRunner for subsequent |
60 // LoadService calls for now. | 66 // Load calls for now. |
61 thread_.reset(new base::DelegateSimpleThread(this, thread_name_)); | 67 thread_.reset(new base::DelegateSimpleThread(this, thread_name_)); |
62 thread_->Start(); | 68 thread_->Start(); |
63 message_loop_created_.Wait(); | 69 message_loop_created_.Wait(); |
64 DCHECK(task_runner_); | 70 DCHECK(task_runner_); |
65 } | 71 } |
66 | 72 |
67 task_runner_->PostTask(FROM_HERE, | 73 task_runner_->PostTask(FROM_HERE, |
68 base::Bind(&BackgroundShellServiceLoader::LoadServiceOnBackgroundThread, | 74 base::Bind(&BackgroundShellServiceLoader::LoadOnBackgroundThread, |
69 base::Unretained(this), manager, url, | 75 base::Unretained(this), manager, url, |
70 base::Owned( | 76 base::Owned( |
71 new ScopedMessagePipeHandle(shell_handle.Pass())))); | 77 new ScopedMessagePipeHandle(shell_handle.Pass())))); |
72 } | 78 } |
73 | 79 |
74 void BackgroundShellServiceLoader::OnServiceError( | 80 void BackgroundShellServiceLoader::OnServiceError( |
75 ServiceManager* manager, const GURL& url) { | 81 ServiceManager* manager, const GURL& url) { |
76 task_runner_->PostTask(FROM_HERE, | 82 task_runner_->PostTask(FROM_HERE, |
77 base::Bind( | 83 base::Bind( |
78 &BackgroundShellServiceLoader::OnServiceErrorOnBackgroundThread, | 84 &BackgroundShellServiceLoader::OnServiceErrorOnBackgroundThread, |
79 base::Unretained(this), manager, url)); | 85 base::Unretained(this), manager, url)); |
80 } | 86 } |
81 | 87 |
82 void BackgroundShellServiceLoader::Run() { | 88 void BackgroundShellServiceLoader::Run() { |
83 base::MessageLoop message_loop(message_loop_type_); | 89 base::MessageLoop message_loop(message_loop_type_); |
84 base::RunLoop loop; | 90 base::RunLoop loop; |
85 task_runner_ = message_loop.task_runner(); | 91 task_runner_ = message_loop.task_runner(); |
86 quit_closure_ = loop.QuitClosure(); | 92 quit_closure_ = loop.QuitClosure(); |
87 message_loop_created_.Signal(); | 93 message_loop_created_.Signal(); |
88 loop.Run(); | 94 loop.Run(); |
89 | 95 |
90 delete background_loader_; | 96 delete background_loader_; |
91 background_loader_ = NULL; | 97 background_loader_ = NULL; |
92 // Destroy |loader_| on the thread it's actually used on. | 98 // Destroy |loader_| on the thread it's actually used on. |
93 loader_.reset(); | 99 loader_.reset(); |
94 } | 100 } |
95 | 101 |
96 void BackgroundShellServiceLoader::LoadServiceOnBackgroundThread( | 102 void BackgroundShellServiceLoader::LoadOnBackgroundThread( |
97 ServiceManager* manager, | 103 ServiceManager* manager, |
98 const GURL& url, | 104 const GURL& url, |
99 ScopedMessagePipeHandle* shell_handle) { | 105 ScopedMessagePipeHandle* shell_handle) { |
100 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 106 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
101 if (!background_loader_) | 107 if (!background_loader_) |
102 background_loader_ = new BackgroundLoader(loader_.get()); | 108 background_loader_ = new BackgroundLoader(loader_.get()); |
103 background_loader_->LoadService(manager, url, shell_handle->Pass()); | 109 background_loader_->Load(manager, url, shell_handle->Pass()); |
104 } | 110 } |
105 | 111 |
106 void BackgroundShellServiceLoader::OnServiceErrorOnBackgroundThread( | 112 void BackgroundShellServiceLoader::OnServiceErrorOnBackgroundThread( |
107 ServiceManager* manager, const GURL& url) { | 113 ServiceManager* manager, const GURL& url) { |
108 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 114 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
109 if (!background_loader_) | 115 if (!background_loader_) |
110 background_loader_ = new BackgroundLoader(loader_.get()); | 116 background_loader_ = new BackgroundLoader(loader_.get()); |
111 background_loader_->OnServiceError(manager, url); | 117 background_loader_->OnServiceError(manager, url); |
112 } | 118 } |
113 | 119 |
114 } // namespace mojo | 120 } // namespace mojo |
OLD | NEW |