Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: chrome/browser/extensions/api/media_galleries_private/gallery_watch_state_tracker.cc

Issue 14556015: [Media Galleries] Lazily initialize the storage monitor. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebase Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 if (preferences_ != NULL) {
159 DCHECK_EQ(preferences_, preferences);
160 return;
161 }
162 preferences_ = preferences;
163 }
164
163 void GalleryWatchStateTracker::OnGalleryWatchAdded( 165 void GalleryWatchStateTracker::OnGalleryWatchAdded(
164 const std::string& extension_id, 166 const std::string& extension_id,
165 chrome::MediaGalleryPrefId gallery_id) { 167 chrome::MediaGalleryPrefId gallery_id) {
166 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 168 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
167 bool update_storage = 169 bool update_storage =
168 AddWatchedGalleryIdInfoForExtension(extension_id, gallery_id); 170 AddWatchedGalleryIdInfoForExtension(extension_id, gallery_id);
169 if (update_storage) 171 if (update_storage)
170 WriteToStorage(extension_id); 172 WriteToStorage(extension_id);
171 } 173 }
172 174
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 WatchedGalleryIdsToValue(gallery_ids).PassAs<base::Value>()); 241 WatchedGalleryIdsToValue(gallery_ids).PassAs<base::Value>());
240 } 242 }
241 243
242 void GalleryWatchStateTracker::ReadFromStorage(const std::string& extension_id, 244 void GalleryWatchStateTracker::ReadFromStorage(const std::string& extension_id,
243 scoped_ptr<base::Value> value) { 245 scoped_ptr<base::Value> value) {
244 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 246 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
245 base::ListValue* list = NULL; 247 base::ListValue* list = NULL;
246 if (!value.get() || !value->GetAsList(&list)) 248 if (!value.get() || !value->GetAsList(&list))
247 return; 249 return;
248 chrome::MediaGalleryPrefIdSet gallery_ids = WatchedGalleryIdsFromValue(list); 250 chrome::MediaGalleryPrefIdSet gallery_ids = WatchedGalleryIdsFromValue(list);
251 if (!gallery_ids.empty()) {
252 g_browser_process->media_file_system_registry()->GetPreferencesAsync(
253 profile_,
254 base::Bind(&GalleryWatchStateTracker::ReadFromStorageWithPreferences,
255 AsWeakPtr(), extension_id, gallery_ids));
256 }
257 }
258
259 void GalleryWatchStateTracker::ReadFromStorageWithPreferences(
260 const std::string& extension_id,
261 chrome::MediaGalleryPrefIdSet gallery_ids,
262 chrome::MediaGalleriesPreferences* preferences) {
263 SetPreferences(preferences);
264
249 for (chrome::MediaGalleryPrefIdSet::const_iterator id_iter = 265 for (chrome::MediaGalleryPrefIdSet::const_iterator id_iter =
250 gallery_ids.begin(); 266 gallery_ids.begin();
251 id_iter != gallery_ids.end(); ++id_iter) { 267 id_iter != gallery_ids.end(); ++id_iter) {
252 watched_extensions_map_[extension_id][*id_iter] = false; 268 watched_extensions_map_[extension_id][*id_iter] = false;
253 SetupGalleryWatch(extension_id, *id_iter); 269 SetupGalleryWatch(extension_id, *id_iter);
254 } 270 }
255 } 271 }
256 272
257 void GalleryWatchStateTracker::SetupGalleryWatch( 273 void GalleryWatchStateTracker::SetupGalleryWatch(
258 const std::string& extension_id, 274 const std::string& extension_id,
259 chrome::MediaGalleryPrefId gallery_id) { 275 chrome::MediaGalleryPrefId gallery_id) {
260 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 276 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
261 const Extension* extension = GetExtensionById(profile_, extension_id); 277 const Extension* extension = GetExtensionById(profile_, extension_id);
262 DCHECK(extension); 278 DCHECK(extension);
263 base::FilePath gallery_file_path( 279 DCHECK(preferences_);
264 GetMediaGalleryPreferences(profile_)->LookUpGalleryPathForExtension( 280 base::FilePath gallery_file_path(preferences_->LookUpGalleryPathForExtension(
vandebo (ex-Chrome) 2013/05/16 18:56:27 Why are we sure preferences will be set by this po
Lei Zhang 2013/05/16 21:09:12 I looked at the caller chain and someone must have
Lei Zhang 2013/05/16 22:17:52 Oh, SetupGalleryWatch() has two callers: 1) ReadFr
Greg Billock 2013/05/16 23:27:55 One call is from ReadFromStorageWithPreferences ab
Greg Billock 2013/05/16 23:27:55 Is there a race where the prefs can get initialize
265 gallery_id, extension, false)); 281 gallery_id, extension, false));
266 if (gallery_file_path.empty()) 282 if (gallery_file_path.empty())
267 return; 283 return;
268 MediaGalleriesPrivateEventRouter* router = 284 MediaGalleriesPrivateEventRouter* router =
269 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); 285 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter();
270 DCHECK(router); 286 DCHECK(router);
271 content::BrowserThread::PostTaskAndReplyWithResult( 287 content::BrowserThread::PostTaskAndReplyWithResult(
272 content::BrowserThread::FILE, 288 content::BrowserThread::FILE,
273 FROM_HERE, 289 FROM_HERE,
274 base::Bind(&GalleryWatchManager::SetupGalleryWatch, 290 base::Bind(&GalleryWatchManager::SetupGalleryWatch,
275 profile_, 291 profile_,
276 gallery_id, 292 gallery_id,
277 gallery_file_path, 293 gallery_file_path,
278 extension_id, 294 extension_id,
279 router->AsWeakPtr()), 295 router->AsWeakPtr()),
280 base::Bind(&GalleryWatchStateTracker::HandleSetupGalleryWatchResponse, 296 base::Bind(&GalleryWatchStateTracker::HandleSetupGalleryWatchResponse,
281 AsWeakPtr(), 297 AsWeakPtr(),
282 extension_id, 298 extension_id,
283 gallery_id)); 299 gallery_id));
284 } 300 }
285 301
286 void GalleryWatchStateTracker::RemoveGalleryWatch( 302 void GalleryWatchStateTracker::RemoveGalleryWatch(
287 const std::string& extension_id, 303 const std::string& extension_id,
288 chrome::MediaGalleryPrefId gallery_id) { 304 chrome::MediaGalleryPrefId gallery_id) {
289 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 305 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
290 const Extension* extension = GetExtensionById(profile_, extension_id); 306 const Extension* extension = GetExtensionById(profile_, extension_id);
291 DCHECK(extension); 307 DCHECK(extension);
292 base::FilePath gallery_file_path( 308 DCHECK(preferences_);
293 GetMediaGalleryPreferences(profile_)->LookUpGalleryPathForExtension( 309 base::FilePath gallery_file_path(preferences_->LookUpGalleryPathForExtension(
vandebo (ex-Chrome) 2013/05/16 18:56:27 ditto
Greg Billock 2013/05/16 23:27:55 Call situation here is only from OnGalleryPermissi
294 gallery_id, extension, true)); 310 gallery_id, extension, true));
295 if (gallery_file_path.empty()) 311 if (gallery_file_path.empty())
296 return; 312 return;
297 content::BrowserThread::PostTask( 313 content::BrowserThread::PostTask(
298 content::BrowserThread::FILE, FROM_HERE, 314 content::BrowserThread::FILE, FROM_HERE,
299 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, 315 base::Bind(&GalleryWatchManager::RemoveGalleryWatch,
300 profile_, 316 profile_,
301 gallery_file_path, 317 gallery_file_path,
302 extension_id)); 318 extension_id));
303 watched_extensions_map_[extension_id][gallery_id] = false; 319 watched_extensions_map_[extension_id][gallery_id] = false;
304 } 320 }
(...skipping 23 matching lines...) Expand all
328 const std::string& extension_id, 344 const std::string& extension_id,
329 chrome::MediaGalleryPrefId gallery_id) { 345 chrome::MediaGalleryPrefId gallery_id) {
330 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 346 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
331 if (HasGalleryWatchInfo(extension_id, gallery_id, true)) 347 if (HasGalleryWatchInfo(extension_id, gallery_id, true))
332 return false; 348 return false;
333 watched_extensions_map_[extension_id][gallery_id] = true; 349 watched_extensions_map_[extension_id][gallery_id] = true;
334 return true; 350 return true;
335 } 351 }
336 352
337 } // namespace extensions 353 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698