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

Side by Side Diff: chrome/browser/chromeos/extensions/file_manager/file_tasks.cc

Issue 23532010: file_manager: Introduce FullTaskDescriptor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 7 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 | Annotate | Revision Log
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_manager/file_tasks.h" 5 #include "chrome/browser/chromeos/extensions/file_manager/file_tasks.h"
6 6
7 #include "apps/launcher.h" 7 #include "apps/launcher.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 !backend->IsAccessAllowed(files[i])) { 104 !backend->IsAccessAllowed(files[i])) {
105 return false; 105 return false;
106 } 106 }
107 } 107 }
108 108
109 return true; 109 return true;
110 } 110 }
111 111
112 } // namespace 112 } // namespace
113 113
114 FullTaskDescriptor::FullTaskDescriptor(
115 const TaskDescriptor& task_descriptor,
116 const std::string& task_title,
117 const GURL& icon_url,
118 bool is_default)
119 : task_descriptor_(task_descriptor),
120 task_title_(task_title),
121 icon_url_(icon_url),
122 is_default_(is_default){
123 }
124
125 scoped_ptr<base::DictionaryValue>
126 FullTaskDescriptor::AsDictionaryValue() const {
127 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue);
128 dictionary->SetString("taskId", TaskDescriptorToId(task_descriptor_));
129 if (!icon_url_.is_empty())
130 dictionary->SetString("iconUrl", icon_url_.spec());
131 dictionary->SetBoolean("driveApp",
132 task_descriptor_.task_type == TASK_TYPE_DRIVE_APP);
133 dictionary->SetString("title", task_title_);
134 dictionary->SetBoolean("isDefault", is_default_);
135 return dictionary.Pass();
136 }
137
114 void UpdateDefaultTask(PrefService* pref_service, 138 void UpdateDefaultTask(PrefService* pref_service,
115 const std::string& task_id, 139 const std::string& task_id,
116 const std::set<std::string>& suffixes, 140 const std::set<std::string>& suffixes,
117 const std::set<std::string>& mime_types) { 141 const std::set<std::string>& mime_types) {
118 if (!pref_service) 142 if (!pref_service)
119 return; 143 return;
120 144
121 if (!mime_types.empty()) { 145 if (!mime_types.empty()) {
122 DictionaryPrefUpdate mime_type_pref(pref_service, 146 DictionaryPrefUpdate mime_type_pref(pref_service,
123 prefs::kDefaultTasksByMimeType); 147 prefs::kDefaultTasksByMimeType);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 pref_service.GetDictionary(prefs::kDefaultTasksBySuffix); 187 pref_service.GetDictionary(prefs::kDefaultTasksBySuffix);
164 DCHECK(suffix_task_prefs); 188 DCHECK(suffix_task_prefs);
165 LOG_IF(ERROR, !suffix_task_prefs) << "Unable to open suffix prefs"; 189 LOG_IF(ERROR, !suffix_task_prefs) << "Unable to open suffix prefs";
166 std::string lower_suffix = StringToLowerASCII(suffix); 190 std::string lower_suffix = StringToLowerASCII(suffix);
167 if (suffix_task_prefs) 191 if (suffix_task_prefs)
168 suffix_task_prefs->GetStringWithoutPathExpansion(lower_suffix, &task_id); 192 suffix_task_prefs->GetStringWithoutPathExpansion(lower_suffix, &task_id);
169 VLOG_IF(1, !task_id.empty()) << "Found suffix default handler: " << task_id; 193 VLOG_IF(1, !task_id.empty()) << "Found suffix default handler: " << task_id;
170 return task_id; 194 return task_id;
171 } 195 }
172 196
173 std::string MakeTaskID(const std::string& extension_id, 197 std::string MakeTaskID(const std::string& app_id,
174 TaskType task_type, 198 TaskType task_type,
175 const std::string& action_id) { 199 const std::string& action_id) {
176 return base::StringPrintf("%s|%s|%s", 200 return base::StringPrintf("%s|%s|%s",
177 extension_id.c_str(), 201 app_id.c_str(),
178 TaskTypeToString(task_type).c_str(), 202 TaskTypeToString(task_type).c_str(),
179 action_id.c_str()); 203 action_id.c_str());
180 } 204 }
181 205
182 std::string MakeDriveAppTaskId(const std::string& app_id) { 206 std::string MakeDriveAppTaskId(const std::string& app_id) {
183 return MakeTaskID(app_id, TASK_TYPE_DRIVE_APP, "open-with"); 207 return MakeTaskID(app_id, TASK_TYPE_DRIVE_APP, "open-with");
184 } 208 }
185 209
210 std::string TaskDescriptorToId(const TaskDescriptor& task_descriptor) {
211 return MakeTaskID(task_descriptor.app_id,
212 task_descriptor.task_type,
213 task_descriptor.action_id);
214 }
215
186 bool ParseTaskID(const std::string& task_id, TaskDescriptor* task) { 216 bool ParseTaskID(const std::string& task_id, TaskDescriptor* task) {
187 DCHECK(task); 217 DCHECK(task);
188 218
189 std::vector<std::string> result; 219 std::vector<std::string> result;
190 int count = Tokenize(task_id, std::string("|"), &result); 220 int count = Tokenize(task_id, std::string("|"), &result);
191 221
192 // Parse a legacy task ID that only contain two parts. Drive tasks are 222 // Parse a legacy task ID that only contain two parts. Drive tasks are
193 // identified by a prefix "drive-app:" on the extension ID. The legacy task 223 // identified by a prefix "drive-app:" on the extension ID. The legacy task
194 // IDs can be stored in preferences. 224 // IDs can be stored in preferences.
195 // TODO(satorux): We should get rid of this code: crbug.com/267359. 225 // TODO(satorux): We should get rid of this code: crbug.com/267359.
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 std::string task_id = file_tasks::GetDefaultTaskIdFromPrefs( 376 std::string task_id = file_tasks::GetDefaultTaskIdFromPrefs(
347 pref_service, mime_type, file_path.Extension()); 377 pref_service, mime_type, file_path.Extension());
348 if (task_info_map.find(task_id) != task_info_map.end()) 378 if (task_info_map.find(task_id) != task_info_map.end())
349 default_tasks->insert(task_id); 379 default_tasks->insert(task_id);
350 } 380 }
351 } 381 }
352 382
353 void CreateDriveTasks( 383 void CreateDriveTasks(
354 const TaskInfoMap& task_info_map, 384 const TaskInfoMap& task_info_map,
355 const std::set<std::string>& default_tasks, 385 const std::set<std::string>& default_tasks,
356 ListValue* result_list, 386 std::vector<FullTaskDescriptor>* result_list,
357 bool* default_already_set) { 387 bool* default_already_set) {
358 DCHECK(result_list); 388 DCHECK(result_list);
359 DCHECK(default_already_set); 389 DCHECK(default_already_set);
360 390
361 for (TaskInfoMap::const_iterator iter = task_info_map.begin(); 391 for (TaskInfoMap::const_iterator iter = task_info_map.begin();
362 iter != task_info_map.end(); ++iter) { 392 iter != task_info_map.end(); ++iter) {
363 DictionaryValue* task = new DictionaryValue; 393 bool is_default = false;
364 task->SetString("taskId", iter->first);
365 task->SetString("title", iter->second.app_name);
366
367 const GURL& icon_url = iter->second.icon_url;
368 if (!icon_url.is_empty())
369 task->SetString("iconUrl", icon_url.spec());
370
371 task->SetBoolean("driveApp", true);
372
373 // Once we set a default app, we don't want to set any more. 394 // Once we set a default app, we don't want to set any more.
374 if (!(*default_already_set) && 395 if (!(*default_already_set) &&
375 default_tasks.find(iter->first) != default_tasks.end()) { 396 default_tasks.find(iter->first) != default_tasks.end()) {
376 task->SetBoolean("isDefault", true); 397 is_default = true;
377 *default_already_set = true; 398 *default_already_set = true;
378 } else {
379 task->SetBoolean("isDefault", false);
380 } 399 }
381 result_list->Append(task); 400
401 TaskDescriptor descriptor;
402 DCHECK(ParseTaskID(iter->first, &descriptor));
403 result_list->push_back(
404 FullTaskDescriptor(descriptor,
405 iter->second.app_name,
406 iter->second.icon_url,
407 is_default));
382 } 408 }
383 } 409 }
384 410
385 void FindDriveAppTasks( 411 void FindDriveAppTasks(
386 Profile* profile, 412 Profile* profile,
387 const PathAndMimeTypeSet& path_mime_set, 413 const PathAndMimeTypeSet& path_mime_set,
388 ListValue* result_list, 414 std::vector<FullTaskDescriptor>* result_list,
389 bool* default_already_set) { 415 bool* default_already_set) {
390 DCHECK(!path_mime_set.empty()); 416 DCHECK(!path_mime_set.empty());
391 DCHECK(result_list); 417 DCHECK(result_list);
392 DCHECK(default_already_set); 418 DCHECK(default_already_set);
393 419
394 drive::DriveIntegrationService* integration_service = 420 drive::DriveIntegrationService* integration_service =
395 drive::DriveIntegrationServiceFactory::GetForProfile(profile); 421 drive::DriveIntegrationServiceFactory::GetForProfile(profile);
396 // |integration_service| is NULL if Drive is disabled. 422 // |integration_service| is NULL if Drive is disabled.
397 if (!integration_service || !integration_service->drive_app_registry()) 423 if (!integration_service || !integration_service->drive_app_registry())
398 return; 424 return;
399 425
400 // Map of task_id to TaskInfo of available tasks. 426 // Map of task_id to TaskInfo of available tasks.
401 TaskInfoMap task_info_map; 427 TaskInfoMap task_info_map;
402 GetAvailableDriveTasks(*integration_service->drive_app_registry(), 428 GetAvailableDriveTasks(*integration_service->drive_app_registry(),
403 path_mime_set, 429 path_mime_set,
404 &task_info_map); 430 &task_info_map);
405 431
406 std::set<std::string> default_tasks; 432 std::set<std::string> default_tasks;
407 FindDefaultDriveTasks(*profile->GetPrefs(), 433 FindDefaultDriveTasks(*profile->GetPrefs(),
408 path_mime_set, 434 path_mime_set,
409 task_info_map, 435 task_info_map,
410 &default_tasks); 436 &default_tasks);
411 CreateDriveTasks( 437 CreateDriveTasks(
412 task_info_map, default_tasks, result_list, default_already_set); 438 task_info_map, default_tasks, result_list, default_already_set);
413 } 439 }
414 440
415 void FindFileHandlerTasks( 441 void FindFileHandlerTasks(
416 Profile* profile, 442 Profile* profile,
417 const PathAndMimeTypeSet& path_mime_set, 443 const PathAndMimeTypeSet& path_mime_set,
418 ListValue* result_list, 444 std::vector<FullTaskDescriptor>* result_list,
419 bool* default_already_set) { 445 bool* default_already_set) {
420 DCHECK(!path_mime_set.empty()); 446 DCHECK(!path_mime_set.empty());
421 DCHECK(result_list); 447 DCHECK(result_list);
422 DCHECK(default_already_set); 448 DCHECK(default_already_set);
423 449
424 ExtensionService* service = profile->GetExtensionService(); 450 ExtensionService* service = profile->GetExtensionService();
425 if (!service) 451 if (!service)
426 return; 452 return;
427 453
428 std::set<std::string> default_tasks; 454 std::set<std::string> default_tasks;
(...skipping 17 matching lines...) Expand all
446 continue; 472 continue;
447 473
448 typedef std::vector<const extensions::FileHandlerInfo*> FileHandlerList; 474 typedef std::vector<const extensions::FileHandlerInfo*> FileHandlerList;
449 FileHandlerList file_handlers = 475 FileHandlerList file_handlers =
450 FindFileHandlersForFiles(*extension, path_mime_set); 476 FindFileHandlersForFiles(*extension, path_mime_set);
451 if (file_handlers.empty()) 477 if (file_handlers.empty())
452 continue; 478 continue;
453 479
454 for (FileHandlerList::iterator i = file_handlers.begin(); 480 for (FileHandlerList::iterator i = file_handlers.begin();
455 i != file_handlers.end(); ++i) { 481 i != file_handlers.end(); ++i) {
456 DictionaryValue* task = new DictionaryValue;
457 std::string task_id = file_tasks::MakeTaskID( 482 std::string task_id = file_tasks::MakeTaskID(
458 extension->id(), file_tasks::TASK_TYPE_FILE_HANDLER, (*i)->id); 483 extension->id(), file_tasks::TASK_TYPE_FILE_HANDLER, (*i)->id);
459 task->SetString("taskId", task_id); 484
460 task->SetString("title", (*i)->title); 485 bool is_default = false;
461 if (!(*default_already_set) && ContainsKey(default_tasks, task_id)) { 486 if (!(*default_already_set) && ContainsKey(default_tasks, task_id)) {
462 task->SetBoolean("isDefault", true); 487 is_default = true;
463 *default_already_set = true; 488 *default_already_set = true;
464 } else {
465 task->SetBoolean("isDefault", false);
466 } 489 }
467 490
468 GURL best_icon = extensions::ExtensionIconSource::GetIconURL( 491 GURL best_icon = extensions::ExtensionIconSource::GetIconURL(
469 extension, 492 extension,
470 drive::util::kPreferredIconSize, 493 drive::util::kPreferredIconSize,
471 ExtensionIconSet::MATCH_BIGGER, 494 ExtensionIconSet::MATCH_BIGGER,
472 false, // grayscale 495 false, // grayscale
473 NULL); // exists 496 NULL); // exists
474 if (!best_icon.is_empty())
475 task->SetString("iconUrl", best_icon.spec());
476 else
477 task->SetString("iconUrl", kDefaultIcon);
478 497
479 task->SetBoolean("driveApp", false); 498 result_list->push_back(FullTaskDescriptor(
480 result_list->Append(task); 499 TaskDescriptor(extension->id(),
500 file_tasks::TASK_TYPE_FILE_HANDLER,
501 (*i)->id),
502 (*i)->title,
503 best_icon,
504 is_default));
481 } 505 }
482 } 506 }
483 } 507 }
484 508
485 void FindFileBrowserHandlerTasks( 509 void FindFileBrowserHandlerTasks(
486 Profile* profile, 510 Profile* profile,
487 const std::vector<GURL>& file_urls, 511 const std::vector<GURL>& file_urls,
488 const std::vector<base::FilePath>& file_paths, 512 const std::vector<base::FilePath>& file_paths,
489 ListValue* result_list, 513 std::vector<FullTaskDescriptor>* result_list,
490 bool* default_already_set) { 514 bool* default_already_set) {
491 DCHECK(!file_paths.empty()); 515 DCHECK(!file_paths.empty());
492 DCHECK(!file_urls.empty()); 516 DCHECK(!file_urls.empty());
493 DCHECK(result_list); 517 DCHECK(result_list);
494 DCHECK(default_already_set); 518 DCHECK(default_already_set);
495 519
496 file_browser_handlers::FileBrowserHandlerList common_tasks = 520 file_browser_handlers::FileBrowserHandlerList common_tasks =
497 file_browser_handlers::FindCommonFileBrowserHandlers(profile, file_urls); 521 file_browser_handlers::FindCommonFileBrowserHandlers(profile, file_urls);
498 if (common_tasks.empty()) 522 if (common_tasks.empty())
499 return; 523 return;
500 file_browser_handlers::FileBrowserHandlerList default_tasks = 524 file_browser_handlers::FileBrowserHandlerList default_tasks =
501 file_browser_handlers::FindDefaultFileBrowserHandlers( 525 file_browser_handlers::FindDefaultFileBrowserHandlers(
502 *profile->GetPrefs(), file_paths, common_tasks); 526 *profile->GetPrefs(), file_paths, common_tasks);
503 527
504 ExtensionService* service = 528 ExtensionService* service =
505 extensions::ExtensionSystem::Get(profile)->extension_service(); 529 extensions::ExtensionSystem::Get(profile)->extension_service();
506 for (file_browser_handlers::FileBrowserHandlerList::const_iterator iter = 530 for (file_browser_handlers::FileBrowserHandlerList::const_iterator iter =
507 common_tasks.begin(); 531 common_tasks.begin();
508 iter != common_tasks.end(); 532 iter != common_tasks.end();
509 ++iter) { 533 ++iter) {
510 const FileBrowserHandler* handler = *iter; 534 const FileBrowserHandler* handler = *iter;
511 const std::string extension_id = handler->extension_id(); 535 const std::string extension_id = handler->extension_id();
512 const Extension* extension = service->GetExtensionById(extension_id, false); 536 const Extension* extension = service->GetExtensionById(extension_id, false);
513 CHECK(extension); 537 DCHECK(extension);
514 DictionaryValue* task = new DictionaryValue; 538
515 task->SetString("taskId", file_tasks::MakeTaskID(
516 extension_id,
517 file_tasks::TASK_TYPE_FILE_BROWSER_HANDLER,
518 handler->id()));
519 task->SetString("title", handler->title());
520 // TODO(zelidrag): Figure out how to expose icon URL that task defined in 539 // TODO(zelidrag): Figure out how to expose icon URL that task defined in
521 // manifest instead of the default extension icon. 540 // manifest instead of the default extension icon.
522 GURL icon = extensions::ExtensionIconSource::GetIconURL( 541 const GURL icon_url = extensions::ExtensionIconSource::GetIconURL(
523 extension, 542 extension,
524 extension_misc::EXTENSION_ICON_BITTY, 543 extension_misc::EXTENSION_ICON_BITTY,
525 ExtensionIconSet::MATCH_BIGGER, 544 ExtensionIconSet::MATCH_BIGGER,
526 false, // grayscale 545 false, // grayscale
527 NULL); // exists 546 NULL); // exists
528 task->SetString("iconUrl", icon.spec());
529 task->SetBoolean("driveApp", false);
530 547
531 // Only set the default if there isn't already a default set. 548 // Only set the default if there isn't already a default set.
549 bool is_default = false;
532 if (!*default_already_set && 550 if (!*default_already_set &&
533 std::find(default_tasks.begin(), default_tasks.end(), *iter) != 551 std::find(default_tasks.begin(), default_tasks.end(), *iter) !=
534 default_tasks.end()) { 552 default_tasks.end()) {
535 task->SetBoolean("isDefault", true); 553 is_default = true;
536 *default_already_set = true; 554 *default_already_set = true;
537 } else {
538 task->SetBoolean("isDefault", false);
539 } 555 }
540 556
541 result_list->Append(task); 557 result_list->push_back(FullTaskDescriptor(
558 TaskDescriptor(extension_id,
559 file_tasks::TASK_TYPE_FILE_BROWSER_HANDLER,
560 handler->id()),
561 handler->title(),
562 icon_url,
563 is_default));
542 } 564 }
543 } 565 }
544 566
545 void FindAllTypesOfTasks( 567 void FindAllTypesOfTasks(
546 Profile* profile, 568 Profile* profile,
547 const PathAndMimeTypeSet& path_mime_set, 569 const PathAndMimeTypeSet& path_mime_set,
548 const std::vector<GURL>& file_urls, 570 const std::vector<GURL>& file_urls,
549 const std::vector<base::FilePath>& file_paths, 571 const std::vector<base::FilePath>& file_paths,
550 ListValue* result_list) { 572 std::vector<FullTaskDescriptor>* result_list) {
573 DCHECK(profile);
574 DCHECK(result_list);
575
551 // Check if file_paths contain a google document. 576 // Check if file_paths contain a google document.
552 bool has_google_document = false; 577 bool has_google_document = false;
553 for (size_t i = 0; i < file_paths.size(); ++i) { 578 for (size_t i = 0; i < file_paths.size(); ++i) {
554 if (google_apis::ResourceEntry::ClassifyEntryKindByFileExtension( 579 if (google_apis::ResourceEntry::ClassifyEntryKindByFileExtension(
555 file_paths[i]) & 580 file_paths[i]) &
556 google_apis::ResourceEntry::KIND_OF_GOOGLE_DOCUMENT) { 581 google_apis::ResourceEntry::KIND_OF_GOOGLE_DOCUMENT) {
557 has_google_document = true; 582 has_google_document = true;
558 break; 583 break;
559 } 584 }
560 } 585 }
(...skipping 22 matching lines...) Expand all
583 // be used in the same manifest.json. 608 // be used in the same manifest.json.
584 FindFileBrowserHandlerTasks(profile, 609 FindFileBrowserHandlerTasks(profile,
585 file_urls, 610 file_urls,
586 file_paths, 611 file_paths,
587 result_list, 612 result_list,
588 &default_already_set); 613 &default_already_set);
589 } 614 }
590 615
591 } // namespace file_tasks 616 } // namespace file_tasks
592 } // namespace file_manager 617 } // namespace file_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698