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