| 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/base_paths_posix.h" | 7 #include "base/base_paths_posix.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/i18n/time_formatting.h" | 9 #include "base/i18n/time_formatting.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include "components/user_prefs/pref_registry_syncable.h" | 31 #include "components/user_prefs/pref_registry_syncable.h" |
| 32 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| 33 #include "extensions/browser/extension_system.h" | 33 #include "extensions/browser/extension_system.h" |
| 34 #include "extensions/browser/pref_names.h" | 34 #include "extensions/browser/pref_names.h" |
| 35 #include "extensions/common/extension.h" | 35 #include "extensions/common/extension.h" |
| 36 #include "extensions/common/extension_set.h" | 36 #include "extensions/common/extension_set.h" |
| 37 #include "extensions/common/permissions/api_permission.h" | 37 #include "extensions/common/permissions/api_permission.h" |
| 38 #include "extensions/common/permissions/permissions_data.h" | 38 #include "extensions/common/permissions/permissions_data.h" |
| 39 #include "grit/generated_resources.h" | 39 #include "grit/generated_resources.h" |
| 40 #include "ui/base/l10n/l10n_util.h" | 40 #include "ui/base/l10n/l10n_util.h" |
| 41 #include "ui/base/text/bytes_formatting.h" | |
| 42 | 41 |
| 43 using base::DictionaryValue; | 42 using base::DictionaryValue; |
| 44 using base::ListValue; | 43 using base::ListValue; |
| 45 using extensions::ExtensionPrefs; | 44 using extensions::ExtensionPrefs; |
| 46 using storage_monitor::MediaStorageUtil; | 45 using storage_monitor::MediaStorageUtil; |
| 47 using storage_monitor::StorageInfo; | 46 using storage_monitor::StorageInfo; |
| 48 using storage_monitor::StorageMonitor; | 47 using storage_monitor::StorageMonitor; |
| 49 | 48 |
| 50 namespace { | 49 namespace { |
| 51 | 50 |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 if (dict->GetString(kMediaGalleryIdKey, &string_id) && | 288 if (dict->GetString(kMediaGalleryIdKey, &string_id) && |
| 290 base::StringToUint64(string_id, &out_permission->pref_id) && | 289 base::StringToUint64(string_id, &out_permission->pref_id) && |
| 291 dict->GetBoolean(kMediaGalleryHasPermissionKey, | 290 dict->GetBoolean(kMediaGalleryHasPermissionKey, |
| 292 &out_permission->has_permission)) { | 291 &out_permission->has_permission)) { |
| 293 return true; | 292 return true; |
| 294 } | 293 } |
| 295 NOTREACHED(); | 294 NOTREACHED(); |
| 296 return false; | 295 return false; |
| 297 } | 296 } |
| 298 | 297 |
| 299 base::string16 GetDisplayNameForDevice(uint64 storage_size_in_bytes, | |
| 300 const base::string16& name) { | |
| 301 DCHECK(!name.empty()); | |
| 302 return (storage_size_in_bytes == 0) ? | |
| 303 name : | |
| 304 ui::FormatBytes(storage_size_in_bytes) + base::ASCIIToUTF16(" ") + name; | |
| 305 } | |
| 306 | |
| 307 // For a device with |device_name| and a relative path |sub_folder|, construct | 298 // For a device with |device_name| and a relative path |sub_folder|, construct |
| 308 // a display name. If |sub_folder| is empty, then just return |device_name|. | 299 // a display name. If |sub_folder| is empty, then just return |device_name|. |
| 309 base::string16 GetDisplayNameForSubFolder(const base::string16& device_name, | 300 base::string16 GetDisplayNameForSubFolder(const base::string16& device_name, |
| 310 const base::FilePath& sub_folder) { | 301 const base::FilePath& sub_folder) { |
| 311 if (sub_folder.empty()) | 302 if (sub_folder.empty()) |
| 312 return device_name; | 303 return device_name; |
| 313 return (sub_folder.BaseName().LossyDisplayName() + | 304 return (sub_folder.BaseName().LossyDisplayName() + |
| 314 base::ASCIIToUTF16(" - ") + | 305 base::ASCIIToUTF16(" - ") + |
| 315 device_name); | 306 device_name); |
| 316 } | 307 } |
| 317 | 308 |
| 318 base::string16 GetFullProductName(const base::string16& vendor_name, | |
| 319 const base::string16& model_name) { | |
| 320 if (vendor_name.empty() && model_name.empty()) | |
| 321 return base::string16(); | |
| 322 | |
| 323 base::string16 product_name; | |
| 324 if (vendor_name.empty()) | |
| 325 product_name = model_name; | |
| 326 else if (model_name.empty()) | |
| 327 product_name = vendor_name; | |
| 328 else if (!vendor_name.empty() && !model_name.empty()) | |
| 329 product_name = vendor_name + base::UTF8ToUTF16(", ") + model_name; | |
| 330 | |
| 331 return product_name; | |
| 332 } | |
| 333 | |
| 334 } // namespace | 309 } // namespace |
| 335 | 310 |
| 336 MediaGalleryPrefInfo::MediaGalleryPrefInfo() | 311 MediaGalleryPrefInfo::MediaGalleryPrefInfo() |
| 337 : pref_id(kInvalidMediaGalleryPrefId), | 312 : pref_id(kInvalidMediaGalleryPrefId), |
| 338 type(kInvalidType), | 313 type(kInvalidType), |
| 339 total_size_in_bytes(0), | 314 total_size_in_bytes(0), |
| 340 volume_metadata_valid(false), | 315 volume_metadata_valid(false), |
| 341 audio_count(0), | 316 audio_count(0), |
| 342 image_count(0), | 317 image_count(0), |
| 343 video_count(0), | 318 video_count(0), |
| (...skipping 29 matching lines...) Expand all Loading... |
| 373 base::FilePath relative; | 348 base::FilePath relative; |
| 374 if (download_path.AppendRelativePath(path, &relative)) | 349 if (download_path.AppendRelativePath(path, &relative)) |
| 375 return relative.LossyDisplayName(); | 350 return relative.LossyDisplayName(); |
| 376 } | 351 } |
| 377 return path.BaseName().LossyDisplayName(); | 352 return path.BaseName().LossyDisplayName(); |
| 378 #else | 353 #else |
| 379 return path.LossyDisplayName(); | 354 return path.LossyDisplayName(); |
| 380 #endif | 355 #endif |
| 381 } | 356 } |
| 382 | 357 |
| 383 base::string16 name = display_name; | 358 StorageInfo info(device_id, |
| 384 if (name.empty()) | 359 MediaStorageUtil::FindDevicePathById(device_id).value(), |
| 385 name = volume_label; | 360 volume_label, vendor_name, model_name, total_size_in_bytes); |
| 386 if (name.empty()) | 361 base::string16 name = info.GetDisplayNameWithOverride(display_name, true); |
| 387 name = GetFullProductName(vendor_name, model_name); | |
| 388 if (name.empty()) | |
| 389 name = l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_UNLABELED_DEVICE); | |
| 390 | |
| 391 name = GetDisplayNameForDevice(total_size_in_bytes, name); | |
| 392 | |
| 393 if (!path.empty()) | 362 if (!path.empty()) |
| 394 name = GetDisplayNameForSubFolder(name, path); | 363 name = GetDisplayNameForSubFolder(name, path); |
| 395 | |
| 396 return name; | 364 return name; |
| 397 } | 365 } |
| 398 | 366 |
| 399 base::string16 MediaGalleryPrefInfo::GetGalleryTooltip() const { | 367 base::string16 MediaGalleryPrefInfo::GetGalleryTooltip() const { |
| 400 return AbsolutePath().LossyDisplayName(); | 368 return AbsolutePath().LossyDisplayName(); |
| 401 } | 369 } |
| 402 | 370 |
| 403 base::string16 MediaGalleryPrefInfo::GetGalleryAdditionalDetails() const { | 371 base::string16 MediaGalleryPrefInfo::GetGalleryAdditionalDetails() const { |
| 404 base::string16 attached; | 372 base::string16 attached; |
| 405 if (StorageInfo::IsRemovableDevice(device_id)) { | 373 if (StorageInfo::IsRemovableDevice(device_id)) { |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 }; | 510 }; |
| 543 | 511 |
| 544 for (size_t i = 0; i < arraysize(kDirectoryKeys); ++i) { | 512 for (size_t i = 0; i < arraysize(kDirectoryKeys); ++i) { |
| 545 base::FilePath path; | 513 base::FilePath path; |
| 546 if (!PathService::Get(kDirectoryKeys[i], &path)) | 514 if (!PathService::Get(kDirectoryKeys[i], &path)) |
| 547 continue; | 515 continue; |
| 548 | 516 |
| 549 base::FilePath relative_path; | 517 base::FilePath relative_path; |
| 550 StorageInfo info; | 518 StorageInfo info; |
| 551 if (MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path)) { | 519 if (MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path)) { |
| 552 AddGalleryInternal(info.device_id(), info.name(), relative_path, | 520 AddGalleryInternal(info.device_id(), base::string16(), relative_path, |
| 553 MediaGalleryPrefInfo::kAutoDetected, | 521 MediaGalleryPrefInfo::kAutoDetected, |
| 554 info.storage_label(), info.vendor_name(), | 522 info.storage_label(), info.vendor_name(), |
| 555 info.model_name(), info.total_size_in_bytes(), | 523 info.model_name(), info.total_size_in_bytes(), |
| 556 base::Time(), true, 0, 0, 0, kCurrentPrefsVersion); | 524 base::Time(), true, 0, 0, 0, kCurrentPrefsVersion); |
| 557 } | 525 } |
| 558 } | 526 } |
| 559 } | 527 } |
| 560 | 528 |
| 561 bool MediaGalleriesPreferences::UpdateDeviceIDForSingletonType( | 529 bool MediaGalleriesPreferences::UpdateDeviceIDForSingletonType( |
| 562 const std::string& device_id) { | 530 const std::string& device_id) { |
| (...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1249 if (extension_prefs_for_testing_) | 1217 if (extension_prefs_for_testing_) |
| 1250 return extension_prefs_for_testing_; | 1218 return extension_prefs_for_testing_; |
| 1251 return extensions::ExtensionPrefs::Get(profile_); | 1219 return extensions::ExtensionPrefs::Get(profile_); |
| 1252 } | 1220 } |
| 1253 | 1221 |
| 1254 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( | 1222 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( |
| 1255 extensions::ExtensionPrefs* extension_prefs) { | 1223 extensions::ExtensionPrefs* extension_prefs) { |
| 1256 DCHECK(IsInitialized()); | 1224 DCHECK(IsInitialized()); |
| 1257 extension_prefs_for_testing_ = extension_prefs; | 1225 extension_prefs_for_testing_ = extension_prefs; |
| 1258 } | 1226 } |
| OLD | NEW |