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/shell/dynamic_service_loader.h" | 5 #include "mojo/shell/dynamic_service_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/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "mojo/common/common_type_converters.h" | 13 #include "mojo/common/common_type_converters.h" |
14 #include "mojo/common/data_pipe_utils.h" | 14 #include "mojo/common/data_pipe_utils.h" |
15 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" | 15 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" |
16 #include "mojo/shell/context.h" | 16 #include "mojo/shell/context.h" |
17 #include "mojo/shell/keep_alive.h" | 17 #include "mojo/shell/keep_alive.h" |
18 #include "mojo/shell/switches.h" | 18 #include "mojo/shell/switches.h" |
19 #include "net/base/filename_util.h" | 19 #include "net/base/filename_util.h" |
20 | 20 |
21 namespace mojo { | 21 namespace mojo { |
22 namespace shell { | 22 namespace shell { |
23 | |
24 namespace { | 23 namespace { |
25 | 24 |
26 void RunLibraryComplete(DynamicServiceRunner* runner, | 25 class Loader { |
27 const base::FilePath& temp_file) { | 26 public: |
28 delete runner; | 27 explicit Loader(scoped_ptr<DynamicServiceRunner> runner) |
29 if (!temp_file.empty()) | 28 : runner_(runner.Pass()) { |
30 base::DeleteFile(temp_file, false); | 29 } |
31 } | 30 |
| 31 virtual void Start(const GURL& url, |
| 32 ScopedMessagePipeHandle service_handle, |
| 33 Context* context) = 0; |
| 34 |
| 35 void StartService(const base::FilePath& path, |
| 36 ScopedMessagePipeHandle service_handle, |
| 37 bool path_is_valid) { |
| 38 if (path_is_valid) { |
| 39 runner_->Start(path, service_handle.Pass(), |
| 40 base::Bind(&Loader::AppCompleted, base::Unretained(this))); |
| 41 } else { |
| 42 AppCompleted(); |
| 43 } |
| 44 } |
| 45 |
| 46 protected: |
| 47 virtual ~Loader() {} |
| 48 |
| 49 private: |
| 50 void AppCompleted() { |
| 51 delete this; |
| 52 } |
| 53 |
| 54 scoped_ptr<DynamicServiceRunner> runner_; |
| 55 }; |
| 56 |
| 57 // For loading services via file:// URLs. |
| 58 class LocalLoader : public Loader { |
| 59 public: |
| 60 explicit LocalLoader(scoped_ptr<DynamicServiceRunner> runner) |
| 61 : Loader(runner.Pass()) { |
| 62 } |
| 63 |
| 64 virtual void Start(const GURL& url, |
| 65 ScopedMessagePipeHandle service_handle, |
| 66 Context* context) OVERRIDE { |
| 67 base::FilePath path; |
| 68 net::FileURLToFilePath(url, &path); |
| 69 |
| 70 // Complete asynchronously for consistency with NetworkServiceLoader. |
| 71 base::MessageLoop::current()->PostTask( |
| 72 FROM_HERE, |
| 73 base::Bind(&Loader::StartService, |
| 74 base::Unretained(this), |
| 75 path, |
| 76 base::Passed(&service_handle), |
| 77 base::PathExists(path))); |
| 78 } |
| 79 }; |
| 80 |
| 81 // For loading services via the network stack. |
| 82 class NetworkLoader : public Loader { |
| 83 public: |
| 84 explicit NetworkLoader(scoped_ptr<DynamicServiceRunner> runner, |
| 85 NetworkService* network_service) |
| 86 : Loader(runner.Pass()) { |
| 87 network_service->CreateURLLoader(Get(&url_loader_)); |
| 88 } |
| 89 |
| 90 virtual void Start(const GURL& url, |
| 91 ScopedMessagePipeHandle service_handle, |
| 92 Context* context) OVERRIDE { |
| 93 service_handle_ = service_handle.Pass(); |
| 94 context_ = context; |
| 95 |
| 96 URLRequestPtr request(URLRequest::New()); |
| 97 request->url = String::From(url); |
| 98 request->auto_follow_redirects = true; |
| 99 |
| 100 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 101 switches::kDisableCache)) { |
| 102 request->bypass_cache = true; |
| 103 } |
| 104 |
| 105 url_loader_->Start(request.Pass(), |
| 106 base::Bind(&NetworkLoader::OnReceivedResponse, |
| 107 base::Unretained(this))); |
| 108 } |
| 109 |
| 110 private: |
| 111 virtual ~NetworkLoader() { |
| 112 if (!file_.empty()) |
| 113 base::DeleteFile(file_, false); |
| 114 } |
| 115 |
| 116 void OnReceivedResponse(URLResponsePtr response) { |
| 117 if (response->error) { |
| 118 LOG(ERROR) << "Error (" << response->error->code << ": " |
| 119 << response->error->description << ") while fetching " |
| 120 << response->url; |
| 121 } |
| 122 |
| 123 base::CreateTemporaryFile(&file_); |
| 124 common::CopyToFile(response->body.Pass(), |
| 125 file_, |
| 126 context_->task_runners()->blocking_pool(), |
| 127 base::Bind(&Loader::StartService, |
| 128 base::Unretained(this), |
| 129 file_, |
| 130 base::Passed(&service_handle_))); |
| 131 } |
| 132 |
| 133 Context* context_; |
| 134 NetworkServicePtr network_service_; |
| 135 URLLoaderPtr url_loader_; |
| 136 ScopedMessagePipeHandle service_handle_; |
| 137 base::FilePath file_; |
| 138 }; |
32 | 139 |
33 } // namespace | 140 } // namespace |
34 | 141 |
35 DynamicServiceLoader::DynamicServiceLoader( | 142 DynamicServiceLoader::DynamicServiceLoader( |
36 Context* context, | 143 Context* context, |
37 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) | 144 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) |
38 : context_(context), | 145 : context_(context), |
39 runner_factory_(runner_factory.Pass()), | 146 runner_factory_(runner_factory.Pass()) { |
40 weak_ptr_factory_(this) { | |
41 } | 147 } |
42 | 148 |
43 DynamicServiceLoader::~DynamicServiceLoader() { | 149 DynamicServiceLoader::~DynamicServiceLoader() { |
44 } | 150 } |
45 | 151 |
46 void DynamicServiceLoader::RegisterContentHandler( | 152 void DynamicServiceLoader::LoadService(ServiceManager* manager, |
47 const std::string& mime_type, | 153 const GURL& url, |
48 const GURL& content_handler_url) { | 154 ScopedMessagePipeHandle shell_handle) { |
49 mime_type_to_url_[mime_type] = content_handler_url; | 155 scoped_ptr<DynamicServiceRunner> runner = runner_factory_->Create(context_); |
50 } | |
51 | 156 |
52 void DynamicServiceLoader::Load(ServiceManager* manager, | |
53 const GURL& url, | |
54 scoped_refptr<LoadCallbacks> callbacks) { | |
55 GURL resolved_url; | 157 GURL resolved_url; |
56 if (url.SchemeIs("mojo")) { | 158 if (url.SchemeIs("mojo")) { |
57 resolved_url = context_->mojo_url_resolver()->Resolve(url); | 159 resolved_url = context_->mojo_url_resolver()->Resolve(url); |
58 } else { | 160 } else { |
59 resolved_url = url; | 161 resolved_url = url; |
60 } | 162 } |
61 | 163 |
62 if (resolved_url.SchemeIsFile()) | 164 Loader* loader; |
63 LoadLocalService(resolved_url, callbacks); | 165 if (resolved_url.SchemeIsFile()) { |
64 else | 166 loader = new LocalLoader(runner.Pass()); |
65 LoadNetworkService(resolved_url, callbacks); | 167 } else { |
66 } | 168 if (!network_service_) { |
67 | 169 context_->service_manager()->ConnectToService( |
68 void DynamicServiceLoader::LoadLocalService( | 170 GURL("mojo:mojo_network_service"), |
69 const GURL& resolved_url, | 171 &network_service_); |
70 scoped_refptr<LoadCallbacks> callbacks) { | 172 } |
71 base::FilePath path; | 173 loader = new NetworkLoader(runner.Pass(), network_service_.get()); |
72 net::FileURLToFilePath(resolved_url, &path); | |
73 const bool kDeleteFileAfter = false; | |
74 | |
75 // Async for consistency with network case. | |
76 base::MessageLoop::current()->PostTask( | |
77 FROM_HERE, | |
78 base::Bind(&DynamicServiceLoader::RunLibrary, | |
79 weak_ptr_factory_.GetWeakPtr(), | |
80 path, | |
81 callbacks, | |
82 kDeleteFileAfter, | |
83 base::PathExists(path))); | |
84 } | |
85 | |
86 void DynamicServiceLoader::LoadNetworkService( | |
87 const GURL& resolved_url, | |
88 scoped_refptr<LoadCallbacks> callbacks) { | |
89 if (!network_service_) { | |
90 context_->service_manager()->ConnectToService( | |
91 GURL("mojo:mojo_network_service"), | |
92 &network_service_); | |
93 } | 174 } |
94 if (!url_loader_) | 175 loader->Start(resolved_url, shell_handle.Pass(), context_); |
95 network_service_->CreateURLLoader(Get(&url_loader_)); | |
96 | |
97 URLRequestPtr request(URLRequest::New()); | |
98 request->url = String::From(resolved_url); | |
99 request->auto_follow_redirects = true; | |
100 | |
101 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
102 switches::kDisableCache)) { | |
103 request->bypass_cache = true; | |
104 } | |
105 | |
106 url_loader_->Start( | |
107 request.Pass(), | |
108 base::Bind(&DynamicServiceLoader::OnLoadNetworkServiceComplete, | |
109 weak_ptr_factory_.GetWeakPtr(), | |
110 callbacks)); | |
111 } | |
112 | |
113 void DynamicServiceLoader::OnLoadNetworkServiceComplete( | |
114 scoped_refptr<LoadCallbacks> callbacks, URLResponsePtr response) { | |
115 if (response->error) { | |
116 LOG(ERROR) << "Error (" << response->error->code << ": " | |
117 << response->error->description << ") while fetching " | |
118 << response->url; | |
119 } | |
120 | |
121 MimeTypeToURLMap::iterator iter = | |
122 mime_type_to_url_.find(response->mime_type); | |
123 if (iter != mime_type_to_url_.end()) { | |
124 callbacks->LoadWithContentHandler(iter->second, response.Pass()); | |
125 return; | |
126 } | |
127 | |
128 base::FilePath file; | |
129 base::CreateTemporaryFile(&file); | |
130 | |
131 const bool kDeleteFileAfter = true; | |
132 common::CopyToFile(response->body.Pass(), | |
133 file, | |
134 context_->task_runners()->blocking_pool(), | |
135 base::Bind(&DynamicServiceLoader::RunLibrary, | |
136 weak_ptr_factory_.GetWeakPtr(), | |
137 file, | |
138 callbacks, | |
139 kDeleteFileAfter)); | |
140 } | |
141 | |
142 void DynamicServiceLoader::RunLibrary(const base::FilePath& path, | |
143 scoped_refptr<LoadCallbacks> callbacks, | |
144 bool delete_file_after, | |
145 bool path_exists) { | |
146 // TODO(aa): We need to create a runner, even if we're not going to use it, | |
147 // because it getting destroyed is what causes shell to shut down. If we don't | |
148 // create this, in the case where shell never successfully creates even one | |
149 // app, then shell will never shut down, because no runners are ever | |
150 // destroyed. | |
151 scoped_ptr<DynamicServiceRunner> runner(runner_factory_->Create(context_)); | |
152 if (!path_exists) | |
153 return; | |
154 | |
155 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); | |
156 if (!shell_handle.is_valid()) | |
157 return; | |
158 | |
159 DynamicServiceRunner* runner_raw = runner.release(); | |
160 runner_raw->Start(path, | |
161 shell_handle.Pass(), | |
162 base::Bind(&RunLibraryComplete, | |
163 base::Unretained(runner_raw), | |
164 delete_file_after ? path : base::FilePath())); | |
165 } | 176 } |
166 | 177 |
167 void DynamicServiceLoader::OnServiceError(ServiceManager* manager, | 178 void DynamicServiceLoader::OnServiceError(ServiceManager* manager, |
168 const GURL& url) { | 179 const GURL& url) { |
169 // TODO(darin): What should we do about service errors? This implies that | 180 // TODO(darin): What should we do about service errors? This implies that |
170 // the app closed its handle to the service manager. Maybe we don't care? | 181 // the app closed its handle to the service manager. Maybe we don't care? |
171 } | 182 } |
172 | 183 |
173 } // namespace shell | 184 } // namespace shell |
174 } // namespace mojo | 185 } // namespace mojo |
OLD | NEW |