| 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/platform_file.h" | 15 #include "base/platform_file.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/blob_reader.h" | 22 #include "chrome/browser/extensions/blob_reader.h" |
| 23 #include "chrome/browser/extensions/extension_prefs.h" |
| 22 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 24 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
| 23 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" | 25 #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" |
| 24 #include "chrome/browser/media_galleries/media_galleries_histograms.h" | 26 #include "chrome/browser/media_galleries/media_galleries_histograms.h" |
| 25 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 27 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
| 28 #include "chrome/browser/platform_util.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/browser/storage_monitor/storage_info.h" | 30 #include "chrome/browser/storage_monitor/storage_info.h" |
| 28 #include "chrome/browser/ui/chrome_select_file_policy.h" | 31 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 29 #include "chrome/common/extensions/api/media_galleries.h" | 32 #include "chrome/common/extensions/api/media_galleries.h" |
| 30 #include "chrome/common/extensions/permissions/media_galleries_permission.h" | 33 #include "chrome/common/extensions/permissions/media_galleries_permission.h" |
| 31 #include "chrome/common/pref_names.h" | 34 #include "chrome/common/pref_names.h" |
| 32 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 35 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 33 #include "content/public/browser/browser_thread.h" | 36 #include "content/public/browser/browser_thread.h" |
| 34 #include "content/public/browser/child_process_security_policy.h" | 37 #include "content/public/browser/child_process_security_policy.h" |
| 35 #include "content/public/browser/render_process_host.h" | 38 #include "content/public/browser/render_process_host.h" |
| 36 #include "content/public/browser/render_view_host.h" | 39 #include "content/public/browser/render_view_host.h" |
| 37 #include "content/public/browser/web_contents.h" | 40 #include "content/public/browser/web_contents.h" |
| 41 #include "content/public/browser/web_contents_view.h" |
| 38 #include "extensions/common/extension.h" | 42 #include "extensions/common/extension.h" |
| 39 #include "extensions/common/permissions/api_permission.h" | 43 #include "extensions/common/permissions/api_permission.h" |
| 40 #include "extensions/common/permissions/permissions_data.h" | 44 #include "extensions/common/permissions/permissions_data.h" |
| 45 #include "grit/generated_resources.h" |
| 41 #include "net/base/mime_sniffer.h" | 46 #include "net/base/mime_sniffer.h" |
| 47 #include "ui/base/l10n/l10n_util.h" |
| 42 | 48 |
| 43 using content::WebContents; | 49 using content::WebContents; |
| 44 using web_modal::WebContentsModalDialogManager; | 50 using web_modal::WebContentsModalDialogManager; |
| 45 | 51 |
| 46 namespace extensions { | 52 namespace extensions { |
| 47 | 53 |
| 48 namespace MediaGalleries = api::media_galleries; | 54 namespace MediaGalleries = api::media_galleries; |
| 49 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; | 55 namespace GetMediaFileSystems = MediaGalleries::GetMediaFileSystems; |
| 50 | 56 |
| 51 namespace { | 57 namespace { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 69 return false; | 75 return false; |
| 70 } | 76 } |
| 71 | 77 |
| 72 return true; | 78 return true; |
| 73 } | 79 } |
| 74 | 80 |
| 75 MediaFileSystemRegistry* media_file_system_registry() { | 81 MediaFileSystemRegistry* media_file_system_registry() { |
| 76 return g_browser_process->media_file_system_registry(); | 82 return g_browser_process->media_file_system_registry(); |
| 77 } | 83 } |
| 78 | 84 |
| 85 WebContents* GetWebContents(content::RenderViewHost* rvh, |
| 86 Profile* profile, |
| 87 const std::string& app_id) { |
| 88 WebContents* contents = WebContents::FromRenderViewHost(rvh); |
| 89 WebContentsModalDialogManager* web_contents_modal_dialog_manager = |
| 90 WebContentsModalDialogManager::FromWebContents(contents); |
| 91 if (!web_contents_modal_dialog_manager) { |
| 92 // If there is no WebContentsModalDialogManager, then this contents is |
| 93 // probably the background page for an app. Try to find a shell window to |
| 94 // host the dialog. |
| 95 apps::ShellWindow* window = apps::ShellWindowRegistry::Get( |
| 96 profile)->GetCurrentShellWindowForApp(app_id); |
| 97 contents = window ? window->web_contents() : NULL; |
| 98 } |
| 99 return contents; |
| 100 } |
| 101 |
| 102 base::ListValue* ConstructFileSystemList( |
| 103 content::RenderViewHost* rvh, |
| 104 const Extension* extension, |
| 105 const std::vector<MediaFileSystemInfo>& filesystems) { |
| 106 if (!rvh) |
| 107 return NULL; |
| 108 |
| 109 MediaGalleriesPermission::CheckParam read_param( |
| 110 MediaGalleriesPermission::kReadPermission); |
| 111 bool has_read_permission = PermissionsData::CheckAPIPermissionWithParam( |
| 112 extension, APIPermission::kMediaGalleries, &read_param); |
| 113 MediaGalleriesPermission::CheckParam copy_to_param( |
| 114 MediaGalleriesPermission::kCopyToPermission); |
| 115 bool has_copy_to_permission = PermissionsData::CheckAPIPermissionWithParam( |
| 116 extension, APIPermission::kMediaGalleries, ©_to_param); |
| 117 MediaGalleriesPermission::CheckParam delete_param( |
| 118 MediaGalleriesPermission::kDeletePermission); |
| 119 bool has_delete_permission = PermissionsData::CheckAPIPermissionWithParam( |
| 120 extension, APIPermission::kMediaGalleries, &delete_param); |
| 121 |
| 122 const int child_id = rvh->GetProcess()->GetID(); |
| 123 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 124 for (size_t i = 0; i < filesystems.size(); ++i) { |
| 125 scoped_ptr<base::DictionaryValue> file_system_dict_value( |
| 126 new base::DictionaryValue()); |
| 127 |
| 128 // Send the file system id so the renderer can create a valid FileSystem |
| 129 // object. |
| 130 file_system_dict_value->SetStringWithoutPathExpansion( |
| 131 "fsid", filesystems[i].fsid); |
| 132 |
| 133 file_system_dict_value->SetStringWithoutPathExpansion( |
| 134 kNameKey, filesystems[i].name); |
| 135 file_system_dict_value->SetStringWithoutPathExpansion( |
| 136 kGalleryIdKey, |
| 137 base::Uint64ToString(filesystems[i].pref_id)); |
| 138 if (!filesystems[i].transient_device_id.empty()) { |
| 139 file_system_dict_value->SetStringWithoutPathExpansion( |
| 140 kDeviceIdKey, filesystems[i].transient_device_id); |
| 141 } |
| 142 file_system_dict_value->SetBooleanWithoutPathExpansion( |
| 143 kIsRemovableKey, filesystems[i].removable); |
| 144 file_system_dict_value->SetBooleanWithoutPathExpansion( |
| 145 kIsMediaDeviceKey, filesystems[i].media_device); |
| 146 file_system_dict_value->SetBooleanWithoutPathExpansion( |
| 147 kIsAvailableKey, true); |
| 148 |
| 149 list->Append(file_system_dict_value.release()); |
| 150 |
| 151 if (filesystems[i].path.empty()) |
| 152 continue; |
| 153 |
| 154 if (has_read_permission) { |
| 155 content::ChildProcessSecurityPolicy* policy = |
| 156 content::ChildProcessSecurityPolicy::GetInstance(); |
| 157 policy->GrantReadFileSystem(child_id, filesystems[i].fsid); |
| 158 if (has_delete_permission) { |
| 159 policy->GrantDeleteFromFileSystem(child_id, filesystems[i].fsid); |
| 160 if (has_copy_to_permission) { |
| 161 policy->GrantCopyIntoFileSystem(child_id, filesystems[i].fsid); |
| 162 } |
| 163 } |
| 164 } |
| 165 } |
| 166 |
| 167 return list.release(); |
| 168 } |
| 169 |
| 170 class SelectDirectoryDialog : public ui::SelectFileDialog::Listener, |
| 171 public base::RefCounted<SelectDirectoryDialog> { |
| 172 public: |
| 173 // Selected file path, or an empty path if the user canceled. |
| 174 typedef base::Callback<void(const base::FilePath&)> Callback; |
| 175 |
| 176 SelectDirectoryDialog(WebContents* web_contents, const Callback& callback) |
| 177 : web_contents_(web_contents), |
| 178 callback_(callback) { |
| 179 select_file_dialog_ = ui::SelectFileDialog::Create( |
| 180 this, new ChromeSelectFilePolicy(web_contents)); |
| 181 } |
| 182 |
| 183 void Show(const base::FilePath& default_path) { |
| 184 AddRef(); // Balanced in the two reachable listener outcomes. |
| 185 select_file_dialog_->SelectFile( |
| 186 ui::SelectFileDialog::SELECT_FOLDER, |
| 187 l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY_TITLE), |
| 188 default_path, |
| 189 NULL, |
| 190 0, |
| 191 base::FilePath::StringType(), |
| 192 platform_util::GetTopLevel(web_contents_->GetView()->GetNativeView()), |
| 193 NULL); |
| 194 } |
| 195 |
| 196 // ui::SelectFileDialog::Listener implementation. |
| 197 virtual void FileSelected(const base::FilePath& path, |
| 198 int index, |
| 199 void* params) OVERRIDE { |
| 200 callback_.Run(path); |
| 201 Release(); // Balanced in Show(). |
| 202 } |
| 203 |
| 204 virtual void MultiFilesSelected(const std::vector<base::FilePath>& files, |
| 205 void* params) OVERRIDE { |
| 206 NOTREACHED() << "Should not be able to select multiple files"; |
| 207 } |
| 208 |
| 209 virtual void FileSelectionCanceled(void* params) OVERRIDE { |
| 210 callback_.Run(base::FilePath()); |
| 211 Release(); // Balanced in Show(). |
| 212 } |
| 213 |
| 214 private: |
| 215 friend class base::RefCounted<SelectDirectoryDialog>; |
| 216 virtual ~SelectDirectoryDialog() {} |
| 217 |
| 218 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; |
| 219 WebContents* web_contents_; |
| 220 Callback callback_; |
| 221 |
| 222 DISALLOW_COPY_AND_ASSIGN(SelectDirectoryDialog); |
| 223 }; |
| 224 |
| 79 } // namespace | 225 } // namespace |
| 80 | 226 |
| 81 MediaGalleriesGetMediaFileSystemsFunction:: | 227 MediaGalleriesGetMediaFileSystemsFunction:: |
| 82 ~MediaGalleriesGetMediaFileSystemsFunction() {} | 228 ~MediaGalleriesGetMediaFileSystemsFunction() {} |
| 83 | 229 |
| 84 bool MediaGalleriesGetMediaFileSystemsFunction::RunImpl() { | 230 bool MediaGalleriesGetMediaFileSystemsFunction::RunImpl() { |
| 85 if (!ApiIsAccessible(&error_)) | 231 if (!ApiIsAccessible(&error_)) |
| 86 return false; | 232 return false; |
| 87 | 233 |
| 88 media_galleries::UsageCount(media_galleries::GET_MEDIA_FILE_SYSTEMS); | 234 media_galleries::UsageCount(media_galleries::GET_MEDIA_FILE_SYSTEMS); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 ReturnGalleries(filesystems); | 292 ReturnGalleries(filesystems); |
| 147 } | 293 } |
| 148 | 294 |
| 149 void MediaGalleriesGetMediaFileSystemsFunction::GetAndReturnGalleries() { | 295 void MediaGalleriesGetMediaFileSystemsFunction::GetAndReturnGalleries() { |
| 150 GetMediaFileSystemsForExtension(base::Bind( | 296 GetMediaFileSystemsForExtension(base::Bind( |
| 151 &MediaGalleriesGetMediaFileSystemsFunction::ReturnGalleries, this)); | 297 &MediaGalleriesGetMediaFileSystemsFunction::ReturnGalleries, this)); |
| 152 } | 298 } |
| 153 | 299 |
| 154 void MediaGalleriesGetMediaFileSystemsFunction::ReturnGalleries( | 300 void MediaGalleriesGetMediaFileSystemsFunction::ReturnGalleries( |
| 155 const std::vector<MediaFileSystemInfo>& filesystems) { | 301 const std::vector<MediaFileSystemInfo>& filesystems) { |
| 156 content::RenderViewHost* rvh = render_view_host(); | 302 scoped_ptr<base::ListValue> list( |
| 157 if (!rvh) { | 303 ConstructFileSystemList(render_view_host(), GetExtension(), filesystems)); |
| 304 if (!list.get()) { |
| 158 SendResponse(false); | 305 SendResponse(false); |
| 159 return; | 306 return; |
| 160 } | 307 } |
| 161 MediaGalleriesPermission::CheckParam read_param( | |
| 162 MediaGalleriesPermission::kReadPermission); | |
| 163 bool has_read_permission = PermissionsData::CheckAPIPermissionWithParam( | |
| 164 GetExtension(), APIPermission::kMediaGalleries, &read_param); | |
| 165 MediaGalleriesPermission::CheckParam copy_to_param( | |
| 166 MediaGalleriesPermission::kCopyToPermission); | |
| 167 bool has_copy_to_permission = PermissionsData::CheckAPIPermissionWithParam( | |
| 168 GetExtension(), APIPermission::kMediaGalleries, ©_to_param); | |
| 169 MediaGalleriesPermission::CheckParam delete_param( | |
| 170 MediaGalleriesPermission::kDeletePermission); | |
| 171 bool has_delete_permission = PermissionsData::CheckAPIPermissionWithParam( | |
| 172 GetExtension(), APIPermission::kMediaGalleries, &delete_param); | |
| 173 | |
| 174 const int child_id = rvh->GetProcess()->GetID(); | |
| 175 base::ListValue* list = new base::ListValue(); | |
| 176 for (size_t i = 0; i < filesystems.size(); i++) { | |
| 177 scoped_ptr<base::DictionaryValue> file_system_dict_value( | |
| 178 new base::DictionaryValue()); | |
| 179 | |
| 180 // Send the file system id so the renderer can create a valid FileSystem | |
| 181 // object. | |
| 182 file_system_dict_value->SetStringWithoutPathExpansion( | |
| 183 "fsid", filesystems[i].fsid); | |
| 184 | |
| 185 file_system_dict_value->SetStringWithoutPathExpansion( | |
| 186 kNameKey, filesystems[i].name); | |
| 187 file_system_dict_value->SetStringWithoutPathExpansion( | |
| 188 kGalleryIdKey, | |
| 189 base::Uint64ToString(filesystems[i].pref_id)); | |
| 190 if (!filesystems[i].transient_device_id.empty()) { | |
| 191 file_system_dict_value->SetStringWithoutPathExpansion( | |
| 192 kDeviceIdKey, filesystems[i].transient_device_id); | |
| 193 } | |
| 194 file_system_dict_value->SetBooleanWithoutPathExpansion( | |
| 195 kIsRemovableKey, filesystems[i].removable); | |
| 196 file_system_dict_value->SetBooleanWithoutPathExpansion( | |
| 197 kIsMediaDeviceKey, filesystems[i].media_device); | |
| 198 file_system_dict_value->SetBooleanWithoutPathExpansion( | |
| 199 kIsAvailableKey, true); | |
| 200 | |
| 201 list->Append(file_system_dict_value.release()); | |
| 202 | |
| 203 if (filesystems[i].path.empty()) | |
| 204 continue; | |
| 205 | |
| 206 if (has_read_permission) { | |
| 207 content::ChildProcessSecurityPolicy* policy = | |
| 208 content::ChildProcessSecurityPolicy::GetInstance(); | |
| 209 policy->GrantReadFileSystem(child_id, filesystems[i].fsid); | |
| 210 if (has_delete_permission) { | |
| 211 policy->GrantDeleteFromFileSystem(child_id, filesystems[i].fsid); | |
| 212 if (has_copy_to_permission) { | |
| 213 policy->GrantCopyIntoFileSystem(child_id, filesystems[i].fsid); | |
| 214 } | |
| 215 } | |
| 216 } | |
| 217 } | |
| 218 | 308 |
| 219 // The custom JS binding will use this list to create DOMFileSystem objects. | 309 // The custom JS binding will use this list to create DOMFileSystem objects. |
| 220 SetResult(list); | 310 SetResult(list.release()); |
| 221 SendResponse(true); | 311 SendResponse(true); |
| 222 } | 312 } |
| 223 | 313 |
| 224 void MediaGalleriesGetMediaFileSystemsFunction::ShowDialog() { | 314 void MediaGalleriesGetMediaFileSystemsFunction::ShowDialog() { |
| 225 media_galleries::UsageCount(media_galleries::SHOW_DIALOG); | 315 media_galleries::UsageCount(media_galleries::SHOW_DIALOG); |
| 226 WebContents* contents = WebContents::FromRenderViewHost(render_view_host()); | 316 const Extension* extension = GetExtension(); |
| 227 WebContentsModalDialogManager* web_contents_modal_dialog_manager = | 317 WebContents* contents = |
| 228 WebContentsModalDialogManager::FromWebContents(contents); | 318 GetWebContents(render_view_host(), GetProfile(), extension->id()); |
| 229 if (!web_contents_modal_dialog_manager) { | |
| 230 // If there is no WebContentsModalDialogManager, then this contents is | |
| 231 // probably the background page for an app. Try to find a shell window to | |
| 232 // host the dialog. | |
| 233 apps::ShellWindow* window = apps::ShellWindowRegistry::Get( | |
| 234 GetProfile())->GetCurrentShellWindowForApp(GetExtension()->id()); | |
| 235 if (!window) { | |
| 236 SendResponse(false); | |
| 237 return; | |
| 238 } | |
| 239 contents = window->web_contents(); | |
| 240 } | |
| 241 | 319 |
| 242 // Controller will delete itself. | 320 // Controller will delete itself. |
| 243 base::Closure cb = base::Bind( | 321 base::Closure cb = base::Bind( |
| 244 &MediaGalleriesGetMediaFileSystemsFunction::GetAndReturnGalleries, this); | 322 &MediaGalleriesGetMediaFileSystemsFunction::GetAndReturnGalleries, this); |
| 245 new MediaGalleriesDialogController(contents, *GetExtension(), cb); | 323 new MediaGalleriesDialogController(contents, *extension, cb); |
| 246 } | 324 } |
| 247 | 325 |
| 248 void MediaGalleriesGetMediaFileSystemsFunction::GetMediaFileSystemsForExtension( | 326 void MediaGalleriesGetMediaFileSystemsFunction::GetMediaFileSystemsForExtension( |
| 249 const MediaFileSystemsCallback& cb) { | 327 const MediaFileSystemsCallback& cb) { |
| 250 if (!render_view_host()) { | 328 if (!render_view_host()) { |
| 251 cb.Run(std::vector<MediaFileSystemInfo>()); | 329 cb.Run(std::vector<MediaFileSystemInfo>()); |
| 252 return; | 330 return; |
| 253 } | 331 } |
| 254 MediaFileSystemRegistry* registry = media_file_system_registry(); | 332 MediaFileSystemRegistry* registry = media_file_system_registry(); |
| 255 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); | 333 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 metadata.is_removable = StorageInfo::IsRemovableDevice(gallery.device_id); | 396 metadata.is_removable = StorageInfo::IsRemovableDevice(gallery.device_id); |
| 319 metadata.is_media_device = StorageInfo::IsMediaDevice(gallery.device_id); | 397 metadata.is_media_device = StorageInfo::IsMediaDevice(gallery.device_id); |
| 320 metadata.is_available = ContainsKey(*available_devices, gallery.device_id); | 398 metadata.is_available = ContainsKey(*available_devices, gallery.device_id); |
| 321 list->Append(metadata.ToValue().release()); | 399 list->Append(metadata.ToValue().release()); |
| 322 } | 400 } |
| 323 | 401 |
| 324 SetResult(list); | 402 SetResult(list); |
| 325 SendResponse(true); | 403 SendResponse(true); |
| 326 } | 404 } |
| 327 | 405 |
| 406 MediaGalleriesAddUserSelectedFolderFunction:: |
| 407 ~MediaGalleriesAddUserSelectedFolderFunction() {} |
| 408 |
| 409 bool MediaGalleriesAddUserSelectedFolderFunction::RunImpl() { |
| 410 if (!ApiIsAccessible(&error_)) |
| 411 return false; |
| 412 |
| 413 media_galleries::UsageCount(media_galleries::ADD_USER_SELECTED_FOLDER); |
| 414 MediaGalleriesPreferences* preferences = |
| 415 media_file_system_registry()->GetPreferences(GetProfile()); |
| 416 preferences->EnsureInitialized(base::Bind( |
| 417 &MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit, |
| 418 this)); |
| 419 return true; |
| 420 } |
| 421 |
| 422 void MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit() { |
| 423 Profile* profile = GetProfile(); |
| 424 const std::string& app_id = GetExtension()->id(); |
| 425 WebContents* contents = GetWebContents(render_view_host(), profile, app_id); |
| 426 base::FilePath last_used_path = |
| 427 extensions::file_system_api::GetLastChooseEntryDirectory( |
| 428 extensions::ExtensionPrefs::Get(profile), app_id); |
| 429 SelectDirectoryDialog::Callback callback = base::Bind( |
| 430 &MediaGalleriesAddUserSelectedFolderFunction::OnDirectorySelected, this); |
| 431 scoped_refptr<SelectDirectoryDialog> select_directory_dialog = |
| 432 new SelectDirectoryDialog(contents, callback); |
| 433 select_directory_dialog->Show(last_used_path); |
| 434 } |
| 435 |
| 436 void MediaGalleriesAddUserSelectedFolderFunction::OnDirectorySelected( |
| 437 const base::FilePath& selected_directory) { |
| 438 if (selected_directory.empty()) { |
| 439 // User cancelled case. |
| 440 GetMediaFileSystemsForExtension(base::Bind( |
| 441 &MediaGalleriesAddUserSelectedFolderFunction::ReturnGalleriesAndId, |
| 442 this, |
| 443 kInvalidMediaGalleryPrefId)); |
| 444 return; |
| 445 } |
| 446 |
| 447 extensions::file_system_api::SetLastChooseEntryDirectory( |
| 448 extensions::ExtensionPrefs::Get(GetProfile()), |
| 449 GetExtension()->id(), |
| 450 selected_directory); |
| 451 |
| 452 MediaGalleriesPreferences* preferences = |
| 453 media_file_system_registry()->GetPreferences(GetProfile()); |
| 454 MediaGalleryPrefId pref_id = |
| 455 preferences->AddGalleryByPath(selected_directory); |
| 456 preferences->SetGalleryPermissionForExtension(*GetExtension(), pref_id, true); |
| 457 |
| 458 GetMediaFileSystemsForExtension(base::Bind( |
| 459 &MediaGalleriesAddUserSelectedFolderFunction::ReturnGalleriesAndId, |
| 460 this, |
| 461 pref_id)); |
| 462 } |
| 463 |
| 464 void MediaGalleriesAddUserSelectedFolderFunction::ReturnGalleriesAndId( |
| 465 MediaGalleryPrefId pref_id, |
| 466 const std::vector<MediaFileSystemInfo>& filesystems) { |
| 467 scoped_ptr<base::ListValue> list( |
| 468 ConstructFileSystemList(render_view_host(), GetExtension(), filesystems)); |
| 469 if (!list.get()) { |
| 470 SendResponse(false); |
| 471 return; |
| 472 } |
| 473 |
| 474 int index = -1; |
| 475 if (pref_id != kInvalidMediaGalleryPrefId) { |
| 476 for (size_t i = 0; i < filesystems.size(); ++i) { |
| 477 if (filesystems[i].pref_id == pref_id) { |
| 478 index = i; |
| 479 break; |
| 480 } |
| 481 } |
| 482 } |
| 483 base::DictionaryValue* results = new base::DictionaryValue; |
| 484 results->SetWithoutPathExpansion("mediaFileSystems", list.release()); |
| 485 results->SetIntegerWithoutPathExpansion("selectedFileSystemIndex", index); |
| 486 SetResult(results); |
| 487 SendResponse(true); |
| 488 } |
| 489 |
| 490 void |
| 491 MediaGalleriesAddUserSelectedFolderFunction::GetMediaFileSystemsForExtension( |
| 492 const MediaFileSystemsCallback& cb) { |
| 493 if (!render_view_host()) { |
| 494 cb.Run(std::vector<MediaFileSystemInfo>()); |
| 495 return; |
| 496 } |
| 497 MediaFileSystemRegistry* registry = media_file_system_registry(); |
| 498 DCHECK(registry->GetPreferences(GetProfile())->IsInitialized()); |
| 499 registry->GetMediaFileSystemsForExtension( |
| 500 render_view_host(), GetExtension(), cb); |
| 501 } |
| 502 |
| 328 MediaGalleriesGetMetadataFunction::~MediaGalleriesGetMetadataFunction() {} | 503 MediaGalleriesGetMetadataFunction::~MediaGalleriesGetMetadataFunction() {} |
| 329 | 504 |
| 330 bool MediaGalleriesGetMetadataFunction::RunImpl() { | 505 bool MediaGalleriesGetMetadataFunction::RunImpl() { |
| 331 if (!ApiIsAccessible(&error_)) | 506 if (!ApiIsAccessible(&error_)) |
| 332 return false; | 507 return false; |
| 333 | 508 |
| 334 std::string blob_uuid; | 509 std::string blob_uuid; |
| 335 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &blob_uuid)); | 510 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &blob_uuid)); |
| 336 | 511 |
| 337 const base::Value* options_value = NULL; | 512 const base::Value* options_value = NULL; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 if (mime_type_sniffed) | 555 if (mime_type_sniffed) |
| 381 metadata.mime_type = mime_type; | 556 metadata.mime_type = mime_type; |
| 382 | 557 |
| 383 // TODO(tommycli): Kick off SafeMediaMetadataParser if |mime_type_only| false. | 558 // TODO(tommycli): Kick off SafeMediaMetadataParser if |mime_type_only| false. |
| 384 | 559 |
| 385 SetResult(metadata.ToValue().release()); | 560 SetResult(metadata.ToValue().release()); |
| 386 SendResponse(true); | 561 SendResponse(true); |
| 387 } | 562 } |
| 388 | 563 |
| 389 } // namespace extensions | 564 } // namespace extensions |
| OLD | NEW |