| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/private_api_tasks.h" | 5 #include "chrome/browser/chromeos/extensions/file_manager/private_api_tasks.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chromeos/drive/drive_app_registry.h" | |
| 8 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | |
| 9 #include "chrome/browser/chromeos/extensions/file_manager/file_browser_handlers.
h" | |
| 10 #include "chrome/browser/chromeos/extensions/file_manager/file_tasks.h" | 7 #include "chrome/browser/chromeos/extensions/file_manager/file_tasks.h" |
| 11 #include "chrome/browser/chromeos/extensions/file_manager/fileapi_util.h" | 8 #include "chrome/browser/chromeos/extensions/file_manager/fileapi_util.h" |
| 12 #include "chrome/browser/chromeos/extensions/file_manager/mime_util.h" | 9 #include "chrome/browser/chromeos/extensions/file_manager/mime_util.h" |
| 13 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" | 10 #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" |
| 14 #include "chrome/browser/chromeos/fileapi/file_system_backend.h" | 11 #include "chrome/browser/chromeos/fileapi/file_system_backend.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | |
| 16 #include "chrome/browser/extensions/extension_system.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser_finder.h" | |
| 19 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | |
| 20 #include "chrome/common/extensions/api/file_browser_handlers/file_browser_handle
r.h" | |
| 21 #include "content/public/browser/browser_context.h" | |
| 22 #include "content/public/browser/render_view_host.h" | 13 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/storage_partition.h" | |
| 24 #include "webkit/browser/fileapi/file_system_context.h" | 14 #include "webkit/browser/fileapi/file_system_context.h" |
| 25 #include "webkit/browser/fileapi/file_system_url.h" | 15 #include "webkit/browser/fileapi/file_system_url.h" |
| 26 | 16 |
| 27 using content::BrowserContext; | 17 using extensions::app_file_handler_util::PathAndMimeTypeSet; |
| 28 using extensions::app_file_handler_util::FindFileHandlersForFiles; | |
| 29 using extensions::Extension; | 18 using extensions::Extension; |
| 30 using fileapi::FileSystemURL; | 19 using fileapi::FileSystemURL; |
| 31 | 20 |
| 32 namespace file_manager { | 21 namespace file_manager { |
| 33 namespace { | 22 namespace { |
| 34 | 23 |
| 35 // Error messages. | 24 // Error messages. |
| 36 const char kInvalidFileUrl[] = "Invalid file URL"; | 25 const char kInvalidFileUrl[] = "Invalid file URL"; |
| 37 | 26 |
| 38 // Default icon path for drive docs. | |
| 39 const char kDefaultIcon[] = "images/filetype_generic.png"; | |
| 40 | |
| 41 // Make a set of unique filename suffixes out of the list of file URLs. | 27 // Make a set of unique filename suffixes out of the list of file URLs. |
| 42 std::set<std::string> GetUniqueSuffixes(base::ListValue* file_url_list, | 28 std::set<std::string> GetUniqueSuffixes(base::ListValue* file_url_list, |
| 43 fileapi::FileSystemContext* context) { | 29 fileapi::FileSystemContext* context) { |
| 44 std::set<std::string> suffixes; | 30 std::set<std::string> suffixes; |
| 45 for (size_t i = 0; i < file_url_list->GetSize(); ++i) { | 31 for (size_t i = 0; i < file_url_list->GetSize(); ++i) { |
| 46 std::string url_str; | 32 std::string url_str; |
| 47 if (!file_url_list->GetString(i, &url_str)) | 33 if (!file_url_list->GetString(i, &url_str)) |
| 48 return std::set<std::string>(); | 34 return std::set<std::string>(); |
| 49 FileSystemURL url = context->CrackURL(GURL(url_str)); | 35 FileSystemURL url = context->CrackURL(GURL(url_str)); |
| 50 if (!url.is_valid() || url.path().empty()) | 36 if (!url.is_valid() || url.path().empty()) |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 task, | 116 task, |
| 131 file_urls, | 117 file_urls, |
| 132 base::Bind(&ExecuteTaskFunction::OnTaskExecuted, this)); | 118 base::Bind(&ExecuteTaskFunction::OnTaskExecuted, this)); |
| 133 } | 119 } |
| 134 | 120 |
| 135 void ExecuteTaskFunction::OnTaskExecuted(bool success) { | 121 void ExecuteTaskFunction::OnTaskExecuted(bool success) { |
| 136 SetResult(new base::FundamentalValue(success)); | 122 SetResult(new base::FundamentalValue(success)); |
| 137 SendResponse(true); | 123 SendResponse(true); |
| 138 } | 124 } |
| 139 | 125 |
| 140 struct GetFileTasksFunction::TaskInfo { | |
| 141 TaskInfo(const std::string& app_name, const GURL& icon_url) | |
| 142 : app_name(app_name), icon_url(icon_url) { | |
| 143 } | |
| 144 | |
| 145 std::string app_name; | |
| 146 GURL icon_url; | |
| 147 }; | |
| 148 | |
| 149 GetFileTasksFunction::GetFileTasksFunction() { | 126 GetFileTasksFunction::GetFileTasksFunction() { |
| 150 } | 127 } |
| 151 | 128 |
| 152 GetFileTasksFunction::~GetFileTasksFunction() { | 129 GetFileTasksFunction::~GetFileTasksFunction() { |
| 153 } | 130 } |
| 154 | 131 |
| 155 // static | |
| 156 void GetFileTasksFunction::GetAvailableDriveTasks( | |
| 157 const drive::DriveAppRegistry& drive_app_registry, | |
| 158 const PathAndMimeTypeSet& path_mime_set, | |
| 159 TaskInfoMap* task_info_map) { | |
| 160 DCHECK(task_info_map); | |
| 161 DCHECK(task_info_map->empty()); | |
| 162 | |
| 163 bool is_first = true; | |
| 164 for (PathAndMimeTypeSet::const_iterator it = path_mime_set.begin(); | |
| 165 it != path_mime_set.end(); ++it) { | |
| 166 const base::FilePath& file_path = it->first; | |
| 167 const std::string& mime_type = it->second; | |
| 168 if (file_path.empty()) | |
| 169 continue; | |
| 170 | |
| 171 ScopedVector<drive::DriveAppInfo> app_info_list; | |
| 172 drive_app_registry.GetAppsForFile(file_path, mime_type, &app_info_list); | |
| 173 | |
| 174 if (is_first) { | |
| 175 // For the first file, we store all the info. | |
| 176 for (size_t j = 0; j < app_info_list.size(); ++j) { | |
| 177 const drive::DriveAppInfo& app_info = *app_info_list[j]; | |
| 178 GURL icon_url = drive::util::FindPreferredIcon( | |
| 179 app_info.app_icons, | |
| 180 drive::util::kPreferredIconSize); | |
| 181 task_info_map->insert(std::pair<std::string, TaskInfo>( | |
| 182 file_tasks::MakeDriveAppTaskId(app_info.app_id), | |
| 183 TaskInfo(app_info.app_name, icon_url))); | |
| 184 } | |
| 185 } else { | |
| 186 // For remaining files, take the intersection with the current result, | |
| 187 // based on the task id. | |
| 188 std::set<std::string> task_id_set; | |
| 189 for (size_t j = 0; j < app_info_list.size(); ++j) { | |
| 190 task_id_set.insert( | |
| 191 file_tasks::MakeDriveAppTaskId(app_info_list[j]->app_id)); | |
| 192 } | |
| 193 for (TaskInfoMap::iterator iter = task_info_map->begin(); | |
| 194 iter != task_info_map->end(); ) { | |
| 195 if (task_id_set.find(iter->first) == task_id_set.end()) { | |
| 196 task_info_map->erase(iter++); | |
| 197 } else { | |
| 198 ++iter; | |
| 199 } | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 is_first = false; | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 // static | |
| 208 void GetFileTasksFunction::FindDefaultDriveTasks( | |
| 209 const PrefService& pref_service, | |
| 210 const PathAndMimeTypeSet& path_mime_set, | |
| 211 const TaskInfoMap& task_info_map, | |
| 212 std::set<std::string>* default_tasks) { | |
| 213 DCHECK(default_tasks); | |
| 214 | |
| 215 for (PathAndMimeTypeSet::const_iterator it = path_mime_set.begin(); | |
| 216 it != path_mime_set.end(); ++it) { | |
| 217 const base::FilePath& file_path = it->first; | |
| 218 const std::string& mime_type = it->second; | |
| 219 std::string task_id = file_tasks::GetDefaultTaskIdFromPrefs( | |
| 220 pref_service, mime_type, file_path.Extension()); | |
| 221 if (task_info_map.find(task_id) != task_info_map.end()) | |
| 222 default_tasks->insert(task_id); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 // static | |
| 227 void GetFileTasksFunction::CreateDriveTasks( | |
| 228 const TaskInfoMap& task_info_map, | |
| 229 const std::set<std::string>& default_tasks, | |
| 230 ListValue* result_list, | |
| 231 bool* default_already_set) { | |
| 232 DCHECK(result_list); | |
| 233 DCHECK(default_already_set); | |
| 234 | |
| 235 for (TaskInfoMap::const_iterator iter = task_info_map.begin(); | |
| 236 iter != task_info_map.end(); ++iter) { | |
| 237 DictionaryValue* task = new DictionaryValue; | |
| 238 task->SetString("taskId", iter->first); | |
| 239 task->SetString("title", iter->second.app_name); | |
| 240 | |
| 241 const GURL& icon_url = iter->second.icon_url; | |
| 242 if (!icon_url.is_empty()) | |
| 243 task->SetString("iconUrl", icon_url.spec()); | |
| 244 | |
| 245 task->SetBoolean("driveApp", true); | |
| 246 | |
| 247 // Once we set a default app, we don't want to set any more. | |
| 248 if (!(*default_already_set) && | |
| 249 default_tasks.find(iter->first) != default_tasks.end()) { | |
| 250 task->SetBoolean("isDefault", true); | |
| 251 *default_already_set = true; | |
| 252 } else { | |
| 253 task->SetBoolean("isDefault", false); | |
| 254 } | |
| 255 result_list->Append(task); | |
| 256 } | |
| 257 } | |
| 258 | |
| 259 // static | |
| 260 void GetFileTasksFunction::FindDriveAppTasks( | |
| 261 Profile* profile, | |
| 262 const PathAndMimeTypeSet& path_mime_set, | |
| 263 ListValue* result_list, | |
| 264 bool* default_already_set) { | |
| 265 DCHECK(!path_mime_set.empty()); | |
| 266 DCHECK(result_list); | |
| 267 DCHECK(default_already_set); | |
| 268 | |
| 269 drive::DriveIntegrationService* integration_service = | |
| 270 drive::DriveIntegrationServiceFactory::GetForProfile(profile); | |
| 271 // |integration_service| is NULL if Drive is disabled. | |
| 272 if (!integration_service || !integration_service->drive_app_registry()) | |
| 273 return; | |
| 274 | |
| 275 // Map of task_id to TaskInfo of available tasks. | |
| 276 TaskInfoMap task_info_map; | |
| 277 GetAvailableDriveTasks(*integration_service->drive_app_registry(), | |
| 278 path_mime_set, | |
| 279 &task_info_map); | |
| 280 | |
| 281 std::set<std::string> default_tasks; | |
| 282 FindDefaultDriveTasks(*profile->GetPrefs(), | |
| 283 path_mime_set, | |
| 284 task_info_map, | |
| 285 &default_tasks); | |
| 286 CreateDriveTasks( | |
| 287 task_info_map, default_tasks, result_list, default_already_set); | |
| 288 } | |
| 289 | |
| 290 void GetFileTasksFunction::FindFileHandlerTasks( | |
| 291 Profile* profile, | |
| 292 const PathAndMimeTypeSet& path_mime_set, | |
| 293 ListValue* result_list, | |
| 294 bool* default_already_set) { | |
| 295 DCHECK(!path_mime_set.empty()); | |
| 296 DCHECK(result_list); | |
| 297 DCHECK(default_already_set); | |
| 298 | |
| 299 ExtensionService* service = profile->GetExtensionService(); | |
| 300 if (!service) | |
| 301 return; | |
| 302 | |
| 303 std::set<std::string> default_tasks; | |
| 304 for (PathAndMimeTypeSet::iterator it = path_mime_set.begin(); | |
| 305 it != path_mime_set.end(); ++it) { | |
| 306 default_tasks.insert(file_tasks::GetDefaultTaskIdFromPrefs( | |
| 307 *profile->GetPrefs(), it->second, it->first.Extension())); | |
| 308 } | |
| 309 | |
| 310 for (ExtensionSet::const_iterator iter = service->extensions()->begin(); | |
| 311 iter != service->extensions()->end(); | |
| 312 ++iter) { | |
| 313 const Extension* extension = iter->get(); | |
| 314 | |
| 315 // We don't support using hosted apps to open files. | |
| 316 if (!extension->is_platform_app()) | |
| 317 continue; | |
| 318 | |
| 319 if (profile->IsOffTheRecord() && | |
| 320 !service->IsIncognitoEnabled(extension->id())) | |
| 321 continue; | |
| 322 | |
| 323 typedef std::vector<const extensions::FileHandlerInfo*> FileHandlerList; | |
| 324 FileHandlerList file_handlers = | |
| 325 FindFileHandlersForFiles(*extension, path_mime_set); | |
| 326 if (file_handlers.empty()) | |
| 327 continue; | |
| 328 | |
| 329 for (FileHandlerList::iterator i = file_handlers.begin(); | |
| 330 i != file_handlers.end(); ++i) { | |
| 331 DictionaryValue* task = new DictionaryValue; | |
| 332 std::string task_id = file_tasks::MakeTaskID( | |
| 333 extension->id(), file_tasks::TASK_TYPE_FILE_HANDLER, (*i)->id); | |
| 334 task->SetString("taskId", task_id); | |
| 335 task->SetString("title", (*i)->title); | |
| 336 if (!(*default_already_set) && ContainsKey(default_tasks, task_id)) { | |
| 337 task->SetBoolean("isDefault", true); | |
| 338 *default_already_set = true; | |
| 339 } else { | |
| 340 task->SetBoolean("isDefault", false); | |
| 341 } | |
| 342 | |
| 343 GURL best_icon = extensions::ExtensionIconSource::GetIconURL( | |
| 344 extension, | |
| 345 drive::util::kPreferredIconSize, | |
| 346 ExtensionIconSet::MATCH_BIGGER, | |
| 347 false, // grayscale | |
| 348 NULL); // exists | |
| 349 if (!best_icon.is_empty()) | |
| 350 task->SetString("iconUrl", best_icon.spec()); | |
| 351 else | |
| 352 task->SetString("iconUrl", kDefaultIcon); | |
| 353 | |
| 354 task->SetBoolean("driveApp", false); | |
| 355 result_list->Append(task); | |
| 356 } | |
| 357 } | |
| 358 } | |
| 359 | |
| 360 void GetFileTasksFunction::FindFileBrowserHandlerTasks( | |
| 361 Profile* profile, | |
| 362 const std::vector<GURL>& file_urls, | |
| 363 const std::vector<base::FilePath>& file_paths, | |
| 364 ListValue* result_list, | |
| 365 bool* default_already_set) { | |
| 366 DCHECK(!file_paths.empty()); | |
| 367 DCHECK(!file_urls.empty()); | |
| 368 DCHECK(result_list); | |
| 369 DCHECK(default_already_set); | |
| 370 | |
| 371 file_browser_handlers::FileBrowserHandlerList common_tasks = | |
| 372 file_browser_handlers::FindCommonFileBrowserHandlers(profile, file_urls); | |
| 373 if (common_tasks.empty()) | |
| 374 return; | |
| 375 file_browser_handlers::FileBrowserHandlerList default_tasks = | |
| 376 file_browser_handlers::FindDefaultFileBrowserHandlers( | |
| 377 *profile->GetPrefs(), file_paths, common_tasks); | |
| 378 | |
| 379 ExtensionService* service = | |
| 380 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
| 381 for (file_browser_handlers::FileBrowserHandlerList::const_iterator iter = | |
| 382 common_tasks.begin(); | |
| 383 iter != common_tasks.end(); | |
| 384 ++iter) { | |
| 385 const FileBrowserHandler* handler = *iter; | |
| 386 const std::string extension_id = handler->extension_id(); | |
| 387 const Extension* extension = service->GetExtensionById(extension_id, false); | |
| 388 CHECK(extension); | |
| 389 DictionaryValue* task = new DictionaryValue; | |
| 390 task->SetString("taskId", file_tasks::MakeTaskID( | |
| 391 extension_id, | |
| 392 file_tasks::TASK_TYPE_FILE_BROWSER_HANDLER, | |
| 393 handler->id())); | |
| 394 task->SetString("title", handler->title()); | |
| 395 // TODO(zelidrag): Figure out how to expose icon URL that task defined in | |
| 396 // manifest instead of the default extension icon. | |
| 397 GURL icon = extensions::ExtensionIconSource::GetIconURL( | |
| 398 extension, | |
| 399 extension_misc::EXTENSION_ICON_BITTY, | |
| 400 ExtensionIconSet::MATCH_BIGGER, | |
| 401 false, // grayscale | |
| 402 NULL); // exists | |
| 403 task->SetString("iconUrl", icon.spec()); | |
| 404 task->SetBoolean("driveApp", false); | |
| 405 | |
| 406 // Only set the default if there isn't already a default set. | |
| 407 if (!*default_already_set && | |
| 408 std::find(default_tasks.begin(), default_tasks.end(), *iter) != | |
| 409 default_tasks.end()) { | |
| 410 task->SetBoolean("isDefault", true); | |
| 411 *default_already_set = true; | |
| 412 } else { | |
| 413 task->SetBoolean("isDefault", false); | |
| 414 } | |
| 415 | |
| 416 result_list->Append(task); | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 // static | |
| 421 void GetFileTasksFunction::FindAllTypesOfTasks( | |
| 422 Profile* profile, | |
| 423 const PathAndMimeTypeSet& path_mime_set, | |
| 424 const std::vector<GURL>& file_urls, | |
| 425 const std::vector<base::FilePath>& file_paths, | |
| 426 ListValue* result_list) { | |
| 427 // Check if file_paths contain a google document. | |
| 428 bool has_google_document = false; | |
| 429 for (size_t i = 0; i < file_paths.size(); ++i) { | |
| 430 if (google_apis::ResourceEntry::ClassifyEntryKindByFileExtension( | |
| 431 file_paths[i]) & | |
| 432 google_apis::ResourceEntry::KIND_OF_GOOGLE_DOCUMENT) { | |
| 433 has_google_document = true; | |
| 434 break; | |
| 435 } | |
| 436 } | |
| 437 | |
| 438 // Find the Drive app tasks first, because we want them to take precedence | |
| 439 // when setting the default app. | |
| 440 bool default_already_set = false; | |
| 441 // Google document are not opened by drive apps but file manager. | |
| 442 if (!has_google_document) { | |
| 443 FindDriveAppTasks(profile, | |
| 444 path_mime_set, | |
| 445 result_list, | |
| 446 &default_already_set); | |
| 447 } | |
| 448 | |
| 449 // Find and append file handler tasks. We know there aren't duplicates | |
| 450 // because Drive apps and platform apps are entirely different kinds of | |
| 451 // tasks. | |
| 452 FindFileHandlerTasks(profile, | |
| 453 path_mime_set, | |
| 454 result_list, | |
| 455 &default_already_set); | |
| 456 | |
| 457 // Find and append file browser handler tasks. We know there aren't | |
| 458 // duplicates because "file_browser_handlers" and "file_handlers" shouldn't | |
| 459 // be used in the same manifest.json. | |
| 460 FindFileBrowserHandlerTasks(profile, | |
| 461 file_urls, | |
| 462 file_paths, | |
| 463 result_list, | |
| 464 &default_already_set); | |
| 465 } | |
| 466 | |
| 467 bool GetFileTasksFunction::RunImpl() { | 132 bool GetFileTasksFunction::RunImpl() { |
| 468 // First argument is the list of files to get tasks for. | 133 // First argument is the list of files to get tasks for. |
| 469 ListValue* files_list = NULL; | 134 ListValue* files_list = NULL; |
| 470 if (!args_->GetList(0, &files_list)) | 135 if (!args_->GetList(0, &files_list)) |
| 471 return false; | 136 return false; |
| 472 | 137 |
| 473 if (files_list->GetSize() == 0) | 138 if (files_list->GetSize() == 0) |
| 474 return false; | 139 return false; |
| 475 | 140 |
| 476 // Second argument is the list of mime types of each of the files in the list. | 141 // Second argument is the list of mime types of each of the files in the list. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 // If MIME type is not provided, guess it from the file path. | 180 // If MIME type is not provided, guess it from the file path. |
| 516 if (mime_type.empty()) | 181 if (mime_type.empty()) |
| 517 mime_type = util::GetMimeTypeForPath(file_path); | 182 mime_type = util::GetMimeTypeForPath(file_path); |
| 518 | 183 |
| 519 path_mime_set.insert(std::make_pair(file_path, mime_type)); | 184 path_mime_set.insert(std::make_pair(file_path, mime_type)); |
| 520 } | 185 } |
| 521 | 186 |
| 522 ListValue* result_list = new ListValue(); | 187 ListValue* result_list = new ListValue(); |
| 523 SetResult(result_list); | 188 SetResult(result_list); |
| 524 | 189 |
| 525 FindAllTypesOfTasks(profile_, | 190 file_tasks::FindAllTypesOfTasks(profile_, |
| 526 path_mime_set, | 191 path_mime_set, |
| 527 file_urls, | 192 file_urls, |
| 528 file_paths, | 193 file_paths, |
| 529 result_list); | 194 result_list); |
| 530 SendResponse(true); | 195 SendResponse(true); |
| 531 return true; | 196 return true; |
| 532 } | 197 } |
| 533 | 198 |
| 534 SetDefaultTaskFunction::SetDefaultTaskFunction() { | 199 SetDefaultTaskFunction::SetDefaultTaskFunction() { |
| 535 } | 200 } |
| 536 | 201 |
| 537 SetDefaultTaskFunction::~SetDefaultTaskFunction() { | 202 SetDefaultTaskFunction::~SetDefaultTaskFunction() { |
| 538 } | 203 } |
| 539 | 204 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 | 241 |
| 577 file_tasks::UpdateDefaultTask(profile_->GetPrefs(), | 242 file_tasks::UpdateDefaultTask(profile_->GetPrefs(), |
| 578 task_id, | 243 task_id, |
| 579 suffixes, | 244 suffixes, |
| 580 mime_types); | 245 mime_types); |
| 581 | 246 |
| 582 return true; | 247 return true; |
| 583 } | 248 } |
| 584 | 249 |
| 585 } // namespace file_manager | 250 } // namespace file_manager |
| OLD | NEW |