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 // GalleryWatchStateTracker implementation. | 5 // GalleryWatchStateTracker implementation. |
6 | 6 |
7 #include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_st ate_tracker.h" | 7 #include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_st ate_tracker.h" |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 | 65 |
66 // Looks up an extension by ID. Does not include disabled extensions. | 66 // Looks up an extension by ID. Does not include disabled extensions. |
67 const Extension* GetExtensionById(Profile* profile, | 67 const Extension* GetExtensionById(Profile* profile, |
68 const std::string& extension_id) { | 68 const std::string& extension_id) { |
69 ExtensionService* service = profile->GetExtensionService(); | 69 ExtensionService* service = profile->GetExtensionService(); |
70 if (!service) | 70 if (!service) |
71 return NULL; | 71 return NULL; |
72 return service->GetExtensionById(extension_id, false); | 72 return service->GetExtensionById(extension_id, false); |
73 } | 73 } |
74 | 74 |
75 // Returns the initialized media galleries preferences for the specified | |
76 // |profile|. | |
77 chrome::MediaGalleriesPreferences* GetMediaGalleryPreferences( | |
78 Profile* profile) { | |
79 return g_browser_process->media_file_system_registry()->GetPreferences( | |
80 profile); | |
81 } | |
82 | |
83 } // namespace | 75 } // namespace |
84 | 76 |
85 GalleryWatchStateTracker::GalleryWatchStateTracker(Profile* profile) | 77 GalleryWatchStateTracker::GalleryWatchStateTracker(Profile* profile) |
86 : profile_(profile) { | 78 : profile_(profile), |
79 preferences_(NULL) { | |
87 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 80 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
88 DCHECK(profile_); | 81 DCHECK(profile_); |
89 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 82 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
90 content::Source<Profile>(profile_)); | 83 content::Source<Profile>(profile_)); |
91 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 84 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
92 content::Source<Profile>(profile_)); | 85 content::Source<Profile>(profile_)); |
93 } | 86 } |
94 | 87 |
95 GalleryWatchStateTracker::~GalleryWatchStateTracker() { | 88 GalleryWatchStateTracker::~GalleryWatchStateTracker() { |
96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 89 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 if (extension_id_iter == watched_extensions_map_.end()) | 146 if (extension_id_iter == watched_extensions_map_.end()) |
154 return; | 147 return; |
155 const WatchedGalleriesMap& galleries = extension_id_iter->second; | 148 const WatchedGalleriesMap& galleries = extension_id_iter->second; |
156 for (WatchedGalleriesMap::const_iterator gallery_id_iter = galleries.begin(); | 149 for (WatchedGalleriesMap::const_iterator gallery_id_iter = galleries.begin(); |
157 gallery_id_iter != galleries.end(); ++gallery_id_iter) | 150 gallery_id_iter != galleries.end(); ++gallery_id_iter) |
158 RemoveGalleryWatch(extension_id, gallery_id_iter->second); | 151 RemoveGalleryWatch(extension_id, gallery_id_iter->second); |
159 watched_extensions_map_.erase(extension_id_iter); | 152 watched_extensions_map_.erase(extension_id_iter); |
160 WriteToStorage(extension_id); | 153 WriteToStorage(extension_id); |
161 } | 154 } |
162 | 155 |
156 void GalleryWatchStateTracker::SetPreferences( | |
157 chrome::MediaGalleriesPreferences* preferences) { | |
158 preferences_ = preferences; | |
159 } | |
160 | |
163 void GalleryWatchStateTracker::OnGalleryWatchAdded( | 161 void GalleryWatchStateTracker::OnGalleryWatchAdded( |
164 const std::string& extension_id, | 162 const std::string& extension_id, |
165 chrome::MediaGalleryPrefId gallery_id) { | 163 chrome::MediaGalleryPrefId gallery_id) { |
166 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 164 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
167 bool update_storage = | 165 bool update_storage = |
168 AddWatchedGalleryIdInfoForExtension(extension_id, gallery_id); | 166 AddWatchedGalleryIdInfoForExtension(extension_id, gallery_id); |
169 if (update_storage) | 167 if (update_storage) |
170 WriteToStorage(extension_id); | 168 WriteToStorage(extension_id); |
171 } | 169 } |
172 | 170 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 WatchedGalleryIdsToValue(gallery_ids).PassAs<base::Value>()); | 237 WatchedGalleryIdsToValue(gallery_ids).PassAs<base::Value>()); |
240 } | 238 } |
241 | 239 |
242 void GalleryWatchStateTracker::ReadFromStorage(const std::string& extension_id, | 240 void GalleryWatchStateTracker::ReadFromStorage(const std::string& extension_id, |
243 scoped_ptr<base::Value> value) { | 241 scoped_ptr<base::Value> value) { |
244 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 242 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
245 base::ListValue* list = NULL; | 243 base::ListValue* list = NULL; |
246 if (!value.get() || !value->GetAsList(&list)) | 244 if (!value.get() || !value->GetAsList(&list)) |
247 return; | 245 return; |
248 chrome::MediaGalleryPrefIdSet gallery_ids = WatchedGalleryIdsFromValue(list); | 246 chrome::MediaGalleryPrefIdSet gallery_ids = WatchedGalleryIdsFromValue(list); |
247 if (gallery_ids.size() > 0) { | |
248 g_browser_process->media_file_system_registry()->GetPreferencesAsync( | |
249 profile_, | |
250 base::Bind(&GalleryWatchStateTracker::ReadFromStorageWithPreferences, | |
251 AsWeakPtr(), extension_id, gallery_ids)); | |
252 } | |
253 } | |
254 | |
255 void GalleryWatchStateTracker::ReadFromStorageWithPreferences( | |
256 const std::string& extension_id, | |
257 chrome::MediaGalleryPrefIdSet gallery_ids, | |
258 chrome::MediaGalleriesPreferences* preferences) { | |
259 SetPreferences(preferences); | |
Lei Zhang
2013/05/14 04:12:23
Do we need both this and the SetPreferences() call
Greg Billock
2013/05/14 21:27:13
I think they get touched in different paths. So th
| |
260 | |
249 for (chrome::MediaGalleryPrefIdSet::const_iterator id_iter = | 261 for (chrome::MediaGalleryPrefIdSet::const_iterator id_iter = |
250 gallery_ids.begin(); | 262 gallery_ids.begin(); |
251 id_iter != gallery_ids.end(); ++id_iter) { | 263 id_iter != gallery_ids.end(); ++id_iter) { |
252 watched_extensions_map_[extension_id][*id_iter] = false; | 264 watched_extensions_map_[extension_id][*id_iter] = false; |
253 SetupGalleryWatch(extension_id, *id_iter); | 265 SetupGalleryWatch(extension_id, *id_iter); |
254 } | 266 } |
255 } | 267 } |
256 | 268 |
257 void GalleryWatchStateTracker::SetupGalleryWatch( | 269 void GalleryWatchStateTracker::SetupGalleryWatch( |
258 const std::string& extension_id, | 270 const std::string& extension_id, |
259 chrome::MediaGalleryPrefId gallery_id) { | 271 chrome::MediaGalleryPrefId gallery_id) { |
260 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 272 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
261 const Extension* extension = GetExtensionById(profile_, extension_id); | 273 const Extension* extension = GetExtensionById(profile_, extension_id); |
262 DCHECK(extension); | 274 DCHECK(extension); |
263 base::FilePath gallery_file_path( | 275 DCHECK(preferences_); |
264 GetMediaGalleryPreferences(profile_)->LookUpGalleryPathForExtension( | 276 base::FilePath gallery_file_path(preferences_->LookUpGalleryPathForExtension( |
265 gallery_id, extension, false)); | 277 gallery_id, extension, false)); |
266 if (gallery_file_path.empty()) | 278 if (gallery_file_path.empty()) |
267 return; | 279 return; |
268 MediaGalleriesPrivateEventRouter* router = | 280 MediaGalleriesPrivateEventRouter* router = |
269 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); | 281 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); |
270 DCHECK(router); | 282 DCHECK(router); |
271 content::BrowserThread::PostTaskAndReplyWithResult( | 283 content::BrowserThread::PostTaskAndReplyWithResult( |
272 content::BrowserThread::FILE, | 284 content::BrowserThread::FILE, |
273 FROM_HERE, | 285 FROM_HERE, |
274 base::Bind(&GalleryWatchManager::SetupGalleryWatch, | 286 base::Bind(&GalleryWatchManager::SetupGalleryWatch, |
275 profile_, | 287 profile_, |
276 gallery_id, | 288 gallery_id, |
277 gallery_file_path, | 289 gallery_file_path, |
278 extension_id, | 290 extension_id, |
279 router->AsWeakPtr()), | 291 router->AsWeakPtr()), |
280 base::Bind(&GalleryWatchStateTracker::HandleSetupGalleryWatchResponse, | 292 base::Bind(&GalleryWatchStateTracker::HandleSetupGalleryWatchResponse, |
281 AsWeakPtr(), | 293 AsWeakPtr(), |
282 extension_id, | 294 extension_id, |
283 gallery_id)); | 295 gallery_id)); |
284 } | 296 } |
285 | 297 |
286 void GalleryWatchStateTracker::RemoveGalleryWatch( | 298 void GalleryWatchStateTracker::RemoveGalleryWatch( |
287 const std::string& extension_id, | 299 const std::string& extension_id, |
288 chrome::MediaGalleryPrefId gallery_id) { | 300 chrome::MediaGalleryPrefId gallery_id) { |
289 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 301 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
290 const Extension* extension = GetExtensionById(profile_, extension_id); | 302 const Extension* extension = GetExtensionById(profile_, extension_id); |
291 DCHECK(extension); | 303 DCHECK(extension); |
292 base::FilePath gallery_file_path( | 304 DCHECK(preferences_); |
293 GetMediaGalleryPreferences(profile_)->LookUpGalleryPathForExtension( | 305 base::FilePath gallery_file_path(preferences_->LookUpGalleryPathForExtension( |
294 gallery_id, extension, true)); | 306 gallery_id, extension, true)); |
295 if (gallery_file_path.empty()) | 307 if (gallery_file_path.empty()) |
296 return; | 308 return; |
297 content::BrowserThread::PostTask( | 309 content::BrowserThread::PostTask( |
298 content::BrowserThread::FILE, FROM_HERE, | 310 content::BrowserThread::FILE, FROM_HERE, |
299 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, | 311 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, |
300 profile_, | 312 profile_, |
301 gallery_file_path, | 313 gallery_file_path, |
302 extension_id)); | 314 extension_id)); |
303 watched_extensions_map_[extension_id][gallery_id] = false; | 315 watched_extensions_map_[extension_id][gallery_id] = false; |
304 } | 316 } |
(...skipping 23 matching lines...) Expand all Loading... | |
328 const std::string& extension_id, | 340 const std::string& extension_id, |
329 chrome::MediaGalleryPrefId gallery_id) { | 341 chrome::MediaGalleryPrefId gallery_id) { |
330 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 342 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
331 if (HasGalleryWatchInfo(extension_id, gallery_id, true)) | 343 if (HasGalleryWatchInfo(extension_id, gallery_id, true)) |
332 return false; | 344 return false; |
333 watched_extensions_map_[extension_id][gallery_id] = true; | 345 watched_extensions_map_[extension_id][gallery_id] = true; |
334 return true; | 346 return true; |
335 } | 347 } |
336 | 348 |
337 } // namespace extensions | 349 } // namespace extensions |
OLD | NEW |