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" |
| 22 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 23 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
| 23 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" | 24 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" |
| 24 #include "chrome/browser/media_galleries/media_galleries_histograms.h" | 25 #include "chrome/browser/media_galleries/media_galleries_histograms.h" |
| 25 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 26 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
| 27 #include "chrome/browser/platform_util.h" | |
| 26 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/browser/storage_monitor/storage_info.h" | 29 #include "chrome/browser/storage_monitor/storage_info.h" |
| 28 #include "chrome/browser/ui/chrome_select_file_policy.h" | 30 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 29 #include "chrome/common/extensions/api/media_galleries.h" | 31 #include "chrome/common/extensions/api/media_galleries.h" |
| 30 #include "chrome/common/extensions/permissions/media_galleries_permission.h" | 32 #include "chrome/common/extensions/permissions/media_galleries_permission.h" |
| 31 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
| 32 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 34 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 35 #include "content/public/browser/browser_thread.h" | |
| 33 #include "content/public/browser/child_process_security_policy.h" | 36 #include "content/public/browser/child_process_security_policy.h" |
| 34 #include "content/public/browser/render_process_host.h" | 37 #include "content/public/browser/render_process_host.h" |
| 35 #include "content/public/browser/render_view_host.h" | 38 #include "content/public/browser/render_view_host.h" |
| 36 #include "content/public/browser/web_contents.h" | 39 #include "content/public/browser/web_contents.h" |
| 40 #include "content/public/browser/web_contents_view.h" | |
| 37 #include "extensions/common/extension.h" | 41 #include "extensions/common/extension.h" |
| 38 #include "extensions/common/permissions/api_permission.h" | 42 #include "extensions/common/permissions/api_permission.h" |
| 39 #include "extensions/common/permissions/permissions_data.h" | 43 #include "extensions/common/permissions/permissions_data.h" |
| 40 | 44 |
| 41 using content::WebContents; | 45 using content::WebContents; |
| 42 using web_modal::WebContentsModalDialogManager; | 46 using web_modal::WebContentsModalDialogManager; |
| 43 | 47 |
| 44 namespace extensions { | 48 namespace extensions { |
| 45 | 49 |
| 46 namespace MediaGalleries = api::media_galleries; | 50 namespace MediaGalleries = api::media_galleries; |
| 47 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; | 51 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; |
| 52 namespace ManageMediaLocations = MediaGalleries::ManageMediaLocations; | |
| 48 | 53 |
| 49 namespace { | 54 namespace { |
| 50 | 55 |
| 51 const char kDisallowedByPolicy[] = | 56 const char kDisallowedByPolicy[] = |
| 52 "Media Galleries API is disallowed by policy: "; | 57 "Media Galleries API is disallowed by policy: "; |
| 58 const char kInvalidManageRequestType[] = | |
| 59 "Invalid manageMediaLocations request type"; | |
| 53 | 60 |
| 54 const char kDeviceIdKey[] = "deviceId"; | 61 const char kDeviceIdKey[] = "deviceId"; |
| 55 const char kGalleryIdKey[] = "galleryId"; | 62 const char kGalleryIdKey[] = "galleryId"; |
| 56 const char kIsAvailableKey[] = "isAvailable"; | 63 const char kIsAvailableKey[] = "isAvailable"; |
| 57 const char kIsMediaDeviceKey[] = "isMediaDevice"; | 64 const char kIsMediaDeviceKey[] = "isMediaDevice"; |
| 58 const char kIsRemovableKey[] = "isRemovable"; | 65 const char kIsRemovableKey[] = "isRemovable"; |
| 59 const char kNameKey[] = "name"; | 66 const char kNameKey[] = "name"; |
| 60 | 67 |
| 61 // Checks whether the MediaGalleries API is currently accessible (it may be | 68 // Checks whether the MediaGalleries API is currently accessible (it may be |
| 62 // disallowed even if an extension has the requisite permission). | 69 // disallowed even if an extension has the requisite permission). |
| 63 bool ApiIsAccessible(std::string* error) { | 70 bool ApiIsAccessible(std::string* error) { |
| 64 if (!ChromeSelectFilePolicy::FileSelectDialogsAllowed()) { | 71 if (!ChromeSelectFilePolicy::FileSelectDialogsAllowed()) { |
| 65 *error = std::string(kDisallowedByPolicy) + | 72 *error = std::string(kDisallowedByPolicy) + |
| 66 prefs::kAllowFileSelectionDialogs; | 73 prefs::kAllowFileSelectionDialogs; |
| 67 return false; | 74 return false; |
| 68 } | 75 } |
| 69 | 76 |
| 70 return true; | 77 return true; |
| 71 } | 78 } |
| 72 | 79 |
| 73 MediaFileSystemRegistry* media_file_system_registry() { | 80 MediaFileSystemRegistry* media_file_system_registry() { |
| 74 return g_browser_process->media_file_system_registry(); | 81 return g_browser_process->media_file_system_registry(); |
| 75 } | 82 } |
| 76 | 83 |
| 84 class SelectDirectoryDialog : public ui::SelectFileDialog::Listener, | |
| 85 public base::RefCounted<SelectDirectoryDialog> { | |
| 86 public: | |
| 87 // Selected file path, or an empty path if the user canceled. | |
| 88 typedef base::Callback<void(const base::FilePath&)> Callback; | |
| 89 | |
| 90 SelectDirectoryDialog(WebContents* web_contents, const Callback& callback) | |
| 91 : web_contents_(web_contents), | |
| 92 callback_(callback) { | |
| 93 select_file_dialog_ = ui::SelectFileDialog::Create( | |
| 94 this, new ChromeSelectFilePolicy(web_contents)); | |
| 95 } | |
| 96 | |
| 97 void Show() { | |
| 98 AddRef(); // Balanced in the two reachable listener outcomes. | |
| 99 select_file_dialog_->SelectFile( | |
| 100 ui::SelectFileDialog::SELECT_FOLDER, | |
| 101 base::string16(), | |
| 102 base::FilePath(), | |
| 103 NULL, | |
| 104 0, | |
| 105 base::FilePath::StringType(), | |
| 106 platform_util::GetTopLevel(web_contents_->GetView()->GetNativeView()), | |
| 107 NULL); | |
| 108 } | |
| 109 | |
| 110 // ui::SelectFileDialog::Listener implementation. | |
| 111 virtual void FileSelected(const base::FilePath& path, | |
| 112 int index, | |
| 113 void* params) OVERRIDE { | |
| 114 callback_.Run(path); | |
| 115 Release(); // Balanced in Show(). | |
| 116 } | |
| 117 | |
| 118 virtual void MultiFilesSelected(const std::vector<base::FilePath>& files, | |
| 119 void* params) OVERRIDE { | |
| 120 NOTREACHED() << "Should not be able to select multiple files"; | |
| 121 } | |
| 122 | |
| 123 virtual void FileSelectionCanceled(void* params) OVERRIDE { | |
| 124 callback_.Run(base::FilePath()); | |
| 125 Release(); // Balanced in Show(). | |
| 126 } | |
| 127 | |
| 128 private: | |
| 129 friend class base::RefCounted<SelectDirectoryDialog>; | |
| 130 virtual ~SelectDirectoryDialog() {} | |
| 131 | |
| 132 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; | |
| 133 WebContents* web_contents_; | |
| 134 Callback callback_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(SelectDirectoryDialog); | |
| 137 }; | |
| 138 | |
| 77 } // namespace | 139 } // namespace |
| 78 | 140 |
| 79 MediaGalleriesGetMediaFileSystemsFunction:: | 141 MediaGalleriesGetMediaFileSystemsBase:: |
| 80 ~MediaGalleriesGetMediaFileSystemsFunction() {} | 142 ~MediaGalleriesGetMediaFileSystemsBase() {} |
| 81 | 143 |
| 82 bool MediaGalleriesGetMediaFileSystemsFunction::RunImpl() { | 144 void MediaGalleriesGetMediaFileSystemsBase::ReadPrefsAndShowDialog() { |
| 83 if (!ApiIsAccessible(&error_)) | 145 // The MediaFileSystemRegistry only updates preferences for extensions |
| 84 return false; | 146 // that it knows are in use. Since this may be the first call to |
| 85 | 147 // chrome.{getMediaFileSystems, manageMediaLocations} for this extension, |
| 86 media_galleries::UsageCount(media_galleries::GET_MEDIA_FILE_SYSTEMS); | 148 // call GetMediaFileSystemsForExtension() here solely so that |
| 87 scoped_ptr<GetMediaFileSystems::Params> params( | 149 // MediaFileSystemRegistry will send preference changes. |
| 88 GetMediaFileSystems::Params::Create(*args_)); | 150 GetMediaFileSystemsForExtension(base::Bind( |
| 89 EXTENSION_FUNCTION_VALIDATE(params.get()); | 151 &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 } | 152 } |
| 105 | 153 |
| 106 void MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit( | 154 void MediaGalleriesGetMediaFileSystemsBase::ShowDialog() { |
| 107 MediaGalleries::GetMediaFileSystemsInteractivity interactive) { | 155 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 | 156 |
| 134 void MediaGalleriesGetMediaFileSystemsFunction::AlwaysShowDialog( | 157 WebContents* contents = GetWebContents(); |
| 135 const std::vector<MediaFileSystemInfo>& /*filesystems*/) { | 158 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); | 159 SendResponse(false); |
| 157 return; | 160 return; |
| 158 } | 161 } |
| 162 | |
| 163 // Controller will delete itself. | |
| 164 new MediaGalleriesDialogController( | |
| 165 contents, | |
| 166 *GetExtension(), | |
| 167 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 base::ListValue* list = new base::ListValue(); |
|
vandebo (ex-Chrome)
2013/12/03 21:08:02
nit: this should probably be a scoped_ptr with a r
Lei Zhang
2013/12/05 05:52:15
Done.
| |
| 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; |
| 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 contents = window->web_contents(); | |
| 235 } else { | |
| 236 // Abort showing the dialog. TODO(estade) Perhaps return an error instead. | |
| 237 GetAndReturnGalleries(); | |
| 238 return; | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 // Controller will delete itself. | |
| 243 base::Closure cb = base::Bind( | |
| 244 &MediaGalleriesGetMediaFileSystemsFunction::GetAndReturnGalleries, this); | |
| 245 new MediaGalleriesDialogController(contents, *GetExtension(), cb); | |
| 246 } | |
| 247 | |
| 248 void MediaGalleriesGetMediaFileSystemsFunction::GetMediaFileSystemsForExtension( | |
| 249 const MediaFileSystemsCallback& cb) { | 256 const MediaFileSystemsCallback& cb) { |
| 250 if (!render_view_host()) { | 257 if (!render_view_host()) { |
| 251 cb.Run(std::vector<MediaFileSystemInfo>()); | 258 cb.Run(std::vector<MediaFileSystemInfo>()); |
| 252 return; | 259 return; |
| 253 } | 260 } |
| 254 MediaFileSystemRegistry* registry = media_file_system_registry(); | 261 DCHECK(GetMediaGalleriesPreferences()->IsInitialized()); |
| 255 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); | 262 media_file_system_registry()->GetMediaFileSystemsForExtension( |
| 256 registry->GetMediaFileSystemsForExtension( | |
| 257 render_view_host(), GetExtension(), cb); | 263 render_view_host(), GetExtension(), cb); |
| 258 } | 264 } |
| 259 | 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(); | |
| 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 | |
| 260 MediaGalleriesGetAllMediaFileSystemMetadataFunction:: | 349 MediaGalleriesGetAllMediaFileSystemMetadataFunction:: |
| 261 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() {} | 350 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() {} |
| 262 | 351 |
| 263 bool MediaGalleriesGetAllMediaFileSystemMetadataFunction::RunImpl() { | 352 bool MediaGalleriesGetAllMediaFileSystemMetadataFunction::RunImpl() { |
| 264 if (!ApiIsAccessible(&error_)) | 353 if (!ApiIsAccessible(&error_)) |
| 265 return false; | 354 return false; |
| 266 | 355 |
| 267 media_galleries::UsageCount( | 356 media_galleries::UsageCount( |
| 268 media_galleries::GET_ALL_MEDIA_FILE_SYSTEM_METADATA); | 357 media_galleries::GET_ALL_MEDIA_FILE_SYSTEM_METADATA); |
| 269 MediaGalleriesPreferences* preferences = | 358 MediaGalleriesPreferences* preferences = |
| 270 media_file_system_registry()->GetPreferences(GetProfile()); | 359 media_file_system_registry()->GetPreferences(GetProfile()); |
| 271 preferences->EnsureInitialized(base::Bind( | 360 preferences->EnsureInitialized(base::Bind( |
| 272 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit, | 361 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit, |
| 273 this)); | 362 this)); |
| 274 return true; | 363 return true; |
| 275 } | 364 } |
| 276 | 365 |
| 277 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit() { | 366 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnPreferencesInit() { |
| 278 MediaFileSystemRegistry* registry = media_file_system_registry(); | 367 MediaGalleriesPreferences* prefs = |
| 279 MediaGalleriesPreferences* prefs = registry->GetPreferences(GetProfile()); | 368 media_file_system_registry()->GetPreferences(GetProfile()); |
| 280 DCHECK(prefs->IsInitialized()); | 369 DCHECK(prefs->IsInitialized()); |
| 281 MediaGalleryPrefIdSet permitted_gallery_ids = | 370 MediaGalleryPrefIdSet permitted_gallery_ids = |
| 282 prefs->GalleriesForExtension(*GetExtension()); | 371 prefs->GalleriesForExtension(*GetExtension()); |
| 283 | 372 |
| 284 MediaStorageUtil::DeviceIdSet* device_ids = new MediaStorageUtil::DeviceIdSet; | 373 MediaStorageUtil::DeviceIdSet* device_ids = new MediaStorageUtil::DeviceIdSet; |
| 285 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); | 374 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); |
| 286 for (MediaGalleryPrefIdSet::const_iterator it = permitted_gallery_ids.begin(); | 375 for (MediaGalleryPrefIdSet::const_iterator it = permitted_gallery_ids.begin(); |
| 287 it != permitted_gallery_ids.end(); ++it) { | 376 it != permitted_gallery_ids.end(); ++it) { |
| 288 MediaGalleriesPrefInfoMap::const_iterator gallery_it = galleries.find(*it); | 377 MediaGalleriesPrefInfoMap::const_iterator gallery_it = galleries.find(*it); |
| 289 DCHECK(gallery_it != galleries.end()); | 378 DCHECK(gallery_it != galleries.end()); |
| 290 device_ids->insert(gallery_it->second.device_id); | 379 device_ids->insert(gallery_it->second.device_id); |
| 291 } | 380 } |
| 292 | 381 |
| 293 MediaStorageUtil::FilterAttachedDevices( | 382 MediaStorageUtil::FilterAttachedDevices( |
| 294 device_ids, | 383 device_ids, |
| 295 base::Bind( | 384 base::Bind( |
| 296 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnGetGalleries, | 385 &MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnGetGalleries, |
| 297 this, | 386 this, |
| 298 permitted_gallery_ids, | 387 permitted_gallery_ids, |
| 299 base::Owned(device_ids))); | 388 base::Owned(device_ids))); |
| 300 } | 389 } |
| 301 | 390 |
| 302 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnGetGalleries( | 391 void MediaGalleriesGetAllMediaFileSystemMetadataFunction::OnGetGalleries( |
| 303 const MediaGalleryPrefIdSet& permitted_gallery_ids, | 392 const MediaGalleryPrefIdSet& permitted_gallery_ids, |
| 304 const MediaStorageUtil::DeviceIdSet* available_devices) { | 393 const MediaStorageUtil::DeviceIdSet* available_devices) { |
| 305 MediaFileSystemRegistry* registry = media_file_system_registry(); | 394 MediaGalleriesPreferences* prefs = |
| 306 MediaGalleriesPreferences* prefs = registry->GetPreferences(GetProfile()); | 395 media_file_system_registry()->GetPreferences(GetProfile()); |
| 307 | 396 |
| 308 base::ListValue* list = new base::ListValue(); | 397 base::ListValue* list = new base::ListValue(); |
| 309 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); | 398 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); |
| 310 for (MediaGalleryPrefIdSet::const_iterator it = permitted_gallery_ids.begin(); | 399 for (MediaGalleryPrefIdSet::const_iterator it = permitted_gallery_ids.begin(); |
| 311 it != permitted_gallery_ids.end(); ++it) { | 400 it != permitted_gallery_ids.end(); ++it) { |
| 312 MediaGalleriesPrefInfoMap::const_iterator gallery_it = galleries.find(*it); | 401 MediaGalleriesPrefInfoMap::const_iterator gallery_it = galleries.find(*it); |
| 313 DCHECK(gallery_it != galleries.end()); | 402 DCHECK(gallery_it != galleries.end()); |
| 314 const MediaGalleryPrefInfo& gallery = gallery_it->second; | 403 const MediaGalleryPrefInfo& gallery = gallery_it->second; |
| 315 MediaGalleries::MediaFileSystemMetadata metadata; | 404 MediaGalleries::MediaFileSystemMetadata metadata; |
| 316 metadata.name = base::UTF16ToUTF8(gallery.GetGalleryDisplayName()); | 405 metadata.name = base::UTF16ToUTF8(gallery.GetGalleryDisplayName()); |
| 317 metadata.gallery_id = base::Uint64ToString(gallery.pref_id); | 406 metadata.gallery_id = base::Uint64ToString(gallery.pref_id); |
| 318 metadata.is_removable = StorageInfo::IsRemovableDevice(gallery.device_id); | 407 metadata.is_removable = StorageInfo::IsRemovableDevice(gallery.device_id); |
| 319 metadata.is_media_device = StorageInfo::IsMediaDevice(gallery.device_id); | 408 metadata.is_media_device = StorageInfo::IsMediaDevice(gallery.device_id); |
| 320 metadata.is_available = ContainsKey(*available_devices, gallery.device_id); | 409 metadata.is_available = ContainsKey(*available_devices, gallery.device_id); |
| 321 list->Append(metadata.ToValue().release()); | 410 list->Append(metadata.ToValue().release()); |
| 322 } | 411 } |
| 323 | 412 |
| 324 SetResult(list); | 413 SetResult(list); |
| 325 SendResponse(true); | 414 SendResponse(true); |
| 326 } | 415 } |
| 327 | 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 SelectDirectoryDialog::Callback cb = base::Bind( | |
| 453 &MediaGalleriesManageMediaLocationsFunction::OnDirectorySelected, | |
| 454 this); | |
| 455 scoped_refptr<SelectDirectoryDialog> select_directory_dialog = | |
| 456 new SelectDirectoryDialog(contents, cb); | |
| 457 select_directory_dialog->Show(); | |
| 458 return; | |
| 459 } | |
| 460 case MediaGalleries::MANAGE_REQUEST_TYPE_NONE: | |
| 461 NOTREACHED(); | |
| 462 } | |
| 463 SendResponse(false); | |
| 464 } | |
| 465 | |
| 466 void MediaGalleriesManageMediaLocationsFunction::OnDirectorySelected( | |
| 467 const base::FilePath& selected_directory) { | |
| 468 if (selected_directory.empty()) { | |
| 469 SetResult(new base::ListValue()); | |
| 470 SendResponse(true); | |
| 471 return; | |
| 472 } | |
| 473 | |
| 474 bool posted = base::PostTaskAndReplyWithResult( | |
| 475 content::BrowserThread::GetBlockingPool(), | |
| 476 FROM_HERE, | |
| 477 base::Bind(&base::PathExists, selected_directory), | |
| 478 base::Bind( | |
| 479 &MediaGalleriesManageMediaLocationsFunction::OnDirectoryChecked, | |
| 480 this, | |
| 481 selected_directory)); | |
| 482 if (posted) | |
| 483 return; | |
| 484 SendResponse(false); | |
| 485 } | |
| 486 | |
| 487 void MediaGalleriesManageMediaLocationsFunction::OnDirectoryChecked( | |
| 488 const base::FilePath& selected_directory, bool exists) { | |
| 489 DCHECK(!selected_directory.empty()); | |
| 490 | |
| 491 if (!exists) { | |
| 492 SendResponse(false); | |
| 493 return; | |
| 494 } | |
| 495 | |
| 496 MediaGalleryPrefInfo gallery; | |
| 497 MediaGalleriesPreferences* preferences = GetMediaGalleriesPreferences(); | |
| 498 MediaGalleryPrefId pref_id = | |
| 499 preferences->AddGalleryByPath(selected_directory); | |
| 500 preferences->SetGalleryPermissionForExtension(*GetExtension(), pref_id, true); | |
| 501 | |
| 502 GetMediaFileSystemsForExtension(base::Bind( | |
| 503 &MediaGalleriesManageMediaLocationsFunction::FindGalleryWithPrefId, | |
| 504 this, | |
| 505 pref_id)); | |
| 506 } | |
| 507 | |
| 508 void MediaGalleriesManageMediaLocationsFunction::FindGalleryWithPrefId( | |
| 509 MediaGalleryPrefId pref_id, | |
| 510 const std::vector<MediaFileSystemInfo>& filesystems) { | |
| 511 for (size_t i = 0; i < filesystems.size(); i++) { | |
| 512 if (filesystems[i].pref_id == pref_id) { | |
| 513 std::vector<MediaFileSystemInfo> found_filesystem; | |
| 514 found_filesystem.push_back(filesystems[i]); | |
| 515 scoped_ptr<base::ListValue> list( | |
| 516 ConstructFileSystemList(found_filesystem)); | |
| 517 if (!list.get()) | |
| 518 break; | |
| 519 | |
| 520 SetResult(list.release()); | |
| 521 SendResponse(true); | |
| 522 return; | |
| 523 } | |
| 524 } | |
| 525 SendResponse(false); | |
| 526 } | |
| 527 | |
| 328 } // namespace extensions | 528 } // namespace extensions |
| OLD | NEW |