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_service_loader.h" | 5 #include "mojo/service_manager/background_service_loader.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "mojo/service_manager/service_manager.h" | 8 #include "mojo/service_manager/service_manager.h" |
9 | 9 |
10 namespace mojo { | 10 namespace mojo { |
11 | 11 |
12 class BackgroundServiceLoader::BackgroundLoader { | 12 class BackgroundServiceLoader::BackgroundLoader { |
13 public: | 13 public: |
14 explicit BackgroundLoader(ServiceLoader* loader) : loader_(loader) {} | 14 explicit BackgroundLoader(ServiceLoader* loader) : loader_(loader) {} |
15 ~BackgroundLoader() {} | 15 ~BackgroundLoader() {} |
16 | 16 |
17 void LoadService(ServiceManager* manager, | 17 void LoadService(ServiceManager* manager, |
18 const GURL& url, | 18 const GURL& url, |
19 ScopedMessagePipeHandle shell_handle) { | 19 ScopedMessagePipeHandle shell_handle) { |
20 loader_->LoadService(manager, url, shell_handle.Pass()); | 20 scoped_refptr<LoadServiceCallbacks> callbacks( |
| 21 new ServiceLoader::SimpleLoadServiceCallbacks(shell_handle.Pass())); |
| 22 loader_->LoadService(manager, url, callbacks); |
21 } | 23 } |
22 | 24 |
23 void OnServiceError(ServiceManager* manager, const GURL& url) { | 25 void OnServiceError(ServiceManager* manager, const GURL& url) { |
24 loader_->OnServiceError(manager, url); | 26 loader_->OnServiceError(manager, url); |
25 } | 27 } |
26 | 28 |
27 private: | 29 private: |
28 base::MessageLoop::Type message_loop_type_; | 30 base::MessageLoop::Type message_loop_type_; |
29 ServiceLoader* loader_; // Owned by BackgroundServiceLoader | 31 ServiceLoader* loader_; // Owned by BackgroundServiceLoader |
30 | 32 |
(...skipping 16 matching lines...) Expand all Loading... |
47 FROM_HERE, | 49 FROM_HERE, |
48 base::Bind(&BackgroundServiceLoader::ShutdownOnBackgroundThread, | 50 base::Bind(&BackgroundServiceLoader::ShutdownOnBackgroundThread, |
49 base::Unretained(this))); | 51 base::Unretained(this))); |
50 } | 52 } |
51 thread_.Stop(); | 53 thread_.Stop(); |
52 } | 54 } |
53 | 55 |
54 void BackgroundServiceLoader::LoadService( | 56 void BackgroundServiceLoader::LoadService( |
55 ServiceManager* manager, | 57 ServiceManager* manager, |
56 const GURL& url, | 58 const GURL& url, |
57 ScopedMessagePipeHandle shell_handle) { | 59 scoped_refptr<LoadServiceCallbacks> callbacks) { |
58 const int kDefaultStackSize = 0; | 60 const int kDefaultStackSize = 0; |
| 61 |
| 62 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); |
| 63 if (!shell_handle.is_valid()) { |
| 64 return; |
| 65 } |
| 66 |
59 if (!thread_.IsRunning()) { | 67 if (!thread_.IsRunning()) { |
60 thread_.StartWithOptions( | 68 thread_.StartWithOptions( |
61 base::Thread::Options(message_loop_type_, kDefaultStackSize)); | 69 base::Thread::Options(message_loop_type_, kDefaultStackSize)); |
62 } | 70 } |
63 thread_.message_loop()->PostTask( | 71 thread_.message_loop()->PostTask( |
64 FROM_HERE, | 72 FROM_HERE, |
65 base::Bind(&BackgroundServiceLoader::LoadServiceOnBackgroundThread, | 73 base::Bind(&BackgroundServiceLoader::LoadServiceOnBackgroundThread, |
66 base::Unretained(this), manager, url, | 74 base::Unretained(this), manager, url, |
67 base::Owned( | 75 base::Passed(&shell_handle))); |
68 new ScopedMessagePipeHandle(shell_handle.Pass())))); | |
69 } | 76 } |
70 | 77 |
71 void BackgroundServiceLoader::OnServiceError(ServiceManager* manager, | 78 void BackgroundServiceLoader::OnServiceError(ServiceManager* manager, |
72 const GURL& url) { | 79 const GURL& url) { |
73 if (!thread_.IsRunning()) | 80 if (!thread_.IsRunning()) |
74 thread_.Start(); | 81 thread_.Start(); |
75 thread_.message_loop()->PostTask( | 82 thread_.message_loop()->PostTask( |
76 FROM_HERE, | 83 FROM_HERE, |
77 base::Bind(&BackgroundServiceLoader::OnServiceErrorOnBackgroundThread, | 84 base::Bind(&BackgroundServiceLoader::OnServiceErrorOnBackgroundThread, |
78 base::Unretained(this), manager, url)); | 85 base::Unretained(this), manager, url)); |
79 } | 86 } |
80 | 87 |
81 void BackgroundServiceLoader::LoadServiceOnBackgroundThread( | 88 void BackgroundServiceLoader::LoadServiceOnBackgroundThread( |
82 ServiceManager* manager, | 89 ServiceManager* manager, |
83 const GURL& url, | 90 const GURL& url, |
84 ScopedMessagePipeHandle* shell_handle) { | 91 ScopedMessagePipeHandle shell_handle) { |
85 if (!background_loader_) | 92 if (!background_loader_) |
86 background_loader_ = new BackgroundLoader(loader_.get()); | 93 background_loader_ = new BackgroundLoader(loader_.get()); |
87 background_loader_->LoadService( | 94 background_loader_->LoadService( |
88 manager, url, shell_handle->Pass()); | 95 manager, url, shell_handle.Pass()); |
89 } | 96 } |
90 | 97 |
91 void BackgroundServiceLoader::OnServiceErrorOnBackgroundThread( | 98 void BackgroundServiceLoader::OnServiceErrorOnBackgroundThread( |
92 ServiceManager* manager, | 99 ServiceManager* manager, |
93 const GURL& url) { | 100 const GURL& url) { |
94 if (!background_loader_) | 101 if (!background_loader_) |
95 background_loader_ = new BackgroundLoader(loader_.get()); | 102 background_loader_ = new BackgroundLoader(loader_.get()); |
96 background_loader_->OnServiceError(manager, url); | 103 background_loader_->OnServiceError(manager, url); |
97 } | 104 } |
98 | 105 |
99 void BackgroundServiceLoader::ShutdownOnBackgroundThread() { | 106 void BackgroundServiceLoader::ShutdownOnBackgroundThread() { |
100 delete background_loader_; | 107 delete background_loader_; |
101 // Destroy |loader_| on the thread it's actually used on. | 108 // Destroy |loader_| on the thread it's actually used on. |
102 loader_.reset(); | 109 loader_.reset(); |
103 } | 110 } |
104 | 111 |
105 } // namespace mojo | 112 } // namespace mojo |
OLD | NEW |