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 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PREFERENCES_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PREFERENCES_H_ |
6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PREFERENCES_H_ | 6 #define CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PREFERENCES_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/callback_forward.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
15 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
16 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "chrome/browser/storage_monitor/removable_storage_observer.h" | 19 #include "chrome/browser/storage_monitor/removable_storage_observer.h" |
19 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 20 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
20 | 21 |
21 class Profile; | 22 class Profile; |
22 | 23 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 142 |
142 virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref, | 143 virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref, |
143 MediaGalleryPrefId pref_id) {} | 144 MediaGalleryPrefId pref_id) {} |
144 protected: | 145 protected: |
145 virtual ~GalleryChangeObserver(); | 146 virtual ~GalleryChangeObserver(); |
146 }; | 147 }; |
147 | 148 |
148 explicit MediaGalleriesPreferences(Profile* profile); | 149 explicit MediaGalleriesPreferences(Profile* profile); |
149 virtual ~MediaGalleriesPreferences(); | 150 virtual ~MediaGalleriesPreferences(); |
150 | 151 |
| 152 // Ensures that the preferences is initialized. The provided callback, if |
| 153 // non-null, will be called when initialization is complete. If initialization |
| 154 // has already completed, this callback will be invoked in the calling stack. |
| 155 // Before the callback is run, other calls may not return the correct results. |
| 156 // Should be invoked on the UI thread; callbacks will be run on the UI thread. |
| 157 // This call also ensures that the StorageMonitor is initialized. |
| 158 // Note for unit tests: This requires an active FILE thread and |
| 159 // EnsureMediaDirectoriesExists instance to complete reliably. |
| 160 void EnsureInitialized(base::Closure callback); |
| 161 |
| 162 // Return true if the storage monitor has already been initialized. |
| 163 bool IsInitialized() const; |
| 164 |
151 Profile* profile(); | 165 Profile* profile(); |
152 | 166 |
153 void AddGalleryChangeObserver(GalleryChangeObserver* observer); | 167 void AddGalleryChangeObserver(GalleryChangeObserver* observer); |
154 void RemoveGalleryChangeObserver(GalleryChangeObserver* observer); | 168 void RemoveGalleryChangeObserver(GalleryChangeObserver* observer); |
155 | 169 |
156 // RemovableStorageObserver implementation. | 170 // RemovableStorageObserver implementation. |
157 virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE; | 171 virtual void OnRemovableStorageAttached(const StorageInfo& info) OVERRIDE; |
158 | 172 |
159 // Lookup a media gallery and fill in information about it and return true if | 173 // Lookup a media gallery and fill in information about it and return true if |
160 // it exists. Return false if it does not, filling in default information. | 174 // it exists. Return false if it does not, filling in default information. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 // Removes the gallery identified by |id| from the store. | 208 // Removes the gallery identified by |id| from the store. |
195 void ForgetGalleryById(MediaGalleryPrefId id); | 209 void ForgetGalleryById(MediaGalleryPrefId id); |
196 | 210 |
197 MediaGalleryPrefIdSet GalleriesForExtension( | 211 MediaGalleryPrefIdSet GalleriesForExtension( |
198 const extensions::Extension& extension) const; | 212 const extensions::Extension& extension) const; |
199 | 213 |
200 void SetGalleryPermissionForExtension(const extensions::Extension& extension, | 214 void SetGalleryPermissionForExtension(const extensions::Extension& extension, |
201 MediaGalleryPrefId pref_id, | 215 MediaGalleryPrefId pref_id, |
202 bool has_permission); | 216 bool has_permission); |
203 | 217 |
204 const MediaGalleriesPrefInfoMap& known_galleries() const { | 218 const MediaGalleriesPrefInfoMap& known_galleries() const; |
205 return known_galleries_; | |
206 } | |
207 | 219 |
208 // BrowserContextKeyedService implementation: | 220 // BrowserContextKeyedService implementation: |
209 virtual void Shutdown() OVERRIDE; | 221 virtual void Shutdown() OVERRIDE; |
210 | 222 |
211 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 223 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
212 | 224 |
213 // Returns true if the media gallery preferences system has ever been used | 225 // Returns true if the media gallery preferences system has ever been used |
214 // for this profile. To be exact, it checks if a gallery has ever been added | 226 // for this profile. To be exact, it checks if a gallery has ever been added |
215 // (including defaults). | 227 // (including defaults). |
216 static bool APIHasBeenUsed(Profile* profile); | 228 static bool APIHasBeenUsed(Profile* profile); |
217 | 229 |
218 private: | 230 private: |
219 friend class MediaGalleriesPreferencesTest; | 231 friend class MediaGalleriesPreferencesTest; |
220 friend class MediaGalleriesPermissionsTest; | 232 friend class MediaGalleriesPermissionsTest; |
221 | 233 |
222 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet> | 234 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet> |
223 DeviceIdPrefIdsMap; | 235 DeviceIdPrefIdsMap; |
224 | 236 |
| 237 // These must be called on the UI thread. |
| 238 void OnInitializationCallbackReturned(); |
| 239 void FinishInitialization(); |
| 240 |
225 // Populates the default galleries if this is a fresh profile. | 241 // Populates the default galleries if this is a fresh profile. |
226 void AddDefaultGalleriesIfFreshProfile(); | 242 void AddDefaultGalleriesIfFreshProfile(); |
227 | 243 |
228 // This is a hack - Some devices (iTunes, Picasa) are singletons in that only | 244 // This is a hack - Some devices (iTunes, Picasa) are singletons in that only |
229 // one instance of that type is supported at a time. As such, the device id | 245 // one instance of that type is supported at a time. As such, the device id |
230 // should just be "itunes:" or "picasa:" but that would mean finding the | 246 // should just be "itunes:" or "picasa:" but that would mean finding the |
231 // location of the database file multiple times, which may be an async | 247 // location of the database file multiple times, which may be an async |
232 // operation. Storing the location of the backing database in the device | 248 // operation. Storing the location of the backing database in the device |
233 // id allows that look up to be avoided. However, the cost is that if the | 249 // id allows that look up to be avoided. However, the cost is that if the |
234 // database moves, the device id in preferences has to be updated. This | 250 // database moves, the device id in preferences has to be updated. This |
235 // method searches for a gallery of the type passed in and updates its | 251 // method searches for a gallery of the type passed in and updates its |
236 // device id. It returns true if the device id is up to date. | 252 // device id. It returns true if the device id is up to date. |
237 bool UpdateDeviceIDForSingletonType(const std::string& device_id); | 253 bool UpdateDeviceIDForSingletonType(const std::string& device_id); |
238 | 254 |
239 // Try to add an entry for the iTunes 'device'. | 255 // Handle an iTunes or Picasa finder returning a device ID to us. |
240 void OnITunesDeviceID(const std::string& device_id); | 256 void OnFinderDeviceID(const std::string& device_id); |
241 | |
242 // Try to add an entry for the Picasa 'device'. | |
243 void OnPicasaDeviceID(const std::string& device_id); | |
244 | 257 |
245 // Builds |known_galleries_| from the persistent store. | 258 // Builds |known_galleries_| from the persistent store. |
246 void InitFromPrefs(); | 259 void InitFromPrefs(); |
247 | 260 |
248 MediaGalleryPrefId AddGalleryInternal(const std::string& device_id, | 261 MediaGalleryPrefId AddGalleryInternal(const std::string& device_id, |
249 const string16& display_name, | 262 const string16& display_name, |
250 const base::FilePath& relative_path, | 263 const base::FilePath& relative_path, |
251 bool user_added, | 264 bool user_added, |
252 const string16& volume_label, | 265 const string16& volume_label, |
253 const string16& vendor_name, | 266 const string16& vendor_name, |
(...skipping 27 matching lines...) Expand all Loading... |
281 // Get the ExtensionPrefs to use; this will be either the ExtensionPrefs | 294 // Get the ExtensionPrefs to use; this will be either the ExtensionPrefs |
282 // object associated with |profile_|, or extension_prefs_for_testing_, if | 295 // object associated with |profile_|, or extension_prefs_for_testing_, if |
283 // SetExtensionPrefsForTesting() has been called. | 296 // SetExtensionPrefsForTesting() has been called. |
284 extensions::ExtensionPrefs* GetExtensionPrefs() const; | 297 extensions::ExtensionPrefs* GetExtensionPrefs() const; |
285 | 298 |
286 // Set the ExtensionPrefs object to be returned by GetExtensionPrefs(). | 299 // Set the ExtensionPrefs object to be returned by GetExtensionPrefs(). |
287 void SetExtensionPrefsForTesting(extensions::ExtensionPrefs* extension_prefs); | 300 void SetExtensionPrefsForTesting(extensions::ExtensionPrefs* extension_prefs); |
288 | 301 |
289 base::WeakPtrFactory<MediaGalleriesPreferences> weak_factory_; | 302 base::WeakPtrFactory<MediaGalleriesPreferences> weak_factory_; |
290 | 303 |
| 304 bool initialized_; |
| 305 std::vector<base::Closure> on_initialize_callbacks_; |
| 306 int pre_initialization_callbacks_waiting_; |
| 307 |
291 // The profile that owns |this|. | 308 // The profile that owns |this|. |
292 Profile* profile_; | 309 Profile* profile_; |
293 | 310 |
294 // The ExtensionPrefs used in a testing environment, where | 311 // The ExtensionPrefs used in a testing environment, where |
295 // BrowserContextKeyedServices aren't used. This will be NULL unless it is | 312 // BrowserContextKeyedServices aren't used. This will be NULL unless it is |
296 // set with SetExtensionPrefsForTesting(). | 313 // set with SetExtensionPrefsForTesting(). |
297 extensions::ExtensionPrefs* extension_prefs_for_testing_; | 314 extensions::ExtensionPrefs* extension_prefs_for_testing_; |
298 | 315 |
299 // An in-memory cache of known galleries. | 316 // An in-memory cache of known galleries. |
300 MediaGalleriesPrefInfoMap known_galleries_; | 317 MediaGalleriesPrefInfoMap known_galleries_; |
301 | 318 |
302 // A mapping from device id to the set of gallery pref ids on that device. | 319 // A mapping from device id to the set of gallery pref ids on that device. |
303 // All pref ids in |device_map_| are also in |known_galleries_|. | 320 // All pref ids in |device_map_| are also in |known_galleries_|. |
304 DeviceIdPrefIdsMap device_map_; | 321 DeviceIdPrefIdsMap device_map_; |
305 | 322 |
306 ObserverList<GalleryChangeObserver> gallery_change_observers_; | 323 ObserverList<GalleryChangeObserver> gallery_change_observers_; |
307 | 324 |
308 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferences); | 325 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferences); |
309 }; | 326 }; |
310 | 327 |
311 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PREFERENCES_H_ | 328 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PREFERENCES_H_ |
OLD | NEW |