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 | |
23 namespace { | 24 namespace { |
24 | 25 |
25 class Loader { | 26 void DeleteRunner(DynamicServiceRunner* runner) { |
26 public: | 27 delete runner; |
27 explicit Loader(scoped_ptr<DynamicServiceRunner> runner) | 28 } |
28 : runner_(runner.Pass()) { | |
29 } | |
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 }; | |
139 | 29 |
140 } // namespace | 30 } // namespace |
141 | 31 |
142 DynamicServiceLoader::DynamicServiceLoader( | 32 DynamicServiceLoader::DynamicServiceLoader( |
143 Context* context, | 33 Context* context, |
144 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) | 34 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) |
145 : context_(context), | 35 : context_(context), |
146 runner_factory_(runner_factory.Pass()) { | 36 runner_factory_(runner_factory.Pass()), |
37 weak_ptr_factory_(this) { | |
147 } | 38 } |
148 | 39 |
149 DynamicServiceLoader::~DynamicServiceLoader() { | 40 DynamicServiceLoader::~DynamicServiceLoader() { |
150 } | 41 } |
151 | 42 |
152 void DynamicServiceLoader::LoadService(ServiceManager* manager, | 43 void DynamicServiceLoader::set_content_handler( |
153 const GURL& url, | 44 const std::string& mime_type, |
154 ScopedMessagePipeHandle shell_handle) { | 45 const GURL& content_handler_url) { |
155 scoped_ptr<DynamicServiceRunner> runner = runner_factory_->Create(context_); | 46 content_handler_map_[mime_type] = content_handler_url; |
47 } | |
156 | 48 |
49 void DynamicServiceLoader::LoadService( | |
50 ServiceManager* manager, | |
51 const GURL& url, | |
52 scoped_refptr<LoadServiceCallbacks> callbacks) { | |
157 GURL resolved_url; | 53 GURL resolved_url; |
158 if (url.SchemeIs("mojo")) { | 54 if (url.SchemeIs("mojo")) { |
159 resolved_url = context_->mojo_url_resolver()->Resolve(url); | 55 resolved_url = context_->mojo_url_resolver()->Resolve(url); |
160 } else { | 56 } else { |
161 resolved_url = url; | 57 resolved_url = url; |
162 } | 58 } |
163 | 59 |
164 Loader* loader; | |
165 if (resolved_url.SchemeIsFile()) { | 60 if (resolved_url.SchemeIsFile()) { |
DaveMoore
2014/08/04 21:50:51
Nit: no braces
Aaron Boodman
2014/08/05 05:44:54
Done.
| |
166 loader = new LocalLoader(runner.Pass()); | 61 LoadLocalService(resolved_url, callbacks); |
167 } else { | 62 } else { |
168 if (!network_service_) { | 63 LoadNetworkService(resolved_url, callbacks); |
169 context_->service_manager()->ConnectToService( | |
170 GURL("mojo:mojo_network_service"), | |
171 &network_service_); | |
172 } | |
173 loader = new NetworkLoader(runner.Pass(), network_service_.get()); | |
174 } | 64 } |
175 loader->Start(resolved_url, shell_handle.Pass(), context_); | 65 } |
66 | |
67 void DynamicServiceLoader::LoadLocalService( | |
68 const GURL& resolved_url, | |
69 scoped_refptr<LoadServiceCallbacks> callbacks) { | |
70 base::FilePath path; | |
71 net::FileURLToFilePath(resolved_url, &path); | |
72 RunLibrary(path, callbacks, base::PathExists(path)); | |
73 } | |
74 | |
75 void DynamicServiceLoader::LoadNetworkService( | |
76 const GURL& resolved_url, | |
77 scoped_refptr<LoadServiceCallbacks> callbacks) { | |
78 if (!network_service_) { | |
79 context_->service_manager()->ConnectToService( | |
80 GURL("mojo:mojo_network_service"), | |
81 &network_service_); | |
82 } | |
83 if (!url_loader_) { | |
DaveMoore
2014/08/04 21:50:51
Nit: no braces
Aaron Boodman
2014/08/05 05:44:54
Done.
| |
84 network_service_->CreateURLLoader(Get(&url_loader_)); | |
85 } | |
86 | |
87 URLRequestPtr request(URLRequest::New()); | |
88 request->url = String::From(resolved_url); | |
89 request->auto_follow_redirects = true; | |
90 | |
91 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
92 switches::kDisableCache)) { | |
93 request->bypass_cache = true; | |
94 } | |
95 | |
96 url_loader_->Start( | |
97 request.Pass(), | |
98 base::Bind(&DynamicServiceLoader::OnLoadNetworkServiceComplete, | |
99 weak_ptr_factory_.GetWeakPtr(), | |
100 callbacks)); | |
101 } | |
102 | |
103 void DynamicServiceLoader::OnLoadNetworkServiceComplete( | |
104 scoped_refptr<LoadServiceCallbacks> callbacks, URLResponsePtr response) { | |
105 if (response->error) { | |
106 LOG(ERROR) << "Error (" << response->error->code << ": " | |
107 << response->error->description << ") while fetching " | |
108 << response->url; | |
109 } | |
110 | |
111 ContentHandlerMap::iterator iter = | |
112 content_handler_map_.find(response->mime_type); | |
113 if (iter != content_handler_map_.end()) { | |
114 callbacks->LoadWithContentHandler(iter->second, response.Pass()); | |
115 return; | |
116 } | |
117 | |
118 base::FilePath file; | |
119 base::CreateTemporaryFile(&file); | |
120 common::CopyToFile(response->body.Pass(), | |
121 file, | |
122 context_->task_runners()->blocking_pool(), | |
123 base::Bind(&DynamicServiceLoader::RunLibrary, | |
124 weak_ptr_factory_.GetWeakPtr(), | |
125 file, | |
126 callbacks)); | |
127 } | |
128 | |
129 void DynamicServiceLoader::RunLibrary( | |
130 const base::FilePath& path, | |
131 scoped_refptr<LoadServiceCallbacks> callbacks, | |
132 bool path_exists) { | |
133 if (!path_exists) { | |
DaveMoore
2014/08/04 21:50:52
Nit: no braces
Aaron Boodman
2014/08/05 05:44:55
Done.
| |
134 return; | |
135 } | |
136 | |
137 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); | |
138 if (!shell_handle.is_valid()) { | |
DaveMoore
2014/08/04 21:50:52
Nit: no braces
Aaron Boodman
2014/08/05 05:44:54
Done.
| |
139 return; | |
140 } | |
141 | |
142 DynamicServiceRunner* runner = runner_factory_->Create(context_).release(); | |
143 runner->Start(path, | |
144 shell_handle.Pass(), | |
145 base::Bind(&DeleteRunner, base::Unretained(runner))); | |
176 } | 146 } |
177 | 147 |
178 void DynamicServiceLoader::OnServiceError(ServiceManager* manager, | 148 void DynamicServiceLoader::OnServiceError(ServiceManager* manager, |
179 const GURL& url) { | 149 const GURL& url) { |
180 // TODO(darin): What should we do about service errors? This implies that | 150 // TODO(darin): What should we do about service errors? This implies that |
181 // the app closed its handle to the service manager. Maybe we don't care? | 151 // the app closed its handle to the service manager. Maybe we don't care? |
182 } | 152 } |
183 | 153 |
184 } // namespace shell | 154 } // namespace shell |
185 } // namespace mojo | 155 } // namespace mojo |
OLD | NEW |