OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/file_system_provider/service.h" | 5 #include "chrome/browser/chromeos/file_system_provider/service.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/stl_util.h" |
9 #include "base/values.h" | 9 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" |
10 #include "chrome/browser/chromeos/file_system_provider/observer.h" | 10 #include "chrome/browser/chromeos/file_system_provider/observer.h" |
11 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" | 11 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" |
| 12 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" |
| 13 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" |
12 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" | 14 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" |
13 #include "chrome/browser/chromeos/login/user.h" | |
14 #include "chrome/browser/chromeos/login/user_manager.h" | |
15 #include "chrome/common/extensions/api/file_system_provider.h" | |
16 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
17 #include "extensions/browser/event_router.h" | |
18 #include "extensions/browser/extension_system.h" | 16 #include "extensions/browser/extension_system.h" |
19 #include "webkit/browser/fileapi/external_mount_points.h" | 17 #include "webkit/browser/fileapi/external_mount_points.h" |
20 | 18 |
21 namespace chromeos { | 19 namespace chromeos { |
22 namespace file_system_provider { | 20 namespace file_system_provider { |
23 namespace { | 21 namespace { |
24 | 22 |
25 // Root mount path for all of the provided file systems. | |
26 const base::FilePath::CharType kProvidedMountPointRoot[] = | |
27 FILE_PATH_LITERAL("/provided"); | |
28 | |
29 // Maximum number of file systems to be mounted in the same time, per profile. | 23 // Maximum number of file systems to be mounted in the same time, per profile. |
30 const size_t kMaxFileSystems = 16; | 24 const size_t kMaxFileSystems = 16; |
31 | 25 |
32 // Constructs a safe mount point path for the provided file system represented | 26 // Default factory for provided file systems. The |event_router| nor the |
33 // by |file_system_handle|. The handle is a numeric part of the file system id. | 27 // |request_manager| arguments must not be NULL. |
34 base::FilePath GetMountPointPath(Profile* profile, | 28 ProvidedFileSystemInterface* CreateProvidedFileSystem( |
35 std::string extension_id, | 29 extensions::EventRouter* event_router, |
36 int file_system_id) { | 30 RequestManager* request_manager, |
37 chromeos::User* const user = | 31 const ProvidedFileSystemInfo& file_system_info) { |
38 chromeos::UserManager::IsInitialized() | 32 DCHECK(event_router); |
39 ? chromeos::UserManager::Get()->GetUserByProfile( | 33 DCHECK(request_manager); |
40 profile->GetOriginalProfile()) | 34 return new ProvidedFileSystem( |
41 : NULL; | 35 event_router, request_manager, file_system_info); |
42 const std::string user_suffix = user ? "-" + user->username_hash() : ""; | |
43 return base::FilePath(kProvidedMountPointRoot).AppendASCII( | |
44 extension_id + "-" + base::IntToString(file_system_id) + user_suffix); | |
45 } | |
46 | |
47 // Creates values to be passed to request events. These values can be extended | |
48 // by additional fields. | |
49 scoped_ptr<base::ListValue> CreateRequestValues(int file_system_id, | |
50 int request_id) { | |
51 scoped_ptr<base::ListValue> values(new base::ListValue()); | |
52 values->AppendInteger(file_system_id); | |
53 values->AppendInteger(request_id); | |
54 return values.Pass(); | |
55 } | 36 } |
56 | 37 |
57 } // namespace | 38 } // namespace |
58 | 39 |
59 Service::Service(Profile* profile) | 40 Service::Service(Profile* profile) |
60 : profile_(profile), next_id_(1), weak_ptr_factory_(this) { | 41 : profile_(profile), |
| 42 file_system_factory_(base::Bind(CreateProvidedFileSystem)), |
| 43 next_id_(1), |
| 44 weak_ptr_factory_(this) { |
61 AddObserver(&request_manager_); | 45 AddObserver(&request_manager_); |
62 } | 46 } |
63 | 47 |
64 Service::~Service() {} | 48 Service::~Service() { STLDeleteValues(&file_system_map_); } |
65 | 49 |
66 // static | 50 // static |
67 Service* Service::Get(content::BrowserContext* context) { | 51 Service* Service::Get(content::BrowserContext* context) { |
68 return ServiceFactory::Get(context); | 52 return ServiceFactory::Get(context); |
69 } | 53 } |
70 | 54 |
71 void Service::AddObserver(Observer* observer) { | 55 void Service::AddObserver(Observer* observer) { |
72 DCHECK(observer); | 56 DCHECK(observer); |
73 observers_.AddObserver(observer); | 57 observers_.AddObserver(observer); |
74 } | 58 } |
75 | 59 |
76 void Service::RemoveObserver(Observer* observer) { | 60 void Service::RemoveObserver(Observer* observer) { |
77 DCHECK(observer); | 61 DCHECK(observer); |
78 observers_.RemoveObserver(observer); | 62 observers_.RemoveObserver(observer); |
79 } | 63 } |
80 | 64 |
| 65 void Service::SetFileSystemFactoryForTests( |
| 66 const FileSystemFactoryCallback& factory_callback) { |
| 67 DCHECK(!factory_callback.is_null()); |
| 68 file_system_factory_ = factory_callback; |
| 69 } |
| 70 |
81 int Service::MountFileSystem(const std::string& extension_id, | 71 int Service::MountFileSystem(const std::string& extension_id, |
82 const std::string& file_system_name) { | 72 const std::string& file_system_name) { |
83 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 73 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
84 | 74 |
85 // Restrict number of file systems to prevent system abusing. | 75 // Restrict number of file systems to prevent system abusing. |
86 if (file_systems_.size() + 1 > kMaxFileSystems) { | 76 if (file_system_map_.size() + 1 > kMaxFileSystems) { |
87 FOR_EACH_OBSERVER( | 77 FOR_EACH_OBSERVER( |
88 Observer, | 78 Observer, |
89 observers_, | 79 observers_, |
90 OnProvidedFileSystemMount(ProvidedFileSystem(), | 80 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
91 base::File::FILE_ERROR_TOO_MANY_OPENED)); | 81 base::File::FILE_ERROR_TOO_MANY_OPENED)); |
92 return 0; | 82 return 0; |
93 } | 83 } |
94 | 84 |
95 // The file system id is unique per service, so per profile. | 85 // The provided file system id is unique per service, so per profile. |
96 int file_system_id = next_id_; | 86 int file_system_id = next_id_; |
97 | 87 |
98 fileapi::ExternalMountPoints* const mount_points = | 88 fileapi::ExternalMountPoints* const mount_points = |
99 fileapi::ExternalMountPoints::GetSystemInstance(); | 89 fileapi::ExternalMountPoints::GetSystemInstance(); |
100 DCHECK(mount_points); | 90 DCHECK(mount_points); |
101 | 91 |
102 // The mount point path and name are unique per system, since they are system | 92 // The mount point path and name are unique per system, since they are system |
103 // wide. This is necessary for copying between profiles. | 93 // wide. This is necessary for copying between profiles. |
104 const base::FilePath& mount_point_path = | 94 const base::FilePath& mount_point_path = |
105 GetMountPointPath(profile_, extension_id, file_system_id); | 95 util::GetMountPointPath(profile_, extension_id, file_system_id); |
106 const std::string mount_point_name = | 96 const std::string mount_point_name = |
107 mount_point_path.BaseName().AsUTF8Unsafe(); | 97 mount_point_path.BaseName().AsUTF8Unsafe(); |
108 | 98 |
109 if (!mount_points->RegisterFileSystem(mount_point_name, | 99 if (!mount_points->RegisterFileSystem(mount_point_name, |
110 fileapi::kFileSystemTypeProvided, | 100 fileapi::kFileSystemTypeProvided, |
111 fileapi::FileSystemMountOption(), | 101 fileapi::FileSystemMountOption(), |
112 mount_point_path)) { | 102 mount_point_path)) { |
113 FOR_EACH_OBSERVER( | 103 FOR_EACH_OBSERVER( |
114 Observer, | 104 Observer, |
115 observers_, | 105 observers_, |
116 OnProvidedFileSystemMount(ProvidedFileSystem(), | 106 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
117 base::File::FILE_ERROR_INVALID_OPERATION)); | 107 base::File::FILE_ERROR_INVALID_OPERATION)); |
118 return 0; | 108 return 0; |
119 } | 109 } |
120 | 110 |
121 // Store the file system descriptor. Use the mount point name as the file | 111 // Store the file system descriptor. Use the mount point name as the file |
122 // system provider file system id. | 112 // system provider file system id. |
123 // Examples: | 113 // Examples: |
124 // file_system_id = 41 | 114 // file_system_id = 41 |
125 // mount_point_name = file_system_id = b33f1337-41-5aa5 | 115 // mount_point_name = file_system_id = b33f1337-41-5aa5 |
126 // mount_point_path = /provided/b33f1337-41-5aa5 | 116 // mount_point_path = /provided/b33f1337-41-5aa5 |
127 ProvidedFileSystem file_system( | 117 ProvidedFileSystemInfo file_system_info( |
128 extension_id, file_system_id, file_system_name, mount_point_path); | 118 extension_id, file_system_id, file_system_name, mount_point_path); |
129 file_systems_[file_system_id] = file_system; | 119 |
| 120 // The event router may be NULL for unit tests. |
| 121 extensions::EventRouter* event_router = |
| 122 extensions::ExtensionSystem::Get(profile_)->event_router(); |
| 123 |
| 124 ProvidedFileSystemInterface* file_system = file_system_factory_.Run( |
| 125 event_router, &request_manager_, file_system_info); |
| 126 DCHECK(file_system); |
| 127 file_system_map_[file_system_id] = file_system; |
130 | 128 |
131 FOR_EACH_OBSERVER( | 129 FOR_EACH_OBSERVER( |
132 Observer, | 130 Observer, |
133 observers_, | 131 observers_, |
134 OnProvidedFileSystemMount(file_system, base::File::FILE_OK)); | 132 OnProvidedFileSystemMount(file_system_info, base::File::FILE_OK)); |
135 | 133 |
136 next_id_++; | 134 next_id_++; |
137 return file_system_id; | 135 return file_system_id; |
138 } | 136 } |
139 | 137 |
140 bool Service::UnmountFileSystem(const std::string& extension_id, | 138 bool Service::UnmountFileSystem(const std::string& extension_id, |
141 int file_system_id) { | 139 int file_system_id) { |
142 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 140 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
143 | 141 |
144 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | 142 ProvidedFileSystemMap::iterator file_system_it = |
145 if (file_system_it == file_systems_.end() || | 143 file_system_map_.find(file_system_id); |
146 file_system_it->second.extension_id() != extension_id) { | 144 if (file_system_it == file_system_map_.end() || |
147 const ProvidedFileSystem empty_file_system; | 145 file_system_it->second->GetFileSystemInfo().extension_id() != |
148 FOR_EACH_OBSERVER(Observer, | 146 extension_id) { |
149 observers_, | 147 const ProvidedFileSystemInfo empty_file_system_info; |
150 OnProvidedFileSystemUnmount( | 148 FOR_EACH_OBSERVER( |
151 empty_file_system, base::File::FILE_ERROR_NOT_FOUND)); | 149 Observer, |
| 150 observers_, |
| 151 OnProvidedFileSystemUnmount(empty_file_system_info, |
| 152 base::File::FILE_ERROR_NOT_FOUND)); |
152 return false; | 153 return false; |
153 } | 154 } |
154 | 155 |
155 fileapi::ExternalMountPoints* const mount_points = | 156 fileapi::ExternalMountPoints* const mount_points = |
156 fileapi::ExternalMountPoints::GetSystemInstance(); | 157 fileapi::ExternalMountPoints::GetSystemInstance(); |
157 DCHECK(mount_points); | 158 DCHECK(mount_points); |
158 | 159 |
| 160 const ProvidedFileSystemInfo& file_system_info = |
| 161 file_system_it->second->GetFileSystemInfo(); |
| 162 |
159 const std::string mount_point_name = | 163 const std::string mount_point_name = |
160 file_system_it->second.mount_path().BaseName().value(); | 164 file_system_info.mount_path().BaseName().value(); |
161 if (!mount_points->RevokeFileSystem(mount_point_name)) { | 165 if (!mount_points->RevokeFileSystem(mount_point_name)) { |
162 FOR_EACH_OBSERVER( | 166 FOR_EACH_OBSERVER( |
163 Observer, | 167 Observer, |
164 observers_, | 168 observers_, |
165 OnProvidedFileSystemUnmount(file_system_it->second, | 169 OnProvidedFileSystemUnmount(file_system_info, |
166 base::File::FILE_ERROR_INVALID_OPERATION)); | 170 base::File::FILE_ERROR_INVALID_OPERATION)); |
167 return false; | 171 return false; |
168 } | 172 } |
169 | 173 |
170 FOR_EACH_OBSERVER( | 174 FOR_EACH_OBSERVER( |
171 Observer, | 175 Observer, |
172 observers_, | 176 observers_, |
173 OnProvidedFileSystemUnmount(file_system_it->second, base::File::FILE_OK)); | 177 OnProvidedFileSystemUnmount(file_system_info, base::File::FILE_OK)); |
174 | 178 |
175 file_systems_.erase(file_system_it); | 179 delete file_system_it->second; |
| 180 file_system_map_.erase(file_system_it); |
176 return true; | 181 return true; |
177 } | 182 } |
178 | 183 |
179 std::vector<ProvidedFileSystem> Service::GetMountedFileSystems() { | |
180 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
181 | |
182 std::vector<ProvidedFileSystem> result; | |
183 for (FileSystemMap::const_iterator it = file_systems_.begin(); | |
184 it != file_systems_.end(); | |
185 ++it) { | |
186 result.push_back(it->second); | |
187 } | |
188 return result; | |
189 } | |
190 | |
191 bool Service::FulfillRequest(const std::string& extension_id, | |
192 int file_system_id, | |
193 int request_id, | |
194 scoped_ptr<base::DictionaryValue> result, | |
195 bool has_next) { | |
196 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
197 | |
198 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | |
199 if (file_system_it == file_systems_.end() || | |
200 file_system_it->second.extension_id() != extension_id) { | |
201 return false; | |
202 } | |
203 | |
204 return request_manager_.FulfillRequest( | |
205 file_system_it->second, request_id, result.Pass(), has_next); | |
206 } | |
207 | |
208 bool Service::RejectRequest(const std::string& extension_id, | |
209 int file_system_id, | |
210 int request_id, | |
211 base::File::Error error) { | |
212 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
213 | |
214 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | |
215 if (file_system_it == file_systems_.end() || | |
216 file_system_it->second.extension_id() != extension_id) { | |
217 return false; | |
218 } | |
219 | |
220 return request_manager_.RejectRequest( | |
221 file_system_it->second, request_id, error); | |
222 } | |
223 | |
224 bool Service::RequestUnmount(int file_system_id) { | 184 bool Service::RequestUnmount(int file_system_id) { |
225 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 185 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
226 | 186 |
227 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | 187 ProvidedFileSystemMap::iterator file_system_it = |
228 if (file_system_it == file_systems_.end()) | 188 file_system_map_.find(file_system_id); |
| 189 if (file_system_it == file_system_map_.end()) |
229 return false; | 190 return false; |
230 | 191 |
231 int request_id = | 192 return file_system_it->second->RequestUnmount( |
232 request_manager_.CreateRequest(file_system_it->second, | 193 base::Bind(&Service::OnRequestUnmountStatus, |
233 SuccessCallback(), | 194 weak_ptr_factory_.GetWeakPtr(), |
234 base::Bind(&Service::OnRequestUnmountError, | 195 file_system_it->second->GetFileSystemInfo())); |
235 weak_ptr_factory_.GetWeakPtr(), | 196 } |
236 file_system_it->second)); | |
237 | 197 |
238 if (!request_id) | 198 std::vector<ProvidedFileSystemInfo> Service::GetProvidedFileSystemInfoList() { |
239 return false; | 199 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
240 | 200 |
241 scoped_ptr<base::ListValue> values( | 201 std::vector<ProvidedFileSystemInfo> result; |
242 CreateRequestValues(file_system_id, request_id)); | 202 for (ProvidedFileSystemMap::const_iterator it = file_system_map_.begin(); |
| 203 it != file_system_map_.end(); |
| 204 ++it) { |
| 205 result.push_back(it->second->GetFileSystemInfo()); |
| 206 } |
| 207 return result; |
| 208 } |
243 | 209 |
244 extensions::EventRouter* event_router = | 210 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
245 extensions::ExtensionSystem::Get(profile_)->event_router(); | 211 const std::string& extension_id, |
246 DCHECK(event_router); | 212 int file_system_id) { |
| 213 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
247 | 214 |
248 event_router->DispatchEventToExtension( | 215 ProvidedFileSystemMap::iterator file_system_it = |
249 file_system_it->second.extension_id(), | 216 file_system_map_.find(file_system_id); |
250 make_scoped_ptr(new extensions::Event( | 217 if (file_system_it == file_system_map_.end() || |
251 extensions::api::file_system_provider::OnUnmountRequested::kEventName, | 218 file_system_it->second->GetFileSystemInfo().extension_id() != |
252 values.Pass()))); | 219 extension_id) { |
| 220 return NULL; |
| 221 } |
253 | 222 |
254 return true; | 223 return file_system_it->second; |
255 } | 224 } |
256 | 225 |
257 void Service::Shutdown() { RemoveObserver(&request_manager_); } | 226 void Service::Shutdown() { RemoveObserver(&request_manager_); } |
258 | 227 |
259 void Service::OnRequestUnmountError(const ProvidedFileSystem& file_system, | 228 void Service::OnRequestUnmountStatus( |
260 base::File::Error error) { | 229 const ProvidedFileSystemInfo& file_system_info, |
261 FOR_EACH_OBSERVER( | 230 base::File::Error error) { |
262 Observer, observers_, OnProvidedFileSystemUnmount(file_system, error)); | 231 // Notify observers about failure in unmounting, since mount() will not be |
| 232 // called by the provided file system. In case of success mount() will be |
| 233 // invoked, and observers notified, so there is no need to call them now. |
| 234 if (error != base::File::FILE_OK) { |
| 235 FOR_EACH_OBSERVER(Observer, |
| 236 observers_, |
| 237 OnProvidedFileSystemUnmount(file_system_info, error)); |
| 238 } |
263 } | 239 } |
264 | 240 |
265 } // namespace file_system_provider | 241 } // namespace file_system_provider |
266 } // namespace chromeos | 242 } // namespace chromeos |
OLD | NEW |