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

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

Powered by Google App Engine
This is Rietveld 408576698