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

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

Powered by Google App Engine
This is Rietveld 408576698