| 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 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 5 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
| 6 | 6 |
| 7 #include "base/i18n/time_formatting.h" | 7 #include "base/i18n/time_formatting.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "chrome/browser/storage_monitor/storage_monitor.h" | 26 #include "chrome/browser/storage_monitor/storage_monitor.h" |
| 27 #include "chrome/common/chrome_paths.h" | 27 #include "chrome/common/chrome_paths.h" |
| 28 #include "chrome/common/extensions/extension.h" | 28 #include "chrome/common/extensions/extension.h" |
| 29 #include "chrome/common/extensions/permissions/api_permission.h" | 29 #include "chrome/common/extensions/permissions/api_permission.h" |
| 30 #include "chrome/common/extensions/permissions/media_galleries_permission.h" | 30 #include "chrome/common/extensions/permissions/media_galleries_permission.h" |
| 31 #include "chrome/common/extensions/permissions/permissions_data.h" | 31 #include "chrome/common/extensions/permissions/permissions_data.h" |
| 32 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
| 33 #include "components/user_prefs/pref_registry_syncable.h" | 33 #include "components/user_prefs/pref_registry_syncable.h" |
| 34 #include "grit/generated_resources.h" | 34 #include "grit/generated_resources.h" |
| 35 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
| 36 #include "ui/base/text/bytes_formatting.h" | |
| 37 | 36 |
| 38 using base::DictionaryValue; | 37 using base::DictionaryValue; |
| 39 using base::ListValue; | 38 using base::ListValue; |
| 40 using extensions::ExtensionPrefs; | 39 using extensions::ExtensionPrefs; |
| 41 | 40 |
| 42 namespace chrome { | 41 namespace chrome { |
| 43 | 42 |
| 44 namespace { | 43 namespace { |
| 45 | 44 |
| 46 // Pref key for the list of media gallery permissions. | 45 // Pref key for the list of media gallery permissions. |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 if (dict->GetString(kMediaGalleryIdKey, &string_id) && | 213 if (dict->GetString(kMediaGalleryIdKey, &string_id) && |
| 215 base::StringToUint64(string_id, &out_permission->pref_id) && | 214 base::StringToUint64(string_id, &out_permission->pref_id) && |
| 216 dict->GetBoolean(kMediaGalleryHasPermissionKey, | 215 dict->GetBoolean(kMediaGalleryHasPermissionKey, |
| 217 &out_permission->has_permission)) { | 216 &out_permission->has_permission)) { |
| 218 return true; | 217 return true; |
| 219 } | 218 } |
| 220 NOTREACHED(); | 219 NOTREACHED(); |
| 221 return false; | 220 return false; |
| 222 } | 221 } |
| 223 | 222 |
| 224 string16 GetDisplayNameForDevice(uint64 storage_size_in_bytes, | |
| 225 const string16& name) { | |
| 226 DCHECK(!name.empty()); | |
| 227 return (storage_size_in_bytes == 0) ? | |
| 228 name : ui::FormatBytes(storage_size_in_bytes) + ASCIIToUTF16(" ") + name; | |
| 229 } | |
| 230 | |
| 231 // For a device with |device_name| and a relative path |sub_folder|, construct | 223 // For a device with |device_name| and a relative path |sub_folder|, construct |
| 232 // a display name. If |sub_folder| is empty, then just return |device_name|. | 224 // a display name. If |sub_folder| is empty, then just return |device_name|. |
| 233 string16 GetDisplayNameForSubFolder(const string16& device_name, | 225 string16 GetDisplayNameForSubFolder(const string16& device_name, |
| 234 const base::FilePath& sub_folder) { | 226 const base::FilePath& sub_folder) { |
| 235 if (sub_folder.empty()) | 227 if (sub_folder.empty()) |
| 236 return device_name; | 228 return device_name; |
| 237 return (sub_folder.BaseName().LossyDisplayName() + | 229 return (sub_folder.BaseName().LossyDisplayName() + |
| 238 ASCIIToUTF16(" - ") + | 230 base::ASCIIToUTF16(" - ") + |
| 239 device_name); | 231 device_name); |
| 240 } | 232 } |
| 241 | 233 |
| 242 string16 GetFullProductName(const string16& vendor_name, | |
| 243 const string16& model_name) { | |
| 244 if (vendor_name.empty() && model_name.empty()) | |
| 245 return string16(); | |
| 246 | |
| 247 string16 product_name; | |
| 248 if (vendor_name.empty()) | |
| 249 product_name = model_name; | |
| 250 else if (model_name.empty()) | |
| 251 product_name = vendor_name; | |
| 252 else if (!vendor_name.empty() && !model_name.empty()) | |
| 253 product_name = vendor_name + UTF8ToUTF16(", ") + model_name; | |
| 254 | |
| 255 return product_name; | |
| 256 } | |
| 257 | |
| 258 } // namespace | 234 } // namespace |
| 259 | 235 |
| 260 MediaGalleryPrefInfo::MediaGalleryPrefInfo() | 236 MediaGalleryPrefInfo::MediaGalleryPrefInfo() |
| 261 : pref_id(kInvalidMediaGalleryPrefId), | 237 : pref_id(kInvalidMediaGalleryPrefId), |
| 262 type(kInvalidType), | 238 type(kInvalidType), |
| 263 total_size_in_bytes(0), | 239 total_size_in_bytes(0), |
| 264 volume_metadata_valid(false), | 240 volume_metadata_valid(false), |
| 265 prefs_version(0) { | 241 prefs_version(0) { |
| 266 } | 242 } |
| 267 | 243 |
| 268 MediaGalleryPrefInfo::~MediaGalleryPrefInfo() {} | 244 MediaGalleryPrefInfo::~MediaGalleryPrefInfo() {} |
| 269 | 245 |
| 270 base::FilePath MediaGalleryPrefInfo::AbsolutePath() const { | 246 base::FilePath MediaGalleryPrefInfo::AbsolutePath() const { |
| 271 base::FilePath base_path = MediaStorageUtil::FindDevicePathById(device_id); | 247 base::FilePath base_path = MediaStorageUtil::FindDevicePathById(device_id); |
| 272 DCHECK(!path.IsAbsolute()); | 248 DCHECK(!path.IsAbsolute()); |
| 273 return base_path.empty() ? base_path : base_path.Append(path); | 249 return base_path.empty() ? base_path : base_path.Append(path); |
| 274 } | 250 } |
| 275 | 251 |
| 276 string16 MediaGalleryPrefInfo::GetGalleryDisplayName() const { | 252 string16 MediaGalleryPrefInfo::GetGalleryDisplayName() const { |
| 277 if (!StorageInfo::IsRemovableDevice(device_id)) { | 253 if (!StorageInfo::IsRemovableDevice(device_id)) { |
| 278 // For fixed storage, the name is the directory name, or, in the case | 254 // For fixed storage, the name is the directory name, or, in the case |
| 279 // of a root directory, the root directory name. | 255 // of a root directory, the root directory name. |
| 280 // TODO(gbillock): Using only the BaseName can lead to ambiguity. The | 256 // TODO(gbillock): Using only the BaseName can lead to ambiguity. The |
| 281 // tooltip resolves it. Is that enough? | 257 // tooltip resolves it. Is that enough? |
| 282 base::FilePath path = AbsolutePath(); | 258 base::FilePath abspath = AbsolutePath(); |
| 283 if (!display_name.empty()) | 259 if (!display_name.empty()) |
| 284 return display_name; | 260 return display_name; |
| 285 if (path == path.DirName()) | 261 if (abspath == abspath.DirName()) |
| 286 return path.LossyDisplayName(); | 262 return abspath.LossyDisplayName(); |
| 287 return path.BaseName().LossyDisplayName(); | 263 return abspath.BaseName().LossyDisplayName(); |
| 288 } | 264 } |
| 289 | 265 |
| 290 string16 name = display_name; | 266 StorageInfo info(device_id, |
| 291 if (name.empty()) | 267 MediaStorageUtil::FindDevicePathById(device_id).value(), |
| 292 name = volume_label; | 268 volume_label, vendor_name, model_name, total_size_in_bytes); |
| 293 if (name.empty()) | 269 base::string16 name = info.GetDisplayNameWithOverride(display_name, true); |
| 294 name = GetFullProductName(vendor_name, model_name); | 270 return GetDisplayNameForSubFolder(name, path); |
| 295 if (name.empty()) | |
| 296 name = l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_UNLABELED_DEVICE); | |
| 297 | |
| 298 name = GetDisplayNameForDevice(total_size_in_bytes, name); | |
| 299 | |
| 300 if (!path.empty()) | |
| 301 name = GetDisplayNameForSubFolder(name, path); | |
| 302 | |
| 303 return name; | |
| 304 } | 271 } |
| 305 | 272 |
| 306 string16 MediaGalleryPrefInfo::GetGalleryTooltip() const { | 273 string16 MediaGalleryPrefInfo::GetGalleryTooltip() const { |
| 307 return AbsolutePath().LossyDisplayName(); | 274 return AbsolutePath().LossyDisplayName(); |
| 308 } | 275 } |
| 309 | 276 |
| 310 string16 MediaGalleryPrefInfo::GetGalleryAdditionalDetails() const { | 277 string16 MediaGalleryPrefInfo::GetGalleryAdditionalDetails() const { |
| 311 string16 attached; | 278 string16 attached; |
| 312 if (StorageInfo::IsRemovableDevice(device_id)) { | 279 if (StorageInfo::IsRemovableDevice(device_id)) { |
| 313 if (MediaStorageUtil::IsRemovableStorageAttached(device_id)) { | 280 if (MediaStorageUtil::IsRemovableStorageAttached(device_id)) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 }; | 341 }; |
| 375 | 342 |
| 376 for (size_t i = 0; i < arraysize(kDirectoryKeys); ++i) { | 343 for (size_t i = 0; i < arraysize(kDirectoryKeys); ++i) { |
| 377 base::FilePath path; | 344 base::FilePath path; |
| 378 if (!PathService::Get(kDirectoryKeys[i], &path)) | 345 if (!PathService::Get(kDirectoryKeys[i], &path)) |
| 379 continue; | 346 continue; |
| 380 | 347 |
| 381 base::FilePath relative_path; | 348 base::FilePath relative_path; |
| 382 StorageInfo info; | 349 StorageInfo info; |
| 383 if (MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path)) { | 350 if (MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path)) { |
| 384 AddGalleryInternal(info.device_id(), info.name(), relative_path, false, | 351 AddGalleryInternal(info.device_id(), base::string16(), relative_path, |
| 385 info.storage_label(), info.vendor_name(), | 352 false, info.storage_label(), info.vendor_name(), |
| 386 info.model_name(), info.total_size_in_bytes(), | 353 info.model_name(), info.total_size_in_bytes(), |
| 387 base::Time(), true, 2); | 354 base::Time(), true, 2); |
| 388 } | 355 } |
| 389 } | 356 } |
| 390 } | 357 } |
| 391 | 358 |
| 392 bool MediaGalleriesPreferences::UpdateDeviceIDForSingletonType( | 359 bool MediaGalleriesPreferences::UpdateDeviceIDForSingletonType( |
| 393 const std::string& device_id) { | 360 const std::string& device_id) { |
| 394 StorageInfo::Type singleton_type; | 361 StorageInfo::Type singleton_type; |
| 395 if (!StorageInfo::CrackDeviceId(device_id, &singleton_type, NULL)) | 362 if (!StorageInfo::CrackDeviceId(device_id, &singleton_type, NULL)) |
| (...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 943 return extension_prefs_for_testing_; | 910 return extension_prefs_for_testing_; |
| 944 return extensions::ExtensionPrefs::Get(profile_); | 911 return extensions::ExtensionPrefs::Get(profile_); |
| 945 } | 912 } |
| 946 | 913 |
| 947 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( | 914 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( |
| 948 extensions::ExtensionPrefs* extension_prefs) { | 915 extensions::ExtensionPrefs* extension_prefs) { |
| 949 extension_prefs_for_testing_ = extension_prefs; | 916 extension_prefs_for_testing_ = extension_prefs; |
| 950 } | 917 } |
| 951 | 918 |
| 952 } // namespace chrome | 919 } // namespace chrome |
| OLD | NEW |