Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 chromeos::MountLibrary* lib = | 119 chromeos::MountLibrary* lib = |
| 120 chromeos::CrosLibrary::Get()->GetMountLibrary(); | 120 chromeos::CrosLibrary::Get()->GetMountLibrary(); |
| 121 lib->RemoveObserver(this); | 121 lib->RemoveObserver(this); |
| 122 lib->AddObserver(this); | 122 lib->AddObserver(this); |
| 123 lib->RequestMountInfoRefresh(); | 123 lib->RequestMountInfoRefresh(); |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 | 126 |
| 127 // File watch setup routines. | 127 // File watch setup routines. |
| 128 bool ExtensionFileBrowserEventRouter::AddFileWatch( | 128 bool ExtensionFileBrowserEventRouter::AddFileWatch( |
| 129 const FilePath& local_path, | 129 const FilePath& local_path, |
| 130 const FilePath& virtual_path, | 130 const FilePath& virtual_path, |
| 131 const std::string& extension_id) { | 131 const std::string& extension_id) { |
| 132 base::AutoLock lock(lock_); | 132 base::AutoLock lock(lock_); |
|
zel
2011/09/12 15:58:42
one space only?
Dmitry Zvorygin
2011/09/14 10:15:49
Done.
| |
| 133 WatcherMap::iterator iter = file_watchers_.find(local_path); | 133 WatcherMap::iterator iter = file_watchers_.find(local_path); |
| 134 if (iter == file_watchers_.end()) { | 134 if (iter == file_watchers_.end()) { |
| 135 FileWatcherExtensions* watch = new FileWatcherExtensions(virtual_path, | 135 scoped_ptr<FileWatcherExtensions> |
| 136 extension_id); | 136 watch(new FileWatcherExtensions(virtual_path, extension_id)); |
| 137 file_watchers_[local_path] = watch; | 137 |
| 138 if (!watch->file_watcher->Watch(local_path, delegate_.get())) { | 138 if (watch->file_watcher->Watch(local_path, delegate_.get())) |
| 139 delete iter->second; | 139 file_watchers_[local_path] = watch.release(); |
| 140 file_watchers_.erase(iter); | 140 else |
| 141 return false; | 141 return false; |
| 142 } | 142 } else { |
| 143 } else { | 143 iter->second->AddExtension(extension_id); |
| 144 iter->second->extensions.insert(extension_id); | 144 } |
| 145 } | 145 return true; |
| 146 return true; | |
| 147 } | 146 } |
| 148 | 147 |
| 149 void ExtensionFileBrowserEventRouter::RemoveFileWatch( | 148 void ExtensionFileBrowserEventRouter::RemoveFileWatch( |
| 150 const FilePath& local_path, | 149 const FilePath& local_path, |
| 151 const std::string& extension_id) { | 150 const std::string& extension_id) { |
| 152 base::AutoLock lock(lock_); | 151 base::AutoLock lock(lock_); |
| 153 WatcherMap::iterator iter = file_watchers_.find(local_path); | 152 WatcherMap::iterator iter = file_watchers_.find(local_path); |
| 154 if (iter == file_watchers_.end()) | 153 if (iter == file_watchers_.end()) |
| 155 return; | 154 return; |
| 156 // Remove the renderer process for this watch. | 155 // Remove the renderer process for this watch. |
| 157 iter->second->extensions.erase(extension_id); | 156 iter->second->RemoveExtension(extension_id); |
| 158 if (iter->second->extensions.empty()) { | 157 if (iter->second->GetRefCount() == 0) { |
| 159 delete iter->second; | 158 delete iter->second; |
| 160 file_watchers_.erase(iter); | 159 file_watchers_.erase(iter); |
| 161 } | 160 } |
| 162 } | 161 } |
| 163 | 162 |
| 164 void ExtensionFileBrowserEventRouter::DiskChanged( | 163 void ExtensionFileBrowserEventRouter::DiskChanged( |
| 165 chromeos::MountLibraryEventType event, | 164 chromeos::MountLibraryEventType event, |
| 166 const chromeos::MountLibrary::Disk* disk) { | 165 const chromeos::MountLibrary::Disk* disk) { |
| 167 if (event == chromeos::MOUNT_DISK_ADDED) { | 166 if (event == chromeos::MOUNT_DISK_ADDED) { |
| 168 OnDiskAdded(disk); | 167 OnDiskAdded(disk); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 197 | 196 |
| 198 void ExtensionFileBrowserEventRouter::HandleFileWatchNotification( | 197 void ExtensionFileBrowserEventRouter::HandleFileWatchNotification( |
| 199 const FilePath& local_path, bool got_error) { | 198 const FilePath& local_path, bool got_error) { |
| 200 base::AutoLock lock(lock_); | 199 base::AutoLock lock(lock_); |
| 201 WatcherMap::const_iterator iter = file_watchers_.find(local_path); | 200 WatcherMap::const_iterator iter = file_watchers_.find(local_path); |
| 202 if (iter == file_watchers_.end()) { | 201 if (iter == file_watchers_.end()) { |
| 203 NOTREACHED(); | 202 NOTREACHED(); |
| 204 return; | 203 return; |
| 205 } | 204 } |
| 206 DispatchFolderChangeEvent(iter->second->virtual_path, got_error, | 205 DispatchFolderChangeEvent(iter->second->virtual_path, got_error, |
| 207 iter->second->extensions); | 206 iter->second->GetExtensions()); |
| 208 } | 207 } |
| 209 | 208 |
| 210 void ExtensionFileBrowserEventRouter::DispatchFolderChangeEvent( | 209 void ExtensionFileBrowserEventRouter::DispatchFolderChangeEvent( |
| 211 const FilePath& virtual_path, bool got_error, | 210 const FilePath& virtual_path, bool got_error, |
| 212 const std::set<std::string>& extensions) { | 211 const ExtensionFileBrowserEventRouter::ExtensionUsageRegistry& extensions) { |
| 213 if (!profile_) { | 212 if (!profile_) { |
| 214 NOTREACHED(); | 213 NOTREACHED(); |
| 215 return; | 214 return; |
| 216 } | 215 } |
| 217 | 216 |
| 218 for (std::set<std::string>::const_iterator iter = extensions.begin(); | 217 for (ExtensionUsageRegistry::const_iterator iter = extensions.begin(); |
| 219 iter != extensions.end(); ++iter) { | 218 iter != extensions.end(); ++iter) { |
| 220 GURL target_origin_url(Extension::GetBaseURLFromExtensionId( | 219 GURL target_origin_url(Extension::GetBaseURLFromExtensionId( |
| 221 *iter)); | 220 iter->first)); |
| 222 GURL base_url = fileapi::GetFileSystemRootURI(target_origin_url, | 221 GURL base_url = fileapi::GetFileSystemRootURI(target_origin_url, |
| 223 fileapi::kFileSystemTypeExternal); | 222 fileapi::kFileSystemTypeExternal); |
| 224 GURL target_file_url = GURL(base_url.spec() + virtual_path.value()); | 223 GURL target_file_url = GURL(base_url.spec() + virtual_path.value()); |
| 225 ListValue args; | 224 ListValue args; |
| 226 DictionaryValue* watch_info = new DictionaryValue(); | 225 DictionaryValue* watch_info = new DictionaryValue(); |
| 227 args.Append(watch_info); | 226 args.Append(watch_info); |
| 228 watch_info->SetString("fileUrl", target_file_url.spec()); | 227 watch_info->SetString("fileUrl", target_file_url.spec()); |
| 229 watch_info->SetString("eventType", | 228 watch_info->SetString("eventType", |
| 230 got_error ? kPathWatchError : kPathChanged); | 229 got_error ? kPathWatchError : kPathChanged); |
| 231 | 230 |
| 232 std::string args_json; | 231 std::string args_json; |
| 233 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); | 232 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); |
| 234 | 233 |
| 235 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | 234 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| 236 *iter, extension_event_names::kOnFileChanged, args_json, | 235 iter->first, extension_event_names::kOnFileChanged, args_json, |
| 237 NULL, GURL()); | 236 NULL, GURL()); |
| 238 } | 237 } |
| 239 } | 238 } |
| 240 | 239 |
| 241 void ExtensionFileBrowserEventRouter::DispatchMountEvent( | 240 void ExtensionFileBrowserEventRouter::DispatchMountEvent( |
| 242 const chromeos::MountLibrary::Disk* disk, bool added) { | 241 const chromeos::MountLibrary::Disk* disk, bool added) { |
| 243 if (!profile_) { | 242 if (!profile_) { |
| 244 NOTREACHED(); | 243 NOTREACHED(); |
| 245 return; | 244 return; |
| 246 } | 245 } |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 505 &FileWatcherDelegate::HandleFileWatchOnUIThread, | 504 &FileWatcherDelegate::HandleFileWatchOnUIThread, |
| 506 local_path, | 505 local_path, |
| 507 true)); // got_error | 506 true)); // got_error |
| 508 } | 507 } |
| 509 | 508 |
| 510 void | 509 void |
| 511 ExtensionFileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread( | 510 ExtensionFileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread( |
| 512 const FilePath& local_path, bool got_error) { | 511 const FilePath& local_path, bool got_error) { |
| 513 router_->HandleFileWatchNotification(local_path, got_error); | 512 router_->HandleFileWatchNotification(local_path, got_error); |
| 514 } | 513 } |
| 514 | |
| 515 | |
| 516 ExtensionFileBrowserEventRouter::FileWatcherExtensions::FileWatcherExtensions( | |
| 517 const FilePath& path, const std::string& extension_id) { | |
| 518 file_watcher.reset(new base::files::FilePathWatcher()); | |
| 519 virtual_path = path; | |
| 520 AddExtension(extension_id); | |
| 521 } | |
| 522 | |
| 523 void ExtensionFileBrowserEventRouter::FileWatcherExtensions::AddExtension( | |
| 524 const std::string& extension_id) { | |
| 525 ExtensionUsageRegistry::iterator it = extensions.find(extension_id); | |
| 526 if (it != extensions.end()) { | |
| 527 it->second++; | |
| 528 } else { | |
| 529 extensions.insert(ExtensionUsageRegistry::value_type(extension_id, 1)); | |
| 530 } | |
| 531 | |
| 532 ref_count++; | |
| 533 } | |
| 534 | |
| 535 void ExtensionFileBrowserEventRouter::FileWatcherExtensions::RemoveExtension( | |
| 536 const std::string& extension_id) { | |
| 537 ExtensionUsageRegistry::iterator it = extensions.find(extension_id); | |
| 538 | |
| 539 if (it != extensions.end()) { | |
| 540 // If entry found - decrease it's count and remove if necessary | |
| 541 if (0 == it->second--) { | |
| 542 extensions.erase(it); | |
| 543 } | |
| 544 | |
| 545 ref_count--; | |
| 546 } else { | |
| 547 // Might be reference counting problem - e.g. if some component of | |
| 548 // extension subscribes/unsubscribes correctly, but other component | |
| 549 // only unsubscribes, developer of first one might receive this message | |
| 550 LOG(FATAL) << " Extension [" << extension_id | |
| 551 << "] tries to unsubscribe from folder [" << local_path.value() | |
| 552 << "] it isn't subscribed"; | |
| 553 } | |
| 554 } | |
| 555 | |
| 556 const ExtensionFileBrowserEventRouter::ExtensionUsageRegistry& | |
| 557 ExtensionFileBrowserEventRouter::FileWatcherExtensions::GetExtensions() const { | |
| 558 return extensions; | |
| 559 } | |
| 560 | |
| 561 unsigned int | |
| 562 ExtensionFileBrowserEventRouter::FileWatcherExtensions::GetRefCount() const { | |
| 563 return ref_count; | |
| 564 } | |
| 565 | |
| OLD | NEW |