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

Side by Side Diff: chrome/browser/chromeos/extensions/file_browser_event_router.cc

Issue 13776005: drive: Fix two instances of madness in FileBrowserEventRouter (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 7 years, 8 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/chromeos/extensions/file_browser_event_router.h" 5 #include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
222 222
223 content::BrowserThread::PostBlockingPoolTask( 223 content::BrowserThread::PostBlockingPoolTask(
224 FROM_HERE, 224 FROM_HERE,
225 base::Bind(&DirectoryExistsOnBlockingPool, 225 base::Bind(&DirectoryExistsOnBlockingPool,
226 directory_path, 226 directory_path,
227 success_callback, 227 success_callback,
228 failure_callback)); 228 failure_callback));
229 }; 229 };
230 230
231 // Creates a base::FilePathWatcher and starts watching at |watch_path| with
232 // |callback|. Returns NULL on failure.
233 base::FilePathWatcher* CreateAndStartFilePathWatcher(
234 const base::FilePath& watch_path,
235 const base::FilePathWatcher::Callback& callback) {
236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
237 DCHECK(!callback.is_null());
238
239 base::FilePathWatcher* watcher(new base::FilePathWatcher);
240 if (!watcher->Watch(watch_path, false /* recursive */, callback)) {
241 delete watcher;
242 return NULL;
243 }
244
245 return watcher;
246 }
247
231 } // namespace 248 } // namespace
232 249
233 FileBrowserEventRouter::FileBrowserEventRouter( 250 FileBrowserEventRouter::FileBrowserEventRouter(
234 Profile* profile) 251 Profile* profile)
235 : weak_factory_(this), 252 : weak_factory_(this),
236 notifications_(new FileBrowserNotifications(profile)), 253 notifications_(new FileBrowserNotifications(profile)),
237 pref_change_registrar_(new PrefChangeRegistrar), 254 pref_change_registrar_(new PrefChangeRegistrar),
238 profile_(profile), 255 profile_(profile),
239 num_remote_update_requests_(0), 256 num_remote_update_requests_(0),
240 shift_pressed_(false) { 257 shift_pressed_(false) {
241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
242 259
243 // Bind a weak reference back to |this| to avoid memory errors in case we
244 // shut down while a callback is in flight.
245 file_watcher_callback_ = 260 file_watcher_callback_ =
246 base::Bind(&RelayFileWatcherCallbackToUIThread, 261 base::Bind(&FileBrowserEventRouter::HandleFileWatchNotification,
247 base::Bind(&FileBrowserEventRouter::HandleFileWatchNotification, 262 weak_factory_.GetWeakPtr());
248 weak_factory_.GetWeakPtr()));
249 263
250 // Listen for the Shift modifier's state changes. 264 // Listen for the Shift modifier's state changes.
251 chromeos::SystemKeyEventListener* key_event_listener = 265 chromeos::SystemKeyEventListener* key_event_listener =
252 chromeos::SystemKeyEventListener::GetInstance(); 266 chromeos::SystemKeyEventListener::GetInstance();
253 if (key_event_listener) 267 if (key_event_listener)
254 key_event_listener->AddModifiersObserver(this); 268 key_event_listener->AddModifiersObserver(this);
255 } 269 }
256 270
257 FileBrowserEventRouter::~FileBrowserEventRouter() { 271 FileBrowserEventRouter::~FileBrowserEventRouter() {
258 } 272 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 chromeos::ConnectivityStateHelper::Get()-> 331 chromeos::ConnectivityStateHelper::Get()->
318 AddNetworkManagerObserver(this); 332 AddNetworkManagerObserver(this);
319 } 333 }
320 suspend_state_delegate_.reset(new SuspendStateDelegateImpl()); 334 suspend_state_delegate_.reset(new SuspendStateDelegateImpl());
321 335
322 pref_change_registrar_->Init(profile_->GetPrefs()); 336 pref_change_registrar_->Init(profile_->GetPrefs());
323 337
324 pref_change_registrar_->Add( 338 pref_change_registrar_->Add(
325 prefs::kExternalStorageDisabled, 339 prefs::kExternalStorageDisabled,
326 base::Bind(&FileBrowserEventRouter::OnExternalStorageDisabledChanged, 340 base::Bind(&FileBrowserEventRouter::OnExternalStorageDisabledChanged,
327 base::Unretained(this))); 341 base::Unretained(this)));
satorux1 2013/04/09 01:24:38 replaced this with a weak ptr.
328 342
329 base::Closure callback = 343 base::Closure callback =
330 base::Bind(&FileBrowserEventRouter::OnFileBrowserPrefsChanged, 344 base::Bind(&FileBrowserEventRouter::OnFileBrowserPrefsChanged,
331 base::Unretained(this)); 345 base::Unretained(this));
satorux1 2013/04/09 01:24:38 replaced this with a weak ptr.
332 pref_change_registrar_->Add(prefs::kDisableDriveOverCellular, callback); 346 pref_change_registrar_->Add(prefs::kDisableDriveOverCellular, callback);
333 pref_change_registrar_->Add(prefs::kDisableDriveHostedFiles, callback); 347 pref_change_registrar_->Add(prefs::kDisableDriveHostedFiles, callback);
334 pref_change_registrar_->Add(prefs::kDisableDrive, callback); 348 pref_change_registrar_->Add(prefs::kDisableDrive, callback);
335 pref_change_registrar_->Add(prefs::kUse24HourClock, callback); 349 pref_change_registrar_->Add(prefs::kUse24HourClock, callback);
336 } 350 }
337 351
338 // File watch setup routines. 352 // File watch setup routines.
339 bool FileBrowserEventRouter::AddFileWatch( 353 bool FileBrowserEventRouter::AddFileWatch(
340 const base::FilePath& local_path, 354 const base::FilePath& local_path,
341 const base::FilePath& virtual_path, 355 const base::FilePath& virtual_path,
342 const std::string& extension_id) { 356 const std::string& extension_id,
343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 357 const BoolCallback& callback) {
358 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
359 DCHECK(!callback.is_null());
344 360
345 base::AutoLock lock(lock_);
346 base::FilePath watch_path = local_path; 361 base::FilePath watch_path = local_path;
347 bool is_remote_watch = false; 362 bool is_remote_watch = false;
348 // Tweak watch path for remote sources - we need to drop leading /special 363 // Tweak watch path for remote sources - we need to drop leading /special
349 // directory from there in order to be able to pair these events with 364 // directory from there in order to be able to pair these events with
350 // their change notifications. 365 // their change notifications.
351 if (drive::util::GetSpecialRemoteRootPath().IsParent(watch_path)) { 366 if (drive::util::GetSpecialRemoteRootPath().IsParent(watch_path)) {
352 watch_path = drive::util::ExtractDrivePath(watch_path); 367 watch_path = drive::util::ExtractDrivePath(watch_path);
353 is_remote_watch = true; 368 is_remote_watch = true;
354 BrowserThread::PostTask( 369 HandleRemoteUpdateRequestOnUIThread(true /* start */);
355 BrowserThread::UI, FROM_HERE,
356 base::Bind(&FileBrowserEventRouter::HandleRemoteUpdateRequestOnUIThread,
357 this, true));
358 } 370 }
359 371
360 WatcherMap::iterator iter = file_watchers_.find(watch_path); 372 WatcherMap::iterator iter = file_watchers_.find(watch_path);
361 if (iter == file_watchers_.end()) { 373 if (iter == file_watchers_.end()) {
362 scoped_ptr<FileWatcherExtensions> 374 scoped_ptr<FileWatcherExtensions>
363 watch(new FileWatcherExtensions(virtual_path, 375 watch(new FileWatcherExtensions(virtual_path,
364 extension_id, 376 extension_id,
365 is_remote_watch)); 377 is_remote_watch));
366 378 watch->Watch(watch_path,
367 if (watch->Watch(watch_path, file_watcher_callback_)) 379 file_watcher_callback_,
368 file_watchers_[watch_path] = watch.release(); 380 callback);
369 else 381 file_watchers_[watch_path] = watch.release();
370 return false;
371 } else { 382 } else {
372 iter->second->AddExtension(extension_id); 383 iter->second->AddExtension(extension_id);
384 base::MessageLoopProxy::current()->PostTask(FROM_HERE,
385 base::Bind(callback, true));
373 } 386 }
374 return true; 387 return true;
375 } 388 }
376 389
377 void FileBrowserEventRouter::RemoveFileWatch( 390 void FileBrowserEventRouter::RemoveFileWatch(
378 const base::FilePath& local_path, 391 const base::FilePath& local_path,
379 const std::string& extension_id) { 392 const std::string& extension_id) {
380 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
381 394
382 base::AutoLock lock(lock_);
383 base::FilePath watch_path = local_path; 395 base::FilePath watch_path = local_path;
384 // Tweak watch path for remote sources - we need to drop leading /special 396 // Tweak watch path for remote sources - we need to drop leading /special
385 // directory from there in order to be able to pair these events with 397 // directory from there in order to be able to pair these events with
386 // their change notifications. 398 // their change notifications.
387 if (drive::util::GetSpecialRemoteRootPath().IsParent(watch_path)) { 399 if (drive::util::GetSpecialRemoteRootPath().IsParent(watch_path)) {
388 watch_path = drive::util::ExtractDrivePath(watch_path); 400 watch_path = drive::util::ExtractDrivePath(watch_path);
389 BrowserThread::PostTask( 401 HandleRemoteUpdateRequestOnUIThread(false /* start */);
390 BrowserThread::UI, FROM_HERE,
391 base::Bind(&FileBrowserEventRouter::HandleRemoteUpdateRequestOnUIThread,
392 this, false));
393 } 402 }
394 WatcherMap::iterator iter = file_watchers_.find(watch_path); 403 WatcherMap::iterator iter = file_watchers_.find(watch_path);
395 if (iter == file_watchers_.end()) 404 if (iter == file_watchers_.end())
396 return; 405 return;
397 // Remove the renderer process for this watch. 406 // Remove the renderer process for this watch.
398 iter->second->RemoveExtension(extension_id); 407 iter->second->RemoveExtension(extension_id);
399 if (iter->second->GetRefCount() == 0) { 408 if (iter->second->GetRefCount() == 0) {
400 delete iter->second; 409 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, iter->second);
hashimoto 2013/04/08 11:10:26 Why do you delete FileWatcherExtensions on FILE th
satorux1 2013/04/09 01:24:38 oops. Good catch! I was confused. FilePathWatcher
401 file_watchers_.erase(iter); 410 file_watchers_.erase(iter);
402 } 411 }
403 } 412 }
404 413
405 void FileBrowserEventRouter::MountDrive( 414 void FileBrowserEventRouter::MountDrive(
406 const base::Closure& callback) { 415 const base::Closure& callback) {
407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
408 417
409 // Pass back the gdata mount point path as source path. 418 // Pass back the gdata mount point path as source path.
410 const std::string& gdata_path = drive::util::GetDriveMountPointPathAsString(); 419 const std::string& gdata_path = drive::util::GetDriveMountPointPathAsString();
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
648 extensions::event_names::kOnFileBrowserDriveConnectionStatusChanged, 657 extensions::event_names::kOnFileBrowserDriveConnectionStatusChanged,
649 scoped_ptr<ListValue>(new ListValue()))); 658 scoped_ptr<ListValue>(new ListValue())));
650 extensions::ExtensionSystem::Get(profile_)->event_router()-> 659 extensions::ExtensionSystem::Get(profile_)->event_router()->
651 BroadcastEvent(event.Pass()); 660 BroadcastEvent(event.Pass());
652 } 661 }
653 662
654 void FileBrowserEventRouter::HandleFileWatchNotification( 663 void FileBrowserEventRouter::HandleFileWatchNotification(
655 const base::FilePath& local_path, bool got_error) { 664 const base::FilePath& local_path, bool got_error) {
656 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 665 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
657 666
658 base::AutoLock lock(lock_); 667 base::AutoLock lock(lock_);
hashimoto 2013/04/08 11:10:26 Can't we remove this AutoLock to eliminate |lock_|
satorux1 2013/04/09 01:24:38 oops. I thought I removed lock_ completely. Remove
659 WatcherMap::const_iterator iter = file_watchers_.find(local_path); 668 WatcherMap::const_iterator iter = file_watchers_.find(local_path);
660 if (iter == file_watchers_.end()) { 669 if (iter == file_watchers_.end()) {
661 return; 670 return;
662 } 671 }
663 DispatchDirectoryChangeEvent(iter->second->GetVirtualPath(), got_error, 672 DispatchDirectoryChangeEvent(iter->second->GetVirtualPath(), got_error,
664 iter->second->GetExtensions()); 673 iter->second->GetExtensions());
665 } 674 }
666 675
667 void FileBrowserEventRouter::DispatchDirectoryChangeEvent( 676 void FileBrowserEventRouter::DispatchDirectoryChangeEvent(
668 const base::FilePath& virtual_path, 677 const base::FilePath& virtual_path,
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 chromeos::MOUNT_TYPE_DEVICE); 903 chromeos::MOUNT_TYPE_DEVICE);
895 } else { 904 } else {
896 notifications_->HideNotification(FileBrowserNotifications::FORMAT_START, 905 notifications_->HideNotification(FileBrowserNotifications::FORMAT_START,
897 device_path); 906 device_path);
898 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_FAIL, 907 notifications_->ShowNotification(FileBrowserNotifications::FORMAT_FAIL,
899 device_path); 908 device_path);
900 } 909 }
901 } 910 }
902 911
903 FileBrowserEventRouter::FileWatcherExtensions::FileWatcherExtensions( 912 FileBrowserEventRouter::FileWatcherExtensions::FileWatcherExtensions(
904 const base::FilePath& path, const std::string& extension_id, 913 const base::FilePath& virtual_path,
914 const std::string& extension_id,
905 bool is_remote_file_system) 915 bool is_remote_file_system)
906 : ref_count_(0), 916 : file_watcher_(NULL),
907 is_remote_file_system_(is_remote_file_system) { 917 virtual_path_(virtual_path),
908 if (!is_remote_file_system_) 918 ref_count_(0),
909 file_watcher_.reset(new base::FilePathWatcher()); 919 is_remote_file_system_(is_remote_file_system),
920 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
921 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
910 922
911 virtual_path_ = path;
912 AddExtension(extension_id); 923 AddExtension(extension_id);
913 } 924 }
914 925
915 FileBrowserEventRouter::FileWatcherExtensions::~FileWatcherExtensions() {} 926 FileBrowserEventRouter::FileWatcherExtensions::~FileWatcherExtensions() {
927 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
928
929 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, file_watcher_);
930 }
916 931
917 void FileBrowserEventRouter::FileWatcherExtensions::AddExtension( 932 void FileBrowserEventRouter::FileWatcherExtensions::AddExtension(
918 const std::string& extension_id) { 933 const std::string& extension_id) {
934 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
935
919 ExtensionUsageRegistry::iterator it = extensions_.find(extension_id); 936 ExtensionUsageRegistry::iterator it = extensions_.find(extension_id);
920 if (it != extensions_.end()) { 937 if (it != extensions_.end()) {
921 it->second++; 938 it->second++;
922 } else { 939 } else {
923 extensions_.insert(ExtensionUsageRegistry::value_type(extension_id, 1)); 940 extensions_.insert(ExtensionUsageRegistry::value_type(extension_id, 1));
924 } 941 }
925 942
926 ref_count_++; 943 ref_count_++;
927 } 944 }
928 945
929 void FileBrowserEventRouter::FileWatcherExtensions::RemoveExtension( 946 void FileBrowserEventRouter::FileWatcherExtensions::RemoveExtension(
930 const std::string& extension_id) { 947 const std::string& extension_id) {
948 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
949
931 ExtensionUsageRegistry::iterator it = extensions_.find(extension_id); 950 ExtensionUsageRegistry::iterator it = extensions_.find(extension_id);
932 951
933 if (it != extensions_.end()) { 952 if (it != extensions_.end()) {
934 // If entry found - decrease it's count and remove if necessary 953 // If entry found - decrease it's count and remove if necessary
935 if (0 == it->second--) { 954 if (0 == it->second--) {
936 extensions_.erase(it); 955 extensions_.erase(it);
937 } 956 }
938 957
939 ref_count_--; 958 ref_count_--;
940 } else { 959 } else {
941 // Might be reference counting problem - e.g. if some component of 960 // Might be reference counting problem - e.g. if some component of
942 // extension subscribes/unsubscribes correctly, but other component 961 // extension subscribes/unsubscribes correctly, but other component
943 // only unsubscribes, developer of first one might receive this message 962 // only unsubscribes, developer of first one might receive this message
944 LOG(FATAL) << " Extension [" << extension_id 963 LOG(FATAL) << " Extension [" << extension_id
945 << "] tries to unsubscribe from folder [" << local_path_.value() 964 << "] tries to unsubscribe from folder [" << local_path_.value()
946 << "] it isn't subscribed"; 965 << "] it isn't subscribed";
947 } 966 }
948 } 967 }
949 968
950 const FileBrowserEventRouter::ExtensionUsageRegistry& 969 const FileBrowserEventRouter::ExtensionUsageRegistry&
951 FileBrowserEventRouter::FileWatcherExtensions::GetExtensions() const { 970 FileBrowserEventRouter::FileWatcherExtensions::GetExtensions() const {
971 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
952 return extensions_; 972 return extensions_;
953 } 973 }
954 974
955 unsigned int 975 unsigned int
956 FileBrowserEventRouter::FileWatcherExtensions::GetRefCount() const { 976 FileBrowserEventRouter::FileWatcherExtensions::GetRefCount() const {
977 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
957 return ref_count_; 978 return ref_count_;
958 } 979 }
959 980
960 const base::FilePath& 981 const base::FilePath&
961 FileBrowserEventRouter::FileWatcherExtensions::GetVirtualPath() const { 982 FileBrowserEventRouter::FileWatcherExtensions::GetVirtualPath() const {
983 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
962 return virtual_path_; 984 return virtual_path_;
963 } 985 }
964 986
965 drive::DriveFileSystemInterface* 987 drive::DriveFileSystemInterface*
966 FileBrowserEventRouter::GetRemoteFileSystem() const { 988 FileBrowserEventRouter::GetRemoteFileSystem() const {
967 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 989 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
968 DriveSystemService* system_service = 990 DriveSystemService* system_service =
969 DriveSystemServiceFactory::GetForProfile(profile_); 991 DriveSystemServiceFactory::GetForProfile(profile_);
970 return (system_service ? system_service->file_system() : NULL); 992 return (system_service ? system_service->file_system() : NULL);
971 } 993 }
972 994
973 bool FileBrowserEventRouter::FileWatcherExtensions::Watch( 995 void FileBrowserEventRouter::FileWatcherExtensions::Watch(
974 const base::FilePath& path, 996 const base::FilePath& local_path,
975 const base::FilePathWatcher::Callback& callback) { 997 const base::FilePathWatcher::Callback& file_watcher_callback,
976 if (is_remote_file_system_) 998 const BoolCallback& callback) {
977 return true; 999 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1000 DCHECK(!callback.is_null());
1001 DCHECK(!file_watcher_);
978 1002
979 return file_watcher_->Watch(path, false, callback); 1003 local_path_ = local_path; // For error message in RemoveExtension().
1004
1005 if (is_remote_file_system_) {
1006 base::MessageLoopProxy::current()->PostTask(FROM_HERE,
1007 base::Bind(callback, true));
1008 return;
1009 }
1010
1011 BrowserThread::PostTaskAndReplyWithResult(
1012 BrowserThread::FILE,
1013 FROM_HERE,
1014 base::Bind(&CreateAndStartFilePathWatcher,
1015 local_path,
1016 base::Bind(&RelayFileWatcherCallbackToUIThread,
1017 file_watcher_callback)),
1018 base::Bind(
1019 &FileBrowserEventRouter::FileWatcherExtensions::OnWatcherStarted,
1020 weak_ptr_factory_.GetWeakPtr(),
1021 callback));
980 } 1022 }
1023
1024 void FileBrowserEventRouter::FileWatcherExtensions::OnWatcherStarted(
1025 const BoolCallback& callback,
1026 base::FilePathWatcher* file_watcher) {
1027 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1028
1029 if (file_watcher) {
1030 file_watcher_ = file_watcher;
1031 callback.Run(true);
1032 } else {
1033 callback.Run(false);
1034 }
1035 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698