Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Implements the Chrome Extensions Media Galleries API. | 5 // Implements the Chrome Extensions Media Galleries API. |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/media_galleries/media_galleries_api.h" | 7 #include "chrome/browser/extensions/api/media_galleries/media_galleries_api.h" |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 const char kDisallowedByPolicy[] = | 59 const char kDisallowedByPolicy[] = |
| 60 "Media Galleries API is disallowed by policy: "; | 60 "Media Galleries API is disallowed by policy: "; |
| 61 | 61 |
| 62 const char kDeviceIdKey[] = "deviceId"; | 62 const char kDeviceIdKey[] = "deviceId"; |
| 63 const char kGalleryIdKey[] = "galleryId"; | 63 const char kGalleryIdKey[] = "galleryId"; |
| 64 const char kIsAvailableKey[] = "isAvailable"; | 64 const char kIsAvailableKey[] = "isAvailable"; |
| 65 const char kIsMediaDeviceKey[] = "isMediaDevice"; | 65 const char kIsMediaDeviceKey[] = "isMediaDevice"; |
| 66 const char kIsRemovableKey[] = "isRemovable"; | 66 const char kIsRemovableKey[] = "isRemovable"; |
| 67 const char kNameKey[] = "name"; | 67 const char kNameKey[] = "name"; |
| 68 | 68 |
| 69 MediaFileSystemRegistry* media_file_system_registry() { | |
| 70 return g_browser_process->media_file_system_registry(); | |
| 71 } | |
| 72 | |
| 69 // Checks whether the MediaGalleries API is currently accessible (it may be | 73 // Checks whether the MediaGalleries API is currently accessible (it may be |
| 70 // disallowed even if an extension has the requisite permission). | 74 // disallowed even if an extension has the requisite permission). Then |
| 71 bool ApiIsAccessible(std::string* error) { | 75 // initializes the MediaGalleriesPreferences |
| 76 bool SetupCall(Profile* profile, std::string* error, base::Closure callback) { | |
|
vandebo (ex-Chrome)
2014/01/14 17:03:39
nit: Initialize or Setup
tommycli
2014/01/14 17:42:41
Done.
| |
| 72 if (!ChromeSelectFilePolicy::FileSelectDialogsAllowed()) { | 77 if (!ChromeSelectFilePolicy::FileSelectDialogsAllowed()) { |
| 73 *error = std::string(kDisallowedByPolicy) + | 78 *error = std::string(kDisallowedByPolicy) + |
| 74 prefs::kAllowFileSelectionDialogs; | 79 prefs::kAllowFileSelectionDialogs; |
| 75 return false; | 80 return false; |
| 76 } | 81 } |
| 77 | 82 |
| 83 MediaGalleriesPreferences* preferences = | |
| 84 media_file_system_registry()->GetPreferences(profile); | |
| 85 preferences->EnsureInitialized(callback); | |
| 78 return true; | 86 return true; |
| 79 } | 87 } |
| 80 | 88 |
| 81 MediaFileSystemRegistry* media_file_system_registry() { | |
| 82 return g_browser_process->media_file_system_registry(); | |
| 83 } | |
| 84 | |
| 85 WebContents* GetWebContents(content::RenderViewHost* rvh, | 89 WebContents* GetWebContents(content::RenderViewHost* rvh, |
| 86 Profile* profile, | 90 Profile* profile, |
| 87 const std::string& app_id) { | 91 const std::string& app_id) { |
| 88 WebContents* contents = WebContents::FromRenderViewHost(rvh); | 92 WebContents* contents = WebContents::FromRenderViewHost(rvh); |
| 89 WebContentsModalDialogManager* web_contents_modal_dialog_manager = | 93 WebContentsModalDialogManager* web_contents_modal_dialog_manager = |
| 90 WebContentsModalDialogManager::FromWebContents(contents); | 94 WebContentsModalDialogManager::FromWebContents(contents); |
| 91 if (!web_contents_modal_dialog_manager) { | 95 if (!web_contents_modal_dialog_manager) { |
| 92 // If there is no WebContentsModalDialogManager, then this contents is | 96 // If there is no WebContentsModalDialogManager, then this contents is |
| 93 // probably the background page for an app. Try to find a shell window to | 97 // probably the background page for an app. Try to find a shell window to |
| 94 // host the dialog. | 98 // host the dialog. |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 | 225 |
| 222 DISALLOW_COPY_AND_ASSIGN(SelectDirectoryDialog); | 226 DISALLOW_COPY_AND_ASSIGN(SelectDirectoryDialog); |
| 223 }; | 227 }; |
| 224 | 228 |
| 225 } // namespace | 229 } // namespace |
| 226 | 230 |
| 227 MediaGalleriesGetMediaFileSystemsFunction:: | 231 MediaGalleriesGetMediaFileSystemsFunction:: |
| 228 ~MediaGalleriesGetMediaFileSystemsFunction() {} | 232 ~MediaGalleriesGetMediaFileSystemsFunction() {} |
| 229 | 233 |
| 230 bool MediaGalleriesGetMediaFileSystemsFunction::RunImpl() { | 234 bool MediaGalleriesGetMediaFileSystemsFunction::RunImpl() { |
| 231 if (!ApiIsAccessible(&error_)) | 235 return SetupCall(GetProfile(), &error_, base::Bind( |
| 232 return false; | 236 &MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit, this)); |
| 237 } | |
| 233 | 238 |
| 239 void MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit() { | |
| 234 media_galleries::UsageCount(media_galleries::GET_MEDIA_FILE_SYSTEMS); | 240 media_galleries::UsageCount(media_galleries::GET_MEDIA_FILE_SYSTEMS); |
| 235 scoped_ptr<GetMediaFileSystems::Params> params( | 241 scoped_ptr<GetMediaFileSystems::Params> params( |
| 236 GetMediaFileSystems::Params::Create(*args_)); | 242 GetMediaFileSystems::Params::Create(*args_)); |
| 237 EXTENSION_FUNCTION_VALIDATE(params.get()); | 243 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 238 MediaGalleries::GetMediaFileSystemsInteractivity interactive = | 244 MediaGalleries::GetMediaFileSystemsInteractivity interactive = |
|
vandebo (ex-Chrome)
2014/01/14 17:03:39
Parameter validation should happen before RunImpl
tommycli
2014/01/14 17:42:41
Done. By the way, since the parameter checks now o
vandebo (ex-Chrome)
2014/01/14 17:52:25
That seems ok since none of the parameters checks
| |
| 239 MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NO; | 245 MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NO; |
| 240 if (params->details.get() && params->details->interactive != MediaGalleries:: | 246 if (params->details.get() && params->details->interactive != MediaGalleries:: |
| 241 GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NONE) { | 247 GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NONE) { |
| 242 interactive = params->details->interactive; | 248 interactive = params->details->interactive; |
| 243 } | 249 } |
| 244 | 250 |
| 245 MediaGalleriesPreferences* preferences = | |
| 246 media_file_system_registry()->GetPreferences(GetProfile()); | |
| 247 preferences->EnsureInitialized(base::Bind( | |
| 248 &MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit, | |
| 249 this, | |
| 250 interactive)); | |
| 251 return true; | |
| 252 } | |
| 253 | |
| 254 void MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit( | |
| 255 MediaGalleries::GetMediaFileSystemsInteractivity interactive) { | |
| 256 switch (interactive) { | 251 switch (interactive) { |
| 257 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_YES: { | 252 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_YES: { |
| 258 // The MediaFileSystemRegistry only updates preferences for extensions | 253 // The MediaFileSystemRegistry only updates preferences for extensions |
| 259 // that it knows are in use. Since this may be the first call to | 254 // that it knows are in use. Since this may be the first call to |
| 260 // chrome.getMediaFileSystems for this extension, call | 255 // chrome.getMediaFileSystems for this extension, call |
| 261 // GetMediaFileSystemsForExtension() here solely so that | 256 // GetMediaFileSystemsForExtension() here solely so that |
| 262 // MediaFileSystemRegistry will send preference changes. | 257 // MediaFileSystemRegistry will send preference changes. |
| 263 GetMediaFileSystemsForExtension(base::Bind( | 258 GetMediaFileSystemsForExtension(base::Bind( |
| 264 &MediaGalleriesGetMediaFileSystemsFunction::AlwaysShowDialog, this)); | 259 &MediaGalleriesGetMediaFileSystemsFunction::AlwaysShowDialog, this)); |
| 265 return; | 260 return; |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 MediaFileSystemRegistry* registry = media_file_system_registry(); | 327 MediaFileSystemRegistry* registry = media_file_system_registry(); |
| 333 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); | 328 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); |
| 334 registry->GetMediaFileSystemsForExtension( | 329 registry->GetMediaFileSystemsForExtension( |
| 335 render_view_host(), GetExtension(), cb); | 330 render_view_host(), GetExtension(), cb); |
| 336 } | 331 } |
| 337 | 332 |
| 338 MediaGalleriesGetAllMediaFileSystemMetadataFunction:: | 333 MediaGalleriesGetAllMediaFileSystemMetadataFunction:: |
| 339 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() {} | 334 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() {} |
| 340 | 335 |
| 341 bool MediaGalleriesGetAllMediaFileSystemMetadataFunction::RunImpl() { | 336 bool MediaGalleriesGetAllMediaFileSystemMetadataFunction::RunImpl() { |
| 342 if (!ApiIsAccessible(&error_)) | 337 return SetupCall(GetProfile(), &error_, base::Bind( |
| 343 return false; | |
| 344 | |
| 345 media_galleries::UsageCount( | |
| 346 media_galleries::GET_ALL_MEDIA_FILE_SYSTEM_METADATA); | |
| 347 MediaGalleriesPreferences* preferences = | |
| 348 media_file_system_registry()->GetPreferences(GetProfile()); | |
| 349 preferences->EnsureInitialized(base::Bind( | |
| 350 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit, | 338 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit, |
| 351 this)); | 339 this)); |
| 352 return true; | |
| 353 } | 340 } |
| 354 | 341 |
| 355 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit() { | 342 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit() { |
| 343 media_galleries::UsageCount( | |
| 344 media_galleries::GET_ALL_MEDIA_FILE_SYSTEM_METADATA); | |
| 345 | |
| 356 MediaFileSystemRegistry* registry = media_file_system_registry(); | 346 MediaFileSystemRegistry* registry = media_file_system_registry(); |
| 357 MediaGalleriesPreferences* prefs = registry->GetPreferences(GetProfile()); | 347 MediaGalleriesPreferences* prefs = registry->GetPreferences(GetProfile()); |
| 358 DCHECK(prefs->IsInitialized()); | 348 DCHECK(prefs->IsInitialized()); |
| 359 MediaGalleryPrefIdSet permitted_gallery_ids = | 349 MediaGalleryPrefIdSet permitted_gallery_ids = |
| 360 prefs->GalleriesForExtension(*GetExtension()); | 350 prefs->GalleriesForExtension(*GetExtension()); |
| 361 | 351 |
| 362 MediaStorageUtil::DeviceIdSet* device_ids = new MediaStorageUtil::DeviceIdSet; | 352 MediaStorageUtil::DeviceIdSet* device_ids = new MediaStorageUtil::DeviceIdSet; |
| 363 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); | 353 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); |
| 364 for (MediaGalleryPrefIdSet::const_iterator it = permitted_gallery_ids.begin(); | 354 for (MediaGalleryPrefIdSet::const_iterator it = permitted_gallery_ids.begin(); |
| 365 it != permitted_gallery_ids.end(); ++it) { | 355 it != permitted_gallery_ids.end(); ++it) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 400 } | 390 } |
| 401 | 391 |
| 402 SetResult(list); | 392 SetResult(list); |
| 403 SendResponse(true); | 393 SendResponse(true); |
| 404 } | 394 } |
| 405 | 395 |
| 406 MediaGalleriesAddUserSelectedFolderFunction:: | 396 MediaGalleriesAddUserSelectedFolderFunction:: |
| 407 ~MediaGalleriesAddUserSelectedFolderFunction() {} | 397 ~MediaGalleriesAddUserSelectedFolderFunction() {} |
| 408 | 398 |
| 409 bool MediaGalleriesAddUserSelectedFolderFunction::RunImpl() { | 399 bool MediaGalleriesAddUserSelectedFolderFunction::RunImpl() { |
| 410 if (!ApiIsAccessible(&error_)) | 400 return SetupCall(GetProfile(), &error_, base::Bind( |
| 411 return false; | 401 &MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit, this)); |
| 412 | |
| 413 media_galleries::UsageCount(media_galleries::ADD_USER_SELECTED_FOLDER); | |
| 414 MediaGalleriesPreferences* preferences = | |
| 415 media_file_system_registry()->GetPreferences(GetProfile()); | |
| 416 preferences->EnsureInitialized(base::Bind( | |
| 417 &MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit, | |
| 418 this)); | |
| 419 return true; | |
| 420 } | 402 } |
| 421 | 403 |
| 422 void MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit() { | 404 void MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit() { |
| 405 media_galleries::UsageCount(media_galleries::ADD_USER_SELECTED_FOLDER); | |
| 406 | |
| 423 if (!user_gesture()) { | 407 if (!user_gesture()) { |
| 424 OnDirectorySelected(base::FilePath()); | 408 OnDirectorySelected(base::FilePath()); |
| 425 return; | 409 return; |
| 426 } | 410 } |
| 427 | 411 |
| 428 Profile* profile = GetProfile(); | 412 Profile* profile = GetProfile(); |
| 429 const std::string& app_id = GetExtension()->id(); | 413 const std::string& app_id = GetExtension()->id(); |
| 430 WebContents* contents = GetWebContents(render_view_host(), profile, app_id); | 414 WebContents* contents = GetWebContents(render_view_host(), profile, app_id); |
| 431 base::FilePath last_used_path = | 415 base::FilePath last_used_path = |
| 432 extensions::file_system_api::GetLastChooseEntryDirectory( | 416 extensions::file_system_api::GetLastChooseEntryDirectory( |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 501 } | 485 } |
| 502 MediaFileSystemRegistry* registry = media_file_system_registry(); | 486 MediaFileSystemRegistry* registry = media_file_system_registry(); |
| 503 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); | 487 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); |
| 504 registry->GetMediaFileSystemsForExtension( | 488 registry->GetMediaFileSystemsForExtension( |
| 505 render_view_host(), GetExtension(), cb); | 489 render_view_host(), GetExtension(), cb); |
| 506 } | 490 } |
| 507 | 491 |
| 508 MediaGalleriesGetMetadataFunction::~MediaGalleriesGetMetadataFunction() {} | 492 MediaGalleriesGetMetadataFunction::~MediaGalleriesGetMetadataFunction() {} |
| 509 | 493 |
| 510 bool MediaGalleriesGetMetadataFunction::RunImpl() { | 494 bool MediaGalleriesGetMetadataFunction::RunImpl() { |
| 511 if (!ApiIsAccessible(&error_)) | 495 return SetupCall(GetProfile(), &error_, base::Bind( |
| 512 return false; | 496 &MediaGalleriesGetMetadataFunction::OnPreferencesInit, this)); |
| 497 } | |
| 498 | |
| 499 void MediaGalleriesGetMetadataFunction::OnPreferencesInit() { | |
| 500 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 513 | 501 |
| 514 std::string blob_uuid; | 502 std::string blob_uuid; |
| 515 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &blob_uuid)); | 503 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &blob_uuid)); |
| 516 | 504 |
| 517 const base::Value* options_value = NULL; | 505 const base::Value* options_value = NULL; |
| 518 if (!args_->Get(1, &options_value)) | 506 if (!args_->Get(1, &options_value)) { |
|
vandebo (ex-Chrome)
2014/01/14 17:03:39
And here
tommycli
2014/01/14 17:42:41
Done.
| |
| 519 return false; | 507 SendResponse(false); |
| 508 return; | |
| 509 } | |
| 520 scoped_ptr<MediaGalleries::MediaMetadataOptions> options = | 510 scoped_ptr<MediaGalleries::MediaMetadataOptions> options = |
| 521 MediaGalleries::MediaMetadataOptions::FromValue(*options_value); | 511 MediaGalleries::MediaMetadataOptions::FromValue(*options_value); |
| 522 if (!options) | 512 if (!options) { |
| 523 return false; | 513 SendResponse(false); |
| 514 return; | |
| 515 } | |
| 524 | 516 |
| 525 MediaGalleriesPreferences* preferences = | |
| 526 media_file_system_registry()->GetPreferences(GetProfile()); | |
| 527 bool mime_type_only = options->metadata_type == | 517 bool mime_type_only = options->metadata_type == |
| 528 MediaGalleries::GET_METADATA_TYPE_MIMETYPEONLY; | 518 MediaGalleries::GET_METADATA_TYPE_MIMETYPEONLY; |
| 529 preferences->EnsureInitialized(base::Bind( | |
| 530 &MediaGalleriesGetMetadataFunction::OnPreferencesInit, | |
| 531 this, mime_type_only, blob_uuid)); | |
| 532 | |
| 533 return true; | |
| 534 } | |
| 535 | |
| 536 void MediaGalleriesGetMetadataFunction::OnPreferencesInit( | |
| 537 bool mime_type_only, | |
| 538 const std::string& blob_uuid) { | |
| 539 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 540 | 519 |
| 541 // BlobReader is self-deleting. | 520 // BlobReader is self-deleting. |
| 542 BlobReader* reader = new BlobReader( | 521 BlobReader* reader = new BlobReader( |
| 543 GetProfile(), | 522 GetProfile(), |
| 544 blob_uuid, | 523 blob_uuid, |
| 545 base::Bind(&MediaGalleriesGetMetadataFunction::SniffMimeType, this, | 524 base::Bind(&MediaGalleriesGetMetadataFunction::SniffMimeType, this, |
| 546 mime_type_only)); | 525 mime_type_only)); |
| 547 reader->SetByteRange(0, net::kMaxBytesToSniff); | 526 reader->SetByteRange(0, net::kMaxBytesToSniff); |
| 548 reader->Start(); | 527 reader->Start(); |
| 549 } | 528 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 560 if (mime_type_sniffed) | 539 if (mime_type_sniffed) |
| 561 metadata.mime_type = mime_type; | 540 metadata.mime_type = mime_type; |
| 562 | 541 |
| 563 // TODO(tommycli): Kick off SafeMediaMetadataParser if |mime_type_only| false. | 542 // TODO(tommycli): Kick off SafeMediaMetadataParser if |mime_type_only| false. |
| 564 | 543 |
| 565 SetResult(metadata.ToValue().release()); | 544 SetResult(metadata.ToValue().release()); |
| 566 SendResponse(true); | 545 SendResponse(true); |
| 567 } | 546 } |
| 568 | 547 |
| 569 } // namespace extensions | 548 } // namespace extensions |
| OLD | NEW |