Chromium Code Reviews| 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/chromeos/extensions/file_handler_util.h" | 5 #include "chrome/browser/chromeos/extensions/file_handler_util.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/chromeos/extensions/file_manager_util.h" | 14 #include "chrome/browser/chromeos/extensions/file_manager_util.h" |
| 15 #include "chrome/browser/chromeos/gdata/drive_file_system_util.h" | 15 #include "chrome/browser/chromeos/gdata/drive_file_system_util.h" |
| 16 #include "chrome/browser/chromeos/gdata/drive_task_executor.h" | 16 #include "chrome/browser/chromeos/gdata/drive_task_executor.h" |
| 17 #include "chrome/browser/extensions/event_router.h" | 17 #include "chrome/browser/extensions/event_router.h" |
| 18 #include "chrome/browser/extensions/extension_host.h" | 18 #include "chrome/browser/extensions/extension_host.h" |
| 19 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 20 #include "chrome/browser/extensions/extension_system.h" | 20 #include "chrome/browser/extensions/extension_system.h" |
| 21 #include "chrome/browser/extensions/extension_tab_util.h" | 21 #include "chrome/browser/extensions/extension_tab_util.h" |
| 22 #include "chrome/browser/extensions/lazy_background_task_queue.h" | 22 #include "chrome/browser/extensions/lazy_background_task_queue.h" |
| 23 #include "chrome/browser/extensions/platform_app_launcher.h" | |
| 23 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 24 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/profiles/profile_manager.h" | 26 #include "chrome/browser/profiles/profile_manager.h" |
| 26 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/browser/ui/browser_finder.h" | 28 #include "chrome/browser/ui/browser_finder.h" |
| 28 #include "chrome/browser/ui/browser_tabstrip.h" | 29 #include "chrome/browser/ui/browser_tabstrip.h" |
| 29 #include "chrome/common/extensions/file_browser_handler.h" | 30 #include "chrome/common/extensions/file_browser_handler.h" |
| 30 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
| 31 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/browser/child_process_security_policy.h" | 33 #include "content/public/browser/child_process_security_policy.h" |
| 33 #include "content/public/browser/render_process_host.h" | 34 #include "content/public/browser/render_process_host.h" |
| 34 #include "content/public/browser/site_instance.h" | 35 #include "content/public/browser/site_instance.h" |
| 35 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
| 36 #include "net/base/escape.h" | 37 #include "net/base/escape.h" |
| 37 #include "webkit/chromeos/fileapi/cros_mount_point_provider.h" | 38 #include "webkit/chromeos/fileapi/cros_mount_point_provider.h" |
| 38 #include "webkit/fileapi/file_system_context.h" | 39 #include "webkit/fileapi/file_system_context.h" |
| 39 #include "webkit/fileapi/file_system_url.h" | 40 #include "webkit/fileapi/file_system_url.h" |
| 40 #include "webkit/fileapi/file_system_util.h" | 41 #include "webkit/fileapi/file_system_util.h" |
| 42 #include "webkit/fileapi/isolated_context.h" | |
| 41 | 43 |
| 42 using content::BrowserContext; | 44 using content::BrowserContext; |
| 43 using content::BrowserThread; | 45 using content::BrowserThread; |
| 44 using content::ChildProcessSecurityPolicy; | 46 using content::ChildProcessSecurityPolicy; |
| 45 using content::SiteInstance; | 47 using content::SiteInstance; |
| 46 using content::WebContents; | 48 using content::WebContents; |
| 47 using extensions::Extension; | 49 using extensions::Extension; |
| 48 | 50 |
| 49 namespace file_handler_util { | 51 namespace file_handler_util { |
| 50 | 52 |
| 51 // The prefix used to differentiate drive extensions from Chrome extensions. | |
| 52 const char FileTaskExecutor::kDriveTaskExtensionPrefix[] = "drive-app:"; | |
| 53 const size_t FileTaskExecutor::kDriveTaskExtensionPrefixLength = | |
| 54 arraysize(FileTaskExecutor::kDriveTaskExtensionPrefix) - 1; | |
| 55 | |
| 56 namespace { | 53 namespace { |
| 57 typedef std::set<const FileBrowserHandler*> FileBrowserHandlerSet; | 54 typedef std::set<const FileBrowserHandler*> FileBrowserHandlerSet; |
| 58 | 55 |
| 59 const int kReadWriteFilePermissions = base::PLATFORM_FILE_OPEN | | 56 const int kReadWriteFilePermissions = base::PLATFORM_FILE_OPEN | |
| 60 base::PLATFORM_FILE_CREATE | | 57 base::PLATFORM_FILE_CREATE | |
| 61 base::PLATFORM_FILE_OPEN_ALWAYS | | 58 base::PLATFORM_FILE_OPEN_ALWAYS | |
| 62 base::PLATFORM_FILE_CREATE_ALWAYS | | 59 base::PLATFORM_FILE_CREATE_ALWAYS | |
| 63 base::PLATFORM_FILE_OPEN_TRUNCATED | | 60 base::PLATFORM_FILE_OPEN_TRUNCATED | |
| 64 base::PLATFORM_FILE_READ | | 61 base::PLATFORM_FILE_READ | |
| 65 base::PLATFORM_FILE_WRITE | | 62 base::PLATFORM_FILE_WRITE | |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 for (Extension::FileBrowserHandlerList::const_iterator action_iter = | 102 for (Extension::FileBrowserHandlerList::const_iterator action_iter = |
| 106 extension->file_browser_handlers()->begin(); | 103 extension->file_browser_handlers()->begin(); |
| 107 action_iter != extension->file_browser_handlers()->end(); | 104 action_iter != extension->file_browser_handlers()->end(); |
| 108 ++action_iter) { | 105 ++action_iter) { |
| 109 if (action_iter->get()->id() == action_id) | 106 if (action_iter->get()->id() == action_id) |
| 110 return action_iter->get(); | 107 return action_iter->get(); |
| 111 } | 108 } |
| 112 return NULL; | 109 return NULL; |
| 113 } | 110 } |
| 114 | 111 |
| 115 unsigned int GetAccessPermissionsForHandler(const Extension* extension, | 112 unsigned int GetAccessPermissionsForFileBrowserHandler( |
| 116 const std::string& action_id) { | 113 const Extension* extension, |
| 114 const std::string& action_id) { | |
| 117 const FileBrowserHandler* action = | 115 const FileBrowserHandler* action = |
| 118 FindFileBrowserHandler(extension, action_id); | 116 FindFileBrowserHandler(extension, action_id); |
| 119 if (!action) | 117 if (!action) |
| 120 return 0; | 118 return 0; |
| 121 unsigned int result = 0; | 119 unsigned int result = 0; |
| 122 if (action->CanRead()) | 120 if (action->CanRead()) |
| 123 result |= kReadOnlyFilePermissions; | 121 result |= kReadOnlyFilePermissions; |
| 124 if (action->CanWrite()) | 122 if (action->CanWrite()) |
| 125 result |= kReadWriteFilePermissions; | 123 result |= kReadWriteFilePermissions; |
| 126 // TODO(tbarzic): We don't handle Create yet. | 124 // TODO(tbarzic): We don't handle Create yet. |
| 127 return result; | 125 return result; |
| 128 } | 126 } |
| 129 | 127 |
| 130 | |
| 131 std::string EscapedUtf8ToLower(const std::string& str) { | 128 std::string EscapedUtf8ToLower(const std::string& str) { |
| 132 string16 utf16 = UTF8ToUTF16( | 129 string16 utf16 = UTF8ToUTF16( |
| 133 net::UnescapeURLComponent(str, net::UnescapeRule::NORMAL)); | 130 net::UnescapeURLComponent(str, net::UnescapeRule::NORMAL)); |
| 134 return net::EscapeUrlEncodedData( | 131 return net::EscapeUrlEncodedData( |
| 135 UTF16ToUTF8(base::i18n::ToLower(utf16)), | 132 UTF16ToUTF8(base::i18n::ToLower(utf16)), |
| 136 false /* do not replace space with plus */); | 133 false /* do not replace space with plus */); |
| 137 } | 134 } |
| 138 | 135 |
| 139 bool GetFileBrowserHandlers(Profile* profile, | 136 bool GetFileBrowserHandlers(Profile* profile, |
| 140 const GURL& selected_file_url, | 137 const GURL& selected_file_url, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 | 235 |
| 239 int GetReadWritePermissions() { | 236 int GetReadWritePermissions() { |
| 240 return kReadWriteFilePermissions; | 237 return kReadWriteFilePermissions; |
| 241 } | 238 } |
| 242 | 239 |
| 243 int GetReadOnlyPermissions() { | 240 int GetReadOnlyPermissions() { |
| 244 return kReadOnlyFilePermissions; | 241 return kReadOnlyFilePermissions; |
| 245 } | 242 } |
| 246 | 243 |
| 247 std::string MakeTaskID(const std::string& extension_id, | 244 std::string MakeTaskID(const std::string& extension_id, |
| 245 TaskType task_type, | |
| 248 const std::string& action_id) { | 246 const std::string& action_id) { |
| 249 return base::StringPrintf("%s|%s", extension_id.c_str(), action_id.c_str()); | 247 DCHECK(task_type == TASK_FILE |
| 248 || task_type == TASK_DRIVE | |
|
tbarzic
2012/09/16 04:26:11
nit:
|| should be at the end of the previous line.
thorogood
2012/09/18 02:13:02
Done.
| |
| 249 || task_type == TASK_WEBINTENT); | |
| 250 return base::StringPrintf("%s|%d|%s", | |
| 251 extension_id.c_str(), | |
| 252 task_type, | |
| 253 action_id.c_str()); | |
| 250 } | 254 } |
| 251 | 255 |
| 252 std::string MakeDriveTaskID(const std::string& app_id, | 256 const char kDriveTaskExtensionPrefix[] = "drive-app:"; |
| 253 const std::string& action_id) { | 257 const size_t kDriveTaskExtensionPrefixLength = |
|
tbarzic
2012/09/16 04:26:11
should these go in anonymous namespace?
thorogood
2012/09/18 02:13:02
Done.
| |
| 254 return MakeTaskID(FileTaskExecutor::kDriveTaskExtensionPrefix + app_id, | 258 arraysize(kDriveTaskExtensionPrefix) - 1; |
| 255 action_id); | |
| 256 } | |
| 257 | |
| 258 bool CrackDriveTaskID(const std::string& task_id, | |
| 259 std::string* app_id, | |
| 260 std::string* action_id) { | |
| 261 std::string app_id_tmp; | |
| 262 std::string action_id_tmp; | |
| 263 if (!CrackTaskID(task_id, &app_id_tmp, &action_id_tmp)) | |
| 264 return false; | |
| 265 if (StartsWithASCII(app_id_tmp, | |
| 266 FileTaskExecutor::kDriveTaskExtensionPrefix, | |
| 267 false)) { | |
| 268 // Strip off the prefix from the extension ID so we convert it to an app id. | |
| 269 if (app_id) { | |
| 270 *app_id = app_id_tmp.substr( | |
| 271 FileTaskExecutor::kDriveTaskExtensionPrefixLength); | |
| 272 } | |
| 273 if (action_id) | |
| 274 *action_id = action_id_tmp; | |
| 275 return true; | |
| 276 } | |
| 277 return false; | |
| 278 } | |
| 279 | 259 |
| 280 // Breaks down task_id that is used between getFileTasks() and executeTask() on | 260 // Breaks down task_id that is used between getFileTasks() and executeTask() on |
| 281 // its building blocks. task_id field the following structure: | 261 // its building blocks. task_id field the following structure: |
| 282 // <extension-id>|<task-action-id> | 262 // <extension-id>|<task-type>|<task-action-id> |
| 283 bool CrackTaskID(const std::string& task_id, | 263 bool CrackTaskID(const std::string& task_id, |
| 284 std::string* extension_id, | 264 std::string* extension_id, |
| 265 TaskType* task_type, | |
| 285 std::string* action_id) { | 266 std::string* action_id) { |
| 286 std::vector<std::string> result; | 267 std::vector<std::string> result; |
| 287 int count = Tokenize(task_id, std::string("|"), &result); | 268 int count = Tokenize(task_id, std::string("|"), &result); |
| 288 if (count != 2) | 269 |
| 270 // Parse historic task_id parameters that only contain two parts. Drive tasks | |
| 271 // are identified by a prefix "drive-app:" on the extension ID. | |
| 272 if (count == 2) { | |
| 273 if (StartsWithASCII(result[0], kDriveTaskExtensionPrefix, true)) { | |
| 274 if (task_type) | |
| 275 *task_type = TASK_DRIVE; | |
| 276 | |
| 277 if (extension_id) | |
| 278 *extension_id = result[0].substr(kDriveTaskExtensionPrefixLength); | |
| 279 } else { | |
| 280 if (task_type) | |
| 281 *task_type = TASK_FILE; | |
| 282 | |
| 283 if (extension_id) | |
| 284 *extension_id = result[0]; | |
| 285 } | |
| 286 | |
| 287 if (action_id) | |
| 288 *action_id = result[1]; | |
| 289 | |
| 290 return true; | |
| 291 } | |
| 292 | |
| 293 if (count != 3) | |
| 289 return false; | 294 return false; |
| 295 | |
| 290 if (extension_id) | 296 if (extension_id) |
| 291 *extension_id = result[0]; | 297 *extension_id = result[0]; |
| 298 | |
| 299 if (task_type) { | |
| 300 *task_type = file_handler_util::TaskType(atoi(result[1].c_str())); | |
| 301 DCHECK(*task_type == TASK_FILE | |
| 302 || *task_type == TASK_DRIVE | |
|
tbarzic
2012/09/16 04:26:11
nit:
|| should go at the end of previous line
thorogood
2012/09/18 02:13:02
Done.
| |
| 303 || *task_type == TASK_WEBINTENT); | |
| 304 } | |
| 305 | |
| 292 if (action_id) | 306 if (action_id) |
| 293 *action_id = result[1]; | 307 *action_id = result[2]; |
| 308 | |
| 294 return true; | 309 return true; |
| 295 } | 310 } |
| 296 | 311 |
| 297 // Find a specific handler in the handler list. | 312 // Find a specific handler in the handler list. |
| 298 FileBrowserHandlerSet::iterator FindHandler( | 313 FileBrowserHandlerSet::iterator FindHandler( |
| 299 FileBrowserHandlerSet* handler_set, | 314 FileBrowserHandlerSet* handler_set, |
| 300 const std::string& extension_id, | 315 const std::string& extension_id, |
| 301 const std::string& id) { | 316 const std::string& id) { |
| 302 FileBrowserHandlerSet::iterator iter = handler_set->begin(); | 317 FileBrowserHandlerSet::iterator iter = handler_set->begin(); |
| 303 while (iter != handler_set->end() && | 318 while (iter != handler_set->end() && |
| 304 !((*iter)->extension_id() == extension_id && | 319 !((*iter)->extension_id() == extension_id && |
| 305 (*iter)->id() == id)) { | 320 (*iter)->id() == id)) { |
| 306 iter++; | 321 iter++; |
| 307 } | 322 } |
| 308 return iter; | 323 return iter; |
| 309 } | 324 } |
| 310 | 325 |
| 326 // Given the list of selected files, returns array of file action tasks | |
| 327 // that are shared between them. | |
| 311 void FindDefaultTasks(Profile* profile, | 328 void FindDefaultTasks(Profile* profile, |
| 312 const std::vector<GURL>& files_list, | 329 const std::vector<GURL>& files_list, |
| 313 const FileBrowserHandlerSet& common_tasks, | 330 const FileBrowserHandlerSet& common_tasks, |
| 314 FileBrowserHandlerSet* default_tasks) { | 331 FileBrowserHandlerSet* default_tasks) { |
| 315 DCHECK(default_tasks); | 332 DCHECK(default_tasks); |
| 316 default_tasks->clear(); | 333 default_tasks->clear(); |
| 317 | 334 |
| 318 std::set<std::string> default_ids; | 335 std::set<std::string> default_ids; |
| 319 for (std::vector<GURL>::const_iterator it = files_list.begin(); | 336 for (std::vector<GURL>::const_iterator it = files_list.begin(); |
| 320 it != files_list.end(); ++it) { | 337 it != files_list.end(); ++it) { |
| 321 // Get the default task for this file based only on the extension (since | 338 // Get the default task for this file based only on the extension (since |
| 322 // we don't have MIME types here), and add it to the set of default tasks. | 339 // we don't have MIME types here), and add it to the set of default tasks. |
| 323 fileapi::FileSystemURL filesystem_url(*it); | 340 fileapi::FileSystemURL filesystem_url(*it); |
| 324 if (filesystem_url.is_valid() && | 341 if (filesystem_url.is_valid() && |
| 325 (filesystem_url.type() == fileapi::kFileSystemTypeDrive || | 342 (filesystem_url.type() == fileapi::kFileSystemTypeDrive || |
| 326 filesystem_url.type() == fileapi::kFileSystemTypeNativeMedia || | 343 filesystem_url.type() == fileapi::kFileSystemTypeNativeMedia || |
| 327 filesystem_url.type() == fileapi::kFileSystemTypeNativeLocal)) { | 344 filesystem_url.type() == fileapi::kFileSystemTypeNativeLocal)) { |
| 328 std::string task_id = file_handler_util::GetDefaultTaskIdFromPrefs( | 345 std::string task_id = file_handler_util::GetDefaultTaskIdFromPrefs( |
| 329 profile, "", filesystem_url.virtual_path().Extension()); | 346 profile, "", filesystem_url.virtual_path().Extension()); |
| 330 if (!task_id.empty()) | 347 if (!task_id.empty()) |
| 331 default_ids.insert(task_id); | 348 default_ids.insert(task_id); |
| 332 } | 349 } |
| 333 } | 350 } |
| 334 | 351 |
| 335 // Convert the default task IDs collected above to one of the handler pointers | 352 // Convert the default task IDs collected above to one of the handler pointers |
| 336 // from common_tasks. | 353 // from common_tasks. |
| 337 for (FileBrowserHandlerSet::const_iterator task_iter = common_tasks.begin(); | 354 for (FileBrowserHandlerSet::const_iterator task_iter = common_tasks.begin(); |
| 338 task_iter != common_tasks.end(); ++task_iter) { | 355 task_iter != common_tasks.end(); ++task_iter) { |
| 339 std::string task_id = MakeTaskID((*task_iter)->extension_id(), | 356 std::string task_id = MakeTaskID((*task_iter)->extension_id(), TASK_FILE, |
| 340 (*task_iter)->id()); | 357 (*task_iter)->id()); |
| 341 for (std::set<std::string>::iterator default_iter = default_ids.begin(); | 358 for (std::set<std::string>::iterator default_iter = default_ids.begin(); |
| 342 default_iter != default_ids.end(); ++default_iter) { | 359 default_iter != default_ids.end(); ++default_iter) { |
| 343 if (task_id == *default_iter) { | 360 if (task_id == *default_iter) { |
| 344 default_tasks->insert(*task_iter); | 361 default_tasks->insert(*task_iter); |
| 345 break; | 362 break; |
| 346 } | 363 } |
| 347 } | 364 } |
| 348 } | 365 } |
| 349 } | 366 } |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 484 const GURL& file_system_root, | 501 const GURL& file_system_root, |
| 485 const FileDefinitionList& file_list, | 502 const FileDefinitionList& file_list, |
| 486 int handler_pid_in, | 503 int handler_pid_in, |
| 487 extensions::ExtensionHost* host); | 504 extensions::ExtensionHost* host); |
| 488 | 505 |
| 489 // Populates |handler_host_permissions| with file path-permissions pairs that | 506 // Populates |handler_host_permissions| with file path-permissions pairs that |
| 490 // will be given to the handler extension host process. | 507 // will be given to the handler extension host process. |
| 491 void InitHandlerHostFileAccessPermissions( | 508 void InitHandlerHostFileAccessPermissions( |
| 492 const FileDefinitionList& file_list, | 509 const FileDefinitionList& file_list, |
| 493 const extensions::Extension* handler_extension, | 510 const extensions::Extension* handler_extension, |
| 494 const std::string& action_id, | |
| 495 const base::Closure& callback); | 511 const base::Closure& callback); |
| 496 | 512 |
| 497 // Invoked upon completion of InitHandlerHostFileAccessPermissions initiated | 513 // Invoked upon completion of InitHandlerHostFileAccessPermissions initiated |
| 498 // by ExecuteFileActionsOnUIThread. | 514 // by ExecuteFileActionsOnUIThread. |
| 499 void OnInitAccessForExecuteFileActionsOnUIThread( | 515 void OnInitAccessForExecuteFileActionsOnUIThread( |
| 500 const std::string& file_system_name, | 516 const std::string& file_system_name, |
| 501 const GURL& file_system_root, | 517 const GURL& file_system_root, |
| 502 const FileDefinitionList& file_list, | 518 const FileDefinitionList& file_list, |
| 503 int handler_pid); | 519 int handler_pid); |
| 504 | 520 |
| 505 // Registers file permissions from |handler_host_permissions_| with | 521 // Registers file permissions from |handler_host_permissions_| with |
| 506 // ChildProcessSecurityPolicy for process with id |handler_pid|. | 522 // ChildProcessSecurityPolicy for process with id |handler_pid|. |
| 507 void SetupHandlerHostFileAccessPermissions(int handler_pid); | 523 void SetupHandlerHostFileAccessPermissions(int handler_pid); |
| 508 | 524 |
| 509 // Helper function to get the extension pointer. | |
| 510 const extensions::Extension* GetExtension(); | |
| 511 | |
| 512 const GURL source_url_; | 525 const GURL source_url_; |
| 513 const std::string extension_id_; | |
| 514 const std::string action_id_; | 526 const std::string action_id_; |
| 515 FileTaskFinishedCallback done_; | 527 FileTaskFinishedCallback done_; |
| 516 | 528 |
| 517 // (File path, permission for file path) pairs for the handler. | 529 // (File path, permission for file path) pairs for the handler. |
| 518 std::vector<std::pair<FilePath, int> > handler_host_permissions_; | 530 std::vector<std::pair<FilePath, int> > handler_host_permissions_; |
| 519 }; | 531 }; |
| 520 | 532 |
| 533 class WebIntentTaskExecutor : public FileTaskExecutor { | |
| 534 public: | |
| 535 // FileTaskExecutor overrides. | |
| 536 virtual bool ExecuteAndNotify(const std::vector<GURL>& file_urls, | |
| 537 const FileTaskFinishedCallback& done) OVERRIDE; | |
| 538 | |
| 539 private: | |
| 540 // FileTaskExecutor is the only class allowed to create one. | |
| 541 friend class FileTaskExecutor; | |
| 542 | |
| 543 WebIntentTaskExecutor(Profile* profile, | |
| 544 const GURL source_url, | |
| 545 const std::string& extension_id, | |
| 546 const std::string& action_id); | |
| 547 virtual ~WebIntentTaskExecutor(); | |
| 548 | |
| 549 bool ExecuteForURL(const GURL& file_url); | |
| 550 | |
| 551 const GURL source_url_; | |
| 552 const std::string extension_id_; | |
| 553 const std::string action_id_; | |
| 554 }; | |
| 555 | |
| 521 // static | 556 // static |
| 522 FileTaskExecutor* FileTaskExecutor::Create(Profile* profile, | 557 FileTaskExecutor* FileTaskExecutor::Create(Profile* profile, |
| 523 const GURL source_url, | 558 const GURL source_url, |
| 524 const std::string& extension_id, | 559 const std::string& extension_id, |
| 560 TaskType task_type, | |
| 525 const std::string& action_id) { | 561 const std::string& action_id) { |
| 526 // Check out the extension ID and see if this is a drive task, | 562 switch (task_type) { |
| 527 // and instantiate drive-specific executor if so. | 563 case TASK_DRIVE: |
| 528 if (StartsWithASCII(extension_id, | 564 return new gdata::DriveTaskExecutor(profile, |
| 529 FileTaskExecutor::kDriveTaskExtensionPrefix, | 565 extension_id, // really app_id |
|
tbarzic
2012/09/16 04:26:11
nit:
two spaces before //
thorogood
2012/09/18 02:13:02
Done.
| |
| 530 false)) { | 566 action_id); |
| 531 return new gdata::DriveTaskExecutor(profile, | 567 case TASK_WEBINTENT: |
| 532 extension_id, // really app_id | 568 return new WebIntentTaskExecutor(profile, |
| 533 action_id); | 569 source_url, |
| 534 } else { | 570 extension_id, |
| 535 return new ExtensionTaskExecutor(profile, | 571 action_id); |
| 536 source_url, | 572 case TASK_FILE: |
| 537 extension_id, | 573 return new ExtensionTaskExecutor(profile, |
| 538 action_id); | 574 source_url, |
| 575 extension_id, | |
| 576 action_id); | |
| 577 default: | |
| 578 NOTREACHED(); | |
| 579 return NULL; | |
| 580 // TODO: bigger explosion than this | |
|
tbarzic
2012/09/16 04:26:11
nit: '.' at the end.
thorogood
2012/09/18 02:13:02
I've removed this TODO. It was just a note for me,
tbarzic
2012/09/18 03:21:57
this looks ok
| |
| 539 } | 581 } |
| 540 } | 582 } |
| 541 | 583 |
| 542 FileTaskExecutor::FileTaskExecutor(Profile* profile) : profile_(profile) { | 584 FileTaskExecutor::FileTaskExecutor( |
| 585 Profile* profile, | |
| 586 const std::string& extension_id) | |
| 587 : profile_(profile), | |
| 588 extension_id_(extension_id) { | |
| 543 } | 589 } |
| 544 | 590 |
| 545 FileTaskExecutor::~FileTaskExecutor() { | 591 FileTaskExecutor::~FileTaskExecutor() { |
| 546 } | 592 } |
| 547 | 593 |
| 548 bool FileTaskExecutor::Execute(const std::vector<GURL>& file_urls) { | 594 bool FileTaskExecutor::Execute(const std::vector<GURL>& file_urls) { |
| 549 return ExecuteAndNotify(file_urls, FileTaskFinishedCallback()); | 595 return ExecuteAndNotify(file_urls, FileTaskFinishedCallback()); |
| 550 } | 596 } |
| 551 | 597 |
| 552 Browser* FileTaskExecutor::GetBrowser() const { | 598 Browser* FileTaskExecutor::GetBrowser() const { |
| 553 return browser::FindOrCreateTabbedBrowser( | 599 return browser::FindOrCreateTabbedBrowser( |
| 554 profile_ ? profile_ : ProfileManager::GetDefaultProfileOrOffTheRecord()); | 600 profile_ ? profile_ : ProfileManager::GetDefaultProfileOrOffTheRecord()); |
| 555 } | 601 } |
| 556 | 602 |
| 603 const Extension* FileTaskExecutor::GetExtension() { | |
| 604 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 605 | |
| 606 ExtensionService* service = profile()->GetExtensionService(); | |
| 607 return service ? service->GetExtensionById(extension_id_, false) : | |
| 608 NULL; | |
| 609 } | |
| 610 | |
| 557 ExtensionTaskExecutor::FileDefinition::FileDefinition() : is_directory(false) { | 611 ExtensionTaskExecutor::FileDefinition::FileDefinition() : is_directory(false) { |
| 558 } | 612 } |
| 559 | 613 |
| 560 ExtensionTaskExecutor::FileDefinition::~FileDefinition() { | 614 ExtensionTaskExecutor::FileDefinition::~FileDefinition() { |
| 561 } | 615 } |
| 562 | 616 |
| 563 class ExtensionTaskExecutor::ExecuteTasksFileSystemCallbackDispatcher { | 617 class ExtensionTaskExecutor::ExecuteTasksFileSystemCallbackDispatcher { |
| 564 public: | 618 public: |
| 565 static fileapi::FileSystemContext::OpenFileSystemCallback CreateCallback( | 619 static fileapi::FileSystemContext::OpenFileSystemCallback CreateCallback( |
| 566 ExtensionTaskExecutor* executor, | 620 ExtensionTaskExecutor* executor, |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 716 std::string action_id_; | 770 std::string action_id_; |
| 717 std::vector<GURL> origin_file_urls_; | 771 std::vector<GURL> origin_file_urls_; |
| 718 DISALLOW_COPY_AND_ASSIGN(ExecuteTasksFileSystemCallbackDispatcher); | 772 DISALLOW_COPY_AND_ASSIGN(ExecuteTasksFileSystemCallbackDispatcher); |
| 719 }; | 773 }; |
| 720 | 774 |
| 721 ExtensionTaskExecutor::ExtensionTaskExecutor( | 775 ExtensionTaskExecutor::ExtensionTaskExecutor( |
| 722 Profile* profile, | 776 Profile* profile, |
| 723 const GURL source_url, | 777 const GURL source_url, |
| 724 const std::string& extension_id, | 778 const std::string& extension_id, |
| 725 const std::string& action_id) | 779 const std::string& action_id) |
| 726 : FileTaskExecutor(profile), | 780 : FileTaskExecutor(profile, extension_id), |
| 727 source_url_(source_url), | 781 source_url_(source_url), |
| 728 extension_id_(extension_id), | |
| 729 action_id_(action_id) { | 782 action_id_(action_id) { |
| 730 } | 783 } |
| 731 | 784 |
| 732 ExtensionTaskExecutor::~ExtensionTaskExecutor() {} | 785 ExtensionTaskExecutor::~ExtensionTaskExecutor() {} |
| 733 | 786 |
| 734 bool ExtensionTaskExecutor::ExecuteAndNotify( | 787 bool ExtensionTaskExecutor::ExecuteAndNotify( |
| 735 const std::vector<GURL>& file_urls, | 788 const std::vector<GURL>& file_urls, |
| 736 const FileTaskFinishedCallback& done) { | 789 const FileTaskFinishedCallback& done) { |
| 737 ExtensionService* service = profile()->GetExtensionService(); | 790 scoped_refptr<const Extension> handler = GetExtension(); |
| 738 if (!service) | |
| 739 return false; | |
| 740 | |
| 741 scoped_refptr<const Extension> handler = | |
| 742 service->GetExtensionById(extension_id_, false); | |
| 743 | 791 |
| 744 if (!handler.get()) | 792 if (!handler.get()) |
| 745 return false; | 793 return false; |
| 746 | 794 |
| 747 int handler_pid = ExtractProcessFromExtensionId(handler->id(), profile()); | 795 int handler_pid = ExtractProcessFromExtensionId(handler->id(), profile()); |
| 748 if (handler_pid <= 0) { | 796 if (handler_pid <= 0) { |
| 749 if (!handler->has_lazy_background_page()) | 797 if (!handler->has_lazy_background_page()) |
| 750 return false; | 798 return false; |
| 751 } | 799 } |
| 752 | 800 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 788 file_urls)); | 836 file_urls)); |
| 789 } | 837 } |
| 790 | 838 |
| 791 void ExtensionTaskExecutor::ExecuteDoneOnUIThread(bool success) { | 839 void ExtensionTaskExecutor::ExecuteDoneOnUIThread(bool success) { |
| 792 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 840 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 793 if (!done_.is_null()) | 841 if (!done_.is_null()) |
| 794 done_.Run(success); | 842 done_.Run(success); |
| 795 done_.Reset(); | 843 done_.Reset(); |
| 796 } | 844 } |
| 797 | 845 |
| 798 const Extension* ExtensionTaskExecutor::GetExtension() { | |
| 799 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 800 | |
| 801 ExtensionService* service = profile()->GetExtensionService(); | |
| 802 return service ? service->GetExtensionById(extension_id_, false) : | |
| 803 NULL; | |
| 804 } | |
| 805 | |
| 806 void ExtensionTaskExecutor::ExecuteFileActionsOnUIThread( | 846 void ExtensionTaskExecutor::ExecuteFileActionsOnUIThread( |
| 807 const std::string& file_system_name, | 847 const std::string& file_system_name, |
| 808 const GURL& file_system_root, | 848 const GURL& file_system_root, |
| 809 const FileDefinitionList& file_list, | 849 const FileDefinitionList& file_list, |
| 810 int handler_pid) { | 850 int handler_pid) { |
| 811 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 851 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 812 | 852 |
| 813 const Extension* extension = GetExtension(); | 853 const Extension* extension = GetExtension(); |
| 814 if (!extension) { | 854 if (!extension) { |
| 815 ExecuteDoneOnUIThread(false); | 855 ExecuteDoneOnUIThread(false); |
| 816 return; | 856 return; |
| 817 } | 857 } |
| 818 | 858 |
| 819 InitHandlerHostFileAccessPermissions( | 859 InitHandlerHostFileAccessPermissions( |
| 820 file_list, | 860 file_list, |
| 821 extension, | 861 extension, |
| 822 action_id_, | |
| 823 base::Bind( | 862 base::Bind( |
| 824 &ExtensionTaskExecutor::OnInitAccessForExecuteFileActionsOnUIThread, | 863 &ExtensionTaskExecutor::OnInitAccessForExecuteFileActionsOnUIThread, |
| 825 this, | 864 this, |
| 826 file_system_name, | 865 file_system_name, |
| 827 file_system_root, | 866 file_system_root, |
| 828 file_list, | 867 file_list, |
| 829 handler_pid)); | 868 handler_pid)); |
| 830 } | 869 } |
| 831 | 870 |
| 832 void ExtensionTaskExecutor::OnInitAccessForExecuteFileActionsOnUIThread( | 871 void ExtensionTaskExecutor::OnInitAccessForExecuteFileActionsOnUIThread( |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 848 } else { | 887 } else { |
| 849 // We have to wake the handler background page before we proceed. | 888 // We have to wake the handler background page before we proceed. |
| 850 extensions::LazyBackgroundTaskQueue* queue = | 889 extensions::LazyBackgroundTaskQueue* queue = |
| 851 extensions::ExtensionSystem::Get(profile())-> | 890 extensions::ExtensionSystem::Get(profile())-> |
| 852 lazy_background_task_queue(); | 891 lazy_background_task_queue(); |
| 853 if (!queue->ShouldEnqueueTask(profile(), extension)) { | 892 if (!queue->ShouldEnqueueTask(profile(), extension)) { |
| 854 ExecuteDoneOnUIThread(false); | 893 ExecuteDoneOnUIThread(false); |
| 855 return; | 894 return; |
| 856 } | 895 } |
| 857 queue->AddPendingTask( | 896 queue->AddPendingTask( |
| 858 profile(), extension_id_, | 897 profile(), extension_id(), |
| 859 base::Bind(&ExtensionTaskExecutor::SetupPermissionsAndDispatchEvent, | 898 base::Bind(&ExtensionTaskExecutor::SetupPermissionsAndDispatchEvent, |
| 860 this, file_system_name, file_system_root, file_list, | 899 this, file_system_name, file_system_root, file_list, |
| 861 handler_pid)); | 900 handler_pid)); |
| 862 } | 901 } |
| 863 } | 902 } |
| 864 | 903 |
| 865 void ExtensionTaskExecutor::SetupPermissionsAndDispatchEvent( | 904 void ExtensionTaskExecutor::SetupPermissionsAndDispatchEvent( |
| 866 const std::string& file_system_name, | 905 const std::string& file_system_name, |
| 867 const GURL& file_system_root, | 906 const GURL& file_system_root, |
| 868 const FileDefinitionList& file_list, | 907 const FileDefinitionList& file_list, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 907 | 946 |
| 908 // Get tab id. | 947 // Get tab id. |
| 909 Browser* current_browser = GetBrowser(); | 948 Browser* current_browser = GetBrowser(); |
| 910 if (current_browser) { | 949 if (current_browser) { |
| 911 WebContents* contents = chrome::GetActiveWebContents(current_browser); | 950 WebContents* contents = chrome::GetActiveWebContents(current_browser); |
| 912 if (contents) | 951 if (contents) |
| 913 details->SetInteger("tab_id", ExtensionTabUtil::GetTabId(contents)); | 952 details->SetInteger("tab_id", ExtensionTabUtil::GetTabId(contents)); |
| 914 } | 953 } |
| 915 | 954 |
| 916 event_router->DispatchEventToExtension( | 955 event_router->DispatchEventToExtension( |
| 917 extension_id_, std::string("fileBrowserHandler.onExecute"), | 956 extension_id(), std::string("fileBrowserHandler.onExecute"), |
| 918 event_args.Pass(), profile(), GURL()); | 957 event_args.Pass(), profile(), GURL()); |
| 919 ExecuteDoneOnUIThread(true); | 958 ExecuteDoneOnUIThread(true); |
| 920 } | 959 } |
| 921 | 960 |
| 922 void ExtensionTaskExecutor::InitHandlerHostFileAccessPermissions( | 961 void ExtensionTaskExecutor::InitHandlerHostFileAccessPermissions( |
| 923 const FileDefinitionList& file_list, | 962 const FileDefinitionList& file_list, |
| 924 const Extension* handler_extension, | 963 const Extension* handler_extension, |
| 925 const std::string& action_id, | |
| 926 const base::Closure& callback) { | 964 const base::Closure& callback) { |
| 927 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 965 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 928 | 966 |
| 929 scoped_ptr<std::vector<FilePath> > gdata_paths(new std::vector<FilePath>); | 967 scoped_ptr<std::vector<FilePath> > gdata_paths(new std::vector<FilePath>); |
| 930 for (FileDefinitionList::const_iterator iter = file_list.begin(); | 968 for (FileDefinitionList::const_iterator iter = file_list.begin(); |
| 931 iter != file_list.end(); | 969 iter != file_list.end(); |
| 932 ++iter) { | 970 ++iter) { |
| 933 // Setup permission for file's absolute file. | 971 // Setup permission for file's absolute file. |
| 934 handler_host_permissions_.push_back(std::make_pair( | 972 handler_host_permissions_.push_back(std::make_pair(iter->absolute_path, |
| 935 iter->absolute_path, | 973 GetAccessPermissionsForFileBrowserHandler(handler_extension, |
| 936 GetAccessPermissionsForHandler(handler_extension, action_id))); | 974 action_id_))); |
| 937 | 975 |
| 938 if (gdata::util::IsUnderDriveMountPoint(iter->absolute_path)) | 976 if (gdata::util::IsUnderDriveMountPoint(iter->absolute_path)) |
| 939 gdata_paths->push_back(iter->virtual_path); | 977 gdata_paths->push_back(iter->virtual_path); |
| 940 } | 978 } |
| 941 | 979 |
| 942 if (gdata_paths->empty()) { | 980 if (gdata_paths->empty()) { |
| 943 // Invoke callback if none of the files are on gdata mount point. | 981 // Invoke callback if none of the files are on gdata mount point. |
| 944 callback.Run(); | 982 callback.Run(); |
| 945 return; | 983 return; |
| 946 } | 984 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 959 content::ChildProcessSecurityPolicy::GetInstance()->GrantPermissionsForFile( | 997 content::ChildProcessSecurityPolicy::GetInstance()->GrantPermissionsForFile( |
| 960 handler_pid, | 998 handler_pid, |
| 961 handler_host_permissions_[i].first, | 999 handler_host_permissions_[i].first, |
| 962 handler_host_permissions_[i].second); | 1000 handler_host_permissions_[i].second); |
| 963 } | 1001 } |
| 964 | 1002 |
| 965 // We don't need this anymore. | 1003 // We don't need this anymore. |
| 966 handler_host_permissions_.clear(); | 1004 handler_host_permissions_.clear(); |
| 967 } | 1005 } |
| 968 | 1006 |
| 1007 WebIntentTaskExecutor::WebIntentTaskExecutor( | |
| 1008 Profile* profile, | |
| 1009 const GURL source_url, | |
| 1010 const std::string& extension_id, | |
| 1011 const std::string& action_id) | |
| 1012 : FileTaskExecutor(profile, extension_id), | |
| 1013 source_url_(source_url), | |
| 1014 action_id_(action_id) { | |
| 1015 } | |
| 1016 | |
| 1017 WebIntentTaskExecutor::~WebIntentTaskExecutor() {} | |
| 1018 | |
| 1019 bool WebIntentTaskExecutor::ExecuteAndNotify( | |
| 1020 const std::vector<GURL>& file_urls, | |
| 1021 const FileTaskFinishedCallback& done) { | |
| 1022 bool success = true; | |
| 1023 | |
| 1024 for (std::vector<GURL>::const_iterator i = file_urls.begin(); | |
| 1025 i != file_urls.end(); ++i) { | |
| 1026 if (!ExecuteForURL(*i)) | |
| 1027 success = false; | |
| 1028 } | |
| 1029 | |
| 1030 if (!done.is_null()) | |
| 1031 done.Run(success); | |
| 1032 | |
| 1033 return true; | |
| 1034 } | |
| 1035 | |
| 1036 bool WebIntentTaskExecutor::ExecuteForURL(const GURL& file_url) { | |
| 1037 fileapi::FileSystemURL url(file_url); | |
| 1038 if (!chromeos::CrosMountPointProvider::CanHandleURL(url)) | |
| 1039 return false; | |
| 1040 | |
| 1041 scoped_refptr<fileapi::FileSystemContext> file_system_context = | |
| 1042 BrowserContext::GetFileSystemContext(profile()); | |
| 1043 fileapi::ExternalFileSystemMountPointProvider* external_provider = | |
| 1044 file_system_context->external_provider(); | |
| 1045 if (!external_provider || !external_provider->IsAccessAllowed(url)) | |
| 1046 return false; | |
| 1047 | |
| 1048 // Make sure this url really being used by the right caller extension. | |
| 1049 if (source_url_.GetOrigin() != url.origin()) | |
| 1050 return false; | |
| 1051 | |
| 1052 FilePath local_path = url.path(); | |
| 1053 extensions::LaunchPlatformAppWithPath(profile(), GetExtension(), local_path); | |
| 1054 return true; | |
| 1055 } | |
| 1056 | |
| 969 } // namespace file_handler_util | 1057 } // namespace file_handler_util |
| OLD | NEW |