OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/platform_app_launcher.h" | 5 #include "chrome/browser/extensions/platform_app_launcher.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" | 14 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" |
15 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" | 15 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" |
16 #include "chrome/browser/extensions/extension_host.h" | 16 #include "chrome/browser/extensions/extension_host.h" |
17 #include "chrome/browser/extensions/extension_process_manager.h" | 17 #include "chrome/browser/extensions/extension_process_manager.h" |
18 #include "chrome/browser/extensions/extension_system.h" | 18 #include "chrome/browser/extensions/extension_system.h" |
19 #include "chrome/browser/extensions/lazy_background_task_queue.h" | 19 #include "chrome/browser/extensions/lazy_background_task_queue.h" |
20 #include "chrome/browser/intents/web_intents_util.h" | |
21 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/common/extensions/extension.h" | 21 #include "chrome/common/extensions/extension.h" |
23 #include "chrome/common/extensions/extension_messages.h" | 22 #include "chrome/common/extensions/extension_messages.h" |
24 #include "chrome/common/extensions/web_intents_handler.h" | |
25 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/child_process_security_policy.h" | 24 #include "content/public/browser/child_process_security_policy.h" |
27 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
28 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
29 #include "content/public/browser/web_intents_dispatcher.h" | |
30 #include "net/base/mime_util.h" | 27 #include "net/base/mime_util.h" |
31 #include "net/base/net_util.h" | 28 #include "net/base/net_util.h" |
32 #include "webkit/fileapi/file_system_types.h" | 29 #include "webkit/fileapi/file_system_types.h" |
33 #include "webkit/fileapi/isolated_context.h" | 30 #include "webkit/fileapi/isolated_context.h" |
34 #include "webkit/glue/web_intent_data.h" | |
35 #include "webkit/glue/web_intent_service_data.h" | |
36 | 31 |
37 using content::BrowserThread; | 32 using content::BrowserThread; |
38 using extensions::app_file_handler_util::FileHandlerForId; | 33 using extensions::app_file_handler_util::FileHandlerForId; |
39 using extensions::app_file_handler_util::FileHandlerCanHandleFileWithMimeType; | 34 using extensions::app_file_handler_util::FileHandlerCanHandleFileWithMimeType; |
40 using extensions::app_file_handler_util::FirstFileHandlerForMimeType; | 35 using extensions::app_file_handler_util::FirstFileHandlerForMimeType; |
41 | 36 |
42 namespace extensions { | 37 namespace extensions { |
43 | 38 |
44 namespace { | 39 namespace { |
45 | 40 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | 141 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
147 &PlatformAppPathLauncher::LaunchWithMimeType, this, mime_type)); | 142 &PlatformAppPathLauncher::LaunchWithMimeType, this, mime_type)); |
148 } | 143 } |
149 | 144 |
150 void LaunchWithNoLaunchData() { | 145 void LaunchWithNoLaunchData() { |
151 // This method is required as an entry point on the UI thread. | 146 // This method is required as an entry point on the UI thread. |
152 LaunchPlatformAppWithNoData(profile_, extension_); | 147 LaunchPlatformAppWithNoData(profile_, extension_); |
153 } | 148 } |
154 | 149 |
155 void LaunchWithMimeType(const std::string& mime_type) { | 150 void LaunchWithMimeType(const std::string& mime_type) { |
156 // Find the intent service or file handler from the platform app for the | 151 // Find file handler from the platform app for the file being opened. |
157 // file being opened. | |
158 bool found_service = false; | |
159 | |
160 const FileHandlerInfo* handler = NULL; | 152 const FileHandlerInfo* handler = NULL; |
161 if (!handler_id_.empty()) | 153 if (!handler_id_.empty()) |
162 handler = FileHandlerForId(*extension_, handler_id_); | 154 handler = FileHandlerForId(*extension_, handler_id_); |
163 else | 155 else |
164 handler = FirstFileHandlerForMimeType(*extension_, mime_type); | 156 handler = FirstFileHandlerForMimeType(*extension_, mime_type); |
165 if (handler && | 157 if (handler && |
166 !FileHandlerCanHandleFileWithMimeType(*handler, mime_type)) { | 158 !FileHandlerCanHandleFileWithMimeType(*handler, mime_type)) { |
167 LOG(WARNING) << "Extension does not provide a valid file handler for " | 159 LOG(WARNING) << "Extension does not provide a valid file handler for " |
168 << file_path_.value(); | 160 << file_path_.value(); |
169 LaunchWithNoLaunchData(); | 161 LaunchWithNoLaunchData(); |
170 return; | 162 return; |
171 } | 163 } |
172 found_service = !!handler; | |
173 | 164 |
174 // TODO(benwells): remove this once we no longer support the "intents" | 165 // If this app doesn't have a file handler that supports the file, launch |
175 // syntax in platform app manifests. | 166 // with no launch data. |
176 if (!found_service) { | 167 if (!handler) { |
177 #if defined(ENABLE_WEB_INTENTS) | |
178 std::vector<webkit_glue::WebIntentServiceData> services = | |
179 extensions::WebIntentsInfo::GetIntentsServices(extension_); | |
180 for (size_t i = 0; i < services.size(); i++) { | |
181 std::string service_type_ascii = UTF16ToASCII(services[i].type); | |
182 if (services[i].action == ASCIIToUTF16(web_intents::kActionView) && | |
183 net::MatchesMimeType(service_type_ascii, mime_type)) { | |
184 found_service = true; | |
185 break; | |
186 } | |
187 } | |
188 #endif | |
189 } | |
190 | |
191 // If this app doesn't have an intent that supports the file, launch with | |
192 // no launch data. | |
193 if (!found_service) { | |
194 LOG(WARNING) << "Extension does not provide a valid file handler for " | 168 LOG(WARNING) << "Extension does not provide a valid file handler for " |
195 << file_path_.value(); | 169 << file_path_.value(); |
196 LaunchWithNoLaunchData(); | 170 LaunchWithNoLaunchData(); |
197 return; | 171 return; |
198 } | 172 } |
199 | 173 |
200 // Access needs to be granted to the file for the process associated with | 174 // Access needs to be granted to the file for the process associated with |
201 // the extension. To do this the ExtensionHost is needed. This might not be | 175 // the extension. To do this the ExtensionHost is needed. This might not be |
202 // available, or it might be in the process of being unloaded, in which case | 176 // available, or it might be in the process of being unloaded, in which case |
203 // the lazy background task queue is used to load the extension and then | 177 // the lazy background task queue is used to load the extension and then |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 fileapi::IsolatedContext* isolated_context = | 216 fileapi::IsolatedContext* isolated_context = |
243 fileapi::IsolatedContext::GetInstance(); | 217 fileapi::IsolatedContext::GetInstance(); |
244 DCHECK(isolated_context); | 218 DCHECK(isolated_context); |
245 std::string filesystem_id = isolated_context->RegisterFileSystemForPath( | 219 std::string filesystem_id = isolated_context->RegisterFileSystemForPath( |
246 fileapi::kFileSystemTypeNativeLocal, file_path_, ®istered_name); | 220 fileapi::kFileSystemTypeNativeLocal, file_path_, ®istered_name); |
247 // Granting read file system permission as well to allow file-system | 221 // Granting read file system permission as well to allow file-system |
248 // read operations. | 222 // read operations. |
249 policy->GrantReadFileSystem(renderer_id, filesystem_id); | 223 policy->GrantReadFileSystem(renderer_id, filesystem_id); |
250 | 224 |
251 AppEventRouter::DispatchOnLaunchedEventWithFileEntry( | 225 AppEventRouter::DispatchOnLaunchedEventWithFileEntry( |
252 profile_, extension_, ASCIIToUTF16(web_intents::kActionView), | 226 profile_, extension_, handler_id_, mime_type, filesystem_id, |
253 handler_id_, mime_type, filesystem_id, registered_name); | 227 registered_name); |
254 } | 228 } |
255 | 229 |
256 // The profile the app should be run in. | 230 // The profile the app should be run in. |
257 Profile* profile_; | 231 Profile* profile_; |
258 // The extension providing the app. | 232 // The extension providing the app. |
259 const Extension* extension_; | 233 const Extension* extension_; |
260 // The path to be passed through to the app. | 234 // The path to be passed through to the app. |
261 const base::FilePath file_path_; | 235 const base::FilePath file_path_; |
262 // The ID of the file handler used to launch the app. | 236 // The ID of the file handler used to launch the app. |
263 std::string handler_id_; | 237 std::string handler_id_; |
264 | 238 |
265 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); | 239 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); |
266 }; | 240 }; |
267 | 241 |
268 #if defined(ENABLE_WEB_INTENTS) | |
269 // Class to handle launching of platform apps with WebIntent data. | |
270 // An instance of this class is created for each launch. The lifetime of these | |
271 // instances is managed by reference counted pointers. As long as an instance | |
272 // has outstanding tasks on a message queue it will be retained; once all | |
273 // outstanding tasks are completed it will be deleted. | |
274 class PlatformAppWebIntentLauncher | |
275 : public base::RefCountedThreadSafe<PlatformAppWebIntentLauncher> { | |
276 public: | |
277 PlatformAppWebIntentLauncher( | |
278 Profile* profile, | |
279 const Extension* extension, | |
280 content::WebIntentsDispatcher* intents_dispatcher, | |
281 content::WebContents* source) | |
282 : profile_(profile), | |
283 extension_(extension), | |
284 intents_dispatcher_(intents_dispatcher), | |
285 source_(source), | |
286 data_(intents_dispatcher->GetIntent()) {} | |
287 | |
288 void Launch() { | |
289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
290 if (data_.data_type != webkit_glue::WebIntentData::BLOB && | |
291 data_.data_type != webkit_glue::WebIntentData::FILESYSTEM) { | |
292 InternalLaunch(); | |
293 return; | |
294 } | |
295 | |
296 // Access needs to be granted to the file or filesystem for the process | |
297 // associated with the extension. To do this the ExtensionHost is needed. | |
298 // This might not be available, or it might be in the process of being | |
299 // unloaded, in which case the lazy background task queue is used to load | |
300 // he extension and then call back to us. | |
301 LazyBackgroundTaskQueue* queue = | |
302 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); | |
303 if (queue->ShouldEnqueueTask(profile_, extension_)) { | |
304 queue->AddPendingTask(profile_, extension_->id(), base::Bind( | |
305 &PlatformAppWebIntentLauncher::GrantAccessToFileAndLaunch, | |
306 this)); | |
307 return; | |
308 } | |
309 ExtensionProcessManager* process_manager = | |
310 ExtensionSystem::Get(profile_)->process_manager(); | |
311 ExtensionHost* host = | |
312 process_manager->GetBackgroundHostForExtension(extension_->id()); | |
313 DCHECK(host); | |
314 GrantAccessToFileAndLaunch(host); | |
315 } | |
316 | |
317 private: | |
318 friend class base::RefCountedThreadSafe<PlatformAppWebIntentLauncher>; | |
319 | |
320 virtual ~PlatformAppWebIntentLauncher() {} | |
321 | |
322 void GrantAccessToFileAndLaunch(ExtensionHost* host) { | |
323 // If there was an error loading the app page, |host| will be NULL. | |
324 if (!host) { | |
325 LOG(ERROR) << "Could not load app page for " << extension_->id(); | |
326 return; | |
327 } | |
328 | |
329 content::ChildProcessSecurityPolicy* policy = | |
330 content::ChildProcessSecurityPolicy::GetInstance(); | |
331 int renderer_id = host->render_process_host()->GetID(); | |
332 | |
333 if (data_.data_type == webkit_glue::WebIntentData::BLOB) { | |
334 // Granting read file permission to allow reading file content. | |
335 // If the renderer already has permission to read these paths, it is not | |
336 // regranted, as this would overwrite any other permissions which the | |
337 // renderer may already have. | |
338 if (!policy->CanReadFile(renderer_id, data_.blob_file)) | |
339 policy->GrantReadFile(renderer_id, data_.blob_file); | |
340 } else if (data_.data_type == webkit_glue::WebIntentData::FILESYSTEM) { | |
341 // Grant read filesystem and read directory permission to allow reading | |
342 // any part of the specified filesystem. | |
343 base::FilePath path; | |
344 const bool valid = | |
345 fileapi::IsolatedContext::GetInstance()->GetRegisteredPath( | |
346 data_.filesystem_id, &path); | |
347 DCHECK(valid); | |
348 if (!policy->CanReadFile(renderer_id, path)) | |
349 policy->GrantReadFile(renderer_id, path); | |
350 policy->GrantReadFileSystem(renderer_id, data_.filesystem_id); | |
351 } else { | |
352 NOTREACHED(); | |
353 } | |
354 InternalLaunch(); | |
355 } | |
356 | |
357 void InternalLaunch() { | |
358 AppEventRouter::DispatchOnLaunchedEventWithWebIntent( | |
359 profile_, extension_, intents_dispatcher_, source_); | |
360 } | |
361 | |
362 // The profile the app should be run in. | |
363 Profile* profile_; | |
364 // The extension providing the app. | |
365 const Extension* extension_; | |
366 // The dispatcher so that platform apps can respond to this intent. | |
367 content::WebIntentsDispatcher* intents_dispatcher_; | |
368 // The source of this intent. | |
369 content::WebContents* source_; | |
370 // The WebIntent data from the dispatcher. | |
371 const webkit_glue::WebIntentData data_; | |
372 | |
373 DISALLOW_COPY_AND_ASSIGN(PlatformAppWebIntentLauncher); | |
374 }; | |
375 #endif | |
376 | |
377 } // namespace | 242 } // namespace |
378 | 243 |
379 void LaunchPlatformApp(Profile* profile, | 244 void LaunchPlatformApp(Profile* profile, |
380 const Extension* extension, | 245 const Extension* extension, |
381 const CommandLine* command_line, | 246 const CommandLine* command_line, |
382 const base::FilePath& current_directory) { | 247 const base::FilePath& current_directory) { |
383 base::FilePath path; | 248 base::FilePath path; |
384 if (!GetAbsolutePathFromCommandLine(command_line, current_directory, &path)) { | 249 if (!GetAbsolutePathFromCommandLine(command_line, current_directory, &path)) { |
385 LaunchPlatformAppWithNoData(profile, extension); | 250 LaunchPlatformAppWithNoData(profile, extension); |
386 return; | 251 return; |
(...skipping 16 matching lines...) Expand all Loading... |
403 | 268 |
404 void LaunchPlatformAppWithFileHandler(Profile* profile, | 269 void LaunchPlatformAppWithFileHandler(Profile* profile, |
405 const Extension* extension, | 270 const Extension* extension, |
406 const std::string& handler_id, | 271 const std::string& handler_id, |
407 const base::FilePath& file_path) { | 272 const base::FilePath& file_path) { |
408 scoped_refptr<PlatformAppPathLauncher> launcher = | 273 scoped_refptr<PlatformAppPathLauncher> launcher = |
409 new PlatformAppPathLauncher(profile, extension, file_path); | 274 new PlatformAppPathLauncher(profile, extension, file_path); |
410 launcher->LaunchWithHandler(handler_id); | 275 launcher->LaunchWithHandler(handler_id); |
411 } | 276 } |
412 | 277 |
413 #if defined(ENABLE_WEB_INTENTS) | |
414 void LaunchPlatformAppWithWebIntent( | |
415 Profile* profile, | |
416 const Extension* extension, | |
417 content::WebIntentsDispatcher* intents_dispatcher, | |
418 content::WebContents* source) { | |
419 scoped_refptr<PlatformAppWebIntentLauncher> launcher = | |
420 new PlatformAppWebIntentLauncher( | |
421 profile, extension, intents_dispatcher, source); | |
422 launcher->Launch(); | |
423 } | |
424 #endif | |
425 | |
426 } // namespace extensions | 278 } // namespace extensions |
OLD | NEW |