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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "apps/shell_window.h" | 13 #include "apps/shell_window.h" |
| 14 #include "apps/shell_window_registry.h" | 14 #include "apps/shell_window_registry.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/platform_file.h" | 16 #include "base/platform_file.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/task_runner_util.h" | |
| 20 #include "base/values.h" | 21 #include "base/values.h" |
| 21 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
| 23 #include "chrome/browser/extensions/api/file_system/file_system_api.h" | |
| 24 #include "chrome/browser/extensions/extension_prefs.h" | |
| 22 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 25 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
| 23 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" | 26 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" |
| 24 #include "chrome/browser/media_galleries/media_galleries_histograms.h" | 27 #include "chrome/browser/media_galleries/media_galleries_histograms.h" |
| 25 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 28 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
| 29 #include "chrome/browser/platform_util.h" | |
| 26 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/browser/storage_monitor/storage_info.h" | 31 #include "chrome/browser/storage_monitor/storage_info.h" |
| 28 #include "chrome/browser/ui/chrome_select_file_policy.h" | 32 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 29 #include "chrome/common/extensions/api/media_galleries.h" | 33 #include "chrome/common/extensions/api/media_galleries.h" |
| 30 #include "chrome/common/extensions/permissions/media_galleries_permission.h" | 34 #include "chrome/common/extensions/permissions/media_galleries_permission.h" |
| 31 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
| 32 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 36 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 37 #include "content/public/browser/browser_thread.h" | |
| 33 #include "content/public/browser/child_process_security_policy.h" | 38 #include "content/public/browser/child_process_security_policy.h" |
| 34 #include "content/public/browser/render_process_host.h" | 39 #include "content/public/browser/render_process_host.h" |
| 35 #include "content/public/browser/render_view_host.h" | 40 #include "content/public/browser/render_view_host.h" |
| 36 #include "content/public/browser/web_contents.h" | 41 #include "content/public/browser/web_contents.h" |
| 42 #include "content/public/browser/web_contents_view.h" | |
| 37 #include "extensions/common/extension.h" | 43 #include "extensions/common/extension.h" |
| 38 #include "extensions/common/permissions/api_permission.h" | 44 #include "extensions/common/permissions/api_permission.h" |
| 39 #include "extensions/common/permissions/permissions_data.h" | 45 #include "extensions/common/permissions/permissions_data.h" |
| 40 | 46 |
| 41 using content::WebContents; | 47 using content::WebContents; |
| 42 using web_modal::WebContentsModalDialogManager; | 48 using web_modal::WebContentsModalDialogManager; |
| 43 | 49 |
| 44 namespace extensions { | 50 namespace extensions { |
| 45 | 51 |
| 46 namespace MediaGalleries = api::media_galleries; | 52 namespace MediaGalleries = api::media_galleries; |
| 47 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; | 53 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; |
| 54 namespace ManageMediaLocations = MediaGalleries::ManageMediaLocations; | |
| 48 | 55 |
| 49 namespace { | 56 namespace { |
| 50 | 57 |
| 51 const char kDisallowedByPolicy[] = | 58 const char kDisallowedByPolicy[] = |
| 52 "Media Galleries API is disallowed by policy: "; | 59 "Media Galleries API is disallowed by policy: "; |
| 60 const char kInvalidManageRequestType[] = | |
| 61 "Invalid manageMediaLocations request type"; | |
| 53 | 62 |
| 54 const char kDeviceIdKey[] = "deviceId"; | 63 const char kDeviceIdKey[] = "deviceId"; |
| 55 const char kGalleryIdKey[] = "galleryId"; | 64 const char kGalleryIdKey[] = "galleryId"; |
| 56 const char kIsAvailableKey[] = "isAvailable"; | 65 const char kIsAvailableKey[] = "isAvailable"; |
| 57 const char kIsMediaDeviceKey[] = "isMediaDevice"; | 66 const char kIsMediaDeviceKey[] = "isMediaDevice"; |
| 58 const char kIsRemovableKey[] = "isRemovable"; | 67 const char kIsRemovableKey[] = "isRemovable"; |
| 59 const char kNameKey[] = "name"; | 68 const char kNameKey[] = "name"; |
| 60 | 69 |
| 61 // Checks whether the MediaGalleries API is currently accessible (it may be | 70 // Checks whether the MediaGalleries API is currently accessible (it may be |
| 62 // disallowed even if an extension has the requisite permission). | 71 // disallowed even if an extension has the requisite permission). |
| 63 bool ApiIsAccessible(std::string* error) { | 72 bool ApiIsAccessible(std::string* error) { |
| 64 if (!ChromeSelectFilePolicy::FileSelectDialogsAllowed()) { | 73 if (!ChromeSelectFilePolicy::FileSelectDialogsAllowed()) { |
| 65 *error = std::string(kDisallowedByPolicy) + | 74 *error = std::string(kDisallowedByPolicy) + |
| 66 prefs::kAllowFileSelectionDialogs; | 75 prefs::kAllowFileSelectionDialogs; |
| 67 return false; | 76 return false; |
| 68 } | 77 } |
| 69 | 78 |
| 70 return true; | 79 return true; |
| 71 } | 80 } |
| 72 | 81 |
| 73 MediaFileSystemRegistry* media_file_system_registry() { | 82 MediaFileSystemRegistry* media_file_system_registry() { |
| 74 return g_browser_process->media_file_system_registry(); | 83 return g_browser_process->media_file_system_registry(); |
| 75 } | 84 } |
| 76 | 85 |
| 86 class SelectDirectoryDialog : public ui::SelectFileDialog::Listener, | |
| 87 public base::RefCounted<SelectDirectoryDialog> { | |
| 88 public: | |
| 89 // Selected file path, or an empty path if the user canceled. | |
| 90 typedef base::Callback<void(const base::FilePath&)> Callback; | |
| 91 | |
| 92 SelectDirectoryDialog(WebContents* web_contents, const Callback& callback) | |
| 93 : web_contents_(web_contents), | |
| 94 callback_(callback) { | |
| 95 select_file_dialog_ = ui::SelectFileDialog::Create( | |
| 96 this, new ChromeSelectFilePolicy(web_contents)); | |
| 97 } | |
| 98 | |
| 99 void Show(const base::FilePath& default_path) { | |
| 100 AddRef(); // Balanced in the two reachable listener outcomes. | |
| 101 select_file_dialog_->SelectFile( | |
| 102 ui::SelectFileDialog::SELECT_FOLDER, | |
| 103 base::string16(), // TODO(thestig) Add a title. | |
|
vandebo (ex-Chrome)
2013/12/18 20:15:53
This is a privacy requirement. Probably should pu
Lei Zhang
2014/01/08 23:49:54
Done. Re-using the one from the media galleries pe
| |
| 104 default_path, | |
| 105 NULL, | |
| 106 0, | |
| 107 base::FilePath::StringType(), | |
| 108 platform_util::GetTopLevel(web_contents_->GetView()->GetNativeView()), | |
| 109 NULL); | |
| 110 } | |
| 111 | |
| 112 // ui::SelectFileDialog::Listener implementation. | |
| 113 virtual void FileSelected(const base::FilePath& path, | |
| 114 int index, | |
| 115 void* params) OVERRIDE { | |
| 116 callback_.Run(path); | |
| 117 Release(); // Balanced in Show(). | |
| 118 } | |
| 119 | |
| 120 virtual void MultiFilesSelected(const std::vector<base::FilePath>& files, | |
| 121 void* params) OVERRIDE { | |
| 122 NOTREACHED() << "Should not be able to select multiple files"; | |
| 123 } | |
| 124 | |
| 125 virtual void FileSelectionCanceled(void* params) OVERRIDE { | |
| 126 callback_.Run(base::FilePath()); | |
| 127 Release(); // Balanced in Show(). | |
| 128 } | |
| 129 | |
| 130 private: | |
| 131 friend class base::RefCounted<SelectDirectoryDialog>; | |
| 132 virtual ~SelectDirectoryDialog() {} | |
| 133 | |
| 134 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; | |
| 135 WebContents* web_contents_; | |
| 136 Callback callback_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(SelectDirectoryDialog); | |
| 139 }; | |
| 140 | |
| 77 } // namespace | 141 } // namespace |
| 78 | 142 |
| 79 MediaGalleriesGetMediaFileSystemsFunction:: | 143 MediaGalleriesGetMediaFileSystemsBase:: |
| 80 ~MediaGalleriesGetMediaFileSystemsFunction() {} | 144 ~MediaGalleriesGetMediaFileSystemsBase() {} |
| 81 | 145 |
| 82 bool MediaGalleriesGetMediaFileSystemsFunction::RunImpl() { | 146 void MediaGalleriesGetMediaFileSystemsBase::ReadPrefsAndShowDialog() { |
| 83 if (!ApiIsAccessible(&error_)) | 147 // The MediaFileSystemRegistry only updates preferences for extensions |
| 84 return false; | 148 // that it knows are in use. Since this may be the first call to |
| 85 | 149 // chrome.{getMediaFileSystems, manageMediaLocations} for this extension, |
| 86 media_galleries::UsageCount(media_galleries::GET_MEDIA_FILE_SYSTEMS); | 150 // call GetMediaFileSystemsForExtension() here solely so that |
| 87 scoped_ptr<GetMediaFileSystems::Params> params( | 151 // MediaFileSystemRegistry will send preference changes. |
| 88 GetMediaFileSystems::Params::Create(*args_)); | 152 GetMediaFileSystemsForExtension(base::Bind( |
| 89 EXTENSION_FUNCTION_VALIDATE(params.get()); | 153 &MediaGalleriesGetMediaFileSystemsBase::AlwaysShowDialog, this)); |
| 90 MediaGalleries::GetMediaFileSystemsInteractivity interactive = | |
| 91 MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NO; | |
| 92 if (params->details.get() && params->details->interactive != MediaGalleries:: | |
| 93 GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NONE) { | |
| 94 interactive = params->details->interactive; | |
| 95 } | |
| 96 | |
| 97 MediaGalleriesPreferences* preferences = | |
| 98 media_file_system_registry()->GetPreferences(GetProfile()); | |
| 99 preferences->EnsureInitialized(base::Bind( | |
| 100 &MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit, | |
| 101 this, | |
| 102 interactive)); | |
| 103 return true; | |
| 104 } | 154 } |
| 105 | 155 |
| 106 void MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit( | 156 void MediaGalleriesGetMediaFileSystemsBase::ShowDialog() { |
| 107 MediaGalleries::GetMediaFileSystemsInteractivity interactive) { | 157 media_galleries::UsageCount(media_galleries::SHOW_DIALOG); |
| 108 switch (interactive) { | |
| 109 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_YES: { | |
| 110 // The MediaFileSystemRegistry only updates preferences for extensions | |
| 111 // that it knows are in use. Since this may be the first call to | |
| 112 // chrome.getMediaFileSystems for this extension, call | |
| 113 // GetMediaFileSystemsForExtension() here solely so that | |
| 114 // MediaFileSystemRegistry will send preference changes. | |
| 115 GetMediaFileSystemsForExtension(base::Bind( | |
| 116 &MediaGalleriesGetMediaFileSystemsFunction::AlwaysShowDialog, this)); | |
| 117 return; | |
| 118 } | |
| 119 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_IF_NEEDED: { | |
| 120 GetMediaFileSystemsForExtension(base::Bind( | |
| 121 &MediaGalleriesGetMediaFileSystemsFunction::ShowDialogIfNoGalleries, | |
| 122 this)); | |
| 123 return; | |
| 124 } | |
| 125 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NO: | |
| 126 GetAndReturnGalleries(); | |
| 127 return; | |
| 128 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NONE: | |
| 129 NOTREACHED(); | |
| 130 } | |
| 131 SendResponse(false); | |
| 132 } | |
| 133 | 158 |
| 134 void MediaGalleriesGetMediaFileSystemsFunction::AlwaysShowDialog( | 159 WebContents* contents = GetWebContents(); |
| 135 const std::vector<MediaFileSystemInfo>& /*filesystems*/) { | 160 if (!contents) { |
| 136 ShowDialog(); | |
| 137 } | |
| 138 | |
| 139 void MediaGalleriesGetMediaFileSystemsFunction::ShowDialogIfNoGalleries( | |
| 140 const std::vector<MediaFileSystemInfo>& filesystems) { | |
| 141 if (filesystems.empty()) | |
| 142 ShowDialog(); | |
| 143 else | |
| 144 ReturnGalleries(filesystems); | |
| 145 } | |
| 146 | |
| 147 void MediaGalleriesGetMediaFileSystemsFunction::GetAndReturnGalleries() { | |
| 148 GetMediaFileSystemsForExtension(base::Bind( | |
| 149 &MediaGalleriesGetMediaFileSystemsFunction::ReturnGalleries, this)); | |
| 150 } | |
| 151 | |
| 152 void MediaGalleriesGetMediaFileSystemsFunction::ReturnGalleries( | |
| 153 const std::vector<MediaFileSystemInfo>& filesystems) { | |
| 154 content::RenderViewHost* rvh = render_view_host(); | |
| 155 if (!rvh) { | |
| 156 SendResponse(false); | 161 SendResponse(false); |
| 157 return; | 162 return; |
| 158 } | 163 } |
| 164 | |
| 165 // Controller will delete itself. | |
| 166 new MediaGalleriesDialogController( | |
| 167 contents, *GetExtension(), base::Bind( | |
| 168 &MediaGalleriesGetMediaFileSystemsBase::GetAndReturnGalleries, this)); | |
| 169 } | |
| 170 | |
| 171 void MediaGalleriesGetMediaFileSystemsBase::GetAndReturnGalleries() { | |
| 172 GetMediaFileSystemsForExtension(base::Bind( | |
| 173 &MediaGalleriesGetMediaFileSystemsBase::ReturnGalleries, this)); | |
| 174 } | |
| 175 | |
| 176 void MediaGalleriesGetMediaFileSystemsBase::ReturnGalleries( | |
| 177 const std::vector<MediaFileSystemInfo>& filesystems) { | |
| 178 scoped_ptr<base::ListValue> list(ConstructFileSystemList(filesystems)); | |
| 179 if (!list.get()) { | |
| 180 SendResponse(false); | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 SetResult(list.release()); | |
| 185 SendResponse(true); | |
| 186 } | |
| 187 | |
| 188 base::ListValue* MediaGalleriesGetMediaFileSystemsBase::ConstructFileSystemList( | |
| 189 const std::vector<MediaFileSystemInfo>& filesystems) { | |
| 190 content::RenderViewHost* rvh = render_view_host(); | |
| 191 if (!rvh) | |
| 192 return NULL; | |
| 193 | |
| 159 MediaGalleriesPermission::CheckParam read_param( | 194 MediaGalleriesPermission::CheckParam read_param( |
| 160 MediaGalleriesPermission::kReadPermission); | 195 MediaGalleriesPermission::kReadPermission); |
| 161 bool has_read_permission = PermissionsData::CheckAPIPermissionWithParam( | 196 bool has_read_permission = PermissionsData::CheckAPIPermissionWithParam( |
| 162 GetExtension(), APIPermission::kMediaGalleries, &read_param); | 197 GetExtension(), APIPermission::kMediaGalleries, &read_param); |
| 163 MediaGalleriesPermission::CheckParam copy_to_param( | 198 MediaGalleriesPermission::CheckParam copy_to_param( |
| 164 MediaGalleriesPermission::kCopyToPermission); | 199 MediaGalleriesPermission::kCopyToPermission); |
| 165 bool has_copy_to_permission = PermissionsData::CheckAPIPermissionWithParam( | 200 bool has_copy_to_permission = PermissionsData::CheckAPIPermissionWithParam( |
| 166 GetExtension(), APIPermission::kMediaGalleries, ©_to_param); | 201 GetExtension(), APIPermission::kMediaGalleries, ©_to_param); |
| 167 MediaGalleriesPermission::CheckParam delete_param( | 202 MediaGalleriesPermission::CheckParam delete_param( |
| 168 MediaGalleriesPermission::kDeletePermission); | 203 MediaGalleriesPermission::kDeletePermission); |
| 169 bool has_delete_permission = PermissionsData::CheckAPIPermissionWithParam( | 204 bool has_delete_permission = PermissionsData::CheckAPIPermissionWithParam( |
| 170 GetExtension(), APIPermission::kMediaGalleries, &delete_param); | 205 GetExtension(), APIPermission::kMediaGalleries, &delete_param); |
| 171 | 206 |
| 172 const int child_id = rvh->GetProcess()->GetID(); | 207 const int child_id = rvh->GetProcess()->GetID(); |
| 173 base::ListValue* list = new base::ListValue(); | 208 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 174 for (size_t i = 0; i < filesystems.size(); i++) { | 209 for (size_t i = 0; i < filesystems.size(); i++) { |
| 175 scoped_ptr<base::DictionaryValue> file_system_dict_value( | 210 scoped_ptr<base::DictionaryValue> file_system_dict_value( |
| 176 new base::DictionaryValue()); | 211 new base::DictionaryValue()); |
| 177 | 212 |
| 178 // Send the file system id so the renderer can create a valid FileSystem | 213 // Send the file system id so the renderer can create a valid FileSystem |
| 179 // object. | 214 // object. |
| 180 file_system_dict_value->SetStringWithoutPathExpansion( | 215 file_system_dict_value->SetStringWithoutPathExpansion( |
| 181 "fsid", filesystems[i].fsid); | 216 "fsid", filesystems[i].fsid); |
| 182 | 217 |
| 183 file_system_dict_value->SetStringWithoutPathExpansion( | 218 file_system_dict_value->SetStringWithoutPathExpansion( |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 207 policy->GrantReadFileSystem(child_id, filesystems[i].fsid); | 242 policy->GrantReadFileSystem(child_id, filesystems[i].fsid); |
| 208 if (has_delete_permission) { | 243 if (has_delete_permission) { |
| 209 policy->GrantDeleteFromFileSystem(child_id, filesystems[i].fsid); | 244 policy->GrantDeleteFromFileSystem(child_id, filesystems[i].fsid); |
| 210 if (has_copy_to_permission) { | 245 if (has_copy_to_permission) { |
| 211 policy->GrantCopyIntoFileSystem(child_id, filesystems[i].fsid); | 246 policy->GrantCopyIntoFileSystem(child_id, filesystems[i].fsid); |
| 212 } | 247 } |
| 213 } | 248 } |
| 214 } | 249 } |
| 215 } | 250 } |
| 216 | 251 |
| 217 // The custom JS binding will use this list to create DOMFileSystem objects. | 252 return list.release(); |
| 218 SetResult(list); | |
| 219 SendResponse(true); | |
| 220 } | 253 } |
| 221 | 254 |
| 222 void MediaGalleriesGetMediaFileSystemsFunction::ShowDialog() { | 255 void MediaGalleriesGetMediaFileSystemsBase::GetMediaFileSystemsForExtension( |
| 223 media_galleries::UsageCount(media_galleries::SHOW_DIALOG); | |
| 224 WebContents* contents = WebContents::FromRenderViewHost(render_view_host()); | |
| 225 WebContentsModalDialogManager* web_contents_modal_dialog_manager = | |
| 226 WebContentsModalDialogManager::FromWebContents(contents); | |
| 227 if (!web_contents_modal_dialog_manager) { | |
| 228 // If there is no WebContentsModalDialogManager, then this contents is | |
| 229 // probably the background page for an app. Try to find a shell window to | |
| 230 // host the dialog. | |
| 231 apps::ShellWindow* window = apps::ShellWindowRegistry::Get( | |
| 232 GetProfile())->GetCurrentShellWindowForApp(GetExtension()->id()); | |
| 233 if (!window) { | |
| 234 SendResponse(false); | |
| 235 return; | |
| 236 } | |
| 237 contents = window->web_contents(); | |
| 238 } | |
| 239 | |
| 240 // Controller will delete itself. | |
| 241 base::Closure cb = base::Bind( | |
| 242 &MediaGalleriesGetMediaFileSystemsFunction::GetAndReturnGalleries, this); | |
| 243 new MediaGalleriesDialogController(contents, *GetExtension(), cb); | |
| 244 } | |
| 245 | |
| 246 void MediaGalleriesGetMediaFileSystemsFunction::GetMediaFileSystemsForExtension( | |
| 247 const MediaFileSystemsCallback& cb) { | 256 const MediaFileSystemsCallback& cb) { |
| 248 if (!render_view_host()) { | 257 if (!render_view_host()) { |
| 249 cb.Run(std::vector<MediaFileSystemInfo>()); | 258 cb.Run(std::vector<MediaFileSystemInfo>()); |
| 250 return; | 259 return; |
| 251 } | 260 } |
| 252 MediaFileSystemRegistry* registry = media_file_system_registry(); | 261 DCHECK(GetMediaGalleriesPreferences()->IsInitialized()); |
| 253 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); | 262 media_file_system_registry()->GetMediaFileSystemsForExtension( |
| 254 registry->GetMediaFileSystemsForExtension( | |
| 255 render_view_host(), GetExtension(), cb); | 263 render_view_host(), GetExtension(), cb); |
| 256 } | 264 } |
| 257 | 265 |
| 266 WebContents* MediaGalleriesGetMediaFileSystemsBase::GetWebContents() { | |
| 267 WebContents* contents = WebContents::FromRenderViewHost(render_view_host()); | |
| 268 WebContentsModalDialogManager* web_contents_modal_dialog_manager = | |
| 269 WebContentsModalDialogManager::FromWebContents(contents); | |
| 270 if (web_contents_modal_dialog_manager) | |
| 271 return contents; | |
| 272 | |
| 273 // If there is no WebContentsModalDialogManager, then this contents is | |
| 274 // probably the background page for an app. Try to find a shell window to | |
| 275 // host the dialog. | |
| 276 apps::ShellWindow* window = apps::ShellWindowRegistry::Get( | |
| 277 GetProfile())->GetCurrentShellWindowForApp(GetExtension()->id()); | |
| 278 return window ? window->web_contents() : NULL; | |
| 279 } | |
| 280 | |
| 281 MediaGalleriesPreferences* | |
| 282 MediaGalleriesGetMediaFileSystemsBase::GetMediaGalleriesPreferences() { | |
| 283 return media_file_system_registry()->GetPreferences(GetProfile()); | |
| 284 } | |
| 285 | |
| 286 void MediaGalleriesGetMediaFileSystemsBase::AlwaysShowDialog( | |
| 287 const std::vector<MediaFileSystemInfo>& /*filesystems*/) { | |
| 288 ShowDialog(); | |
| 289 } | |
| 290 | |
| 291 | |
| 292 MediaGalleriesGetMediaFileSystemsFunction:: | |
| 293 ~MediaGalleriesGetMediaFileSystemsFunction() {} | |
| 294 | |
| 295 bool MediaGalleriesGetMediaFileSystemsFunction::RunImpl() { | |
| 296 if (!ApiIsAccessible(&error_)) | |
| 297 return false; | |
| 298 | |
| 299 media_galleries::UsageCount(media_galleries::GET_MEDIA_FILE_SYSTEMS); | |
| 300 scoped_ptr<GetMediaFileSystems::Params> params( | |
| 301 GetMediaFileSystems::Params::Create(*args_)); | |
| 302 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 303 MediaGalleries::GetMediaFileSystemsInteractivity interactive = | |
| 304 MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NO; | |
| 305 if (params->details.get() && params->details->interactive != MediaGalleries:: | |
| 306 GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NONE) { | |
| 307 interactive = params->details->interactive; | |
| 308 } | |
| 309 | |
| 310 MediaGalleriesPreferences* preferences = GetMediaGalleriesPreferences(); | |
| 311 preferences->EnsureInitialized(base::Bind( | |
| 312 &MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit, | |
| 313 this, | |
| 314 interactive)); | |
| 315 return true; | |
| 316 } | |
| 317 | |
| 318 void MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit( | |
| 319 MediaGalleries::GetMediaFileSystemsInteractivity interactive) { | |
| 320 switch (interactive) { | |
| 321 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_YES: { | |
| 322 ReadPrefsAndShowDialog(); | |
|
vandebo (ex-Chrome)
2013/12/18 20:15:53
Shouldn't this just be ShowDialog since the prefs
| |
| 323 return; | |
| 324 } | |
| 325 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_IF_NEEDED: { | |
| 326 GetMediaFileSystemsForExtension(base::Bind( | |
| 327 &MediaGalleriesGetMediaFileSystemsFunction::ShowDialogIfNoGalleries, | |
| 328 this)); | |
| 329 return; | |
| 330 } | |
| 331 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NO: | |
| 332 GetAndReturnGalleries(); | |
| 333 return; | |
| 334 case MediaGalleries::GET_MEDIA_FILE_SYSTEMS_INTERACTIVITY_NONE: | |
| 335 NOTREACHED(); | |
| 336 } | |
| 337 SendResponse(false); | |
| 338 } | |
| 339 | |
| 340 void MediaGalleriesGetMediaFileSystemsFunction::ShowDialogIfNoGalleries( | |
| 341 const std::vector<MediaFileSystemInfo>& filesystems) { | |
| 342 if (filesystems.empty()) | |
| 343 ShowDialog(); | |
| 344 else | |
| 345 ReturnGalleries(filesystems); | |
| 346 } | |
| 347 | |
| 348 | |
| 258 MediaGalleriesGetAllMediaFileSystemMetadataFunction:: | 349 MediaGalleriesGetAllMediaFileSystemMetadataFunction:: |
| 259 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() {} | 350 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() {} |
| 260 | 351 |
| 261 bool MediaGalleriesGetAllMediaFileSystemMetadataFunction::RunImpl() { | 352 bool MediaGalleriesGetAllMediaFileSystemMetadataFunction::RunImpl() { |
| 262 if (!ApiIsAccessible(&error_)) | 353 if (!ApiIsAccessible(&error_)) |
| 263 return false; | 354 return false; |
| 264 | 355 |
| 265 media_galleries::UsageCount( | 356 media_galleries::UsageCount( |
| 266 media_galleries::GET_ALL_MEDIA_FILE_SYSTEM_METADATA); | 357 media_galleries::GET_ALL_MEDIA_FILE_SYSTEM_METADATA); |
| 267 MediaGalleriesPreferences* preferences = | 358 MediaGalleriesPreferences* preferences = |
| 268 media_file_system_registry()->GetPreferences(GetProfile()); | 359 media_file_system_registry()->GetPreferences(GetProfile()); |
| 269 preferences->EnsureInitialized(base::Bind( | 360 preferences->EnsureInitialized(base::Bind( |
| 270 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit, | 361 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit, |
| 271 this)); | 362 this)); |
| 272 return true; | 363 return true; |
| 273 } | 364 } |
| 274 | 365 |
| 275 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit() { | 366 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit() { |
| 276 MediaFileSystemRegistry* registry = media_file_system_registry(); | 367 MediaGalleriesPreferences* prefs = |
| 277 MediaGalleriesPreferences* prefs = registry->GetPreferences(GetProfile()); | 368 media_file_system_registry()->GetPreferences(GetProfile()); |
| 278 DCHECK(prefs->IsInitialized()); | 369 DCHECK(prefs->IsInitialized()); |
| 279 MediaGalleryPrefIdSet permitted_gallery_ids = | 370 MediaGalleryPrefIdSet permitted_gallery_ids = |
| 280 prefs->GalleriesForExtension(*GetExtension()); | 371 prefs->GalleriesForExtension(*GetExtension()); |
| 281 | 372 |
| 282 MediaStorageUtil::DeviceIdSet* device_ids = new MediaStorageUtil::DeviceIdSet; | 373 MediaStorageUtil::DeviceIdSet* device_ids = new MediaStorageUtil::DeviceIdSet; |
| 283 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); | 374 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); |
| 284 for (MediaGalleryPrefIdSet::const_iterator it = permitted_gallery_ids.begin(); | 375 for (MediaGalleryPrefIdSet::const_iterator it = permitted_gallery_ids.begin(); |
| 285 it != permitted_gallery_ids.end(); ++it) { | 376 it != permitted_gallery_ids.end(); ++it) { |
| 286 MediaGalleriesPrefInfoMap::const_iterator gallery_it = galleries.find(*it); | 377 MediaGalleriesPrefInfoMap::const_iterator gallery_it = galleries.find(*it); |
| 287 DCHECK(gallery_it != galleries.end()); | 378 DCHECK(gallery_it != galleries.end()); |
| 288 device_ids->insert(gallery_it->second.device_id); | 379 device_ids->insert(gallery_it->second.device_id); |
| 289 } | 380 } |
| 290 | 381 |
| 291 MediaStorageUtil::FilterAttachedDevices( | 382 MediaStorageUtil::FilterAttachedDevices( |
| 292 device_ids, | 383 device_ids, |
| 293 base::Bind( | 384 base::Bind( |
| 294 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnGetGalleries, | 385 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnGetGalleries, |
| 295 this, | 386 this, |
| 296 permitted_gallery_ids, | 387 permitted_gallery_ids, |
| 297 base::Owned(device_ids))); | 388 base::Owned(device_ids))); |
| 298 } | 389 } |
| 299 | 390 |
| 300 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnGetGalleries( | 391 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnGetGalleries( |
| 301 const MediaGalleryPrefIdSet& permitted_gallery_ids, | 392 const MediaGalleryPrefIdSet& permitted_gallery_ids, |
| 302 const MediaStorageUtil::DeviceIdSet* available_devices) { | 393 const MediaStorageUtil::DeviceIdSet* available_devices) { |
| 303 MediaFileSystemRegistry* registry = media_file_system_registry(); | 394 MediaGalleriesPreferences* prefs = |
| 304 MediaGalleriesPreferences* prefs = registry->GetPreferences(GetProfile()); | 395 media_file_system_registry()->GetPreferences(GetProfile()); |
| 305 | 396 |
| 306 base::ListValue* list = new base::ListValue(); | 397 base::ListValue* list = new base::ListValue(); |
| 307 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); | 398 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); |
| 308 for (MediaGalleryPrefIdSet::const_iterator it = permitted_gallery_ids.begin(); | 399 for (MediaGalleryPrefIdSet::const_iterator it = permitted_gallery_ids.begin(); |
| 309 it != permitted_gallery_ids.end(); ++it) { | 400 it != permitted_gallery_ids.end(); ++it) { |
| 310 MediaGalleriesPrefInfoMap::const_iterator gallery_it = galleries.find(*it); | 401 MediaGalleriesPrefInfoMap::const_iterator gallery_it = galleries.find(*it); |
| 311 DCHECK(gallery_it != galleries.end()); | 402 DCHECK(gallery_it != galleries.end()); |
| 312 const MediaGalleryPrefInfo& gallery = gallery_it->second; | 403 const MediaGalleryPrefInfo& gallery = gallery_it->second; |
| 313 MediaGalleries::MediaFileSystemMetadata metadata; | 404 MediaGalleries::MediaFileSystemMetadata metadata; |
| 314 metadata.name = base::UTF16ToUTF8(gallery.GetGalleryDisplayName()); | 405 metadata.name = base::UTF16ToUTF8(gallery.GetGalleryDisplayName()); |
| 315 metadata.gallery_id = base::Uint64ToString(gallery.pref_id); | 406 metadata.gallery_id = base::Uint64ToString(gallery.pref_id); |
| 316 metadata.is_removable = StorageInfo::IsRemovableDevice(gallery.device_id); | 407 metadata.is_removable = StorageInfo::IsRemovableDevice(gallery.device_id); |
| 317 metadata.is_media_device = StorageInfo::IsMediaDevice(gallery.device_id); | 408 metadata.is_media_device = StorageInfo::IsMediaDevice(gallery.device_id); |
| 318 metadata.is_available = ContainsKey(*available_devices, gallery.device_id); | 409 metadata.is_available = ContainsKey(*available_devices, gallery.device_id); |
| 319 list->Append(metadata.ToValue().release()); | 410 list->Append(metadata.ToValue().release()); |
| 320 } | 411 } |
| 321 | 412 |
| 322 SetResult(list); | 413 SetResult(list); |
| 323 SendResponse(true); | 414 SendResponse(true); |
| 324 } | 415 } |
| 325 | 416 |
| 417 MediaGalleriesManageMediaLocationsFunction:: | |
| 418 ~MediaGalleriesManageMediaLocationsFunction() {} | |
| 419 | |
| 420 bool MediaGalleriesManageMediaLocationsFunction::RunImpl() { | |
| 421 if (!ApiIsAccessible(&error_)) | |
| 422 return false; | |
| 423 | |
| 424 media_galleries::UsageCount(media_galleries::MANAGE_MEDIA_LOCATIONS); | |
| 425 scoped_ptr<ManageMediaLocations::Params> params( | |
| 426 ManageMediaLocations::Params::Create(*args_)); | |
| 427 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 428 if (params->details.type == MediaGalleries::MANAGE_REQUEST_TYPE_NONE) | |
| 429 EXTENSION_FUNCTION_ERROR(kInvalidManageRequestType); // returns false | |
| 430 | |
| 431 MediaGalleriesPreferences* preferences = GetMediaGalleriesPreferences(); | |
| 432 preferences->EnsureInitialized(base::Bind( | |
| 433 &MediaGalleriesManageMediaLocationsFunction::OnPreferencesInit, | |
| 434 this, | |
| 435 params->details.type)); | |
| 436 return true; | |
| 437 } | |
| 438 | |
| 439 void MediaGalleriesManageMediaLocationsFunction::OnPreferencesInit( | |
| 440 MediaGalleries::ManageRequestType request_type) { | |
| 441 switch (request_type) { | |
| 442 case MediaGalleries::MANAGE_REQUEST_TYPE_MANAGE: { | |
| 443 ReadPrefsAndShowDialog(); | |
| 444 return; | |
| 445 } | |
| 446 case MediaGalleries::MANAGE_REQUEST_TYPE_DIRECTORY_PROMPT: { | |
| 447 WebContents* contents = GetWebContents(); | |
| 448 if (!contents) { | |
| 449 SendResponse(false); | |
| 450 return; | |
| 451 } | |
| 452 base::FilePath last_used_path = | |
| 453 extensions::file_system_api::GetLastChooseEntryDirectory( | |
| 454 extensions::ExtensionPrefs::Get(GetProfile()), | |
| 455 GetExtension()->id()); | |
| 456 SelectDirectoryDialog::Callback cb = base::Bind( | |
| 457 &MediaGalleriesManageMediaLocationsFunction::OnDirectorySelected, | |
| 458 this); | |
| 459 scoped_refptr<SelectDirectoryDialog> select_directory_dialog = | |
| 460 new SelectDirectoryDialog(contents, cb); | |
| 461 select_directory_dialog->Show(last_used_path); | |
| 462 return; | |
| 463 } | |
| 464 case MediaGalleries::MANAGE_REQUEST_TYPE_NONE: | |
| 465 NOTREACHED(); | |
| 466 } | |
| 467 SendResponse(false); | |
| 468 } | |
| 469 | |
| 470 void MediaGalleriesManageMediaLocationsFunction::OnDirectorySelected( | |
| 471 const base::FilePath& selected_directory) { | |
| 472 if (selected_directory.empty()) { | |
| 473 // User cancelled case. | |
| 474 GetAndReturnGalleries(); | |
| 475 return; | |
| 476 } | |
| 477 | |
| 478 extensions::file_system_api::SetLastChooseEntryDirectory( | |
| 479 extensions::ExtensionPrefs::Get(GetProfile()), | |
| 480 GetExtension()->id(), | |
| 481 selected_directory); | |
| 482 | |
| 483 MediaGalleryPrefInfo gallery; | |
| 484 MediaGalleriesPreferences* preferences = GetMediaGalleriesPreferences(); | |
| 485 MediaGalleryPrefId pref_id = | |
| 486 preferences->AddGalleryByPath(selected_directory); | |
| 487 preferences->SetGalleryPermissionForExtension(*GetExtension(), pref_id, true); | |
| 488 | |
| 489 GetMediaFileSystemsForExtension(base::Bind( | |
| 490 &MediaGalleriesManageMediaLocationsFunction::ReturnGalleriesAndId, | |
| 491 this, | |
| 492 pref_id)); | |
| 493 } | |
| 494 | |
| 495 void MediaGalleriesManageMediaLocationsFunction::ReturnGalleriesAndId( | |
| 496 MediaGalleryPrefId pref_id, | |
| 497 const std::vector<MediaFileSystemInfo>& filesystems) { | |
| 498 scoped_ptr<base::ListValue> list(ConstructFileSystemList(filesystems)); | |
| 499 if (!list.get()) { | |
| 500 SendResponse(false); | |
| 501 return; | |
| 502 } | |
| 503 | |
| 504 std::string fsid; | |
| 505 for (size_t i = 0; i < filesystems.size(); ++i) { | |
| 506 if (filesystems[i].pref_id == pref_id) { | |
| 507 fsid = filesystems[i].fsid; | |
|
vandebo (ex-Chrome)
2013/12/18 20:15:53
If the just added directory "isn't attached," we w
| |
| 508 break; | |
| 509 } | |
| 510 } | |
| 511 list->AppendString(fsid); | |
| 512 SetResult(list.release()); | |
| 513 SendResponse(true); | |
| 514 } | |
| 515 | |
| 326 } // namespace extensions | 516 } // namespace extensions |
| OLD | NEW |