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> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/numerics/safe_conversions.h" | 15 #include "base/numerics/safe_conversions.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
21 #include "chrome/browser/extensions/api/file_system/file_system_api.h" | 21 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
22 #include "chrome/browser/extensions/blob_reader.h" | 22 #include "chrome/browser/extensions/blob_reader.h" |
| 23 #include "chrome/browser/extensions/chrome_extension_function_details.h" |
23 #include "chrome/browser/extensions/extension_tab_util.h" | 24 #include "chrome/browser/extensions/extension_tab_util.h" |
24 #include "chrome/browser/media_galleries/fileapi/safe_media_metadata_parser.h" | 25 #include "chrome/browser/media_galleries/fileapi/safe_media_metadata_parser.h" |
25 #include "chrome/browser/media_galleries/gallery_watch_manager.h" | 26 #include "chrome/browser/media_galleries/gallery_watch_manager.h" |
26 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 27 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
27 #include "chrome/browser/media_galleries/media_galleries_histograms.h" | 28 #include "chrome/browser/media_galleries/media_galleries_histograms.h" |
28 #include "chrome/browser/media_galleries/media_galleries_permission_controller.h
" | 29 #include "chrome/browser/media_galleries/media_galleries_permission_controller.h
" |
29 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 30 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
30 #include "chrome/browser/media_galleries/media_galleries_scan_result_controller.
h" | 31 #include "chrome/browser/media_galleries/media_galleries_scan_result_controller.
h" |
31 #include "chrome/browser/media_galleries/media_scan_manager.h" | 32 #include "chrome/browser/media_galleries/media_scan_manager.h" |
32 #include "chrome/browser/platform_util.h" | 33 #include "chrome/browser/platform_util.h" |
33 #include "chrome/browser/profiles/profile.h" | 34 #include "chrome/browser/profiles/profile.h" |
34 #include "chrome/browser/ui/chrome_select_file_policy.h" | 35 #include "chrome/browser/ui/chrome_select_file_policy.h" |
35 #include "chrome/common/extensions/api/media_galleries.h" | 36 #include "chrome/common/extensions/api/media_galleries.h" |
36 #include "chrome/common/pref_names.h" | 37 #include "chrome/common/pref_names.h" |
37 #include "chrome/grit/generated_resources.h" | 38 #include "chrome/grit/generated_resources.h" |
38 #include "components/storage_monitor/storage_info.h" | 39 #include "components/storage_monitor/storage_info.h" |
39 #include "components/web_modal/web_contents_modal_dialog_manager.h" | |
40 #include "content/public/browser/blob_handle.h" | 40 #include "content/public/browser/blob_handle.h" |
41 #include "content/public/browser/browser_context.h" | 41 #include "content/public/browser/browser_context.h" |
42 #include "content/public/browser/browser_thread.h" | 42 #include "content/public/browser/browser_thread.h" |
43 #include "content/public/browser/child_process_security_policy.h" | 43 #include "content/public/browser/child_process_security_policy.h" |
44 #include "content/public/browser/render_frame_host.h" | 44 #include "content/public/browser/render_frame_host.h" |
45 #include "content/public/browser/render_process_host.h" | 45 #include "content/public/browser/render_process_host.h" |
46 #include "content/public/browser/render_view_host.h" | 46 #include "content/public/browser/render_view_host.h" |
47 #include "content/public/browser/web_contents.h" | 47 #include "content/public/browser/web_contents.h" |
48 #include "extensions/browser/app_window/app_window.h" | |
49 #include "extensions/browser/app_window/app_window_registry.h" | |
50 #include "extensions/browser/blob_holder.h" | 48 #include "extensions/browser/blob_holder.h" |
51 #include "extensions/browser/extension_prefs.h" | 49 #include "extensions/browser/extension_prefs.h" |
52 #include "extensions/browser/extension_system.h" | 50 #include "extensions/browser/extension_system.h" |
53 #include "extensions/common/extension.h" | 51 #include "extensions/common/extension.h" |
54 #include "extensions/common/permissions/api_permission.h" | 52 #include "extensions/common/permissions/api_permission.h" |
55 #include "extensions/common/permissions/media_galleries_permission.h" | 53 #include "extensions/common/permissions/media_galleries_permission.h" |
56 #include "extensions/common/permissions/permissions_data.h" | 54 #include "extensions/common/permissions/permissions_data.h" |
57 #include "net/base/mime_sniffer.h" | 55 #include "net/base/mime_sniffer.h" |
58 #include "storage/browser/blob/blob_data_handle.h" | 56 #include "storage/browser/blob/blob_data_handle.h" |
59 #include "ui/base/l10n/l10n_util.h" | 57 #include "ui/base/l10n/l10n_util.h" |
60 | 58 |
61 using content::WebContents; | 59 using content::WebContents; |
62 using storage_monitor::MediaStorageUtil; | 60 using storage_monitor::MediaStorageUtil; |
63 using storage_monitor::StorageInfo; | 61 using storage_monitor::StorageInfo; |
64 using web_modal::WebContentsModalDialogManager; | |
65 | 62 |
66 namespace extensions { | 63 namespace extensions { |
67 | 64 |
68 namespace MediaGalleries = api::media_galleries; | 65 namespace MediaGalleries = api::media_galleries; |
69 namespace DropPermissionForMediaFileSystem = | 66 namespace DropPermissionForMediaFileSystem = |
70 MediaGalleries::DropPermissionForMediaFileSystem; | 67 MediaGalleries::DropPermissionForMediaFileSystem; |
71 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; | 68 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; |
72 namespace AddGalleryWatch = MediaGalleries::AddGalleryWatch; | 69 namespace AddGalleryWatch = MediaGalleries::AddGalleryWatch; |
73 namespace RemoveGalleryWatch = MediaGalleries::RemoveGalleryWatch; | 70 namespace RemoveGalleryWatch = MediaGalleries::RemoveGalleryWatch; |
74 namespace GetAllGalleryWatch = MediaGalleries::GetAllGalleryWatch; | 71 namespace GetAllGalleryWatch = MediaGalleries::GetAllGalleryWatch; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 g_browser_process->media_file_system_registry()->GetPreferences(profile); | 138 g_browser_process->media_file_system_registry()->GetPreferences(profile); |
142 base::FilePath file_path( | 139 base::FilePath file_path( |
143 preferences->LookUpGalleryPathForExtension(pref_id, extension, false)); | 140 preferences->LookUpGalleryPathForExtension(pref_id, extension, false)); |
144 if (file_path.empty()) | 141 if (file_path.empty()) |
145 return false; | 142 return false; |
146 *gallery_pref_id = pref_id; | 143 *gallery_pref_id = pref_id; |
147 *gallery_file_path = file_path; | 144 *gallery_file_path = file_path; |
148 return true; | 145 return true; |
149 } | 146 } |
150 | 147 |
151 WebContents* GetWebContents(content::RenderFrameHost* rfh, | |
152 Profile* profile, | |
153 const std::string& app_id) { | |
154 WebContents* contents = WebContents::FromRenderFrameHost(rfh); | |
155 WebContentsModalDialogManager* web_contents_modal_dialog_manager = | |
156 WebContentsModalDialogManager::FromWebContents(contents); | |
157 if (!web_contents_modal_dialog_manager) { | |
158 // If there is no WebContentsModalDialogManager, then this contents is | |
159 // probably the background page for an app. Try to find a app window to | |
160 // host the dialog. | |
161 AppWindow* window = AppWindowRegistry::Get(profile) | |
162 ->GetCurrentAppWindowForApp(app_id); | |
163 contents = window ? window->web_contents() : nullptr; | |
164 } | |
165 return contents; | |
166 } | |
167 | |
168 base::ListValue* ConstructFileSystemList( | 148 base::ListValue* ConstructFileSystemList( |
169 content::RenderFrameHost* rfh, | 149 content::RenderFrameHost* rfh, |
170 const Extension* extension, | 150 const Extension* extension, |
171 const std::vector<MediaFileSystemInfo>& filesystems) { | 151 const std::vector<MediaFileSystemInfo>& filesystems) { |
172 if (!rfh) | 152 if (!rfh) |
173 return NULL; | 153 return NULL; |
174 | 154 |
175 MediaGalleriesPermission::CheckParam read_param( | 155 MediaGalleriesPermission::CheckParam read_param( |
176 MediaGalleriesPermission::kReadPermission); | 156 MediaGalleriesPermission::kReadPermission); |
177 const PermissionsData* permissions_data = extension->permissions_data(); | 157 const PermissionsData* permissions_data = extension->permissions_data(); |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 } | 516 } |
537 | 517 |
538 // The custom JS binding will use this list to create DOMFileSystem objects. | 518 // The custom JS binding will use this list to create DOMFileSystem objects. |
539 SetResult(list.release()); | 519 SetResult(list.release()); |
540 SendResponse(true); | 520 SendResponse(true); |
541 } | 521 } |
542 | 522 |
543 void MediaGalleriesGetMediaFileSystemsFunction::ShowDialog() { | 523 void MediaGalleriesGetMediaFileSystemsFunction::ShowDialog() { |
544 media_galleries::UsageCount(media_galleries::SHOW_DIALOG); | 524 media_galleries::UsageCount(media_galleries::SHOW_DIALOG); |
545 WebContents* contents = | 525 WebContents* contents = |
546 GetWebContents(render_frame_host(), GetProfile(), extension()->id()); | 526 ChromeExtensionFunctionDetails(this).GetOriginWebContents(); |
547 if (!contents) { | 527 if (!contents) { |
548 SendResponse(false); | 528 SendResponse(false); |
549 return; | 529 return; |
550 } | 530 } |
551 | 531 |
552 // Controller will delete itself. | 532 // Controller will delete itself. |
553 base::Closure cb = base::Bind( | 533 base::Closure cb = base::Bind( |
554 &MediaGalleriesGetMediaFileSystemsFunction::GetAndReturnGalleries, this); | 534 &MediaGalleriesGetMediaFileSystemsFunction::GetAndReturnGalleries, this); |
555 new MediaGalleriesPermissionController(contents, *extension(), cb); | 535 new MediaGalleriesPermissionController(contents, *extension(), cb); |
556 } | 536 } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 | 621 |
642 bool MediaGalleriesAddUserSelectedFolderFunction::RunAsync() { | 622 bool MediaGalleriesAddUserSelectedFolderFunction::RunAsync() { |
643 media_galleries::UsageCount(media_galleries::ADD_USER_SELECTED_FOLDER); | 623 media_galleries::UsageCount(media_galleries::ADD_USER_SELECTED_FOLDER); |
644 return Setup(GetProfile(), &error_, base::Bind( | 624 return Setup(GetProfile(), &error_, base::Bind( |
645 &MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit, this)); | 625 &MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit, this)); |
646 } | 626 } |
647 | 627 |
648 void MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit() { | 628 void MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit() { |
649 Profile* profile = GetProfile(); | 629 Profile* profile = GetProfile(); |
650 const std::string& app_id = extension()->id(); | 630 const std::string& app_id = extension()->id(); |
651 WebContents* contents = GetWebContents(render_frame_host(), profile, app_id); | 631 WebContents* contents = |
| 632 ChromeExtensionFunctionDetails(this).GetOriginWebContents(); |
652 if (!contents) { | 633 if (!contents) { |
653 // When the request originated from a background page, but there is no app | 634 SendResponse(false); |
654 // window open, check to see if it originated from a tab and display the | 635 return; |
655 // dialog in that tab. | |
656 bool found_tab = extensions::ExtensionTabUtil::GetTabById( | |
657 source_tab_id(), profile, profile->IsOffTheRecord(), | |
658 NULL, NULL, &contents, NULL); | |
659 if (!found_tab || !contents) { | |
660 SendResponse(false); | |
661 return; | |
662 } | |
663 } | 636 } |
664 | 637 |
665 if (!user_gesture()) { | 638 if (!user_gesture()) { |
666 OnDirectorySelected(base::FilePath()); | 639 OnDirectorySelected(base::FilePath()); |
667 return; | 640 return; |
668 } | 641 } |
669 | 642 |
670 base::FilePath last_used_path = | 643 base::FilePath last_used_path = |
671 extensions::file_system_api::GetLastChooseEntryDirectory( | 644 extensions::file_system_api::GetLastChooseEntryDirectory( |
672 extensions::ExtensionPrefs::Get(profile), app_id); | 645 extensions::ExtensionPrefs::Get(profile), app_id); |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
876 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 849 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
877 MediaGalleriesPreferences* preferences = | 850 MediaGalleriesPreferences* preferences = |
878 media_file_system_registry()->GetPreferences(GetProfile()); | 851 media_file_system_registry()->GetPreferences(GetProfile()); |
879 if (MediaGalleriesScanResultController::ScanResultCountForExtension( | 852 if (MediaGalleriesScanResultController::ScanResultCountForExtension( |
880 preferences, extension()) == 0) { | 853 preferences, extension()) == 0) { |
881 GetAndReturnGalleries(); | 854 GetAndReturnGalleries(); |
882 return; | 855 return; |
883 } | 856 } |
884 | 857 |
885 WebContents* contents = | 858 WebContents* contents = |
886 GetWebContents(render_frame_host(), GetProfile(), extension()->id()); | 859 ChromeExtensionFunctionDetails(this).GetOriginWebContents(); |
887 if (!contents) { | 860 if (!contents) { |
888 SendResponse(false); | 861 SendResponse(false); |
889 return; | 862 return; |
890 } | 863 } |
891 | 864 |
892 base::Closure cb = base::Bind( | 865 base::Closure cb = base::Bind( |
893 &MediaGalleriesAddScanResultsFunction::GetAndReturnGalleries, this); | 866 &MediaGalleriesAddScanResultsFunction::GetAndReturnGalleries, this); |
894 MakeDialog(contents, *extension(), cb); | 867 MakeDialog(contents, *extension(), cb); |
895 } | 868 } |
896 | 869 |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1273 &MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit, this)); | 1246 &MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit, this)); |
1274 return true; | 1247 return true; |
1275 } | 1248 } |
1276 | 1249 |
1277 void MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit() { | 1250 void MediaGalleriesRemoveAllGalleryWatchFunction::OnPreferencesInit() { |
1278 gallery_watch_manager()->RemoveAllWatches(GetProfile(), extension_id()); | 1251 gallery_watch_manager()->RemoveAllWatches(GetProfile(), extension_id()); |
1279 SendResponse(true); | 1252 SendResponse(true); |
1280 } | 1253 } |
1281 | 1254 |
1282 } // namespace extensions | 1255 } // namespace extensions |
OLD | NEW |