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

Side by Side Diff: chrome/browser/extensions/api/media_galleries/media_galleries_api.cc

Issue 137313003: Media Galleries API Extension functions: Refactor Preference initialization block. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 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 // 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
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 Setup(Profile* profile, std::string* error, base::Closure callback) {
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
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_))
232 return false;
233
234 media_galleries::UsageCount(media_galleries::GET_MEDIA_FILE_SYSTEMS); 235 media_galleries::UsageCount(media_galleries::GET_MEDIA_FILE_SYSTEMS);
235 scoped_ptr<GetMediaFileSystems::Params> params( 236 scoped_ptr<GetMediaFileSystems::Params> params(
236 GetMediaFileSystems::Params::Create(*args_)); 237 GetMediaFileSystems::Params::Create(*args_));
237 EXTENSION_FUNCTION_VALIDATE(params.get()); 238 EXTENSION_FUNCTION_VALIDATE(params.get());
238 MediaGalleries::GetMediaFileSystemsInteractivity interactive = 239 MediaGalleries::GetMediaFileSystemsInteractivity interactive =
239 MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NO; 240 MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NO;
240 if (params->details.get() && params->details->interactive != MediaGalleries:: 241 if (params->details.get() && params->details->interactive != MediaGalleries::
241 GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NONE) { 242 GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NONE) {
242 interactive = params->details->interactive; 243 interactive = params->details->interactive;
243 } 244 }
244 245
245 MediaGalleriesPreferences* preferences = 246 return Setup(GetProfile(), &error_, base::Bind(
246 media_file_system_registry()->GetPreferences(GetProfile()); 247 &MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit, this,
247 preferences->EnsureInitialized(base::Bind(
248 &MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit,
249 this,
250 interactive)); 248 interactive));
251 return true;
252 } 249 }
253 250
254 void MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit( 251 void MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit(
255 MediaGalleries::GetMediaFileSystemsInteractivity interactive) { 252 MediaGalleries::GetMediaFileSystemsInteractivity interactive) {
256 switch (interactive) { 253 switch (interactive) {
257 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_YES: { 254 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_YES: {
258 // The MediaFileSystemRegistry only updates preferences for extensions 255 // The MediaFileSystemRegistry only updates preferences for extensions
259 // that it knows are in use. Since this may be the first call to 256 // that it knows are in use. Since this may be the first call to
260 // chrome.getMediaFileSystems for this extension, call 257 // chrome.getMediaFileSystems for this extension, call
261 // GetMediaFileSystemsForExtension() here solely so that 258 // GetMediaFileSystemsForExtension() here solely so that
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 MediaFileSystemRegistry* registry = media_file_system_registry(); 329 MediaFileSystemRegistry* registry = media_file_system_registry();
333 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); 330 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized());
334 registry->GetMediaFileSystemsForExtension( 331 registry->GetMediaFileSystemsForExtension(
335 render_view_host(), GetExtension(), cb); 332 render_view_host(), GetExtension(), cb);
336 } 333 }
337 334
338 MediaGalleriesGetAllMediaFileSystemMetadataFunction:: 335 MediaGalleriesGetAllMediaFileSystemMetadataFunction::
339 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() {} 336 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() {}
340 337
341 bool MediaGalleriesGetAllMediaFileSystemMetadataFunction::RunImpl() { 338 bool MediaGalleriesGetAllMediaFileSystemMetadataFunction::RunImpl() {
342 if (!ApiIsAccessible(&error_))
343 return false;
344
345 media_galleries::UsageCount( 339 media_galleries::UsageCount(
346 media_galleries::GET_ALL_MEDIA_FILE_SYSTEM_METADATA); 340 media_galleries::GET_ALL_MEDIA_FILE_SYSTEM_METADATA);
347 MediaGalleriesPreferences* preferences = 341 return Setup(GetProfile(), &error_, base::Bind(
348 media_file_system_registry()->GetPreferences(GetProfile());
349 preferences->EnsureInitialized(base::Bind(
350 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit, 342 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit,
351 this)); 343 this));
352 return true;
353 } 344 }
354 345
355 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit() { 346 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit() {
356 MediaFileSystemRegistry* registry = media_file_system_registry(); 347 MediaFileSystemRegistry* registry = media_file_system_registry();
357 MediaGalleriesPreferences* prefs = registry->GetPreferences(GetProfile()); 348 MediaGalleriesPreferences* prefs = registry->GetPreferences(GetProfile());
358 DCHECK(prefs->IsInitialized()); 349 DCHECK(prefs->IsInitialized());
359 MediaGalleryPrefIdSet permitted_gallery_ids = 350 MediaGalleryPrefIdSet permitted_gallery_ids =
360 prefs->GalleriesForExtension(*GetExtension()); 351 prefs->GalleriesForExtension(*GetExtension());
361 352
362 MediaStorageUtil::DeviceIdSet* device_ids = new MediaStorageUtil::DeviceIdSet; 353 MediaStorageUtil::DeviceIdSet* device_ids = new MediaStorageUtil::DeviceIdSet;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 } 391 }
401 392
402 SetResult(list); 393 SetResult(list);
403 SendResponse(true); 394 SendResponse(true);
404 } 395 }
405 396
406 MediaGalleriesAddUserSelectedFolderFunction:: 397 MediaGalleriesAddUserSelectedFolderFunction::
407 ~MediaGalleriesAddUserSelectedFolderFunction() {} 398 ~MediaGalleriesAddUserSelectedFolderFunction() {}
408 399
409 bool MediaGalleriesAddUserSelectedFolderFunction::RunImpl() { 400 bool MediaGalleriesAddUserSelectedFolderFunction::RunImpl() {
410 if (!ApiIsAccessible(&error_))
411 return false;
412
413 media_galleries::UsageCount(media_galleries::ADD_USER_SELECTED_FOLDER); 401 media_galleries::UsageCount(media_galleries::ADD_USER_SELECTED_FOLDER);
414 MediaGalleriesPreferences* preferences = 402 return Setup(GetProfile(), &error_, base::Bind(
415 media_file_system_registry()->GetPreferences(GetProfile()); 403 &MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit, this));
416 preferences->EnsureInitialized(base::Bind(
417 &MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit,
418 this));
419 return true;
420 } 404 }
421 405
422 void MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit() { 406 void MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit() {
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();
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
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_))
512 return false;
513
514 std::string blob_uuid; 495 std::string blob_uuid;
515 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &blob_uuid)); 496 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &blob_uuid));
516 497
517 const base::Value* options_value = NULL; 498 const base::Value* options_value = NULL;
518 if (!args_->Get(1, &options_value)) 499 if (!args_->Get(1, &options_value))
519 return false; 500 return false;
520 scoped_ptr<MediaGalleries::MediaMetadataOptions> options = 501 scoped_ptr<MediaGalleries::MediaMetadataOptions> options =
521 MediaGalleries::MediaMetadataOptions::FromValue(*options_value); 502 MediaGalleries::MediaMetadataOptions::FromValue(*options_value);
522 if (!options) 503 if (!options)
523 return false; 504 return false;
524 505
525 MediaGalleriesPreferences* preferences =
526 media_file_system_registry()->GetPreferences(GetProfile());
527 bool mime_type_only = options->metadata_type == 506 bool mime_type_only = options->metadata_type ==
528 MediaGalleries::GET_METADATA_TYPE_MIMETYPEONLY; 507 MediaGalleries::GET_METADATA_TYPE_MIMETYPEONLY;
529 preferences->EnsureInitialized(base::Bind(
530 &MediaGalleriesGetMetadataFunction::OnPreferencesInit,
531 this, mime_type_only, blob_uuid));
532 508
533 return true; 509 return Setup(GetProfile(), &error_, base::Bind(
510 &MediaGalleriesGetMetadataFunction::OnPreferencesInit, this,
511 mime_type_only, blob_uuid));
534 } 512 }
535 513
536 void MediaGalleriesGetMetadataFunction::OnPreferencesInit( 514 void MediaGalleriesGetMetadataFunction::OnPreferencesInit(
537 bool mime_type_only, 515 bool mime_type_only, const std::string& blob_uuid) {
538 const std::string& blob_uuid) {
539 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 516 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
540 517
541 // BlobReader is self-deleting. 518 // BlobReader is self-deleting.
542 BlobReader* reader = new BlobReader( 519 BlobReader* reader = new BlobReader(
543 GetProfile(), 520 GetProfile(),
544 blob_uuid, 521 blob_uuid,
545 base::Bind(&MediaGalleriesGetMetadataFunction::SniffMimeType, this, 522 base::Bind(&MediaGalleriesGetMetadataFunction::SniffMimeType, this,
546 mime_type_only)); 523 mime_type_only));
547 reader->SetByteRange(0, net::kMaxBytesToSniff); 524 reader->SetByteRange(0, net::kMaxBytesToSniff);
548 reader->Start(); 525 reader->Start();
(...skipping 11 matching lines...) Expand all
560 if (mime_type_sniffed) 537 if (mime_type_sniffed)
561 metadata.mime_type = mime_type; 538 metadata.mime_type = mime_type;
562 539
563 // TODO(tommycli): Kick off SafeMediaMetadataParser if |mime_type_only| false. 540 // TODO(tommycli): Kick off SafeMediaMetadataParser if |mime_type_only| false.
564 541
565 SetResult(metadata.ToValue().release()); 542 SetResult(metadata.ToValue().release());
566 SendResponse(true); 543 SendResponse(true);
567 } 544 }
568 545
569 } // namespace extensions 546 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698