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 #include "chrome/browser/media_gallery/media_galleries_preferences.h" | 5 #include "chrome/browser/media_gallery/media_galleries_preferences.h" |
| 6 | 6 |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 : pref_id(kInvalidMediaGalleryPrefId), | 129 : pref_id(kInvalidMediaGalleryPrefId), |
| 130 type(kInvalidType) { | 130 type(kInvalidType) { |
| 131 } | 131 } |
| 132 MediaGalleryPrefInfo::~MediaGalleryPrefInfo() {} | 132 MediaGalleryPrefInfo::~MediaGalleryPrefInfo() {} |
| 133 | 133 |
| 134 FilePath MediaGalleryPrefInfo::AbsolutePath() const { | 134 FilePath MediaGalleryPrefInfo::AbsolutePath() const { |
| 135 FilePath base_path = MediaStorageUtil::FindDevicePathById(device_id); | 135 FilePath base_path = MediaStorageUtil::FindDevicePathById(device_id); |
| 136 return base_path.Append(path); | 136 return base_path.Append(path); |
| 137 } | 137 } |
| 138 | 138 |
| 139 MediaGalleriesPreferences::GalleryChangeObserver::~GalleryChangeObserver() {} | |
| 140 | |
| 139 MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile) | 141 MediaGalleriesPreferences::MediaGalleriesPreferences(Profile* profile) |
| 140 : profile_(profile) { | 142 : profile_(profile) { |
| 141 AddDefaultGalleriesIfFreshProfile(); | 143 AddDefaultGalleriesIfFreshProfile(); |
| 142 InitFromPrefs(); | 144 InitFromPrefs(false /*no notification*/); |
| 143 } | 145 } |
| 144 | 146 |
| 145 MediaGalleriesPreferences::~MediaGalleriesPreferences() {} | 147 MediaGalleriesPreferences::~MediaGalleriesPreferences() {} |
| 146 | 148 |
| 147 void MediaGalleriesPreferences::AddDefaultGalleriesIfFreshProfile() { | 149 void MediaGalleriesPreferences::AddDefaultGalleriesIfFreshProfile() { |
| 148 // Only add defaults the first time. | 150 // Only add defaults the first time. |
| 149 if (APIHasBeenUsed(profile_)) | 151 if (APIHasBeenUsed(profile_)) |
| 150 return; | 152 return; |
| 151 | 153 |
| 152 // Fresh profile case. | 154 // Fresh profile case. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 164 std::string device_id; | 166 std::string device_id; |
| 165 string16 display_name; | 167 string16 display_name; |
| 166 FilePath relative_path; | 168 FilePath relative_path; |
| 167 if (MediaStorageUtil::GetDeviceInfoFromPath(path, &device_id, &display_name, | 169 if (MediaStorageUtil::GetDeviceInfoFromPath(path, &device_id, &display_name, |
| 168 &relative_path)) { | 170 &relative_path)) { |
| 169 AddGallery(device_id, display_name, relative_path, false /*user added*/); | 171 AddGallery(device_id, display_name, relative_path, false /*user added*/); |
| 170 } | 172 } |
| 171 } | 173 } |
| 172 } | 174 } |
| 173 | 175 |
| 174 void MediaGalleriesPreferences::InitFromPrefs() { | 176 void MediaGalleriesPreferences::InitFromPrefs(bool notify_observers) { |
| 175 known_galleries_.clear(); | 177 known_galleries_.clear(); |
| 176 device_map_.clear(); | 178 device_map_.clear(); |
| 177 | 179 |
| 178 PrefService* prefs = profile_->GetPrefs(); | 180 PrefService* prefs = profile_->GetPrefs(); |
| 179 const ListValue* list = prefs->GetList( | 181 const ListValue* list = prefs->GetList( |
| 180 prefs::kMediaGalleriesRememberedGalleries); | 182 prefs::kMediaGalleriesRememberedGalleries); |
| 181 if (!list) | 183 if (!list) { |
|
vandebo (ex-Chrome)
2013/01/31 01:27:56
To remove the code duplication, what do you think
Lei Zhang
2013/01/31 01:34:35
Done.
| |
| 184 if (notify_observers) | |
| 185 NotifyChangeObservers(); | |
| 182 return; | 186 return; |
| 187 } | |
| 183 | 188 |
| 184 for (ListValue::const_iterator it = list->begin(); it != list->end(); ++it) { | 189 for (ListValue::const_iterator it = list->begin(); it != list->end(); ++it) { |
| 185 const DictionaryValue* dict = NULL; | 190 const DictionaryValue* dict = NULL; |
| 186 if (!(*it)->GetAsDictionary(&dict)) | 191 if (!(*it)->GetAsDictionary(&dict)) |
| 187 continue; | 192 continue; |
| 188 | 193 |
| 189 MediaGalleryPrefInfo gallery_info; | 194 MediaGalleryPrefInfo gallery_info; |
| 190 if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) | 195 if (!PopulateGalleryPrefInfoFromDictionary(*dict, &gallery_info)) |
| 191 continue; | 196 continue; |
| 192 | 197 |
| 193 known_galleries_[gallery_info.pref_id] = gallery_info; | 198 known_galleries_[gallery_info.pref_id] = gallery_info; |
| 194 device_map_[gallery_info.device_id].insert(gallery_info.pref_id); | 199 device_map_[gallery_info.device_id].insert(gallery_info.pref_id); |
| 195 } | 200 } |
| 201 if (notify_observers) | |
| 202 NotifyChangeObservers(); | |
| 203 } | |
| 204 | |
| 205 void MediaGalleriesPreferences::NotifyChangeObservers() { | |
| 206 FOR_EACH_OBSERVER(GalleryChangeObserver, | |
| 207 gallery_change_observers_, | |
| 208 OnGalleryChanged(this)); | |
| 209 } | |
| 210 | |
| 211 void MediaGalleriesPreferences::AddGalleryChangeObserver( | |
| 212 GalleryChangeObserver* observer) { | |
| 213 gallery_change_observers_.AddObserver(observer); | |
| 214 } | |
| 215 | |
| 216 void MediaGalleriesPreferences::RemoveGalleryChangeObserver( | |
| 217 GalleryChangeObserver* observer) { | |
| 218 gallery_change_observers_.RemoveObserver(observer); | |
| 196 } | 219 } |
| 197 | 220 |
| 198 bool MediaGalleriesPreferences::LookUpGalleryByPath( | 221 bool MediaGalleriesPreferences::LookUpGalleryByPath( |
| 199 const FilePath& path, | 222 const FilePath& path, |
| 200 MediaGalleryPrefInfo* gallery_info) const { | 223 MediaGalleryPrefInfo* gallery_info) const { |
| 201 std::string device_id; | 224 std::string device_id; |
| 202 string16 device_name; | 225 string16 device_name; |
| 203 FilePath relative_path; | 226 FilePath relative_path; |
| 204 if (!MediaStorageUtil::GetDeviceInfoFromPath(path, &device_id, &device_name, | 227 if (!MediaStorageUtil::GetDeviceInfoFromPath(path, &device_id, &device_name, |
| 205 &relative_path)) { | 228 &relative_path)) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 MediaGalleryPrefId iter_id; | 297 MediaGalleryPrefId iter_id; |
| 275 if ((*list_iter)->GetAsDictionary(&dict) && | 298 if ((*list_iter)->GetAsDictionary(&dict) && |
| 276 GetPrefId(*dict, &iter_id) && | 299 GetPrefId(*dict, &iter_id) && |
| 277 *it == iter_id) { | 300 *it == iter_id) { |
| 278 if (update_gallery_type) { | 301 if (update_gallery_type) { |
| 279 dict->SetString(kMediaGalleriesTypeKey, | 302 dict->SetString(kMediaGalleriesTypeKey, |
| 280 kMediaGalleriesTypeAutoDetectedValue); | 303 kMediaGalleriesTypeAutoDetectedValue); |
| 281 } | 304 } |
| 282 if (update_gallery_name) | 305 if (update_gallery_name) |
| 283 dict->SetString(kMediaGalleriesDisplayNameKey, display_name); | 306 dict->SetString(kMediaGalleriesDisplayNameKey, display_name); |
| 284 InitFromPrefs(); | 307 InitFromPrefs(true /* notify observers */); |
| 285 break; | 308 break; |
| 286 } | 309 } |
| 287 } | 310 } |
| 288 } | 311 } |
| 289 return *it; | 312 return *it; |
| 290 } | 313 } |
| 291 | 314 |
| 292 PrefService* prefs = profile_->GetPrefs(); | 315 PrefService* prefs = profile_->GetPrefs(); |
| 293 | 316 |
| 294 MediaGalleryPrefInfo gallery_info; | 317 MediaGalleryPrefInfo gallery_info; |
| 295 gallery_info.pref_id = prefs->GetUint64(prefs::kMediaGalleriesUniqueId); | 318 gallery_info.pref_id = prefs->GetUint64(prefs::kMediaGalleriesUniqueId); |
| 296 prefs->SetUint64(prefs::kMediaGalleriesUniqueId, gallery_info.pref_id + 1); | 319 prefs->SetUint64(prefs::kMediaGalleriesUniqueId, gallery_info.pref_id + 1); |
| 297 gallery_info.display_name = display_name; | 320 gallery_info.display_name = display_name; |
| 298 gallery_info.device_id = device_id; | 321 gallery_info.device_id = device_id; |
| 299 gallery_info.path = normalized_relative_path; | 322 gallery_info.path = normalized_relative_path; |
| 300 gallery_info.type = MediaGalleryPrefInfo::kAutoDetected; | 323 gallery_info.type = MediaGalleryPrefInfo::kAutoDetected; |
| 301 if (user_added) | 324 if (user_added) |
| 302 gallery_info.type = MediaGalleryPrefInfo::kUserAdded; | 325 gallery_info.type = MediaGalleryPrefInfo::kUserAdded; |
| 303 | 326 |
| 304 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); | 327 ListPrefUpdate update(prefs, prefs::kMediaGalleriesRememberedGalleries); |
| 305 ListValue* list = update.Get(); | 328 ListValue* list = update.Get(); |
| 306 list->Append(CreateGalleryPrefInfoDictionary(gallery_info)); | 329 list->Append(CreateGalleryPrefInfoDictionary(gallery_info)); |
| 307 InitFromPrefs(); | 330 InitFromPrefs(true /* notify observers */); |
| 308 | 331 |
| 309 return gallery_info.pref_id; | 332 return gallery_info.pref_id; |
| 310 } | 333 } |
| 311 | 334 |
| 312 MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryByPath( | 335 MediaGalleryPrefId MediaGalleriesPreferences::AddGalleryByPath( |
| 313 const FilePath& path) { | 336 const FilePath& path) { |
| 314 MediaGalleryPrefInfo gallery_info; | 337 MediaGalleryPrefInfo gallery_info; |
| 315 if (LookUpGalleryByPath(path, &gallery_info) && | 338 if (LookUpGalleryByPath(path, &gallery_info) && |
| 316 gallery_info.type != MediaGalleryPrefInfo::kBlackListed) { | 339 gallery_info.type != MediaGalleryPrefInfo::kBlackListed) { |
| 317 return gallery_info.pref_id; | 340 return gallery_info.pref_id; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 335 pref_id == iter_id) { | 358 pref_id == iter_id) { |
| 336 GetExtensionPrefs()->RemoveMediaGalleryPermissions(pref_id); | 359 GetExtensionPrefs()->RemoveMediaGalleryPermissions(pref_id); |
| 337 MediaGalleryPrefInfo::Type type; | 360 MediaGalleryPrefInfo::Type type; |
| 338 if (GetType(*dict, &type) && | 361 if (GetType(*dict, &type) && |
| 339 type == MediaGalleryPrefInfo::kAutoDetected) { | 362 type == MediaGalleryPrefInfo::kAutoDetected) { |
| 340 dict->SetString(kMediaGalleriesTypeKey, | 363 dict->SetString(kMediaGalleriesTypeKey, |
| 341 kMediaGalleriesTypeBlackListedValue); | 364 kMediaGalleriesTypeBlackListedValue); |
| 342 } else { | 365 } else { |
| 343 list->Erase(iter, NULL); | 366 list->Erase(iter, NULL); |
| 344 } | 367 } |
| 345 InitFromPrefs(); | 368 InitFromPrefs(true /* notify observers */); |
| 346 return; | 369 return; |
| 347 } | 370 } |
| 348 } | 371 } |
| 349 } | 372 } |
| 350 | 373 |
| 351 MediaGalleryPrefIdSet MediaGalleriesPreferences::GalleriesForExtension( | 374 MediaGalleryPrefIdSet MediaGalleriesPreferences::GalleriesForExtension( |
| 352 const extensions::Extension& extension) const { | 375 const extensions::Extension& extension) const { |
| 353 MediaGalleryPrefIdSet result; | 376 MediaGalleryPrefIdSet result; |
| 354 | 377 |
| 355 if (HasAutoDetectedGalleryPermission(extension)) { | 378 if (HasAutoDetectedGalleryPermission(extension)) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 429 } | 452 } |
| 430 | 453 |
| 431 extensions::ExtensionPrefs* | 454 extensions::ExtensionPrefs* |
| 432 MediaGalleriesPreferences::GetExtensionPrefs() const { | 455 MediaGalleriesPreferences::GetExtensionPrefs() const { |
| 433 ExtensionService* extension_service = | 456 ExtensionService* extension_service = |
| 434 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 457 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| 435 return extension_service->extension_prefs(); | 458 return extension_service->extension_prefs(); |
| 436 } | 459 } |
| 437 | 460 |
| 438 } // namespace chrome | 461 } // namespace chrome |
| OLD | NEW |