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/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 } | 119 } |
120 | 120 |
121 // File watch setup routines. | 121 // File watch setup routines. |
122 bool ExtensionFileBrowserEventRouter::AddFileWatch( | 122 bool ExtensionFileBrowserEventRouter::AddFileWatch( |
123 const FilePath& local_path, | 123 const FilePath& local_path, |
124 const FilePath& virtual_path, | 124 const FilePath& virtual_path, |
125 const std::string& extension_id) { | 125 const std::string& extension_id) { |
126 base::AutoLock lock(lock_); | 126 base::AutoLock lock(lock_); |
127 WatcherMap::iterator iter = file_watchers_.find(local_path); | 127 WatcherMap::iterator iter = file_watchers_.find(local_path); |
128 if (iter == file_watchers_.end()) { | 128 if (iter == file_watchers_.end()) { |
129 FileWatcherExtensions* watch = new FileWatcherExtensions(virtual_path, | 129 scoped_ptr<FileWatcherExtensions> |
130 extension_id); | 130 watch(new FileWatcherExtensions(virtual_path, extension_id)); |
131 file_watchers_[local_path] = watch; | 131 |
132 if (!watch->file_watcher->Watch(local_path, delegate_.get())) { | 132 if (watch->Watch(local_path, delegate_.get())) |
133 delete iter->second; | 133 file_watchers_[local_path] = watch.release(); |
134 file_watchers_.erase(iter); | 134 else |
135 return false; | 135 return false; |
136 } | |
137 } else { | 136 } else { |
138 iter->second->extensions.insert(extension_id); | 137 iter->second->AddExtension(extension_id); |
139 } | 138 } |
140 return true; | 139 return true; |
141 } | 140 } |
142 | 141 |
143 void ExtensionFileBrowserEventRouter::RemoveFileWatch( | 142 void ExtensionFileBrowserEventRouter::RemoveFileWatch( |
144 const FilePath& local_path, | 143 const FilePath& local_path, |
145 const std::string& extension_id) { | 144 const std::string& extension_id) { |
146 base::AutoLock lock(lock_); | 145 base::AutoLock lock(lock_); |
147 WatcherMap::iterator iter = file_watchers_.find(local_path); | 146 WatcherMap::iterator iter = file_watchers_.find(local_path); |
148 if (iter == file_watchers_.end()) | 147 if (iter == file_watchers_.end()) |
149 return; | 148 return; |
150 // Remove the renderer process for this watch. | 149 // Remove the renderer process for this watch. |
151 iter->second->extensions.erase(extension_id); | 150 iter->second->RemoveExtension(extension_id); |
152 if (iter->second->extensions.empty()) { | 151 if (iter->second->GetRefCount() == 0) { |
153 delete iter->second; | 152 delete iter->second; |
154 file_watchers_.erase(iter); | 153 file_watchers_.erase(iter); |
155 } | 154 } |
156 } | 155 } |
157 | 156 |
158 void ExtensionFileBrowserEventRouter::DiskChanged( | 157 void ExtensionFileBrowserEventRouter::DiskChanged( |
159 chromeos::MountLibraryEventType event, | 158 chromeos::MountLibraryEventType event, |
160 const chromeos::MountLibrary::Disk* disk) { | 159 const chromeos::MountLibrary::Disk* disk) { |
161 // Disregard hidden devices. | 160 // Disregard hidden devices. |
162 if (disk->is_hidden()) | 161 if (disk->is_hidden()) |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 } | 217 } |
219 | 218 |
220 void ExtensionFileBrowserEventRouter::HandleFileWatchNotification( | 219 void ExtensionFileBrowserEventRouter::HandleFileWatchNotification( |
221 const FilePath& local_path, bool got_error) { | 220 const FilePath& local_path, bool got_error) { |
222 base::AutoLock lock(lock_); | 221 base::AutoLock lock(lock_); |
223 WatcherMap::const_iterator iter = file_watchers_.find(local_path); | 222 WatcherMap::const_iterator iter = file_watchers_.find(local_path); |
224 if (iter == file_watchers_.end()) { | 223 if (iter == file_watchers_.end()) { |
225 NOTREACHED(); | 224 NOTREACHED(); |
226 return; | 225 return; |
227 } | 226 } |
228 DispatchFolderChangeEvent(iter->second->virtual_path, got_error, | 227 DispatchFolderChangeEvent(iter->second->GetVirtualPath(), got_error, |
229 iter->second->extensions); | 228 iter->second->GetExtensions()); |
230 } | 229 } |
231 | 230 |
232 void ExtensionFileBrowserEventRouter::DispatchFolderChangeEvent( | 231 void ExtensionFileBrowserEventRouter::DispatchFolderChangeEvent( |
233 const FilePath& virtual_path, bool got_error, | 232 const FilePath& virtual_path, bool got_error, |
234 const std::set<std::string>& extensions) { | 233 const ExtensionFileBrowserEventRouter::ExtensionUsageRegistry& extensions) { |
235 if (!profile_) { | 234 if (!profile_) { |
236 NOTREACHED(); | 235 NOTREACHED(); |
237 return; | 236 return; |
238 } | 237 } |
239 | 238 |
240 for (std::set<std::string>::const_iterator iter = extensions.begin(); | 239 for (ExtensionUsageRegistry::const_iterator iter = extensions.begin(); |
241 iter != extensions.end(); ++iter) { | 240 iter != extensions.end(); ++iter) { |
242 GURL target_origin_url(Extension::GetBaseURLFromExtensionId( | 241 GURL target_origin_url(Extension::GetBaseURLFromExtensionId( |
243 *iter)); | 242 iter->first)); |
244 GURL base_url = fileapi::GetFileSystemRootURI(target_origin_url, | 243 GURL base_url = fileapi::GetFileSystemRootURI(target_origin_url, |
245 fileapi::kFileSystemTypeExternal); | 244 fileapi::kFileSystemTypeExternal); |
246 GURL target_file_url = GURL(base_url.spec() + virtual_path.value()); | 245 GURL target_file_url = GURL(base_url.spec() + virtual_path.value()); |
247 ListValue args; | 246 ListValue args; |
248 DictionaryValue* watch_info = new DictionaryValue(); | 247 DictionaryValue* watch_info = new DictionaryValue(); |
249 args.Append(watch_info); | 248 args.Append(watch_info); |
250 watch_info->SetString("fileUrl", target_file_url.spec()); | 249 watch_info->SetString("fileUrl", target_file_url.spec()); |
251 watch_info->SetString("eventType", | 250 watch_info->SetString("eventType", |
252 got_error ? kPathWatchError : kPathChanged); | 251 got_error ? kPathWatchError : kPathChanged); |
253 | 252 |
254 std::string args_json; | 253 std::string args_json; |
255 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); | 254 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); |
256 | 255 |
257 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | 256 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
258 *iter, extension_event_names::kOnFileChanged, args_json, | 257 iter->first, extension_event_names::kOnFileChanged, args_json, |
259 NULL, GURL()); | 258 NULL, GURL()); |
260 } | 259 } |
261 } | 260 } |
262 | 261 |
263 void ExtensionFileBrowserEventRouter::DispatchDiskEvent( | 262 void ExtensionFileBrowserEventRouter::DispatchDiskEvent( |
264 const chromeos::MountLibrary::Disk* disk, bool added) { | 263 const chromeos::MountLibrary::Disk* disk, bool added) { |
265 if (!profile_) { | 264 if (!profile_) { |
266 NOTREACHED(); | 265 NOTREACHED(); |
267 return; | 266 return; |
268 } | 267 } |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 base::Bind(&FileWatcherDelegate::HandleFileWatchOnUIThread, | 458 base::Bind(&FileWatcherDelegate::HandleFileWatchOnUIThread, |
460 this, local_path, true)); | 459 this, local_path, true)); |
461 } | 460 } |
462 | 461 |
463 void | 462 void |
464 ExtensionFileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread( | 463 ExtensionFileBrowserEventRouter::FileWatcherDelegate::HandleFileWatchOnUIThread( |
465 const FilePath& local_path, bool got_error) { | 464 const FilePath& local_path, bool got_error) { |
466 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 465 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
467 router_->HandleFileWatchNotification(local_path, got_error); | 466 router_->HandleFileWatchNotification(local_path, got_error); |
468 } | 467 } |
| 468 |
| 469 |
| 470 ExtensionFileBrowserEventRouter::FileWatcherExtensions::FileWatcherExtensions( |
| 471 const FilePath& path, const std::string& extension_id) { |
| 472 file_watcher.reset(new base::files::FilePathWatcher()); |
| 473 virtual_path = path; |
| 474 AddExtension(extension_id); |
| 475 } |
| 476 |
| 477 void ExtensionFileBrowserEventRouter::FileWatcherExtensions::AddExtension( |
| 478 const std::string& extension_id) { |
| 479 ExtensionUsageRegistry::iterator it = extensions.find(extension_id); |
| 480 if (it != extensions.end()) { |
| 481 it->second++; |
| 482 } else { |
| 483 extensions.insert(ExtensionUsageRegistry::value_type(extension_id, 1)); |
| 484 } |
| 485 |
| 486 ref_count++; |
| 487 } |
| 488 |
| 489 void ExtensionFileBrowserEventRouter::FileWatcherExtensions::RemoveExtension( |
| 490 const std::string& extension_id) { |
| 491 ExtensionUsageRegistry::iterator it = extensions.find(extension_id); |
| 492 |
| 493 if (it != extensions.end()) { |
| 494 // If entry found - decrease it's count and remove if necessary |
| 495 if (0 == it->second--) { |
| 496 extensions.erase(it); |
| 497 } |
| 498 |
| 499 ref_count--; |
| 500 } else { |
| 501 // Might be reference counting problem - e.g. if some component of |
| 502 // extension subscribes/unsubscribes correctly, but other component |
| 503 // only unsubscribes, developer of first one might receive this message |
| 504 LOG(FATAL) << " Extension [" << extension_id |
| 505 << "] tries to unsubscribe from folder [" << local_path.value() |
| 506 << "] it isn't subscribed"; |
| 507 } |
| 508 } |
| 509 |
| 510 const ExtensionFileBrowserEventRouter::ExtensionUsageRegistry& |
| 511 ExtensionFileBrowserEventRouter::FileWatcherExtensions::GetExtensions() const { |
| 512 return extensions; |
| 513 } |
| 514 |
| 515 unsigned int |
| 516 ExtensionFileBrowserEventRouter::FileWatcherExtensions::GetRefCount() const { |
| 517 return ref_count; |
| 518 } |
| 519 |
| 520 const FilePath& |
| 521 ExtensionFileBrowserEventRouter::FileWatcherExtensions::GetVirtualPath() const { |
| 522 return virtual_path; |
| 523 } |
| 524 |
| 525 bool ExtensionFileBrowserEventRouter::FileWatcherExtensions::Watch |
| 526 (const FilePath& path, FileWatcherDelegate* delegate) { |
| 527 return file_watcher->Watch(path, delegate); |
| 528 } |
OLD | NEW |