Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: chrome/browser/chromeos/extensions/file_handler_util.cc

Issue 10834383: Chrome OS "open with" picker allowing Web Intents (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: reorder class, revert net/* Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 27 matching lines...) Expand all
93 for (Extension::FileBrowserHandlerList::const_iterator action_iter = 90 for (Extension::FileBrowserHandlerList::const_iterator action_iter =
94 extension->file_browser_handlers()->begin(); 91 extension->file_browser_handlers()->begin();
95 action_iter != extension->file_browser_handlers()->end(); 92 action_iter != extension->file_browser_handlers()->end();
96 ++action_iter) { 93 ++action_iter) {
97 if (action_iter->get()->id() == action_id) 94 if (action_iter->get()->id() == action_id)
98 return action_iter->get(); 95 return action_iter->get();
99 } 96 }
100 return NULL; 97 return NULL;
101 } 98 }
102 99
103 unsigned int GetAccessPermissionsForHandler(const Extension* extension, 100 unsigned int GetAccessPermissionsForFileBrowserHandler(
104 const std::string& action_id) { 101 const Extension* extension,
102 const std::string& action_id) {
105 const FileBrowserHandler* action = 103 const FileBrowserHandler* action =
106 FindFileBrowserHandler(extension, action_id); 104 FindFileBrowserHandler(extension, action_id);
107 if (!action) 105 if (!action)
108 return 0; 106 return 0;
109 unsigned int result = 0; 107 unsigned int result = 0;
110 if (action->CanRead()) 108 if (action->CanRead())
111 result |= kReadOnlyFilePermissions; 109 result |= kReadOnlyFilePermissions;
112 if (action->CanWrite()) 110 if (action->CanWrite())
113 result |= kReadWriteFilePermissions; 111 result |= kReadWriteFilePermissions;
114 // TODO(tbarzic): We don't handle Create yet. 112 // TODO(tbarzic): We don't handle Create yet.
115 return result; 113 return result;
116 } 114 }
117 115
118
119 std::string EscapedUtf8ToLower(const std::string& str) { 116 std::string EscapedUtf8ToLower(const std::string& str) {
120 string16 utf16 = UTF8ToUTF16( 117 string16 utf16 = UTF8ToUTF16(
121 net::UnescapeURLComponent(str, net::UnescapeRule::NORMAL)); 118 net::UnescapeURLComponent(str, net::UnescapeRule::NORMAL));
122 return net::EscapeUrlEncodedData( 119 return net::EscapeUrlEncodedData(
123 UTF16ToUTF8(base::i18n::ToLower(utf16)), 120 UTF16ToUTF8(base::i18n::ToLower(utf16)),
124 false /* do not replace space with plus */); 121 false /* do not replace space with plus */);
125 } 122 }
126 123
127 bool GetFileBrowserHandlers(Profile* profile, 124 bool GetFileBrowserHandlers(Profile* profile,
128 const GURL& selected_file_url, 125 const GURL& selected_file_url,
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 223
227 int GetReadWritePermissions() { 224 int GetReadWritePermissions() {
228 return kReadWriteFilePermissions; 225 return kReadWriteFilePermissions;
229 } 226 }
230 227
231 int GetReadOnlyPermissions() { 228 int GetReadOnlyPermissions() {
232 return kReadOnlyFilePermissions; 229 return kReadOnlyFilePermissions;
233 } 230 }
234 231
235 std::string MakeTaskID(const std::string& extension_id, 232 std::string MakeTaskID(const std::string& extension_id,
233 TaskType task_type,
236 const std::string& action_id) { 234 const std::string& action_id) {
237 return base::StringPrintf("%s|%s", extension_id.c_str(), action_id.c_str()); 235 DCHECK(task_type == TASK_FILE
236 || task_type == TASK_DRIVE
237 || task_type == TASK_WEBINTENT);
238 return base::StringPrintf("%s|%d|%s",
239 extension_id.c_str(),
240 task_type,
241 action_id.c_str());
238 } 242 }
239 243
240 std::string MakeDriveTaskID(const std::string& app_id, 244 const char kDriveTaskExtensionPrefix[] = "drive-app:";
241 const std::string& action_id) { 245 const size_t kDriveTaskExtensionPrefixLength =
242 return MakeTaskID(FileTaskExecutor::kDriveTaskExtensionPrefix + app_id, 246 arraysize(kDriveTaskExtensionPrefix) - 1;
243 action_id);
244 }
245
246 bool CrackDriveTaskID(const std::string& task_id,
247 std::string* app_id,
248 std::string* action_id) {
249 std::string app_id_tmp;
250 std::string action_id_tmp;
251 if (!CrackTaskID(task_id, &app_id_tmp, &action_id_tmp))
252 return false;
253 if (StartsWithASCII(app_id_tmp,
254 FileTaskExecutor::kDriveTaskExtensionPrefix,
255 false)) {
256 // Strip off the prefix from the extension ID so we convert it to an app id.
257 if (app_id) {
258 *app_id = app_id_tmp.substr(
259 FileTaskExecutor::kDriveTaskExtensionPrefixLength);
260 }
261 if (action_id)
262 *action_id = action_id_tmp;
263 return true;
264 }
265 return false;
266 }
267 247
268 // Breaks down task_id that is used between getFileTasks() and executeTask() on 248 // Breaks down task_id that is used between getFileTasks() and executeTask() on
269 // its building blocks. task_id field the following structure: 249 // its building blocks. task_id field the following structure:
270 // <extension-id>|<task-action-id> 250 // <extension-id>|<task-type>|<task-action-id>
271 bool CrackTaskID(const std::string& task_id, 251 bool CrackTaskID(const std::string& task_id,
272 std::string* extension_id, 252 std::string* extension_id,
253 TaskType* task_type,
273 std::string* action_id) { 254 std::string* action_id) {
274 std::vector<std::string> result; 255 std::vector<std::string> result;
275 int count = Tokenize(task_id, std::string("|"), &result); 256 int count = Tokenize(task_id, std::string("|"), &result);
276 if (count != 2) 257
258 // Parse historic task_id parameters that only contain two parts. Drive tasks
259 // are identified by a prefix "drive-app:" on the extension ID.
260 if (count == 2) {
261 if (StartsWithASCII(result[0], kDriveTaskExtensionPrefix, true)) {
262 if (task_type)
263 *task_type = TASK_DRIVE;
264
265 if (extension_id)
266 *extension_id = result[0].substr(kDriveTaskExtensionPrefixLength);
267 } else {
268 if (task_type)
269 *task_type = TASK_FILE;
270
271 if (extension_id)
272 *extension_id = result[0];
273 }
274
275 if (action_id)
276 *action_id = result[1];
277
278 return true;
279 }
280
281 if (count != 3)
277 return false; 282 return false;
283
278 if (extension_id) 284 if (extension_id)
279 *extension_id = result[0]; 285 *extension_id = result[0];
286
287 if (task_type) {
288 *task_type = file_handler_util::TaskType(atoi(result[1].c_str()));
289 DCHECK(*task_type == TASK_FILE
290 || *task_type == TASK_DRIVE
291 || *task_type == TASK_WEBINTENT);
292 }
293
280 if (action_id) 294 if (action_id)
281 *action_id = result[1]; 295 *action_id = result[2];
296
282 return true; 297 return true;
283 } 298 }
284 299
285 // Find a specific handler in the handler list. 300 // Find a specific handler in the handler list.
286 FileBrowserHandlerSet::iterator FindHandler( 301 FileBrowserHandlerSet::iterator FindHandler(
287 FileBrowserHandlerSet* handler_set, 302 FileBrowserHandlerSet* handler_set,
288 const std::string& extension_id, 303 const std::string& extension_id,
289 const std::string& id) { 304 const std::string& id) {
290 FileBrowserHandlerSet::iterator iter = handler_set->begin(); 305 FileBrowserHandlerSet::iterator iter = handler_set->begin();
291 while (iter != handler_set->end() && 306 while (iter != handler_set->end() &&
292 !((*iter)->extension_id() == extension_id && 307 !((*iter)->extension_id() == extension_id &&
293 (*iter)->id() == id)) { 308 (*iter)->id() == id)) {
294 iter++; 309 iter++;
295 } 310 }
296 return iter; 311 return iter;
297 } 312 }
298 313
314 // Given the list of selected files, returns array of file action tasks
315 // that are shared between them.
299 void FindDefaultTasks(Profile* profile, 316 void FindDefaultTasks(Profile* profile,
300 const std::vector<GURL>& files_list, 317 const std::vector<GURL>& files_list,
301 const FileBrowserHandlerSet& common_tasks, 318 const FileBrowserHandlerSet& common_tasks,
302 FileBrowserHandlerSet* default_tasks) { 319 FileBrowserHandlerSet* default_tasks) {
303 DCHECK(default_tasks); 320 DCHECK(default_tasks);
304 default_tasks->clear(); 321 default_tasks->clear();
305 322
306 std::set<std::string> default_ids; 323 std::set<std::string> default_ids;
307 for (std::vector<GURL>::const_iterator it = files_list.begin(); 324 for (std::vector<GURL>::const_iterator it = files_list.begin();
308 it != files_list.end(); ++it) { 325 it != files_list.end(); ++it) {
309 // Get the default task for this file based only on the extension (since 326 // Get the default task for this file based only on the extension (since
310 // we don't have MIME types here), and add it to the set of default tasks. 327 // we don't have MIME types here), and add it to the set of default tasks.
311 fileapi::FileSystemURL filesystem_url(*it); 328 fileapi::FileSystemURL filesystem_url(*it);
312 if (filesystem_url.is_valid() && 329 if (filesystem_url.is_valid() &&
313 (filesystem_url.type() == fileapi::kFileSystemTypeDrive || 330 (filesystem_url.type() == fileapi::kFileSystemTypeDrive ||
314 filesystem_url.type() == fileapi::kFileSystemTypeNativeMedia || 331 filesystem_url.type() == fileapi::kFileSystemTypeNativeMedia ||
315 filesystem_url.type() == fileapi::kFileSystemTypeNativeLocal)) { 332 filesystem_url.type() == fileapi::kFileSystemTypeNativeLocal)) {
316 std::string task_id = file_handler_util::GetDefaultTaskIdFromPrefs( 333 std::string task_id = file_handler_util::GetDefaultTaskIdFromPrefs(
317 profile, "", filesystem_url.virtual_path().Extension()); 334 profile, "", filesystem_url.virtual_path().Extension());
318 if (!task_id.empty()) 335 if (!task_id.empty())
319 default_ids.insert(task_id); 336 default_ids.insert(task_id);
320 } 337 }
321 } 338 }
322 339
323 // Convert the default task IDs collected above to one of the handler pointers 340 // Convert the default task IDs collected above to one of the handler pointers
324 // from common_tasks. 341 // from common_tasks.
325 for (FileBrowserHandlerSet::const_iterator task_iter = common_tasks.begin(); 342 for (FileBrowserHandlerSet::const_iterator task_iter = common_tasks.begin();
326 task_iter != common_tasks.end(); ++task_iter) { 343 task_iter != common_tasks.end(); ++task_iter) {
327 std::string task_id = MakeTaskID((*task_iter)->extension_id(), 344 std::string task_id = MakeTaskID((*task_iter)->extension_id(), TASK_FILE,
328 (*task_iter)->id()); 345 (*task_iter)->id());
329 for (std::set<std::string>::iterator default_iter = default_ids.begin(); 346 for (std::set<std::string>::iterator default_iter = default_ids.begin();
330 default_iter != default_ids.end(); ++default_iter) { 347 default_iter != default_ids.end(); ++default_iter) {
331 if (task_id == *default_iter) { 348 if (task_id == *default_iter) {
332 default_tasks->insert(*task_iter); 349 default_tasks->insert(*task_iter);
333 break; 350 break;
334 } 351 }
335 } 352 }
336 } 353 }
337 } 354 }
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 const GURL& file_system_root, 474 const GURL& file_system_root,
458 const FileDefinitionList& file_list, 475 const FileDefinitionList& file_list,
459 int handler_pid_in, 476 int handler_pid_in,
460 extensions::ExtensionHost* host); 477 extensions::ExtensionHost* host);
461 478
462 // Populates |handler_host_permissions| with file path-permissions pairs that 479 // Populates |handler_host_permissions| with file path-permissions pairs that
463 // will be given to the handler extension host process. 480 // will be given to the handler extension host process.
464 void InitHandlerHostFileAccessPermissions( 481 void InitHandlerHostFileAccessPermissions(
465 const FileDefinitionList& file_list, 482 const FileDefinitionList& file_list,
466 const extensions::Extension* handler_extension, 483 const extensions::Extension* handler_extension,
467 const std::string& action_id,
468 const base::Closure& callback); 484 const base::Closure& callback);
469 485
470 // Invoked upon completion of InitHandlerHostFileAccessPermissions initiated 486 // Invoked upon completion of InitHandlerHostFileAccessPermissions initiated
471 // by ExecuteFileActionsOnUIThread. 487 // by ExecuteFileActionsOnUIThread.
472 void OnInitAccessForExecuteFileActionsOnUIThread( 488 void OnInitAccessForExecuteFileActionsOnUIThread(
473 const std::string& file_system_name, 489 const std::string& file_system_name,
474 const GURL& file_system_root, 490 const GURL& file_system_root,
475 const FileDefinitionList& file_list, 491 const FileDefinitionList& file_list,
476 int handler_pid); 492 int handler_pid);
477 493
478 // Registers file permissions from |handler_host_permissions_| with 494 // Registers file permissions from |handler_host_permissions_| with
479 // ChildProcessSecurityPolicy for process with id |handler_pid|. 495 // ChildProcessSecurityPolicy for process with id |handler_pid|.
480 void SetupHandlerHostFileAccessPermissions(int handler_pid); 496 void SetupHandlerHostFileAccessPermissions(int handler_pid);
481 497
482 // Helper function to get the extension pointer.
483 const extensions::Extension* GetExtension();
484
485 const GURL source_url_; 498 const GURL source_url_;
486 const std::string extension_id_;
487 const std::string action_id_; 499 const std::string action_id_;
488 FileTaskFinishedCallback done_; 500 FileTaskFinishedCallback done_;
489 501
490 // (File path, permission for file path) pairs for the handler. 502 // (File path, permission for file path) pairs for the handler.
491 std::vector<std::pair<FilePath, int> > handler_host_permissions_; 503 std::vector<std::pair<FilePath, int> > handler_host_permissions_;
492 }; 504 };
493 505
494 // static 506 class WebIntentTaskExecutor : public FileTaskExecutor {
507 public:
508 // FileTaskExecutor overrides.
509 virtual bool ExecuteAndNotify(const std::vector<GURL>& file_urls,
510 const FileTaskFinishedCallback& done) OVERRIDE;
511
512 private:
513 // FileTaskExecutor is the only class allowed to create one.
514 friend class FileTaskExecutor;
515
516 WebIntentTaskExecutor(Profile* profile,
517 const GURL source_url,
518 const std::string& extension_id,
519 const std::string& action_id);
520 virtual ~WebIntentTaskExecutor();
521
522 bool ExecuteForURL(const GURL& file_url);
523
524 const GURL source_url_;
525 const std::string extension_id_;
526 const std::string action_id_;
527 };
528
529 //
tbarzic 2012/09/13 04:29:17 you lost 'static' in the comment
thorogood 2012/09/13 08:14:04 Done.
495 FileTaskExecutor* FileTaskExecutor::Create(Profile* profile, 530 FileTaskExecutor* FileTaskExecutor::Create(Profile* profile,
496 const GURL source_url, 531 const GURL source_url,
497 const std::string& extension_id, 532 const std::string& extension_id,
533 TaskType task_type,
498 const std::string& action_id) { 534 const std::string& action_id) {
499 // Check out the extension ID and see if this is a drive task, 535 switch (task_type) {
500 // and instantiate drive-specific executor if so. 536 case TASK_DRIVE:
501 if (StartsWithASCII(extension_id, 537 return new gdata::DriveTaskExecutor(profile,
502 FileTaskExecutor::kDriveTaskExtensionPrefix, 538 extension_id, // really app_id
503 false)) { 539 action_id);
504 return new gdata::DriveTaskExecutor(profile, 540 case TASK_WEBINTENT:
505 extension_id, // really app_id 541 return new WebIntentTaskExecutor(profile,
506 action_id); 542 source_url,
507 } else { 543 extension_id,
508 return new ExtensionTaskExecutor(profile, 544 action_id);
509 source_url, 545 case TASK_FILE:
510 extension_id, 546 return new ExtensionTaskExecutor(profile,
511 action_id); 547 source_url,
548 extension_id,
549 action_id);
550 default:
551 NOTREACHED();
552 return NULL;
553 // TODO: bigger explosion than this
512 } 554 }
513 } 555 }
514 556
515 FileTaskExecutor::FileTaskExecutor(Profile* profile) : profile_(profile) { 557 FileTaskExecutor::FileTaskExecutor(
558 Profile* profile,
559 const std::string& extension_id)
560 : profile_(profile),
561 extension_id_(extension_id) {
516 } 562 }
517 563
518 FileTaskExecutor::~FileTaskExecutor() { 564 FileTaskExecutor::~FileTaskExecutor() {
519 } 565 }
520 566
521 bool FileTaskExecutor::Execute(const std::vector<GURL>& file_urls) { 567 bool FileTaskExecutor::Execute(const std::vector<GURL>& file_urls) {
522 return ExecuteAndNotify(file_urls, FileTaskFinishedCallback()); 568 return ExecuteAndNotify(file_urls, FileTaskFinishedCallback());
523 } 569 }
524 570
525 Browser* FileTaskExecutor::GetBrowser() const { 571 Browser* FileTaskExecutor::GetBrowser() const {
526 return browser::FindOrCreateTabbedBrowser( 572 return browser::FindOrCreateTabbedBrowser(
527 profile_ ? profile_ : ProfileManager::GetDefaultProfileOrOffTheRecord()); 573 profile_ ? profile_ : ProfileManager::GetDefaultProfileOrOffTheRecord());
528 } 574 }
529 575
576 const Extension* FileTaskExecutor::GetExtension() {
577 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
578
579 ExtensionService* service = profile()->GetExtensionService();
580 return service ? service->GetExtensionById(extension_id_, false) :
581 NULL;
582 }
583
530 ExtensionTaskExecutor::FileDefinition::FileDefinition() : is_directory(false) { 584 ExtensionTaskExecutor::FileDefinition::FileDefinition() : is_directory(false) {
531 } 585 }
532 586
533 ExtensionTaskExecutor::FileDefinition::~FileDefinition() { 587 ExtensionTaskExecutor::FileDefinition::~FileDefinition() {
534 } 588 }
535 589
536 class ExtensionTaskExecutor::ExecuteTasksFileSystemCallbackDispatcher { 590 class ExtensionTaskExecutor::ExecuteTasksFileSystemCallbackDispatcher {
537 public: 591 public:
538 static fileapi::FileSystemContext::OpenFileSystemCallback CreateCallback( 592 static fileapi::FileSystemContext::OpenFileSystemCallback CreateCallback(
539 ExtensionTaskExecutor* executor, 593 ExtensionTaskExecutor* executor,
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 std::string action_id_; 743 std::string action_id_;
690 std::vector<GURL> origin_file_urls_; 744 std::vector<GURL> origin_file_urls_;
691 DISALLOW_COPY_AND_ASSIGN(ExecuteTasksFileSystemCallbackDispatcher); 745 DISALLOW_COPY_AND_ASSIGN(ExecuteTasksFileSystemCallbackDispatcher);
692 }; 746 };
693 747
694 ExtensionTaskExecutor::ExtensionTaskExecutor( 748 ExtensionTaskExecutor::ExtensionTaskExecutor(
695 Profile* profile, 749 Profile* profile,
696 const GURL source_url, 750 const GURL source_url,
697 const std::string& extension_id, 751 const std::string& extension_id,
698 const std::string& action_id) 752 const std::string& action_id)
699 : FileTaskExecutor(profile), 753 : FileTaskExecutor(profile, extension_id),
700 source_url_(source_url), 754 source_url_(source_url),
701 extension_id_(extension_id),
702 action_id_(action_id) { 755 action_id_(action_id) {
703 } 756 }
704 757
705 ExtensionTaskExecutor::~ExtensionTaskExecutor() {} 758 ExtensionTaskExecutor::~ExtensionTaskExecutor() {}
706 759
707 bool ExtensionTaskExecutor::ExecuteAndNotify( 760 bool ExtensionTaskExecutor::ExecuteAndNotify(
708 const std::vector<GURL>& file_urls, 761 const std::vector<GURL>& file_urls,
709 const FileTaskFinishedCallback& done) { 762 const FileTaskFinishedCallback& done) {
710 ExtensionService* service = profile()->GetExtensionService(); 763 scoped_refptr<const Extension> handler = GetExtension();
711 if (!service)
712 return false;
713
714 scoped_refptr<const Extension> handler =
715 service->GetExtensionById(extension_id_, false);
716 764
717 if (!handler.get()) 765 if (!handler.get())
718 return false; 766 return false;
719 767
720 int handler_pid = ExtractProcessFromExtensionId(handler->id(), profile()); 768 int handler_pid = ExtractProcessFromExtensionId(handler->id(), profile());
721 if (handler_pid <= 0) { 769 if (handler_pid <= 0) {
722 if (!handler->has_lazy_background_page()) 770 if (!handler->has_lazy_background_page())
723 return false; 771 return false;
724 } 772 }
725 773
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 file_urls)); 809 file_urls));
762 } 810 }
763 811
764 void ExtensionTaskExecutor::ExecuteDoneOnUIThread(bool success) { 812 void ExtensionTaskExecutor::ExecuteDoneOnUIThread(bool success) {
765 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 813 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
766 if (!done_.is_null()) 814 if (!done_.is_null())
767 done_.Run(success); 815 done_.Run(success);
768 done_.Reset(); 816 done_.Reset();
769 } 817 }
770 818
771 const Extension* ExtensionTaskExecutor::GetExtension() {
772 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
773
774 ExtensionService* service = profile()->GetExtensionService();
775 return service ? service->GetExtensionById(extension_id_, false) :
776 NULL;
777 }
778
779 void ExtensionTaskExecutor::ExecuteFileActionsOnUIThread( 819 void ExtensionTaskExecutor::ExecuteFileActionsOnUIThread(
780 const std::string& file_system_name, 820 const std::string& file_system_name,
781 const GURL& file_system_root, 821 const GURL& file_system_root,
782 const FileDefinitionList& file_list, 822 const FileDefinitionList& file_list,
783 int handler_pid) { 823 int handler_pid) {
784 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 824 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
785 825
786 const Extension* extension = GetExtension(); 826 const Extension* extension = GetExtension();
787 if (!extension) { 827 if (!extension) {
788 ExecuteDoneOnUIThread(false); 828 ExecuteDoneOnUIThread(false);
789 return; 829 return;
790 } 830 }
791 831
792 InitHandlerHostFileAccessPermissions( 832 InitHandlerHostFileAccessPermissions(
793 file_list, 833 file_list,
794 extension, 834 extension,
795 action_id_,
796 base::Bind( 835 base::Bind(
797 &ExtensionTaskExecutor::OnInitAccessForExecuteFileActionsOnUIThread, 836 &ExtensionTaskExecutor::OnInitAccessForExecuteFileActionsOnUIThread,
798 this, 837 this,
799 file_system_name, 838 file_system_name,
800 file_system_root, 839 file_system_root,
801 file_list, 840 file_list,
802 handler_pid)); 841 handler_pid));
803 } 842 }
804 843
805 void ExtensionTaskExecutor::OnInitAccessForExecuteFileActionsOnUIThread( 844 void ExtensionTaskExecutor::OnInitAccessForExecuteFileActionsOnUIThread(
(...skipping 15 matching lines...) Expand all
821 } else { 860 } else {
822 // We have to wake the handler background page before we proceed. 861 // We have to wake the handler background page before we proceed.
823 extensions::LazyBackgroundTaskQueue* queue = 862 extensions::LazyBackgroundTaskQueue* queue =
824 extensions::ExtensionSystem::Get(profile())-> 863 extensions::ExtensionSystem::Get(profile())->
825 lazy_background_task_queue(); 864 lazy_background_task_queue();
826 if (!queue->ShouldEnqueueTask(profile(), extension)) { 865 if (!queue->ShouldEnqueueTask(profile(), extension)) {
827 ExecuteDoneOnUIThread(false); 866 ExecuteDoneOnUIThread(false);
828 return; 867 return;
829 } 868 }
830 queue->AddPendingTask( 869 queue->AddPendingTask(
831 profile(), extension_id_, 870 profile(), extension_id(),
832 base::Bind(&ExtensionTaskExecutor::SetupPermissionsAndDispatchEvent, 871 base::Bind(&ExtensionTaskExecutor::SetupPermissionsAndDispatchEvent,
833 this, file_system_name, file_system_root, file_list, 872 this, file_system_name, file_system_root, file_list,
834 handler_pid)); 873 handler_pid));
835 } 874 }
836 } 875 }
837 876
838 void ExtensionTaskExecutor::SetupPermissionsAndDispatchEvent( 877 void ExtensionTaskExecutor::SetupPermissionsAndDispatchEvent(
839 const std::string& file_system_name, 878 const std::string& file_system_name,
840 const GURL& file_system_root, 879 const GURL& file_system_root,
841 const FileDefinitionList& file_list, 880 const FileDefinitionList& file_list,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 919
881 // Get tab id. 920 // Get tab id.
882 Browser* current_browser = GetBrowser(); 921 Browser* current_browser = GetBrowser();
883 if (current_browser) { 922 if (current_browser) {
884 WebContents* contents = chrome::GetActiveWebContents(current_browser); 923 WebContents* contents = chrome::GetActiveWebContents(current_browser);
885 if (contents) 924 if (contents)
886 details->SetInteger("tab_id", ExtensionTabUtil::GetTabId(contents)); 925 details->SetInteger("tab_id", ExtensionTabUtil::GetTabId(contents));
887 } 926 }
888 927
889 event_router->DispatchEventToExtension( 928 event_router->DispatchEventToExtension(
890 extension_id_, std::string("fileBrowserHandler.onExecute"), 929 extension_id(), std::string("fileBrowserHandler.onExecute"),
891 event_args.Pass(), profile(), GURL()); 930 event_args.Pass(), profile(), GURL());
892 ExecuteDoneOnUIThread(true); 931 ExecuteDoneOnUIThread(true);
893 } 932 }
894 933
895 void ExtensionTaskExecutor::InitHandlerHostFileAccessPermissions( 934 void ExtensionTaskExecutor::InitHandlerHostFileAccessPermissions(
896 const FileDefinitionList& file_list, 935 const FileDefinitionList& file_list,
897 const Extension* handler_extension, 936 const Extension* handler_extension,
898 const std::string& action_id,
899 const base::Closure& callback) { 937 const base::Closure& callback) {
900 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 938 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
901 939
902 scoped_ptr<std::vector<FilePath> > gdata_paths(new std::vector<FilePath>); 940 scoped_ptr<std::vector<FilePath> > gdata_paths(new std::vector<FilePath>);
903 for (FileDefinitionList::const_iterator iter = file_list.begin(); 941 for (FileDefinitionList::const_iterator iter = file_list.begin();
904 iter != file_list.end(); 942 iter != file_list.end();
905 ++iter) { 943 ++iter) {
906 // Setup permission for file's absolute file. 944 // Setup permission for file's absolute file.
907 handler_host_permissions_.push_back(std::make_pair( 945 handler_host_permissions_.push_back(std::make_pair(iter->absolute_path,
908 iter->absolute_path, 946 GetAccessPermissionsForFileBrowserHandler(handler_extension,
909 GetAccessPermissionsForHandler(handler_extension, action_id))); 947 action_id_)));
910 948
911 if (gdata::util::IsUnderDriveMountPoint(iter->absolute_path)) 949 if (gdata::util::IsUnderDriveMountPoint(iter->absolute_path))
912 gdata_paths->push_back(iter->virtual_path); 950 gdata_paths->push_back(iter->virtual_path);
913 } 951 }
914 952
915 if (gdata_paths->empty()) { 953 if (gdata_paths->empty()) {
916 // Invoke callback if none of the files are on gdata mount point. 954 // Invoke callback if none of the files are on gdata mount point.
917 callback.Run(); 955 callback.Run();
918 return; 956 return;
919 } 957 }
(...skipping 12 matching lines...) Expand all
932 content::ChildProcessSecurityPolicy::GetInstance()->GrantPermissionsForFile( 970 content::ChildProcessSecurityPolicy::GetInstance()->GrantPermissionsForFile(
933 handler_pid, 971 handler_pid,
934 handler_host_permissions_[i].first, 972 handler_host_permissions_[i].first,
935 handler_host_permissions_[i].second); 973 handler_host_permissions_[i].second);
936 } 974 }
937 975
938 // We don't need this anymore. 976 // We don't need this anymore.
939 handler_host_permissions_.clear(); 977 handler_host_permissions_.clear();
940 } 978 }
941 979
980 WebIntentTaskExecutor::WebIntentTaskExecutor(
981 Profile* profile,
982 const GURL source_url,
983 const std::string& extension_id,
984 const std::string& action_id)
985 : FileTaskExecutor(profile, extension_id),
986 source_url_(source_url),
987 action_id_(action_id) {
988 }
989
990 WebIntentTaskExecutor::~WebIntentTaskExecutor() {}
991
992 bool WebIntentTaskExecutor::ExecuteAndNotify(
993 const std::vector<GURL>& file_urls,
994 const FileTaskFinishedCallback& done) {
995 bool success = true;
996
997 // If we're a Web Intent action (to an extension), short-circuit and deliver
tbarzic 2012/09/13 04:29:17 probably don't need the comment anymore.
thorogood 2012/09/13 08:14:04 Done.
998 // the Web Intent via LaunchPlatformAppWithPath.
999 for (std::vector<GURL>::const_iterator i = file_urls.begin();
1000 i != file_urls.end(); ++i) {
1001 if (!ExecuteForURL(*i))
1002 success = false;
1003 }
1004
1005 if (!done.is_null())
1006 done.Run(success);
1007
1008 return true;
1009 }
1010
1011 bool WebIntentTaskExecutor::ExecuteForURL(const GURL& file_url) {
1012 fileapi::FileSystemURL url(file_url);
1013 if (!chromeos::CrosMountPointProvider::CanHandleURL(url))
1014 return false;
1015
1016 scoped_refptr<fileapi::FileSystemContext> file_system_context =
1017 BrowserContext::GetFileSystemContext(profile());
1018 fileapi::ExternalFileSystemMountPointProvider* external_provider =
1019 file_system_context->external_provider();
1020 if (!external_provider || !external_provider->IsAccessAllowed(url))
1021 return false;
1022
1023 // Make sure this url really being used by the right caller extension.
1024 if (source_url_.GetOrigin() != url.origin())
1025 return false;
1026
1027 FilePath local_path = url.path();
1028
1029 bool is_drive_file = url.type() == fileapi::kFileSystemTypeDrive;
tbarzic 2012/09/13 04:29:17 I don't think this is needed.
thorogood 2012/09/13 08:14:04 Done.
1030 DCHECK(!is_drive_file || gdata::util::IsUnderDriveMountPoint(local_path));
1031
1032 extensions::LaunchPlatformAppWithPath(profile(), GetExtension(), local_path);
1033 return true;
1034 }
1035
942 } // namespace file_handler_util 1036 } // namespace file_handler_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698