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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 namespace MediaGalleries = api::media_galleries; | 58 namespace MediaGalleries = api::media_galleries; |
59 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; | 59 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; |
60 | 60 |
61 namespace { | 61 namespace { |
62 | 62 |
63 const char kDisallowedByPolicy[] = | 63 const char kDisallowedByPolicy[] = |
64 "Media Galleries API is disallowed by policy: "; | 64 "Media Galleries API is disallowed by policy: "; |
65 const char kMissingEventListener[] = | 65 const char kMissingEventListener[] = |
66 "Missing event listener registration."; | 66 "Missing event listener registration."; |
| 67 const char kNoScanPermission[] = |
| 68 "No permission to scan."; |
67 | 69 |
68 const char kDeviceIdKey[] = "deviceId"; | 70 const char kDeviceIdKey[] = "deviceId"; |
69 const char kGalleryIdKey[] = "galleryId"; | 71 const char kGalleryIdKey[] = "galleryId"; |
70 const char kIsAvailableKey[] = "isAvailable"; | 72 const char kIsAvailableKey[] = "isAvailable"; |
71 const char kIsMediaDeviceKey[] = "isMediaDevice"; | 73 const char kIsMediaDeviceKey[] = "isMediaDevice"; |
72 const char kIsRemovableKey[] = "isRemovable"; | 74 const char kIsRemovableKey[] = "isRemovable"; |
73 const char kNameKey[] = "name"; | 75 const char kNameKey[] = "name"; |
74 | 76 |
75 MediaFileSystemRegistry* media_file_system_registry() { | 77 MediaFileSystemRegistry* media_file_system_registry() { |
76 return g_browser_process->media_file_system_registry(); | 78 return g_browser_process->media_file_system_registry(); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 if (has_copy_to_permission) { | 176 if (has_copy_to_permission) { |
175 policy->GrantCopyIntoFileSystem(child_id, filesystems[i].fsid); | 177 policy->GrantCopyIntoFileSystem(child_id, filesystems[i].fsid); |
176 } | 178 } |
177 } | 179 } |
178 } | 180 } |
179 } | 181 } |
180 | 182 |
181 return list.release(); | 183 return list.release(); |
182 } | 184 } |
183 | 185 |
| 186 bool CheckScanPermission(const extensions::Extension* extension, |
| 187 std::string* error) { |
| 188 DCHECK(extension); |
| 189 DCHECK(error); |
| 190 MediaGalleriesPermission::CheckParam scan_param( |
| 191 MediaGalleriesPermission::kScanPermission); |
| 192 bool has_scan_permission = PermissionsData::CheckAPIPermissionWithParam( |
| 193 extension, APIPermission::kMediaGalleries, &scan_param); |
| 194 if (!has_scan_permission) |
| 195 *error = kNoScanPermission; |
| 196 return has_scan_permission; |
| 197 } |
| 198 |
184 class SelectDirectoryDialog : public ui::SelectFileDialog::Listener, | 199 class SelectDirectoryDialog : public ui::SelectFileDialog::Listener, |
185 public base::RefCounted<SelectDirectoryDialog> { | 200 public base::RefCounted<SelectDirectoryDialog> { |
186 public: | 201 public: |
187 // Selected file path, or an empty path if the user canceled. | 202 // Selected file path, or an empty path if the user canceled. |
188 typedef base::Callback<void(const base::FilePath&)> Callback; | 203 typedef base::Callback<void(const base::FilePath&)> Callback; |
189 | 204 |
190 SelectDirectoryDialog(WebContents* web_contents, const Callback& callback) | 205 SelectDirectoryDialog(WebContents* web_contents, const Callback& callback) |
191 : web_contents_(web_contents), | 206 : web_contents_(web_contents), |
192 callback_(callback) { | 207 callback_(callback) { |
193 select_file_dialog_ = ui::SelectFileDialog::Create( | 208 select_file_dialog_ = ui::SelectFileDialog::Create( |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 details.gallery_count.reset(new int(gallery_count)); | 323 details.gallery_count.reset(new int(gallery_count)); |
309 details.image_count.reset(new int(image_count)); | 324 details.image_count.reset(new int(image_count)); |
310 details.audio_count.reset(new int(audio_count)); | 325 details.audio_count.reset(new int(audio_count)); |
311 details.video_count.reset(new int(video_count)); | 326 details.video_count.reset(new int(video_count)); |
312 DispatchEventToExtension( | 327 DispatchEventToExtension( |
313 extension_id, | 328 extension_id, |
314 MediaGalleries::OnScanProgress::kEventName, | 329 MediaGalleries::OnScanProgress::kEventName, |
315 MediaGalleries::OnScanProgress::Create(details).Pass()); | 330 MediaGalleries::OnScanProgress::Create(details).Pass()); |
316 } | 331 } |
317 | 332 |
| 333 void MediaGalleriesEventRouter::OnScanError( |
| 334 const std::string& extension_id) { |
| 335 MediaGalleries::ScanProgressDetails details; |
| 336 details.type = MediaGalleries::SCAN_PROGRESS_TYPE_ERROR; |
| 337 DispatchEventToExtension( |
| 338 extension_id, |
| 339 MediaGalleries::OnScanProgress::kEventName, |
| 340 MediaGalleries::OnScanProgress::Create(details).Pass()); |
| 341 } |
| 342 |
318 void MediaGalleriesEventRouter::DispatchEventToExtension( | 343 void MediaGalleriesEventRouter::DispatchEventToExtension( |
319 const std::string& extension_id, | 344 const std::string& extension_id, |
320 const std::string& event_name, | 345 const std::string& event_name, |
321 scoped_ptr<base::ListValue> event_args) { | 346 scoped_ptr<base::ListValue> event_args) { |
322 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 347 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
323 EventRouter* router = | 348 EventRouter* router = |
324 extensions::ExtensionSystem::Get(profile_)->event_router(); | 349 extensions::ExtensionSystem::Get(profile_)->event_router(); |
325 if (!router->ExtensionHasEventListener(extension_id, event_name)) | 350 if (!router->ExtensionHasEventListener(extension_id, event_name)) |
326 return; | 351 return; |
327 | 352 |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 MediaFileSystemRegistry* registry = media_file_system_registry(); | 623 MediaFileSystemRegistry* registry = media_file_system_registry(); |
599 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); | 624 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); |
600 registry->GetMediaFileSystemsForExtension( | 625 registry->GetMediaFileSystemsForExtension( |
601 render_view_host(), GetExtension(), cb); | 626 render_view_host(), GetExtension(), cb); |
602 } | 627 } |
603 | 628 |
604 MediaGalleriesStartMediaScanFunction::~MediaGalleriesStartMediaScanFunction() {} | 629 MediaGalleriesStartMediaScanFunction::~MediaGalleriesStartMediaScanFunction() {} |
605 | 630 |
606 bool MediaGalleriesStartMediaScanFunction::RunImpl() { | 631 bool MediaGalleriesStartMediaScanFunction::RunImpl() { |
607 media_galleries::UsageCount(media_galleries::START_MEDIA_SCAN); | 632 media_galleries::UsageCount(media_galleries::START_MEDIA_SCAN); |
| 633 if (!CheckScanPermission(GetExtension(), &error_)) { |
| 634 MediaGalleriesEventRouter::Get(GetProfile())->OnScanError( |
| 635 GetExtension()->id()); |
| 636 return false; |
| 637 } |
608 return Setup(GetProfile(), &error_, base::Bind( | 638 return Setup(GetProfile(), &error_, base::Bind( |
609 &MediaGalleriesStartMediaScanFunction::OnPreferencesInit, this)); | 639 &MediaGalleriesStartMediaScanFunction::OnPreferencesInit, this)); |
610 } | 640 } |
611 | 641 |
612 void MediaGalleriesStartMediaScanFunction::OnPreferencesInit() { | 642 void MediaGalleriesStartMediaScanFunction::OnPreferencesInit() { |
613 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 643 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
614 MediaGalleriesEventRouter* api = MediaGalleriesEventRouter::Get(GetProfile()); | 644 MediaGalleriesEventRouter* api = MediaGalleriesEventRouter::Get(GetProfile()); |
615 if (!api->ExtensionHasScanProgressListener(GetExtension()->id())) { | 645 if (!api->ExtensionHasScanProgressListener(GetExtension()->id())) { |
616 error_ = kMissingEventListener; | 646 error_ = kMissingEventListener; |
617 SendResponse(false); | 647 SendResponse(false); |
618 return; | 648 return; |
619 } | 649 } |
620 | 650 |
621 media_scan_manager()->StartScan(GetProfile(), GetExtension()->id()); | 651 media_scan_manager()->StartScan(GetProfile(), GetExtension()->id()); |
622 SendResponse(true); | 652 SendResponse(true); |
623 } | 653 } |
624 | 654 |
625 MediaGalleriesCancelMediaScanFunction:: | 655 MediaGalleriesCancelMediaScanFunction:: |
626 ~MediaGalleriesCancelMediaScanFunction() { | 656 ~MediaGalleriesCancelMediaScanFunction() { |
627 } | 657 } |
628 | 658 |
629 bool MediaGalleriesCancelMediaScanFunction::RunImpl() { | 659 bool MediaGalleriesCancelMediaScanFunction::RunImpl() { |
630 media_galleries::UsageCount(media_galleries::CANCEL_MEDIA_SCAN); | 660 media_galleries::UsageCount(media_galleries::CANCEL_MEDIA_SCAN); |
| 661 if (!CheckScanPermission(GetExtension(), &error_)) { |
| 662 MediaGalleriesEventRouter::Get(GetProfile())->OnScanError( |
| 663 GetExtension()->id()); |
| 664 return false; |
| 665 } |
631 return Setup(GetProfile(), &error_, base::Bind( | 666 return Setup(GetProfile(), &error_, base::Bind( |
632 &MediaGalleriesCancelMediaScanFunction::OnPreferencesInit, this)); | 667 &MediaGalleriesCancelMediaScanFunction::OnPreferencesInit, this)); |
633 } | 668 } |
634 | 669 |
635 void MediaGalleriesCancelMediaScanFunction::OnPreferencesInit() { | 670 void MediaGalleriesCancelMediaScanFunction::OnPreferencesInit() { |
636 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 671 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
637 media_scan_manager()->CancelScan(GetProfile(), GetExtension()->id()); | 672 media_scan_manager()->CancelScan(GetProfile(), GetExtension()->id()); |
638 SendResponse(true); | 673 SendResponse(true); |
639 } | 674 } |
640 | 675 |
641 MediaGalleriesAddScanResultsFunction::~MediaGalleriesAddScanResultsFunction() {} | 676 MediaGalleriesAddScanResultsFunction::~MediaGalleriesAddScanResultsFunction() {} |
642 | 677 |
643 bool MediaGalleriesAddScanResultsFunction::RunImpl() { | 678 bool MediaGalleriesAddScanResultsFunction::RunImpl() { |
644 media_galleries::UsageCount(media_galleries::ADD_SCAN_RESULTS); | 679 media_galleries::UsageCount(media_galleries::ADD_SCAN_RESULTS); |
| 680 if (!CheckScanPermission(GetExtension(), &error_)) { |
| 681 // We don't fire a scan progress error here, as it would be unintuitive. |
| 682 return false; |
| 683 } |
645 return Setup(GetProfile(), &error_, base::Bind( | 684 return Setup(GetProfile(), &error_, base::Bind( |
646 &MediaGalleriesAddScanResultsFunction::OnPreferencesInit, this)); | 685 &MediaGalleriesAddScanResultsFunction::OnPreferencesInit, this)); |
647 } | 686 } |
648 | 687 |
649 void MediaGalleriesAddScanResultsFunction::OnPreferencesInit() { | 688 void MediaGalleriesAddScanResultsFunction::OnPreferencesInit() { |
650 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 689 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
651 const Extension* extension = GetExtension(); | 690 const Extension* extension = GetExtension(); |
652 WebContents* contents = | 691 WebContents* contents = |
653 GetWebContents(render_view_host(), GetProfile(), extension->id()); | 692 GetWebContents(render_view_host(), GetProfile(), extension->id()); |
654 if (!contents) { | 693 if (!contents) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
738 if (mime_type_sniffed) | 777 if (mime_type_sniffed) |
739 metadata.mime_type = mime_type; | 778 metadata.mime_type = mime_type; |
740 | 779 |
741 // TODO(tommycli): Kick off SafeMediaMetadataParser if |mime_type_only| false. | 780 // TODO(tommycli): Kick off SafeMediaMetadataParser if |mime_type_only| false. |
742 | 781 |
743 SetResult(metadata.ToValue().release()); | 782 SetResult(metadata.ToValue().release()); |
744 SendResponse(true); | 783 SendResponse(true); |
745 } | 784 } |
746 | 785 |
747 } // namespace extensions | 786 } // namespace extensions |
OLD | NEW |