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

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

Issue 15742010: Use MediaGalleryPreferences directly in GalleryWatchStateTracker interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Make sure to get all call sites Created 7 years, 6 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 | Annotate | Revision Log
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) {
87 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 79 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
88 DCHECK(profile_); 80 DCHECK(profile_);
89 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, 81 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
90 content::Source<Profile>(profile_)); 82 content::Source<Profile>(profile_));
91 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 83 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
92 content::Source<Profile>(profile_)); 84 content::Source<Profile>(profile_));
(...skipping 16 matching lines...) Expand all
109 if (!private_api) 101 if (!private_api)
110 return NULL; // In unit tests, we don't have a MediaGalleriesPrivateAPI. 102 return NULL; // In unit tests, we don't have a MediaGalleriesPrivateAPI.
111 return private_api->GetGalleryWatchStateTracker(); 103 return private_api->GetGalleryWatchStateTracker();
112 #endif 104 #endif
113 return NULL; 105 return NULL;
114 } 106 }
115 107
116 void GalleryWatchStateTracker::OnGalleryPermissionChanged( 108 void GalleryWatchStateTracker::OnGalleryPermissionChanged(
117 const std::string& extension_id, 109 const std::string& extension_id,
118 chrome::MediaGalleryPrefId gallery_id, 110 chrome::MediaGalleryPrefId gallery_id,
119 bool has_permission) { 111 bool has_permission,
112 chrome::MediaGalleriesPreferences* preferences) {
120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 113 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
121 // Granted gallery permission. 114 // Granted gallery permission.
122 if (has_permission && HasGalleryWatchInfo(extension_id, gallery_id, false)) { 115 if (has_permission && HasGalleryWatchInfo(extension_id, gallery_id, false)) {
123 SetupGalleryWatch(extension_id, gallery_id); 116 SetupGalleryWatch(extension_id, gallery_id, preferences);
124 return; 117 return;
125 } 118 }
126 119
127 // Revoked gallery permission. 120 // Revoked gallery permission.
128 if (!has_permission && HasGalleryWatchInfo(extension_id, gallery_id, true)) 121 if (!has_permission && HasGalleryWatchInfo(extension_id, gallery_id, true))
129 RemoveGalleryWatch(extension_id, gallery_id); 122 RemoveGalleryWatch(extension_id, gallery_id, preferences);
130 } 123 }
131 124
132 chrome::MediaGalleryPrefIdSet 125 chrome::MediaGalleryPrefIdSet
133 GalleryWatchStateTracker::GetAllWatchedGalleryIDsForExtension( 126 GalleryWatchStateTracker::GetAllWatchedGalleryIDsForExtension(
134 const std::string& extension_id) const { 127 const std::string& extension_id) const {
135 chrome::MediaGalleryPrefIdSet gallery_ids; 128 chrome::MediaGalleryPrefIdSet gallery_ids;
136 WatchedExtensionsMap::const_iterator extension_id_iter = 129 WatchedExtensionsMap::const_iterator extension_id_iter =
137 watched_extensions_map_.find(extension_id); 130 watched_extensions_map_.find(extension_id);
138 if (extension_id_iter != watched_extensions_map_.end()) { 131 if (extension_id_iter != watched_extensions_map_.end()) {
139 for (WatchedGalleriesMap::const_iterator gallery_id_iter = 132 for (WatchedGalleriesMap::const_iterator gallery_id_iter =
140 extension_id_iter->second.begin(); 133 extension_id_iter->second.begin();
141 gallery_id_iter != extension_id_iter->second.end(); 134 gallery_id_iter != extension_id_iter->second.end();
142 ++gallery_id_iter) { 135 ++gallery_id_iter) {
143 gallery_ids.insert(gallery_id_iter->first); 136 gallery_ids.insert(gallery_id_iter->first);
144 } 137 }
145 } 138 }
146 return gallery_ids; 139 return gallery_ids;
147 } 140 }
148 141
149 void GalleryWatchStateTracker::RemoveAllGalleryWatchersForExtension( 142 void GalleryWatchStateTracker::RemoveAllGalleryWatchersForExtension(
150 const std::string& extension_id) { 143 const std::string& extension_id,
144 chrome::MediaGalleriesPreferences* preferences) {
151 WatchedExtensionsMap::iterator extension_id_iter = 145 WatchedExtensionsMap::iterator extension_id_iter =
152 watched_extensions_map_.find(extension_id); 146 watched_extensions_map_.find(extension_id);
153 if (extension_id_iter == watched_extensions_map_.end()) 147 if (extension_id_iter == watched_extensions_map_.end())
154 return; 148 return;
155 const WatchedGalleriesMap& galleries = extension_id_iter->second; 149 const WatchedGalleriesMap& galleries = extension_id_iter->second;
156 for (WatchedGalleriesMap::const_iterator gallery_id_iter = galleries.begin(); 150 for (WatchedGalleriesMap::const_iterator gallery_id_iter = galleries.begin();
157 gallery_id_iter != galleries.end(); ++gallery_id_iter) 151 gallery_id_iter != galleries.end(); ++gallery_id_iter)
158 RemoveGalleryWatch(extension_id, gallery_id_iter->second); 152 RemoveGalleryWatch(extension_id, gallery_id_iter->second, preferences);
159 watched_extensions_map_.erase(extension_id_iter); 153 watched_extensions_map_.erase(extension_id_iter);
160 WriteToStorage(extension_id); 154 WriteToStorage(extension_id);
161 } 155 }
162 156
163 void GalleryWatchStateTracker::OnGalleryWatchAdded( 157 void GalleryWatchStateTracker::OnGalleryWatchAdded(
164 const std::string& extension_id, 158 const std::string& extension_id,
165 chrome::MediaGalleryPrefId gallery_id) { 159 chrome::MediaGalleryPrefId gallery_id) {
166 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 160 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
167 bool update_storage = 161 bool update_storage =
168 AddWatchedGalleryIdInfoForExtension(extension_id, gallery_id); 162 AddWatchedGalleryIdInfoForExtension(extension_id, gallery_id);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 if (!storage) 226 if (!storage)
233 return; 227 return;
234 chrome::MediaGalleryPrefIdSet gallery_ids = 228 chrome::MediaGalleryPrefIdSet gallery_ids =
235 GetAllWatchedGalleryIDsForExtension(extension_id); 229 GetAllWatchedGalleryIDsForExtension(extension_id);
236 storage->SetExtensionValue( 230 storage->SetExtensionValue(
237 extension_id, 231 extension_id,
238 kRegisteredGalleryWatchers, 232 kRegisteredGalleryWatchers,
239 WatchedGalleryIdsToValue(gallery_ids).PassAs<base::Value>()); 233 WatchedGalleryIdsToValue(gallery_ids).PassAs<base::Value>());
240 } 234 }
241 235
242 void GalleryWatchStateTracker::ReadFromStorage(const std::string& extension_id, 236 void GalleryWatchStateTracker::ReadFromStorage(
243 scoped_ptr<base::Value> value) { 237 const std::string& extension_id,
238 scoped_ptr<base::Value> value) {
244 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 239 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
240 chrome::MediaGalleriesPreferences* preferences =
241 g_browser_process->media_file_system_registry()->GetPreferences(profile_);
245 base::ListValue* list = NULL; 242 base::ListValue* list = NULL;
246 if (!value.get() || !value->GetAsList(&list)) 243 if (!value.get() || !value->GetAsList(&list))
247 return; 244 return;
248 chrome::MediaGalleryPrefIdSet gallery_ids = WatchedGalleryIdsFromValue(list); 245 chrome::MediaGalleryPrefIdSet gallery_ids = WatchedGalleryIdsFromValue(list);
246 if (gallery_ids.empty())
247 return;
248
249 for (chrome::MediaGalleryPrefIdSet::const_iterator id_iter = 249 for (chrome::MediaGalleryPrefIdSet::const_iterator id_iter =
250 gallery_ids.begin(); 250 gallery_ids.begin();
251 id_iter != gallery_ids.end(); ++id_iter) { 251 id_iter != gallery_ids.end(); ++id_iter) {
252 watched_extensions_map_[extension_id][*id_iter] = false; 252 watched_extensions_map_[extension_id][*id_iter] = false;
253 SetupGalleryWatch(extension_id, *id_iter); 253 SetupGalleryWatch(extension_id, *id_iter, preferences);
254 } 254 }
255 } 255 }
256 256
257 void GalleryWatchStateTracker::SetupGalleryWatch( 257 void GalleryWatchStateTracker::SetupGalleryWatch(
258 const std::string& extension_id, 258 const std::string& extension_id,
259 chrome::MediaGalleryPrefId gallery_id) { 259 chrome::MediaGalleryPrefId gallery_id,
260 chrome::MediaGalleriesPreferences* preferences) {
260 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 261 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
261 const Extension* extension = GetExtensionById(profile_, extension_id); 262 const Extension* extension = GetExtensionById(profile_, extension_id);
262 DCHECK(extension); 263 DCHECK(extension);
263 base::FilePath gallery_file_path( 264 base::FilePath gallery_file_path(preferences->LookUpGalleryPathForExtension(
264 GetMediaGalleryPreferences(profile_)->LookUpGalleryPathForExtension( 265 gallery_id, extension, false));
265 gallery_id, extension, false));
266 if (gallery_file_path.empty()) 266 if (gallery_file_path.empty())
267 return; 267 return;
268 MediaGalleriesPrivateEventRouter* router = 268 MediaGalleriesPrivateEventRouter* router =
269 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); 269 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter();
270 DCHECK(router); 270 DCHECK(router);
271 content::BrowserThread::PostTaskAndReplyWithResult( 271 content::BrowserThread::PostTaskAndReplyWithResult(
272 content::BrowserThread::FILE, 272 content::BrowserThread::FILE,
273 FROM_HERE, 273 FROM_HERE,
274 base::Bind(&GalleryWatchManager::SetupGalleryWatch, 274 base::Bind(&GalleryWatchManager::SetupGalleryWatch,
275 profile_, 275 profile_,
276 gallery_id, 276 gallery_id,
277 gallery_file_path, 277 gallery_file_path,
278 extension_id, 278 extension_id,
279 router->AsWeakPtr()), 279 router->AsWeakPtr()),
280 base::Bind(&GalleryWatchStateTracker::HandleSetupGalleryWatchResponse, 280 base::Bind(&GalleryWatchStateTracker::HandleSetupGalleryWatchResponse,
281 AsWeakPtr(), 281 AsWeakPtr(),
282 extension_id, 282 extension_id,
283 gallery_id)); 283 gallery_id));
284 } 284 }
285 285
286 void GalleryWatchStateTracker::RemoveGalleryWatch( 286 void GalleryWatchStateTracker::RemoveGalleryWatch(
287 const std::string& extension_id, 287 const std::string& extension_id,
288 chrome::MediaGalleryPrefId gallery_id) { 288 chrome::MediaGalleryPrefId gallery_id,
289 chrome::MediaGalleriesPreferences* preferences) {
289 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 290 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
290 const Extension* extension = GetExtensionById(profile_, extension_id); 291 const Extension* extension = GetExtensionById(profile_, extension_id);
291 DCHECK(extension); 292 DCHECK(extension);
292 base::FilePath gallery_file_path( 293 base::FilePath gallery_file_path(preferences->LookUpGalleryPathForExtension(
293 GetMediaGalleryPreferences(profile_)->LookUpGalleryPathForExtension( 294 gallery_id, extension, true));
294 gallery_id, extension, true));
295 if (gallery_file_path.empty()) 295 if (gallery_file_path.empty())
296 return; 296 return;
297 content::BrowserThread::PostTask( 297 content::BrowserThread::PostTask(
298 content::BrowserThread::FILE, FROM_HERE, 298 content::BrowserThread::FILE, FROM_HERE,
299 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, 299 base::Bind(&GalleryWatchManager::RemoveGalleryWatch,
300 profile_, 300 profile_,
301 gallery_file_path, 301 gallery_file_path,
302 extension_id)); 302 extension_id));
303 watched_extensions_map_[extension_id][gallery_id] = false; 303 watched_extensions_map_[extension_id][gallery_id] = false;
304 } 304 }
(...skipping 23 matching lines...) Expand all
328 const std::string& extension_id, 328 const std::string& extension_id,
329 chrome::MediaGalleryPrefId gallery_id) { 329 chrome::MediaGalleryPrefId gallery_id) {
330 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 330 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
331 if (HasGalleryWatchInfo(extension_id, gallery_id, true)) 331 if (HasGalleryWatchInfo(extension_id, gallery_id, true))
332 return false; 332 return false;
333 watched_extensions_map_[extension_id][gallery_id] = true; 333 watched_extensions_map_[extension_id][gallery_id] = true;
334 return true; 334 return true;
335 } 335 }
336 336
337 } // namespace extensions 337 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698