| 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 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 Callback callback_; | 255 Callback callback_; |
| 256 | 256 |
| 257 DISALLOW_COPY_AND_ASSIGN(SelectDirectoryDialog); | 257 DISALLOW_COPY_AND_ASSIGN(SelectDirectoryDialog); |
| 258 }; | 258 }; |
| 259 | 259 |
| 260 } // namespace | 260 } // namespace |
| 261 | 261 |
| 262 MediaGalleriesEventRouter::MediaGalleriesEventRouter( | 262 MediaGalleriesEventRouter::MediaGalleriesEventRouter( |
| 263 content::BrowserContext* context) | 263 content::BrowserContext* context) |
| 264 : profile_(Profile::FromBrowserContext(context)), weak_ptr_factory_(this) { | 264 : profile_(Profile::FromBrowserContext(context)), weak_ptr_factory_(this) { |
| 265 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 265 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 266 DCHECK(profile_); | 266 DCHECK(profile_); |
| 267 media_scan_manager()->AddObserver(profile_, this); | 267 media_scan_manager()->AddObserver(profile_, this); |
| 268 } | 268 } |
| 269 | 269 |
| 270 MediaGalleriesEventRouter::~MediaGalleriesEventRouter() { | 270 MediaGalleriesEventRouter::~MediaGalleriesEventRouter() { |
| 271 } | 271 } |
| 272 | 272 |
| 273 void MediaGalleriesEventRouter::Shutdown() { | 273 void MediaGalleriesEventRouter::Shutdown() { |
| 274 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 274 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 275 weak_ptr_factory_.InvalidateWeakPtrs(); | 275 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 276 media_scan_manager()->RemoveObserver(profile_); | 276 media_scan_manager()->RemoveObserver(profile_); |
| 277 media_scan_manager()->CancelScansForProfile(profile_); | 277 media_scan_manager()->CancelScansForProfile(profile_); |
| 278 } | 278 } |
| 279 | 279 |
| 280 static base::LazyInstance< | 280 static base::LazyInstance< |
| 281 BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter> > g_factory = | 281 BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter> > g_factory = |
| 282 LAZY_INSTANCE_INITIALIZER; | 282 LAZY_INSTANCE_INITIALIZER; |
| 283 | 283 |
| 284 // static | 284 // static |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 DispatchEventToExtension( | 346 DispatchEventToExtension( |
| 347 extension_id, | 347 extension_id, |
| 348 MediaGalleries::OnScanProgress::kEventName, | 348 MediaGalleries::OnScanProgress::kEventName, |
| 349 MediaGalleries::OnScanProgress::Create(details).Pass()); | 349 MediaGalleries::OnScanProgress::Create(details).Pass()); |
| 350 } | 350 } |
| 351 | 351 |
| 352 void MediaGalleriesEventRouter::DispatchEventToExtension( | 352 void MediaGalleriesEventRouter::DispatchEventToExtension( |
| 353 const std::string& extension_id, | 353 const std::string& extension_id, |
| 354 const std::string& event_name, | 354 const std::string& event_name, |
| 355 scoped_ptr<base::ListValue> event_args) { | 355 scoped_ptr<base::ListValue> event_args) { |
| 356 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 356 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 357 EventRouter* router = | 357 EventRouter* router = |
| 358 extensions::ExtensionSystem::Get(profile_)->event_router(); | 358 extensions::ExtensionSystem::Get(profile_)->event_router(); |
| 359 if (!router->ExtensionHasEventListener(extension_id, event_name)) | 359 if (!router->ExtensionHasEventListener(extension_id, event_name)) |
| 360 return; | 360 return; |
| 361 | 361 |
| 362 scoped_ptr<extensions::Event> event( | 362 scoped_ptr<extensions::Event> event( |
| 363 new extensions::Event(event_name, event_args.Pass())); | 363 new extensions::Event(event_name, event_args.Pass())); |
| 364 router->DispatchEventToExtension(extension_id, event.Pass()); | 364 router->DispatchEventToExtension(extension_id, event.Pass()); |
| 365 } | 365 } |
| 366 | 366 |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 if (!CheckScanPermission(GetExtension(), &error_)) { | 650 if (!CheckScanPermission(GetExtension(), &error_)) { |
| 651 MediaGalleriesEventRouter::Get(GetProfile())->OnScanError( | 651 MediaGalleriesEventRouter::Get(GetProfile())->OnScanError( |
| 652 GetExtension()->id()); | 652 GetExtension()->id()); |
| 653 return false; | 653 return false; |
| 654 } | 654 } |
| 655 return Setup(GetProfile(), &error_, base::Bind( | 655 return Setup(GetProfile(), &error_, base::Bind( |
| 656 &MediaGalleriesStartMediaScanFunction::OnPreferencesInit, this)); | 656 &MediaGalleriesStartMediaScanFunction::OnPreferencesInit, this)); |
| 657 } | 657 } |
| 658 | 658 |
| 659 void MediaGalleriesStartMediaScanFunction::OnPreferencesInit() { | 659 void MediaGalleriesStartMediaScanFunction::OnPreferencesInit() { |
| 660 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 660 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 661 MediaGalleriesEventRouter* api = MediaGalleriesEventRouter::Get(GetProfile()); | 661 MediaGalleriesEventRouter* api = MediaGalleriesEventRouter::Get(GetProfile()); |
| 662 if (!api->ExtensionHasScanProgressListener(GetExtension()->id())) { | 662 if (!api->ExtensionHasScanProgressListener(GetExtension()->id())) { |
| 663 error_ = kMissingEventListener; | 663 error_ = kMissingEventListener; |
| 664 SendResponse(false); | 664 SendResponse(false); |
| 665 return; | 665 return; |
| 666 } | 666 } |
| 667 | 667 |
| 668 media_scan_manager()->StartScan(GetProfile(), GetExtension(), user_gesture()); | 668 media_scan_manager()->StartScan(GetProfile(), GetExtension(), user_gesture()); |
| 669 SendResponse(true); | 669 SendResponse(true); |
| 670 } | 670 } |
| 671 | 671 |
| 672 MediaGalleriesCancelMediaScanFunction:: | 672 MediaGalleriesCancelMediaScanFunction:: |
| 673 ~MediaGalleriesCancelMediaScanFunction() { | 673 ~MediaGalleriesCancelMediaScanFunction() { |
| 674 } | 674 } |
| 675 | 675 |
| 676 bool MediaGalleriesCancelMediaScanFunction::RunImpl() { | 676 bool MediaGalleriesCancelMediaScanFunction::RunImpl() { |
| 677 media_galleries::UsageCount(media_galleries::CANCEL_MEDIA_SCAN); | 677 media_galleries::UsageCount(media_galleries::CANCEL_MEDIA_SCAN); |
| 678 if (!CheckScanPermission(GetExtension(), &error_)) { | 678 if (!CheckScanPermission(GetExtension(), &error_)) { |
| 679 MediaGalleriesEventRouter::Get(GetProfile())->OnScanError( | 679 MediaGalleriesEventRouter::Get(GetProfile())->OnScanError( |
| 680 GetExtension()->id()); | 680 GetExtension()->id()); |
| 681 return false; | 681 return false; |
| 682 } | 682 } |
| 683 return Setup(GetProfile(), &error_, base::Bind( | 683 return Setup(GetProfile(), &error_, base::Bind( |
| 684 &MediaGalleriesCancelMediaScanFunction::OnPreferencesInit, this)); | 684 &MediaGalleriesCancelMediaScanFunction::OnPreferencesInit, this)); |
| 685 } | 685 } |
| 686 | 686 |
| 687 void MediaGalleriesCancelMediaScanFunction::OnPreferencesInit() { | 687 void MediaGalleriesCancelMediaScanFunction::OnPreferencesInit() { |
| 688 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 688 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 689 media_scan_manager()->CancelScan(GetProfile(), GetExtension()); | 689 media_scan_manager()->CancelScan(GetProfile(), GetExtension()); |
| 690 SendResponse(true); | 690 SendResponse(true); |
| 691 } | 691 } |
| 692 | 692 |
| 693 MediaGalleriesAddScanResultsFunction::~MediaGalleriesAddScanResultsFunction() {} | 693 MediaGalleriesAddScanResultsFunction::~MediaGalleriesAddScanResultsFunction() {} |
| 694 | 694 |
| 695 bool MediaGalleriesAddScanResultsFunction::RunImpl() { | 695 bool MediaGalleriesAddScanResultsFunction::RunImpl() { |
| 696 media_galleries::UsageCount(media_galleries::ADD_SCAN_RESULTS); | 696 media_galleries::UsageCount(media_galleries::ADD_SCAN_RESULTS); |
| 697 if (!CheckScanPermission(GetExtension(), &error_)) { | 697 if (!CheckScanPermission(GetExtension(), &error_)) { |
| 698 // We don't fire a scan progress error here, as it would be unintuitive. | 698 // We don't fire a scan progress error here, as it would be unintuitive. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 709 MediaGalleriesAddScanResultsFunction::MakeDialog( | 709 MediaGalleriesAddScanResultsFunction::MakeDialog( |
| 710 content::WebContents* web_contents, | 710 content::WebContents* web_contents, |
| 711 const extensions::Extension& extension, | 711 const extensions::Extension& extension, |
| 712 const base::Closure& on_finish) { | 712 const base::Closure& on_finish) { |
| 713 // Controller will delete itself. | 713 // Controller will delete itself. |
| 714 return new MediaGalleriesScanResultDialogController(web_contents, extension, | 714 return new MediaGalleriesScanResultDialogController(web_contents, extension, |
| 715 on_finish); | 715 on_finish); |
| 716 } | 716 } |
| 717 | 717 |
| 718 void MediaGalleriesAddScanResultsFunction::OnPreferencesInit() { | 718 void MediaGalleriesAddScanResultsFunction::OnPreferencesInit() { |
| 719 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 719 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 720 const Extension* extension = GetExtension(); | 720 const Extension* extension = GetExtension(); |
| 721 MediaGalleriesPreferences* preferences = | 721 MediaGalleriesPreferences* preferences = |
| 722 media_file_system_registry()->GetPreferences(GetProfile()); | 722 media_file_system_registry()->GetPreferences(GetProfile()); |
| 723 if (MediaGalleriesScanResultDialogController::ScanResultCountForExtension( | 723 if (MediaGalleriesScanResultDialogController::ScanResultCountForExtension( |
| 724 preferences, extension) == 0) { | 724 preferences, extension) == 0) { |
| 725 GetAndReturnGalleries(); | 725 GetAndReturnGalleries(); |
| 726 return; | 726 return; |
| 727 } | 727 } |
| 728 | 728 |
| 729 WebContents* contents = | 729 WebContents* contents = |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 782 bool mime_type_only = options->metadata_type == | 782 bool mime_type_only = options->metadata_type == |
| 783 MediaGalleries::GET_METADATA_TYPE_MIMETYPEONLY; | 783 MediaGalleries::GET_METADATA_TYPE_MIMETYPEONLY; |
| 784 | 784 |
| 785 return Setup(GetProfile(), &error_, base::Bind( | 785 return Setup(GetProfile(), &error_, base::Bind( |
| 786 &MediaGalleriesGetMetadataFunction::OnPreferencesInit, this, | 786 &MediaGalleriesGetMetadataFunction::OnPreferencesInit, this, |
| 787 mime_type_only, blob_uuid)); | 787 mime_type_only, blob_uuid)); |
| 788 } | 788 } |
| 789 | 789 |
| 790 void MediaGalleriesGetMetadataFunction::OnPreferencesInit( | 790 void MediaGalleriesGetMetadataFunction::OnPreferencesInit( |
| 791 bool mime_type_only, const std::string& blob_uuid) { | 791 bool mime_type_only, const std::string& blob_uuid) { |
| 792 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 792 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 793 | 793 |
| 794 // BlobReader is self-deleting. | 794 // BlobReader is self-deleting. |
| 795 BlobReader* reader = new BlobReader( | 795 BlobReader* reader = new BlobReader( |
| 796 GetProfile(), | 796 GetProfile(), |
| 797 blob_uuid, | 797 blob_uuid, |
| 798 base::Bind(&MediaGalleriesGetMetadataFunction::SniffMimeType, this, | 798 base::Bind(&MediaGalleriesGetMetadataFunction::SniffMimeType, this, |
| 799 mime_type_only, blob_uuid)); | 799 mime_type_only, blob_uuid)); |
| 800 reader->SetByteRange(0, net::kMaxBytesToSniff); | 800 reader->SetByteRange(0, net::kMaxBytesToSniff); |
| 801 reader->Start(); | 801 reader->Start(); |
| 802 } | 802 } |
| 803 | 803 |
| 804 void MediaGalleriesGetMetadataFunction::SniffMimeType( | 804 void MediaGalleriesGetMetadataFunction::SniffMimeType( |
| 805 bool mime_type_only, const std::string& blob_uuid, | 805 bool mime_type_only, const std::string& blob_uuid, |
| 806 scoped_ptr<std::string> blob_header, int64 total_blob_length) { | 806 scoped_ptr<std::string> blob_header, int64 total_blob_length) { |
| 807 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 807 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 808 | 808 |
| 809 std::string mime_type; | 809 std::string mime_type; |
| 810 bool mime_type_sniffed = net::SniffMimeTypeFromLocalData( | 810 bool mime_type_sniffed = net::SniffMimeTypeFromLocalData( |
| 811 blob_header->c_str(), blob_header->size(), &mime_type); | 811 blob_header->c_str(), blob_header->size(), &mime_type); |
| 812 | 812 |
| 813 if (!mime_type_sniffed) { | 813 if (!mime_type_sniffed) { |
| 814 SendResponse(false); | 814 SendResponse(false); |
| 815 return; | 815 return; |
| 816 } | 816 } |
| 817 | 817 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 835 if (!parse_success) { | 835 if (!parse_success) { |
| 836 SendResponse(false); | 836 SendResponse(false); |
| 837 return; | 837 return; |
| 838 } | 838 } |
| 839 | 839 |
| 840 SetResult(metadata_dictionary->DeepCopy()); | 840 SetResult(metadata_dictionary->DeepCopy()); |
| 841 SendResponse(true); | 841 SendResponse(true); |
| 842 } | 842 } |
| 843 | 843 |
| 844 } // namespace extensions | 844 } // namespace extensions |
| OLD | NEW |