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" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_ma nager.h" | 16 #include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_ma nager.h" |
17 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_api.h" | 17 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_api.h" |
18 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_event_router.h" | 18 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_event_router.h" |
19 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
20 #include "chrome/browser/extensions/extension_system.h" | 20 #include "chrome/browser/extensions/extension_system.h" |
21 #include "chrome/browser/extensions/state_store.h" | 21 #include "chrome/browser/extensions/state_store.h" |
22 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 22 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
23 #include "chrome/browser/media_galleries/media_galleries_preferences.h" | 23 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
24 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/storage_monitor/storage_monitor.h" | |
25 #include "chrome/common/chrome_notification_types.h" | 26 #include "chrome/common/chrome_notification_types.h" |
26 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
27 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
28 #include "content/public/browser/notification_details.h" | 29 #include "content/public/browser/notification_details.h" |
29 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
30 | 31 |
31 namespace extensions { | 32 namespace extensions { |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
(...skipping 30 matching lines...) Expand all Loading... | |
65 | 66 |
66 // Looks up an extension by ID. Does not include disabled extensions. | 67 // Looks up an extension by ID. Does not include disabled extensions. |
67 const Extension* GetExtensionById(Profile* profile, | 68 const Extension* GetExtensionById(Profile* profile, |
68 const std::string& extension_id) { | 69 const std::string& extension_id) { |
69 ExtensionService* service = profile->GetExtensionService(); | 70 ExtensionService* service = profile->GetExtensionService(); |
70 if (!service) | 71 if (!service) |
71 return NULL; | 72 return NULL; |
72 return service->GetExtensionById(extension_id, false); | 73 return service->GetExtensionById(extension_id, false); |
73 } | 74 } |
74 | 75 |
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 | 76 } // namespace |
84 | 77 |
85 GalleryWatchStateTracker::GalleryWatchStateTracker(Profile* profile) | 78 GalleryWatchStateTracker::GalleryWatchStateTracker(Profile* profile) |
86 : profile_(profile) { | 79 : profile_(profile) { |
87 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 80 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
88 DCHECK(profile_); | 81 DCHECK(profile_); |
82 chrome::StorageMonitor::GetInstance()->Initialize( | |
83 base::Bind(&GalleryWatchStateTracker::OnStorageMonitorInitialized, | |
84 AsWeakPtr())); | |
85 } | |
86 | |
87 void GalleryWatchStateTracker::OnStorageMonitorInitialized() { | |
89 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 88 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
vandebo (ex-Chrome)
2013/05/22 16:27:52
Why do we need to wait for storage monitor to be i
Greg Billock
2013/05/22 18:42:52
Observe() calls ReadFromStorage(), which requires
vandebo (ex-Chrome)
2013/05/22 21:40:34
I dug into this a bit and I think it can be untang
Greg Billock
2013/05/23 00:55:59
Shall I do that here? That's part of the refactor
vandebo (ex-Chrome)
2013/05/23 15:04:17
Seems like that too would benefit from its own CL.
Greg Billock
2013/05/30 22:17:47
It was in the other one
| |
90 content::Source<Profile>(profile_)); | 89 content::Source<Profile>(profile_)); |
91 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 90 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
92 content::Source<Profile>(profile_)); | 91 content::Source<Profile>(profile_)); |
93 } | 92 } |
94 | 93 |
95 GalleryWatchStateTracker::~GalleryWatchStateTracker() { | 94 GalleryWatchStateTracker::~GalleryWatchStateTracker() { |
96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 95 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
97 } | 96 } |
98 | 97 |
99 // static | 98 // static |
100 GalleryWatchStateTracker* GalleryWatchStateTracker::GetForProfile( | 99 GalleryWatchStateTracker* GalleryWatchStateTracker::GetForProfile( |
101 Profile* profile) { | 100 Profile* profile) { |
102 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 101 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
103 #if defined(OS_WIN) | 102 #if defined(OS_WIN) |
104 // Gallery watch operation is supported only on windows. | 103 // Gallery watch operation is supported only on windows. |
105 // Please refer to crbug.com/144491 for more details. | 104 // Please refer to crbug.com/144491 for more details. |
106 DCHECK(profile); | 105 DCHECK(profile); |
107 MediaGalleriesPrivateAPI* private_api = | 106 MediaGalleriesPrivateAPI* private_api = |
108 MediaGalleriesPrivateAPI::Get(profile); | 107 MediaGalleriesPrivateAPI::Get(profile); |
109 if (!private_api) | 108 if (!private_api) |
110 return NULL; // In unit tests, we don't have a MediaGalleriesPrivateAPI. | 109 return NULL; // In unit tests, we don't have a MediaGalleriesPrivateAPI. |
111 return private_api->GetGalleryWatchStateTracker(); | 110 return private_api->GetGalleryWatchStateTracker(); |
112 #endif | 111 #endif |
113 return NULL; | 112 return NULL; |
114 } | 113 } |
115 | 114 |
116 void GalleryWatchStateTracker::OnGalleryPermissionChanged( | 115 void GalleryWatchStateTracker::OnGalleryPermissionChanged( |
117 const std::string& extension_id, | 116 const std::string& extension_id, |
118 chrome::MediaGalleryPrefId gallery_id, | 117 chrome::MediaGalleryPrefId gallery_id, |
119 bool has_permission) { | 118 bool has_permission, |
119 chrome::MediaGalleriesPreferences* preferences) { | |
120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
121 // Granted gallery permission. | 121 // Granted gallery permission. |
122 if (has_permission && HasGalleryWatchInfo(extension_id, gallery_id, false)) { | 122 if (has_permission && HasGalleryWatchInfo(extension_id, gallery_id, false)) { |
123 SetupGalleryWatch(extension_id, gallery_id); | 123 SetupGalleryWatch(extension_id, gallery_id, preferences); |
124 return; | 124 return; |
125 } | 125 } |
126 | 126 |
127 // Revoked gallery permission. | 127 // Revoked gallery permission. |
128 if (!has_permission && HasGalleryWatchInfo(extension_id, gallery_id, true)) | 128 if (!has_permission && HasGalleryWatchInfo(extension_id, gallery_id, true)) |
129 RemoveGalleryWatch(extension_id, gallery_id); | 129 RemoveGalleryWatch(extension_id, gallery_id, preferences); |
130 } | 130 } |
131 | 131 |
132 chrome::MediaGalleryPrefIdSet | 132 chrome::MediaGalleryPrefIdSet |
133 GalleryWatchStateTracker::GetAllWatchedGalleryIDsForExtension( | 133 GalleryWatchStateTracker::GetAllWatchedGalleryIDsForExtension( |
134 const std::string& extension_id) const { | 134 const std::string& extension_id) const { |
135 chrome::MediaGalleryPrefIdSet gallery_ids; | 135 chrome::MediaGalleryPrefIdSet gallery_ids; |
136 WatchedExtensionsMap::const_iterator extension_id_iter = | 136 WatchedExtensionsMap::const_iterator extension_id_iter = |
137 watched_extensions_map_.find(extension_id); | 137 watched_extensions_map_.find(extension_id); |
138 if (extension_id_iter != watched_extensions_map_.end()) { | 138 if (extension_id_iter != watched_extensions_map_.end()) { |
139 for (WatchedGalleriesMap::const_iterator gallery_id_iter = | 139 for (WatchedGalleriesMap::const_iterator gallery_id_iter = |
140 extension_id_iter->second.begin(); | 140 extension_id_iter->second.begin(); |
141 gallery_id_iter != extension_id_iter->second.end(); | 141 gallery_id_iter != extension_id_iter->second.end(); |
142 ++gallery_id_iter) { | 142 ++gallery_id_iter) { |
143 gallery_ids.insert(gallery_id_iter->first); | 143 gallery_ids.insert(gallery_id_iter->first); |
144 } | 144 } |
145 } | 145 } |
146 return gallery_ids; | 146 return gallery_ids; |
147 } | 147 } |
148 | 148 |
149 void GalleryWatchStateTracker::RemoveAllGalleryWatchersForExtension( | 149 void GalleryWatchStateTracker::RemoveAllGalleryWatchersForExtension( |
150 const std::string& extension_id) { | 150 const std::string& extension_id, |
151 chrome::MediaGalleriesPreferences* preferences) { | |
151 WatchedExtensionsMap::iterator extension_id_iter = | 152 WatchedExtensionsMap::iterator extension_id_iter = |
152 watched_extensions_map_.find(extension_id); | 153 watched_extensions_map_.find(extension_id); |
153 if (extension_id_iter == watched_extensions_map_.end()) | 154 if (extension_id_iter == watched_extensions_map_.end()) |
154 return; | 155 return; |
155 const WatchedGalleriesMap& galleries = extension_id_iter->second; | 156 const WatchedGalleriesMap& galleries = extension_id_iter->second; |
156 for (WatchedGalleriesMap::const_iterator gallery_id_iter = galleries.begin(); | 157 for (WatchedGalleriesMap::const_iterator gallery_id_iter = galleries.begin(); |
157 gallery_id_iter != galleries.end(); ++gallery_id_iter) | 158 gallery_id_iter != galleries.end(); ++gallery_id_iter) |
158 RemoveGalleryWatch(extension_id, gallery_id_iter->second); | 159 RemoveGalleryWatch(extension_id, gallery_id_iter->second, preferences); |
159 watched_extensions_map_.erase(extension_id_iter); | 160 watched_extensions_map_.erase(extension_id_iter); |
160 WriteToStorage(extension_id); | 161 WriteToStorage(extension_id); |
161 } | 162 } |
162 | 163 |
163 void GalleryWatchStateTracker::OnGalleryWatchAdded( | 164 void GalleryWatchStateTracker::OnGalleryWatchAdded( |
164 const std::string& extension_id, | 165 const std::string& extension_id, |
165 chrome::MediaGalleryPrefId gallery_id) { | 166 chrome::MediaGalleryPrefId gallery_id) { |
166 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 167 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
167 bool update_storage = | 168 bool update_storage = |
168 AddWatchedGalleryIdInfoForExtension(extension_id, gallery_id); | 169 AddWatchedGalleryIdInfoForExtension(extension_id, gallery_id); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 if (!storage) | 233 if (!storage) |
233 return; | 234 return; |
234 chrome::MediaGalleryPrefIdSet gallery_ids = | 235 chrome::MediaGalleryPrefIdSet gallery_ids = |
235 GetAllWatchedGalleryIDsForExtension(extension_id); | 236 GetAllWatchedGalleryIDsForExtension(extension_id); |
236 storage->SetExtensionValue( | 237 storage->SetExtensionValue( |
237 extension_id, | 238 extension_id, |
238 kRegisteredGalleryWatchers, | 239 kRegisteredGalleryWatchers, |
239 WatchedGalleryIdsToValue(gallery_ids).PassAs<base::Value>()); | 240 WatchedGalleryIdsToValue(gallery_ids).PassAs<base::Value>()); |
240 } | 241 } |
241 | 242 |
242 void GalleryWatchStateTracker::ReadFromStorage(const std::string& extension_id, | 243 void GalleryWatchStateTracker::ReadFromStorage( |
243 scoped_ptr<base::Value> value) { | 244 const std::string& extension_id, |
245 scoped_ptr<base::Value> value) { | |
244 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 246 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
247 chrome::MediaGalleriesPreferences* preferences = | |
vandebo (ex-Chrome)
2013/05/22 16:27:52
DCHECK that StorageMonitor is initialized?
Greg Billock
2013/05/22 18:42:52
This is done in GetPreferences().
| |
248 g_browser_process->media_file_system_registry()->GetPreferences(profile_); | |
245 base::ListValue* list = NULL; | 249 base::ListValue* list = NULL; |
246 if (!value.get() || !value->GetAsList(&list)) | 250 if (!value.get() || !value->GetAsList(&list)) |
247 return; | 251 return; |
248 chrome::MediaGalleryPrefIdSet gallery_ids = WatchedGalleryIdsFromValue(list); | 252 chrome::MediaGalleryPrefIdSet gallery_ids = WatchedGalleryIdsFromValue(list); |
253 if (gallery_ids.empty()) | |
254 return; | |
255 | |
249 for (chrome::MediaGalleryPrefIdSet::const_iterator id_iter = | 256 for (chrome::MediaGalleryPrefIdSet::const_iterator id_iter = |
250 gallery_ids.begin(); | 257 gallery_ids.begin(); |
251 id_iter != gallery_ids.end(); ++id_iter) { | 258 id_iter != gallery_ids.end(); ++id_iter) { |
252 watched_extensions_map_[extension_id][*id_iter] = false; | 259 watched_extensions_map_[extension_id][*id_iter] = false; |
253 SetupGalleryWatch(extension_id, *id_iter); | 260 SetupGalleryWatch(extension_id, *id_iter, preferences); |
254 } | 261 } |
255 } | 262 } |
256 | 263 |
257 void GalleryWatchStateTracker::SetupGalleryWatch( | 264 void GalleryWatchStateTracker::SetupGalleryWatch( |
258 const std::string& extension_id, | 265 const std::string& extension_id, |
259 chrome::MediaGalleryPrefId gallery_id) { | 266 chrome::MediaGalleryPrefId gallery_id, |
267 chrome::MediaGalleriesPreferences* preferences) { | |
260 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 268 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
261 const Extension* extension = GetExtensionById(profile_, extension_id); | 269 const Extension* extension = GetExtensionById(profile_, extension_id); |
262 DCHECK(extension); | 270 DCHECK(extension); |
263 base::FilePath gallery_file_path( | 271 base::FilePath gallery_file_path(preferences->LookUpGalleryPathForExtension( |
264 GetMediaGalleryPreferences(profile_)->LookUpGalleryPathForExtension( | 272 gallery_id, extension, false)); |
265 gallery_id, extension, false)); | |
266 if (gallery_file_path.empty()) | 273 if (gallery_file_path.empty()) |
267 return; | 274 return; |
268 MediaGalleriesPrivateEventRouter* router = | 275 MediaGalleriesPrivateEventRouter* router = |
269 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); | 276 MediaGalleriesPrivateAPI::Get(profile_)->GetEventRouter(); |
270 DCHECK(router); | 277 DCHECK(router); |
271 content::BrowserThread::PostTaskAndReplyWithResult( | 278 content::BrowserThread::PostTaskAndReplyWithResult( |
272 content::BrowserThread::FILE, | 279 content::BrowserThread::FILE, |
273 FROM_HERE, | 280 FROM_HERE, |
274 base::Bind(&GalleryWatchManager::SetupGalleryWatch, | 281 base::Bind(&GalleryWatchManager::SetupGalleryWatch, |
275 profile_, | 282 profile_, |
276 gallery_id, | 283 gallery_id, |
277 gallery_file_path, | 284 gallery_file_path, |
278 extension_id, | 285 extension_id, |
279 router->AsWeakPtr()), | 286 router->AsWeakPtr()), |
280 base::Bind(&GalleryWatchStateTracker::HandleSetupGalleryWatchResponse, | 287 base::Bind(&GalleryWatchStateTracker::HandleSetupGalleryWatchResponse, |
281 AsWeakPtr(), | 288 AsWeakPtr(), |
282 extension_id, | 289 extension_id, |
283 gallery_id)); | 290 gallery_id)); |
284 } | 291 } |
285 | 292 |
286 void GalleryWatchStateTracker::RemoveGalleryWatch( | 293 void GalleryWatchStateTracker::RemoveGalleryWatch( |
287 const std::string& extension_id, | 294 const std::string& extension_id, |
288 chrome::MediaGalleryPrefId gallery_id) { | 295 chrome::MediaGalleryPrefId gallery_id, |
296 chrome::MediaGalleriesPreferences* preferences) { | |
289 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 297 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
290 const Extension* extension = GetExtensionById(profile_, extension_id); | 298 const Extension* extension = GetExtensionById(profile_, extension_id); |
291 DCHECK(extension); | 299 DCHECK(extension); |
292 base::FilePath gallery_file_path( | 300 base::FilePath gallery_file_path(preferences->LookUpGalleryPathForExtension( |
293 GetMediaGalleryPreferences(profile_)->LookUpGalleryPathForExtension( | 301 gallery_id, extension, true)); |
294 gallery_id, extension, true)); | |
295 if (gallery_file_path.empty()) | 302 if (gallery_file_path.empty()) |
296 return; | 303 return; |
297 content::BrowserThread::PostTask( | 304 content::BrowserThread::PostTask( |
298 content::BrowserThread::FILE, FROM_HERE, | 305 content::BrowserThread::FILE, FROM_HERE, |
299 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, | 306 base::Bind(&GalleryWatchManager::RemoveGalleryWatch, |
300 profile_, | 307 profile_, |
301 gallery_file_path, | 308 gallery_file_path, |
302 extension_id)); | 309 extension_id)); |
303 watched_extensions_map_[extension_id][gallery_id] = false; | 310 watched_extensions_map_[extension_id][gallery_id] = false; |
304 } | 311 } |
(...skipping 23 matching lines...) Expand all Loading... | |
328 const std::string& extension_id, | 335 const std::string& extension_id, |
329 chrome::MediaGalleryPrefId gallery_id) { | 336 chrome::MediaGalleryPrefId gallery_id) { |
330 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 337 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
331 if (HasGalleryWatchInfo(extension_id, gallery_id, true)) | 338 if (HasGalleryWatchInfo(extension_id, gallery_id, true)) |
332 return false; | 339 return false; |
333 watched_extensions_map_[extension_id][gallery_id] = true; | 340 watched_extensions_map_[extension_id][gallery_id] = true; |
334 return true; | 341 return true; |
335 } | 342 } |
336 | 343 |
337 } // namespace extensions | 344 } // namespace extensions |
OLD | NEW |