OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "apps/launcher.h" | 5 #include "apps/launcher.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" | 17 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" |
18 #include "chrome/browser/extensions/api/file_handlers/mime_util.h" | 18 #include "chrome/browser/extensions/api/file_handlers/mime_util.h" |
19 #include "chrome/browser/extensions/api/file_system/file_system_api.h" | 19 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
23 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
24 #include "content/public/common/content_switches.h" | 24 #include "content/public/common/content_switches.h" |
25 #include "content/public/common/url_constants.h" | 25 #include "content/public/common/url_constants.h" |
26 #include "extensions/browser/api/app_runtime/app_runtime_api.h" | 26 #include "extensions/browser/api/app_runtime/app_runtime_api.h" |
27 #include "extensions/browser/event_router.h" | 27 #include "extensions/browser/event_router.h" |
28 #include "extensions/browser/extension_host.h" | 28 #include "extensions/browser/extension_host.h" |
29 #include "extensions/browser/extension_prefs.h" | 29 #include "extensions/browser/extension_prefs.h" |
30 #include "extensions/browser/extension_registry.h" | |
30 #include "extensions/browser/extension_system.h" | 31 #include "extensions/browser/extension_system.h" |
31 #include "extensions/browser/granted_file_entry.h" | 32 #include "extensions/browser/granted_file_entry.h" |
32 #include "extensions/browser/lazy_background_task_queue.h" | 33 #include "extensions/browser/lazy_background_task_queue.h" |
33 #include "extensions/browser/process_manager.h" | 34 #include "extensions/browser/process_manager.h" |
34 #include "extensions/common/api/app_runtime.h" | 35 #include "extensions/common/api/app_runtime.h" |
35 #include "extensions/common/extension.h" | 36 #include "extensions/common/extension.h" |
36 #include "extensions/common/manifest_handlers/kiosk_mode_info.h" | 37 #include "extensions/common/manifest_handlers/kiosk_mode_info.h" |
37 #include "net/base/filename_util.h" | 38 #include "net/base/filename_util.h" |
38 #include "net/base/net_util.h" | 39 #include "net/base/net_util.h" |
39 #include "url/gurl.h" | 40 #include "url/gurl.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 // instances is managed by reference counted pointers. As long as an instance | 91 // instances is managed by reference counted pointers. As long as an instance |
91 // has outstanding tasks on a message queue it will be retained; once all | 92 // has outstanding tasks on a message queue it will be retained; once all |
92 // outstanding tasks are completed it will be deleted. | 93 // outstanding tasks are completed it will be deleted. |
93 class PlatformAppPathLauncher | 94 class PlatformAppPathLauncher |
94 : public base::RefCountedThreadSafe<PlatformAppPathLauncher> { | 95 : public base::RefCountedThreadSafe<PlatformAppPathLauncher> { |
95 public: | 96 public: |
96 PlatformAppPathLauncher(Profile* profile, | 97 PlatformAppPathLauncher(Profile* profile, |
97 const Extension* extension, | 98 const Extension* extension, |
98 const std::vector<base::FilePath>& file_paths) | 99 const std::vector<base::FilePath>& file_paths) |
99 : profile_(profile), | 100 : profile_(profile), |
100 extension_(extension), | 101 extension_id(extension->id()), |
101 file_paths_(file_paths), | 102 file_paths_(file_paths), |
102 collector_(profile) {} | 103 collector_(profile) {} |
103 | 104 |
104 PlatformAppPathLauncher(Profile* profile, | 105 PlatformAppPathLauncher(Profile* profile, |
105 const Extension* extension, | 106 const Extension* extension, |
106 const base::FilePath& file_path) | 107 const base::FilePath& file_path) |
107 : profile_(profile), extension_(extension), collector_(profile) { | 108 : profile_(profile), extension_id(extension->id()), collector_(profile) { |
108 if (!file_path.empty()) | 109 if (!file_path.empty()) |
109 file_paths_.push_back(file_path); | 110 file_paths_.push_back(file_path); |
110 } | 111 } |
111 | 112 |
112 void Launch() { | 113 void Launch() { |
114 const Extension* extension = GetExtension(); | |
115 if (!extension) | |
116 return; | |
117 | |
113 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 118 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
tapted
2014/11/20 06:15:29
nit: maybe have this DCHECK first?
benwells
2014/11/20 07:07:18
Done.
| |
114 if (file_paths_.empty()) { | 119 if (file_paths_.empty()) { |
115 LaunchWithNoLaunchData(); | 120 LaunchWithNoLaunchData(); |
116 return; | 121 return; |
117 } | 122 } |
118 | 123 |
119 for (size_t i = 0; i < file_paths_.size(); ++i) { | 124 for (size_t i = 0; i < file_paths_.size(); ++i) { |
120 DCHECK(file_paths_[i].IsAbsolute()); | 125 DCHECK(file_paths_[i].IsAbsolute()); |
121 } | 126 } |
122 | 127 |
123 if (HasFileSystemWritePermission(extension_)) { | 128 if (HasFileSystemWritePermission(extension)) { |
124 PrepareFilesForWritableApp( | 129 PrepareFilesForWritableApp( |
125 file_paths_, | 130 file_paths_, |
126 profile_, | 131 profile_, |
127 false, | 132 false, |
128 base::Bind(&PlatformAppPathLauncher::OnFilesValid, this), | 133 base::Bind(&PlatformAppPathLauncher::OnFilesValid, this), |
129 base::Bind(&PlatformAppPathLauncher::OnFilesInvalid, this)); | 134 base::Bind(&PlatformAppPathLauncher::OnFilesInvalid, this)); |
130 return; | 135 return; |
131 } | 136 } |
132 | 137 |
133 OnFilesValid(); | 138 OnFilesValid(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 collector_.CollectForLocalPaths( | 182 collector_.CollectForLocalPaths( |
178 file_paths_, | 183 file_paths_, |
179 base::Bind(&PlatformAppPathLauncher::OnMimeTypesCollected, this)); | 184 base::Bind(&PlatformAppPathLauncher::OnMimeTypesCollected, this)); |
180 } | 185 } |
181 | 186 |
182 void OnFilesInvalid(const base::FilePath& /* error_path */) { | 187 void OnFilesInvalid(const base::FilePath& /* error_path */) { |
183 LaunchWithNoLaunchData(); | 188 LaunchWithNoLaunchData(); |
184 } | 189 } |
185 | 190 |
186 void LaunchWithNoLaunchData() { | 191 void LaunchWithNoLaunchData() { |
192 const Extension* extension = GetExtension(); | |
193 if (!extension) | |
194 return; | |
195 | |
187 // This method is required as an entry point on the UI thread. | 196 // This method is required as an entry point on the UI thread. |
188 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 197 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
tapted
2014/11/20 06:15:29
nit: here too
benwells
2014/11/20 07:07:18
Done.
| |
189 AppRuntimeEventRouter::DispatchOnLaunchedEvent( | 198 AppRuntimeEventRouter::DispatchOnLaunchedEvent( |
190 profile_, extension_, extensions::SOURCE_FILE_HANDLER); | 199 profile_, extension, extensions::SOURCE_FILE_HANDLER); |
191 } | 200 } |
192 | 201 |
193 void OnMimeTypesCollected(scoped_ptr<std::vector<std::string> > mime_types) { | 202 void OnMimeTypesCollected(scoped_ptr<std::vector<std::string> > mime_types) { |
194 DCHECK(file_paths_.size() == mime_types->size()); | 203 DCHECK(file_paths_.size() == mime_types->size()); |
195 | 204 |
205 const Extension* extension = GetExtension(); | |
206 if (!extension) | |
207 return; | |
208 | |
196 // If fetching a mime type failed, then use a fallback one. | 209 // If fetching a mime type failed, then use a fallback one. |
197 for (size_t i = 0; i < mime_types->size(); ++i) { | 210 for (size_t i = 0; i < mime_types->size(); ++i) { |
198 const std::string mime_type = | 211 const std::string mime_type = |
199 !(*mime_types)[i].empty() ? (*mime_types)[i] : kFallbackMimeType; | 212 !(*mime_types)[i].empty() ? (*mime_types)[i] : kFallbackMimeType; |
200 mime_types_.push_back(mime_type); | 213 mime_types_.push_back(mime_type); |
201 } | 214 } |
202 | 215 |
203 // Find file handler from the platform app for the file being opened. | 216 // Find file handler from the platform app for the file being opened. |
204 const extensions::FileHandlerInfo* handler = NULL; | 217 const extensions::FileHandlerInfo* handler = NULL; |
205 if (!handler_id_.empty()) { | 218 if (!handler_id_.empty()) { |
206 handler = FileHandlerForId(*extension_, handler_id_); | 219 handler = FileHandlerForId(*extension, handler_id_); |
207 if (handler) { | 220 if (handler) { |
208 for (size_t i = 0; i < file_paths_.size(); ++i) { | 221 for (size_t i = 0; i < file_paths_.size(); ++i) { |
209 if (!FileHandlerCanHandleFile( | 222 if (!FileHandlerCanHandleFile( |
210 *handler, mime_types_[i], file_paths_[i])) { | 223 *handler, mime_types_[i], file_paths_[i])) { |
211 LOG(WARNING) | 224 LOG(WARNING) |
212 << "Extension does not provide a valid file handler for " | 225 << "Extension does not provide a valid file handler for " |
213 << file_paths_[i].value(); | 226 << file_paths_[i].value(); |
214 handler = NULL; | 227 handler = NULL; |
215 break; | 228 break; |
216 } | 229 } |
217 } | 230 } |
218 } | 231 } |
219 } else { | 232 } else { |
220 std::set<std::pair<base::FilePath, std::string> > path_and_file_type_set; | 233 std::set<std::pair<base::FilePath, std::string> > path_and_file_type_set; |
221 for (size_t i = 0; i < file_paths_.size(); ++i) { | 234 for (size_t i = 0; i < file_paths_.size(); ++i) { |
222 path_and_file_type_set.insert( | 235 path_and_file_type_set.insert( |
223 std::make_pair(file_paths_[i], mime_types_[i])); | 236 std::make_pair(file_paths_[i], mime_types_[i])); |
224 } | 237 } |
225 const std::vector<const extensions::FileHandlerInfo*>& handlers = | 238 const std::vector<const extensions::FileHandlerInfo*>& handlers = |
226 extensions::app_file_handler_util::FindFileHandlersForFiles( | 239 extensions::app_file_handler_util::FindFileHandlersForFiles( |
227 *extension_, path_and_file_type_set); | 240 *extension, path_and_file_type_set); |
228 if (!handlers.empty()) | 241 if (!handlers.empty()) |
229 handler = handlers[0]; | 242 handler = handlers[0]; |
230 } | 243 } |
231 | 244 |
232 // If this app doesn't have a file handler that supports the file, launch | 245 // If this app doesn't have a file handler that supports the file, launch |
233 // with no launch data. | 246 // with no launch data. |
234 if (!handler) { | 247 if (!handler) { |
235 LOG(WARNING) << "Extension does not provide a valid file handler."; | 248 LOG(WARNING) << "Extension does not provide a valid file handler."; |
236 LaunchWithNoLaunchData(); | 249 LaunchWithNoLaunchData(); |
237 return; | 250 return; |
238 } | 251 } |
239 | 252 |
240 if (handler_id_.empty()) | 253 if (handler_id_.empty()) |
241 handler_id_ = handler->id; | 254 handler_id_ = handler->id; |
242 | 255 |
243 // Access needs to be granted to the file for the process associated with | 256 // Access needs to be granted to the file for the process associated with |
244 // the extension. To do this the ExtensionHost is needed. This might not be | 257 // the extension. To do this the ExtensionHost is needed. This might not be |
245 // available, or it might be in the process of being unloaded, in which case | 258 // available, or it might be in the process of being unloaded, in which case |
246 // the lazy background task queue is used to load the extension and then | 259 // the lazy background task queue is used to load the extension and then |
247 // call back to us. | 260 // call back to us. |
248 extensions::LazyBackgroundTaskQueue* const queue = | 261 extensions::LazyBackgroundTaskQueue* const queue = |
249 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); | 262 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); |
250 if (queue->ShouldEnqueueTask(profile_, extension_)) { | 263 if (queue->ShouldEnqueueTask(profile_, extension)) { |
251 queue->AddPendingTask( | 264 queue->AddPendingTask( |
252 profile_, | 265 profile_, extension_id, |
253 extension_->id(), | |
254 base::Bind(&PlatformAppPathLauncher::GrantAccessToFilesAndLaunch, | 266 base::Bind(&PlatformAppPathLauncher::GrantAccessToFilesAndLaunch, |
255 this)); | 267 this)); |
256 return; | 268 return; |
257 } | 269 } |
258 | 270 |
259 extensions::ProcessManager* const process_manager = | 271 extensions::ProcessManager* const process_manager = |
260 extensions::ProcessManager::Get(profile_); | 272 extensions::ProcessManager::Get(profile_); |
261 ExtensionHost* const host = | 273 ExtensionHost* const host = |
262 process_manager->GetBackgroundHostForExtension(extension_->id()); | 274 process_manager->GetBackgroundHostForExtension(extension_id); |
263 DCHECK(host); | 275 DCHECK(host); |
264 GrantAccessToFilesAndLaunch(host); | 276 GrantAccessToFilesAndLaunch(host); |
265 } | 277 } |
266 | 278 |
267 void GrantAccessToFilesAndLaunch(ExtensionHost* host) { | 279 void GrantAccessToFilesAndLaunch(ExtensionHost* host) { |
280 const Extension* extension = GetExtension(); | |
281 if (!extension) | |
282 return; | |
283 | |
268 // If there was an error loading the app page, |host| will be NULL. | 284 // If there was an error loading the app page, |host| will be NULL. |
269 if (!host) { | 285 if (!host) { |
270 LOG(ERROR) << "Could not load app page for " << extension_->id(); | 286 LOG(ERROR) << "Could not load app page for " << extension_id; |
271 return; | 287 return; |
272 } | 288 } |
273 | 289 |
274 std::vector<GrantedFileEntry> file_entries; | 290 std::vector<GrantedFileEntry> file_entries; |
275 for (size_t i = 0; i < file_paths_.size(); ++i) { | 291 for (size_t i = 0; i < file_paths_.size(); ++i) { |
276 file_entries.push_back( | 292 file_entries.push_back(CreateFileEntry( |
277 CreateFileEntry(profile_, | 293 profile_, extension, host->render_process_host()->GetID(), |
278 extension_, | 294 file_paths_[i], false)); |
279 host->render_process_host()->GetID(), | |
280 file_paths_[i], | |
281 false)); | |
282 } | 295 } |
283 | 296 |
284 AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries( | 297 AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries( |
285 profile_, extension_, handler_id_, mime_types_, file_entries); | 298 profile_, extension, handler_id_, mime_types_, file_entries); |
299 } | |
300 | |
301 const Extension* GetExtension() const { | |
302 return extensions::ExtensionRegistry::Get(profile_)->GetExtensionById( | |
303 extension_id, extensions::ExtensionRegistry::EVERYTHING); | |
tapted
2014/11/20 06:15:29
maybe just extensions::ExtensionRegistry::ENABLED?
benwells
2014/11/20 07:07:18
I thought about that but didn't want to apply any
| |
286 } | 304 } |
287 | 305 |
288 // The profile the app should be run in. | 306 // The profile the app should be run in. |
289 Profile* profile_; | 307 Profile* profile_; |
290 // The extension providing the app. | 308 // The id of the extension providing the app. A pointer to the extension is |
291 // TODO(benwells): Hold onto the extension ID instead of a pointer as it | 309 // not kept as the extension may be unloaded and deleted during the course of |
292 // is possible the extension will be unloaded while we're doing our thing. | 310 // the launch. |
293 // See http://crbug.com/372270 for details. | 311 std::string extension_id; |
tapted
2014/11/20 06:15:29
nit: declare const?
benwells
2014/11/20 07:07:18
Done.
| |
294 const Extension* extension_; | |
295 // The path to be passed through to the app. | 312 // The path to be passed through to the app. |
296 std::vector<base::FilePath> file_paths_; | 313 std::vector<base::FilePath> file_paths_; |
297 std::vector<std::string> mime_types_; | 314 std::vector<std::string> mime_types_; |
298 // The ID of the file handler used to launch the app. | 315 // The ID of the file handler used to launch the app. |
299 std::string handler_id_; | 316 std::string handler_id_; |
300 extensions::app_file_handler_util::MimeTypeCollector collector_; | 317 extensions::app_file_handler_util::MimeTypeCollector collector_; |
301 | 318 |
302 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); | 319 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); |
303 }; | 320 }; |
304 | 321 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
406 void LaunchPlatformAppWithUrl(Profile* profile, | 423 void LaunchPlatformAppWithUrl(Profile* profile, |
407 const Extension* extension, | 424 const Extension* extension, |
408 const std::string& handler_id, | 425 const std::string& handler_id, |
409 const GURL& url, | 426 const GURL& url, |
410 const GURL& referrer_url) { | 427 const GURL& referrer_url) { |
411 AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl( | 428 AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl( |
412 profile, extension, handler_id, url, referrer_url); | 429 profile, extension, handler_id, url, referrer_url); |
413 } | 430 } |
414 | 431 |
415 } // namespace apps | 432 } // namespace apps |
OLD | NEW |