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

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

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 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_api.h" 5 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_ private_api.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 // by the |gallery_id|. Returns true and set |gallery_file_path| and 56 // by the |gallery_id|. Returns true and set |gallery_file_path| and
57 // |gallery_pref_id| if the |gallery_id| is valid and returns false otherwise. 57 // |gallery_pref_id| if the |gallery_id| is valid and returns false otherwise.
58 bool GetGalleryFilePathAndId(const std::string& gallery_id, 58 bool GetGalleryFilePathAndId(const std::string& gallery_id,
59 Profile* profile, 59 Profile* profile,
60 const Extension* extension, 60 const Extension* extension,
61 base::FilePath* gallery_file_path, 61 base::FilePath* gallery_file_path,
62 MediaGalleryPrefId* gallery_pref_id) { 62 MediaGalleryPrefId* gallery_pref_id) {
63 MediaGalleryPrefId pref_id; 63 MediaGalleryPrefId pref_id;
64 if (!base::StringToUint64(gallery_id, &pref_id)) 64 if (!base::StringToUint64(gallery_id, &pref_id))
65 return false; 65 return false;
66 MediaFileSystemRegistry* registry = 66 MediaGalleriesPreferences* preferences =
67 g_browser_process->media_file_system_registry(); 67 g_browser_process->media_file_system_registry()->GetPreferences(profile);
68 base::FilePath file_path( 68 base::FilePath file_path(
69 registry->GetPreferences(profile)->LookUpGalleryPathForExtension( 69 preferences->LookUpGalleryPathForExtension(pref_id, extension, false));
70 pref_id, extension, false));
71 if (file_path.empty()) 70 if (file_path.empty())
72 return false; 71 return false;
73 *gallery_pref_id = pref_id; 72 *gallery_pref_id = pref_id;
74 *gallery_file_path = file_path; 73 *gallery_file_path = file_path;
75 return true; 74 return true;
76 } 75 }
77 76
78 } // namespace 77 } // namespace
79 78
80 79
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 } 114 }
116 115
117 // static 116 // static
118 MediaGalleriesPrivateAPI* MediaGalleriesPrivateAPI::Get(Profile* profile) { 117 MediaGalleriesPrivateAPI* MediaGalleriesPrivateAPI::Get(Profile* profile) {
119 return 118 return
120 ProfileKeyedAPIFactory<MediaGalleriesPrivateAPI>::GetForProfile(profile); 119 ProfileKeyedAPIFactory<MediaGalleriesPrivateAPI>::GetForProfile(profile);
121 } 120 }
122 121
123 void MediaGalleriesPrivateAPI::OnListenerAdded( 122 void MediaGalleriesPrivateAPI::OnListenerAdded(
124 const EventListenerInfo& details) { 123 const EventListenerInfo& details) {
125 // Start the StorageMonitor if it is not already initialized. After that, 124 // Make sure MediaGalleriesPreferences is initialized. After that,
126 // try to initialize the event router for the listener. 125 // try to initialize the event router for the listener.
127 // This method is called synchronously with the message handler for the 126 // This method is called synchronously with the message handler for the
128 // JS invocation. 127 // JS invocation.
129 128
130 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( 129 MediaGalleriesPreferences* preferences =
130 g_browser_process->media_file_system_registry()->GetPreferences(profile_);
131 preferences->EnsureInitialized(base::Bind(
131 &MediaGalleriesPrivateAPI::MaybeInitializeEventRouterAndTracker, 132 &MediaGalleriesPrivateAPI::MaybeInitializeEventRouterAndTracker,
132 weak_ptr_factory_.GetWeakPtr())); 133 weak_ptr_factory_.GetWeakPtr()));
133 } 134 }
134 135
135 MediaGalleriesPrivateEventRouter* MediaGalleriesPrivateAPI::GetEventRouter() { 136 MediaGalleriesPrivateEventRouter* MediaGalleriesPrivateAPI::GetEventRouter() {
136 MaybeInitializeEventRouterAndTracker(); 137 MaybeInitializeEventRouterAndTracker();
137 return media_galleries_private_event_router_.get(); 138 return media_galleries_private_event_router_.get();
138 } 139 }
139 140
140 GalleryWatchStateTracker* 141 GalleryWatchStateTracker*
(...skipping 22 matching lines...) Expand all
163 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() { 164 bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() {
164 DCHECK(profile_); 165 DCHECK(profile_);
165 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 166 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
166 if (!render_view_host() || !render_view_host()->GetProcess()) 167 if (!render_view_host() || !render_view_host()->GetProcess())
167 return false; 168 return false;
168 169
169 scoped_ptr<AddGalleryWatch::Params> params( 170 scoped_ptr<AddGalleryWatch::Params> params(
170 AddGalleryWatch::Params::Create(*args_)); 171 AddGalleryWatch::Params::Create(*args_));
171 EXTENSION_FUNCTION_VALIDATE(params.get()); 172 EXTENSION_FUNCTION_VALIDATE(params.get());
172 173
173 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( 174 MediaGalleriesPreferences* preferences =
174 &MediaGalleriesPrivateAddGalleryWatchFunction::OnStorageMonitorInit, 175 g_browser_process->media_file_system_registry()->GetPreferences(profile_);
176 preferences->EnsureInitialized(base::Bind(
177 &MediaGalleriesPrivateAddGalleryWatchFunction::OnPreferencesInit,
175 this, 178 this,
176 params->gallery_id)); 179 params->gallery_id));
177 180
178 return true; 181 return true;
179 } 182 }
180 183
181 void MediaGalleriesPrivateAddGalleryWatchFunction::OnStorageMonitorInit( 184 void MediaGalleriesPrivateAddGalleryWatchFunction::OnPreferencesInit(
182 const std::string& pref_id) { 185 const std::string& pref_id) {
183 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); 186 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
184 base::FilePath gallery_file_path; 187 base::FilePath gallery_file_path;
185 MediaGalleryPrefId gallery_pref_id = 0; 188 MediaGalleryPrefId gallery_pref_id = 0;
186 if (!GetGalleryFilePathAndId(pref_id, profile_, GetExtension(), 189 if (!GetGalleryFilePathAndId(pref_id, profile_, GetExtension(),
187 &gallery_file_path, &gallery_pref_id)) { 190 &gallery_file_path, &gallery_pref_id)) {
188 error_ = kInvalidGalleryIDError; 191 error_ = kInvalidGalleryIDError;
189 HandleResponse(gallery_pref_id, false); 192 HandleResponse(gallery_pref_id, false);
190 return; 193 return;
191 } 194 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 246 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
244 if (!render_view_host() || !render_view_host()->GetProcess()) 247 if (!render_view_host() || !render_view_host()->GetProcess())
245 return false; 248 return false;
246 249
247 // Remove gallery watch operation is currently supported on windows platforms. 250 // Remove gallery watch operation is currently supported on windows platforms.
248 // Please refer to crbug.com/144491 for more details. 251 // Please refer to crbug.com/144491 for more details.
249 scoped_ptr<RemoveGalleryWatch::Params> params( 252 scoped_ptr<RemoveGalleryWatch::Params> params(
250 RemoveGalleryWatch::Params::Create(*args_)); 253 RemoveGalleryWatch::Params::Create(*args_));
251 EXTENSION_FUNCTION_VALIDATE(params.get()); 254 EXTENSION_FUNCTION_VALIDATE(params.get());
252 255
253 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( 256 MediaGalleriesPreferences* preferences =
254 &MediaGalleriesPrivateRemoveGalleryWatchFunction::OnStorageMonitorInit, 257 g_browser_process->media_file_system_registry()->GetPreferences(profile_);
258 preferences->EnsureInitialized(base::Bind(
259 &MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferencesInit,
255 this, 260 this,
256 params->gallery_id)); 261 params->gallery_id));
257 return true; 262 return true;
258 } 263 }
259 264
260 void MediaGalleriesPrivateRemoveGalleryWatchFunction::OnStorageMonitorInit( 265 void MediaGalleriesPrivateRemoveGalleryWatchFunction::OnPreferencesInit(
261 const std::string& pref_id) { 266 const std::string& pref_id) {
262 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); 267 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
Lei Zhang 2013/09/27 00:24:26 Ditto in this file, does this dcheck make sense no
tommycli 2013/09/27 17:54:46 Done.
263 #if defined(OS_WIN) 268 #if defined(OS_WIN)
264 base::FilePath gallery_file_path; 269 base::FilePath gallery_file_path;
265 MediaGalleryPrefId gallery_pref_id = 0; 270 MediaGalleryPrefId gallery_pref_id = 0;
266 if (!GetGalleryFilePathAndId(pref_id, profile_, GetExtension(), 271 if (!GetGalleryFilePathAndId(pref_id, profile_, GetExtension(),
267 &gallery_file_path, &gallery_pref_id)) { 272 &gallery_file_path, &gallery_pref_id)) {
268 error_ = kInvalidGalleryIDError; 273 error_ = kInvalidGalleryIDError;
269 SendResponse(false); 274 SendResponse(false);
270 return; 275 return;
271 } 276 }
272 277
(...skipping 17 matching lines...) Expand all
290 295
291 MediaGalleriesPrivateGetAllGalleryWatchFunction:: 296 MediaGalleriesPrivateGetAllGalleryWatchFunction::
292 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() { 297 ~MediaGalleriesPrivateGetAllGalleryWatchFunction() {
293 } 298 }
294 299
295 bool MediaGalleriesPrivateGetAllGalleryWatchFunction::RunImpl() { 300 bool MediaGalleriesPrivateGetAllGalleryWatchFunction::RunImpl() {
296 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 301 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
297 if (!render_view_host() || !render_view_host()->GetProcess()) 302 if (!render_view_host() || !render_view_host()->GetProcess())
298 return false; 303 return false;
299 304
300 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( 305 MediaGalleriesPreferences* preferences =
301 &MediaGalleriesPrivateGetAllGalleryWatchFunction::OnStorageMonitorInit, 306 g_browser_process->media_file_system_registry()->GetPreferences(profile_);
307 preferences->EnsureInitialized(base::Bind(
308 &MediaGalleriesPrivateGetAllGalleryWatchFunction::OnPreferencesInit,
302 this)); 309 this));
303 return true; 310 return true;
304 } 311 }
305 312
306 void MediaGalleriesPrivateGetAllGalleryWatchFunction::OnStorageMonitorInit() { 313 void MediaGalleriesPrivateGetAllGalleryWatchFunction::OnPreferencesInit() {
307 std::vector<std::string> result; 314 std::vector<std::string> result;
308 #if defined(OS_WIN) 315 #if defined(OS_WIN)
309 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); 316 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
310 GalleryWatchStateTracker* state_tracker = 317 GalleryWatchStateTracker* state_tracker =
311 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); 318 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker();
312 MediaGalleryPrefIdSet gallery_ids = 319 MediaGalleryPrefIdSet gallery_ids =
313 state_tracker->GetAllWatchedGalleryIDsForExtension(extension_id()); 320 state_tracker->GetAllWatchedGalleryIDsForExtension(extension_id());
314 for (MediaGalleryPrefIdSet::const_iterator iter = gallery_ids.begin(); 321 for (MediaGalleryPrefIdSet::const_iterator iter = gallery_ids.begin();
315 iter != gallery_ids.end(); ++iter) { 322 iter != gallery_ids.end(); ++iter) {
316 result.push_back(base::Uint64ToString(*iter)); 323 result.push_back(base::Uint64ToString(*iter));
317 } 324 }
318 #endif 325 #endif
319 results_ = GetAllGalleryWatch::Results::Create(result); 326 results_ = GetAllGalleryWatch::Results::Create(result);
320 SendResponse(true); 327 SendResponse(true);
321 } 328 }
322 329
323 /////////////////////////////////////////////////////////////////////////////// 330 ///////////////////////////////////////////////////////////////////////////////
324 // MediaGalleriesPrivateRemoveAllGalleryWatchFunction // 331 // MediaGalleriesPrivateRemoveAllGalleryWatchFunction //
325 /////////////////////////////////////////////////////////////////////////////// 332 ///////////////////////////////////////////////////////////////////////////////
326 333
327 MediaGalleriesPrivateRemoveAllGalleryWatchFunction:: 334 MediaGalleriesPrivateRemoveAllGalleryWatchFunction::
328 ~MediaGalleriesPrivateRemoveAllGalleryWatchFunction() { 335 ~MediaGalleriesPrivateRemoveAllGalleryWatchFunction() {
329 } 336 }
330 337
331 bool MediaGalleriesPrivateRemoveAllGalleryWatchFunction::RunImpl() { 338 bool MediaGalleriesPrivateRemoveAllGalleryWatchFunction::RunImpl() {
332 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 339 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
333 if (!render_view_host() || !render_view_host()->GetProcess()) 340 if (!render_view_host() || !render_view_host()->GetProcess())
334 return false; 341 return false;
335 342
336 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( 343 MediaGalleriesPreferences* preferences =
337 &MediaGalleriesPrivateRemoveAllGalleryWatchFunction::OnStorageMonitorInit, 344 g_browser_process->media_file_system_registry()->GetPreferences(profile_);
345 preferences->EnsureInitialized(base::Bind(
346 &MediaGalleriesPrivateRemoveAllGalleryWatchFunction::OnPreferencesInit,
338 this)); 347 this));
339 return true; 348 return true;
340 } 349 }
341 350
342 void 351 void MediaGalleriesPrivateRemoveAllGalleryWatchFunction::OnPreferencesInit() {
343 MediaGalleriesPrivateRemoveAllGalleryWatchFunction::OnStorageMonitorInit() {
344 #if defined(OS_WIN) 352 #if defined(OS_WIN)
345 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); 353 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
346 MediaFileSystemRegistry* registry = 354 MediaGalleriesPreferences* preferences =
347 g_browser_process->media_file_system_registry(); 355 g_browser_process->media_file_system_registry()->GetPreferences(profile_);
348 MediaGalleriesPreferences* preferences = registry->GetPreferences(profile_);
349 GalleryWatchStateTracker* state_tracker = 356 GalleryWatchStateTracker* state_tracker =
350 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker(); 357 MediaGalleriesPrivateAPI::Get(profile_)->GetGalleryWatchStateTracker();
351 state_tracker->RemoveAllGalleryWatchersForExtension( 358 state_tracker->RemoveAllGalleryWatchersForExtension(
352 extension_id(), preferences); 359 extension_id(), preferences);
353 #endif 360 #endif
354 SendResponse(true); 361 SendResponse(true);
355 } 362 }
356 363
357 /////////////////////////////////////////////////////////////////////////////// 364 ///////////////////////////////////////////////////////////////////////////////
358 // MediaGalleriesPrivateGetHandlersFunction // 365 // MediaGalleriesPrivateGetHandlersFunction //
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 } 405 }
399 } 406 }
400 407
401 SetResult(result_list); 408 SetResult(result_list);
402 SendResponse(true); 409 SendResponse(true);
403 410
404 return true; 411 return true;
405 } 412 }
406 413
407 } // namespace extensions 414 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698