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

Side by Side Diff: chrome/browser/media_galleries/media_galleries_preferences.h

Issue 24269007: Media Galleries API: Fix MediaGalleriesPreferences finders race. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 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 #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
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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 210
197 MediaGalleryPrefIdSet GalleriesForExtension( 211 MediaGalleryPrefIdSet GalleriesForExtension(
198 const extensions::Extension& extension) const; 212 const extensions::Extension& extension) const;
199 213
200 // Returns true if the permission changed. Returns false if there was 214 // Returns true if the permission changed. Returns false if there was
201 // no change. 215 // no change.
202 bool SetGalleryPermissionForExtension(const extensions::Extension& extension, 216 bool SetGalleryPermissionForExtension(const extensions::Extension& extension,
203 MediaGalleryPrefId pref_id, 217 MediaGalleryPrefId pref_id,
204 bool has_permission); 218 bool has_permission);
205 219
206 const MediaGalleriesPrefInfoMap& known_galleries() const { 220 const MediaGalleriesPrefInfoMap& known_galleries() const;
207 return known_galleries_;
208 }
209 221
210 // BrowserContextKeyedService implementation: 222 // BrowserContextKeyedService implementation:
211 virtual void Shutdown() OVERRIDE; 223 virtual void Shutdown() OVERRIDE;
212 224
213 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 225 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
214 226
215 // Returns true if the media gallery preferences system has ever been used 227 // Returns true if the media gallery preferences system has ever been used
216 // for this profile. To be exact, it checks if a gallery has ever been added 228 // for this profile. To be exact, it checks if a gallery has ever been added
217 // (including defaults). 229 // (including defaults).
218 static bool APIHasBeenUsed(Profile* profile); 230 static bool APIHasBeenUsed(Profile* profile);
219 231
220 private: 232 private:
221 friend class MediaGalleriesPreferencesTest; 233 friend class MediaGalleriesPreferencesTest;
222 friend class MediaGalleriesPermissionsTest; 234 friend class MediaGalleriesPermissionsTest;
223 235
224 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet> 236 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet>
225 DeviceIdPrefIdsMap; 237 DeviceIdPrefIdsMap;
226 238
239 // These must be called on the UI thread.
240 void OnInitializationCallbackReturned();
241 void FinishInitialization();
242
227 // Populates the default galleries if this is a fresh profile. 243 // Populates the default galleries if this is a fresh profile.
228 void AddDefaultGalleriesIfFreshProfile(); 244 void AddDefaultGalleriesIfFreshProfile();
229 245
230 // This is a hack - Some devices (iTunes, Picasa) are singletons in that only 246 // This is a hack - Some devices (iTunes, Picasa) are singletons in that only
231 // one instance of that type is supported at a time. As such, the device id 247 // one instance of that type is supported at a time. As such, the device id
232 // should just be "itunes:" or "picasa:" but that would mean finding the 248 // should just be "itunes:" or "picasa:" but that would mean finding the
233 // location of the database file multiple times, which may be an async 249 // location of the database file multiple times, which may be an async
234 // operation. Storing the location of the backing database in the device 250 // operation. Storing the location of the backing database in the device
235 // id allows that look up to be avoided. However, the cost is that if the 251 // id allows that look up to be avoided. However, the cost is that if the
236 // database moves, the device id in preferences has to be updated. This 252 // database moves, the device id in preferences has to be updated. This
237 // method searches for a gallery of the type passed in and updates its 253 // method searches for a gallery of the type passed in and updates its
238 // device id. It returns true if the device id is up to date. 254 // device id. It returns true if the device id is up to date.
239 bool UpdateDeviceIDForSingletonType(const std::string& device_id); 255 bool UpdateDeviceIDForSingletonType(const std::string& device_id);
240 256
241 // Try to add an entry for the iTunes 'device'. 257 // Handle an iPhoto, iTunes, or Picasa finder returning a device ID to us.
242 void OnITunesDeviceID(const std::string& device_id); 258 void OnFinderDeviceID(const std::string& device_id);
243
244 // Try to add an entry for the iPhoto 'device'.
245 void OnIPhotoDeviceID(const std::string& device_id);
246
247 // Try to add an entry for the Picasa 'device'.
248 void OnPicasaDeviceID(const std::string& device_id);
249 259
250 // Builds |known_galleries_| from the persistent store. 260 // Builds |known_galleries_| from the persistent store.
251 void InitFromPrefs(); 261 void InitFromPrefs();
252 262
253 MediaGalleryPrefId AddGalleryInternal(const std::string& device_id, 263 MediaGalleryPrefId AddGalleryInternal(const std::string& device_id,
254 const string16& display_name, 264 const string16& display_name,
255 const base::FilePath& relative_path, 265 const base::FilePath& relative_path,
256 bool user_added, 266 bool user_added,
257 const string16& volume_label, 267 const string16& volume_label,
258 const string16& vendor_name, 268 const string16& vendor_name,
(...skipping 27 matching lines...) Expand all
286 // Get the ExtensionPrefs to use; this will be either the ExtensionPrefs 296 // Get the ExtensionPrefs to use; this will be either the ExtensionPrefs
287 // object associated with |profile_|, or extension_prefs_for_testing_, if 297 // object associated with |profile_|, or extension_prefs_for_testing_, if
288 // SetExtensionPrefsForTesting() has been called. 298 // SetExtensionPrefsForTesting() has been called.
289 extensions::ExtensionPrefs* GetExtensionPrefs() const; 299 extensions::ExtensionPrefs* GetExtensionPrefs() const;
290 300
291 // Set the ExtensionPrefs object to be returned by GetExtensionPrefs(). 301 // Set the ExtensionPrefs object to be returned by GetExtensionPrefs().
292 void SetExtensionPrefsForTesting(extensions::ExtensionPrefs* extension_prefs); 302 void SetExtensionPrefsForTesting(extensions::ExtensionPrefs* extension_prefs);
293 303
294 base::WeakPtrFactory<MediaGalleriesPreferences> weak_factory_; 304 base::WeakPtrFactory<MediaGalleriesPreferences> weak_factory_;
295 305
306 bool initialized_;
307 std::vector<base::Closure> on_initialize_callbacks_;
308 int pre_initialization_callbacks_waiting_;
309
296 // The profile that owns |this|. 310 // The profile that owns |this|.
297 Profile* profile_; 311 Profile* profile_;
298 312
299 // The ExtensionPrefs used in a testing environment, where 313 // The ExtensionPrefs used in a testing environment, where
300 // BrowserContextKeyedServices aren't used. This will be NULL unless it is 314 // BrowserContextKeyedServices aren't used. This will be NULL unless it is
301 // set with SetExtensionPrefsForTesting(). 315 // set with SetExtensionPrefsForTesting().
302 extensions::ExtensionPrefs* extension_prefs_for_testing_; 316 extensions::ExtensionPrefs* extension_prefs_for_testing_;
303 317
304 // An in-memory cache of known galleries. 318 // An in-memory cache of known galleries.
305 MediaGalleriesPrefInfoMap known_galleries_; 319 MediaGalleriesPrefInfoMap known_galleries_;
306 320
307 // A mapping from device id to the set of gallery pref ids on that device. 321 // A mapping from device id to the set of gallery pref ids on that device.
308 // All pref ids in |device_map_| are also in |known_galleries_|. 322 // All pref ids in |device_map_| are also in |known_galleries_|.
309 DeviceIdPrefIdsMap device_map_; 323 DeviceIdPrefIdsMap device_map_;
310 324
311 ObserverList<GalleryChangeObserver> gallery_change_observers_; 325 ObserverList<GalleryChangeObserver> gallery_change_observers_;
312 326
313 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferences); 327 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferences);
314 }; 328 };
315 329
316 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PREFERENCES_H_ 330 #endif // CHROME_BROWSER_MEDIA_GALLERIES_MEDIA_GALLERIES_PREFERENCES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698