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