| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_manager/file_watcher_extension
s.h" | 5 #include "chrome/browser/chromeos/extensions/file_manager/file_watcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
| 9 #include "chrome/browser/google_apis/task_util.h" | 9 #include "chrome/browser/google_apis/task_util.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 | 11 |
| 12 using content::BrowserThread; | 12 using content::BrowserThread; |
| 13 | 13 |
| 14 namespace file_manager { | 14 namespace file_manager { |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Creates a base::FilePathWatcher and starts watching at |watch_path| with | 17 // Creates a base::FilePathWatcher and starts watching at |watch_path| with |
| 18 // |callback|. Returns NULL on failure. | 18 // |callback|. Returns NULL on failure. |
| 19 base::FilePathWatcher* CreateAndStartFilePathWatcher( | 19 base::FilePathWatcher* CreateAndStartFilePathWatcher( |
| 20 const base::FilePath& watch_path, | 20 const base::FilePath& watch_path, |
| 21 const base::FilePathWatcher::Callback& callback) { | 21 const base::FilePathWatcher::Callback& callback) { |
| 22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 23 DCHECK(!callback.is_null()); | 23 DCHECK(!callback.is_null()); |
| 24 | 24 |
| 25 scoped_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher); | 25 scoped_ptr<base::FilePathWatcher> watcher(new base::FilePathWatcher); |
| 26 if (!watcher->Watch(watch_path, false /* recursive */, callback)) | 26 if (!watcher->Watch(watch_path, false /* recursive */, callback)) |
| 27 return NULL; | 27 return NULL; |
| 28 | 28 |
| 29 return watcher.release(); | 29 return watcher.release(); |
| 30 } | 30 } |
| 31 | 31 |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 FileWatcherExtensions::FileWatcherExtensions(const base::FilePath& virtual_path, | 34 FileWatcher::FileWatcher(const base::FilePath& virtual_path, |
| 35 const std::string& extension_id, | 35 const std::string& extension_id, |
| 36 bool is_remote_file_system) | 36 bool is_remote_file_system) |
| 37 : file_watcher_(NULL), | 37 : local_file_watcher_(NULL), |
| 38 virtual_path_(virtual_path), | 38 virtual_path_(virtual_path), |
| 39 ref_count_(0), | 39 ref_count_(0), |
| 40 is_remote_file_system_(is_remote_file_system), | 40 is_remote_file_system_(is_remote_file_system), |
| 41 weak_ptr_factory_(this) { | 41 weak_ptr_factory_(this) { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 43 | 43 |
| 44 AddExtension(extension_id); | 44 AddExtension(extension_id); |
| 45 } | 45 } |
| 46 | 46 |
| 47 FileWatcherExtensions::~FileWatcherExtensions() { | 47 FileWatcher::~FileWatcher() { |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 49 | 49 |
| 50 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, file_watcher_); | 50 BrowserThread::DeleteSoon(BrowserThread::FILE, |
| 51 FROM_HERE, |
| 52 local_file_watcher_); |
| 51 } | 53 } |
| 52 | 54 |
| 53 void FileWatcherExtensions::AddExtension(const std::string& extension_id) { | 55 void FileWatcher::AddExtension(const std::string& extension_id) { |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 55 | 57 |
| 56 extensions_[extension_id]++; | 58 extensions_[extension_id]++; |
| 57 ref_count_++; | 59 ref_count_++; |
| 58 } | 60 } |
| 59 | 61 |
| 60 void FileWatcherExtensions::RemoveExtension(const std::string& extension_id) { | 62 void FileWatcher::RemoveExtension(const std::string& extension_id) { |
| 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 62 | 64 |
| 63 ExtensionUsageRegistry::iterator it = extensions_.find(extension_id); | 65 ExtensionUsageRegistry::iterator it = extensions_.find(extension_id); |
| 64 if (it == extensions_.end()) { | 66 if (it == extensions_.end()) { |
| 65 LOG(ERROR) << " Extension [" << extension_id | 67 LOG(ERROR) << " Extension [" << extension_id |
| 66 << "] tries to unsubscribe from folder [" << local_path_.value() | 68 << "] tries to unsubscribe from folder [" << local_path_.value() |
| 67 << "] it isn't subscribed"; | 69 << "] it isn't subscribed"; |
| 68 return; | 70 return; |
| 69 } | 71 } |
| 70 | 72 |
| 71 // If entry found - decrease it's count and remove if necessary | 73 // If entry found - decrease it's count and remove if necessary |
| 72 if (it->second-- == 0) | 74 if (it->second-- == 0) |
| 73 extensions_.erase(it); | 75 extensions_.erase(it); |
| 74 | 76 |
| 75 ref_count_--; | 77 ref_count_--; |
| 76 } | 78 } |
| 77 | 79 |
| 78 void FileWatcherExtensions::Watch( | 80 void FileWatcher::Watch( |
| 79 const base::FilePath& local_path, | 81 const base::FilePath& local_path, |
| 80 const base::FilePathWatcher::Callback& file_watcher_callback, | 82 const base::FilePathWatcher::Callback& file_watcher_callback, |
| 81 const BoolCallback& callback) { | 83 const BoolCallback& callback) { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 83 DCHECK(!callback.is_null()); | 85 DCHECK(!callback.is_null()); |
| 84 DCHECK(!file_watcher_); | 86 DCHECK(!local_file_watcher_); |
| 85 | 87 |
| 86 local_path_ = local_path; // For error message in RemoveExtension(). | 88 local_path_ = local_path; // For error message in RemoveExtension(). |
| 87 | 89 |
| 88 if (is_remote_file_system_) { | 90 if (is_remote_file_system_) { |
| 89 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | 91 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
| 90 base::Bind(callback, true)); | 92 base::Bind(callback, true)); |
| 91 return; | 93 return; |
| 92 } | 94 } |
| 93 | 95 |
| 94 BrowserThread::PostTaskAndReplyWithResult( | 96 BrowserThread::PostTaskAndReplyWithResult( |
| 95 BrowserThread::FILE, | 97 BrowserThread::FILE, |
| 96 FROM_HERE, | 98 FROM_HERE, |
| 97 base::Bind(&CreateAndStartFilePathWatcher, | 99 base::Bind(&CreateAndStartFilePathWatcher, |
| 98 local_path, | 100 local_path, |
| 99 google_apis::CreateRelayCallback(file_watcher_callback)), | 101 google_apis::CreateRelayCallback(file_watcher_callback)), |
| 100 base::Bind(&FileWatcherExtensions::OnWatcherStarted, | 102 base::Bind(&FileWatcher::OnWatcherStarted, |
| 101 weak_ptr_factory_.GetWeakPtr(), | 103 weak_ptr_factory_.GetWeakPtr(), |
| 102 callback)); | 104 callback)); |
| 103 } | 105 } |
| 104 | 106 |
| 105 void FileWatcherExtensions::OnWatcherStarted( | 107 void FileWatcher::OnWatcherStarted( |
| 106 const BoolCallback& callback, | 108 const BoolCallback& callback, |
| 107 base::FilePathWatcher* file_watcher) { | 109 base::FilePathWatcher* file_watcher) { |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 109 DCHECK(!callback.is_null()); | 111 DCHECK(!callback.is_null()); |
| 110 DCHECK(!file_watcher_); | 112 DCHECK(!local_file_watcher_); |
| 111 | 113 |
| 112 if (file_watcher) { | 114 if (file_watcher) { |
| 113 file_watcher_ = file_watcher; | 115 local_file_watcher_ = file_watcher; |
| 114 callback.Run(true); | 116 callback.Run(true); |
| 115 } else { | 117 } else { |
| 116 callback.Run(false); | 118 callback.Run(false); |
| 117 } | 119 } |
| 118 } | 120 } |
| 119 | 121 |
| 120 } // namespace file_manager | 122 } // namespace file_manager |
| OLD | NEW |