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/strings/string_number_conversions.h" |
9 #include "base/values.h" | |
10 #include "chrome/browser/chromeos/file_system_provider/observer.h" | 9 #include "chrome/browser/chromeos/file_system_provider/observer.h" |
11 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" | 10 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" |
11 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info .h" | |
12 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte rface.h" | |
12 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" | 13 #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.h" |
14 #include "chrome/browser/chromeos/login/user_manager.h" | 15 #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" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "extensions/browser/event_router.h" | 17 #include "extensions/browser/event_router.h" |
18 #include "extensions/browser/extension_system.h" | 18 #include "extensions/browser/extension_system.h" |
19 #include "webkit/browser/fileapi/external_mount_points.h" | 19 #include "webkit/browser/fileapi/external_mount_points.h" |
20 | 20 |
21 namespace chromeos { | 21 namespace chromeos { |
22 namespace file_system_provider { | 22 namespace file_system_provider { |
23 namespace { | 23 namespace { |
24 | 24 |
25 // Root mount path for all of the provided file systems. | 25 // Root mount path for all of the provided file systems. |
(...skipping 11 matching lines...) Expand all Loading... | |
37 chromeos::User* const user = | 37 chromeos::User* const user = |
38 chromeos::UserManager::IsInitialized() | 38 chromeos::UserManager::IsInitialized() |
39 ? chromeos::UserManager::Get()->GetUserByProfile( | 39 ? chromeos::UserManager::Get()->GetUserByProfile( |
40 profile->GetOriginalProfile()) | 40 profile->GetOriginalProfile()) |
41 : NULL; | 41 : NULL; |
42 const std::string user_suffix = user ? "-" + user->username_hash() : ""; | 42 const std::string user_suffix = user ? "-" + user->username_hash() : ""; |
43 return base::FilePath(kProvidedMountPointRoot).AppendASCII( | 43 return base::FilePath(kProvidedMountPointRoot).AppendASCII( |
44 extension_id + "-" + base::IntToString(file_system_id) + user_suffix); | 44 extension_id + "-" + base::IntToString(file_system_id) + user_suffix); |
45 } | 45 } |
46 | 46 |
47 // Utility function, creates values to be passed to request events. These values | |
48 // can be extended 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 } | |
56 | |
57 } // namespace | 47 } // namespace |
58 | 48 |
59 Service::Service(Profile* profile) | 49 Service::Service(Profile* profile) |
60 : profile_(profile), next_id_(1), weak_ptr_factory_(this) { | 50 : profile_(profile), next_id_(1), weak_ptr_factory_(this) { |
61 AddObserver(&request_manager_); | 51 AddObserver(&request_manager_); |
62 } | 52 } |
63 | 53 |
64 Service::~Service() {} | 54 Service::~Service() {} |
65 | 55 |
66 // static | 56 // static |
(...skipping 15 matching lines...) Expand all Loading... | |
82 | 72 |
83 int Service::MountFileSystem(const std::string& extension_id, | 73 int Service::MountFileSystem(const std::string& extension_id, |
84 const std::string& file_system_name) { | 74 const std::string& file_system_name) { |
85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 75 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
86 | 76 |
87 // Restrict number of file systems to prevent system abusing. | 77 // Restrict number of file systems to prevent system abusing. |
88 if (file_systems_.size() + 1 > kMaxFileSystems) { | 78 if (file_systems_.size() + 1 > kMaxFileSystems) { |
89 FOR_EACH_OBSERVER( | 79 FOR_EACH_OBSERVER( |
90 Observer, | 80 Observer, |
91 observers_, | 81 observers_, |
92 OnProvidedFileSystemMount(ProvidedFileSystem(), | 82 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
93 base::File::FILE_ERROR_TOO_MANY_OPENED)); | 83 base::File::FILE_ERROR_TOO_MANY_OPENED)); |
94 return 0; | 84 return 0; |
95 } | 85 } |
96 | 86 |
97 // The file system id is unique per service, so per profile. | 87 // The provided file system id is unique per service, so per profile. |
98 int file_system_id = next_id_; | 88 int file_system_id = next_id_; |
99 | 89 |
100 fileapi::ExternalMountPoints* const mount_points = | 90 fileapi::ExternalMountPoints* const mount_points = |
101 fileapi::ExternalMountPoints::GetSystemInstance(); | 91 fileapi::ExternalMountPoints::GetSystemInstance(); |
102 DCHECK(mount_points); | 92 DCHECK(mount_points); |
103 | 93 |
104 // The mount point path and name are unique per system, since they are system | 94 // The mount point path and name are unique per system, since they are system |
105 // wide. This is necessary for copying between profiles. | 95 // wide. This is necessary for copying between profiles. |
106 const base::FilePath& mount_point_path = | 96 const base::FilePath& mount_point_path = |
107 GetMountPointPath(profile_, extension_id, file_system_id); | 97 GetMountPointPath(profile_, extension_id, file_system_id); |
108 const std::string mount_point_name = | 98 const std::string mount_point_name = |
109 mount_point_path.BaseName().AsUTF8Unsafe(); | 99 mount_point_path.BaseName().AsUTF8Unsafe(); |
110 | 100 |
111 if (!mount_points->RegisterFileSystem(mount_point_name, | 101 if (!mount_points->RegisterFileSystem(mount_point_name, |
112 fileapi::kFileSystemTypeProvided, | 102 fileapi::kFileSystemTypeProvided, |
113 fileapi::FileSystemMountOption(), | 103 fileapi::FileSystemMountOption(), |
114 mount_point_path)) { | 104 mount_point_path)) { |
115 FOR_EACH_OBSERVER( | 105 FOR_EACH_OBSERVER( |
116 Observer, | 106 Observer, |
117 observers_, | 107 observers_, |
118 OnProvidedFileSystemMount(ProvidedFileSystem(), | 108 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
119 base::File::FILE_ERROR_INVALID_OPERATION)); | 109 base::File::FILE_ERROR_INVALID_OPERATION)); |
120 return 0; | 110 return 0; |
121 } | 111 } |
122 | 112 |
123 // Store the file system descriptor. Use the mount point name as the file | 113 // Store the file system descriptor. Use the mount point name as the file |
124 // system provider file system id. | 114 // system provider file system id. |
125 // Examples: | 115 // Examples: |
126 // file_system_id = 41 | 116 // file_system_id = 41 |
127 // mount_point_name = file_system_id = b33f1337-41-5aa5 | 117 // mount_point_name = file_system_id = b33f1337-41-5aa5 |
128 // mount_point_path = /provided/b33f1337-41-5aa5 | 118 // mount_point_path = /provided/b33f1337-41-5aa5 |
129 ProvidedFileSystem file_system( | 119 ProvidedFileSystemInfo file_system_info( |
130 extension_id, file_system_id, file_system_name, mount_point_path); | 120 extension_id, file_system_id, file_system_name, mount_point_path); |
131 file_systems_[file_system_id] = file_system; | 121 |
122 // Create and add the provided file system to the internal map. | |
123 extensions::EventRouter* event_router = | |
124 extensions::ExtensionSystem::Get(profile_)->event_router(); | |
125 DCHECK(event_router); | |
126 | |
127 ProvidedFileSystem* file_system = | |
128 new ProvidedFileSystem(event_router, &request_manager_, file_system_info); | |
129 file_systems_.push_back(file_system); | |
130 file_system_map_[file_system_id] = file_system; | |
132 | 131 |
133 FOR_EACH_OBSERVER( | 132 FOR_EACH_OBSERVER( |
134 Observer, | 133 Observer, |
135 observers_, | 134 observers_, |
136 OnProvidedFileSystemMount(file_system, base::File::FILE_OK)); | 135 OnProvidedFileSystemMount(file_system_info, base::File::FILE_OK)); |
137 | 136 |
138 next_id_++; | 137 next_id_++; |
139 return file_system_id; | 138 return file_system_id; |
140 } | 139 } |
141 | 140 |
142 bool Service::UnmountFileSystem(const std::string& extension_id, | 141 bool Service::UnmountFileSystem(const std::string& extension_id, |
143 int file_system_id) { | 142 int file_system_id) { |
144 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 143 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
145 | 144 |
146 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | 145 ProvidedFileSystemMap::iterator file_system_it = |
147 if (file_system_it == file_systems_.end() || | 146 file_system_map_.find(file_system_id); |
148 file_system_it->second.extension_id() != extension_id) { | 147 if (file_system_it == file_system_map_.end() || |
149 const ProvidedFileSystem empty_file_system; | 148 file_system_it->second->GetFileSystemInfo().extension_id() != |
150 FOR_EACH_OBSERVER(Observer, | 149 extension_id) { |
151 observers_, | 150 const ProvidedFileSystemInfo empty_file_system_info; |
152 OnProvidedFileSystemUnmount( | 151 FOR_EACH_OBSERVER( |
153 empty_file_system, base::File::FILE_ERROR_NOT_FOUND)); | 152 Observer, |
153 observers_, | |
154 OnProvidedFileSystemUnmount(empty_file_system_info, | |
155 base::File::FILE_ERROR_NOT_FOUND)); | |
154 return false; | 156 return false; |
155 } | 157 } |
156 | 158 |
157 fileapi::ExternalMountPoints* const mount_points = | 159 fileapi::ExternalMountPoints* const mount_points = |
158 fileapi::ExternalMountPoints::GetSystemInstance(); | 160 fileapi::ExternalMountPoints::GetSystemInstance(); |
159 DCHECK(mount_points); | 161 DCHECK(mount_points); |
160 | 162 |
163 const ProvidedFileSystemInfo file_system_info = | |
hashimoto
2014/03/26 03:03:01
nit: const ProvidedFileSystemInfo&?
mtomasz
2014/03/26 05:45:13
Done.
mtomasz
2014/03/26 05:45:13
Done.
| |
164 file_system_it->second->GetFileSystemInfo(); | |
165 | |
161 const std::string mount_point_name = | 166 const std::string mount_point_name = |
162 file_system_it->second.mount_path().BaseName().value(); | 167 file_system_info.mount_path().BaseName().value(); |
163 if (!mount_points->RevokeFileSystem(mount_point_name)) { | 168 if (!mount_points->RevokeFileSystem(mount_point_name)) { |
164 FOR_EACH_OBSERVER( | 169 FOR_EACH_OBSERVER( |
165 Observer, | 170 Observer, |
166 observers_, | 171 observers_, |
167 OnProvidedFileSystemUnmount(file_system_it->second, | 172 OnProvidedFileSystemUnmount(file_system_info, |
168 base::File::FILE_ERROR_INVALID_OPERATION)); | 173 base::File::FILE_ERROR_INVALID_OPERATION)); |
169 return false; | 174 return false; |
170 } | 175 } |
171 | 176 |
172 FOR_EACH_OBSERVER( | 177 FOR_EACH_OBSERVER( |
173 Observer, | 178 Observer, |
174 observers_, | 179 observers_, |
175 OnProvidedFileSystemUnmount(file_system_it->second, base::File::FILE_OK)); | 180 OnProvidedFileSystemUnmount(file_system_info, base::File::FILE_OK)); |
176 | 181 |
177 file_systems_.erase(file_system_it); | 182 file_system_map_.erase(file_system_it); |
183 file_systems_.erase(std::find( | |
184 file_systems_.begin(), file_systems_.end(), file_system_it->second)); | |
178 return true; | 185 return true; |
179 } | 186 } |
180 | 187 |
181 std::vector<ProvidedFileSystem> Service::GetMountedFileSystems() { | 188 bool Service::RequestUnmount(const std::string& extension_id, |
189 int file_system_id) { | |
182 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 190 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
183 | 191 |
184 std::vector<ProvidedFileSystem> result; | 192 ProvidedFileSystemMap::iterator file_system_it = |
185 for (FileSystemMap::const_iterator it = file_systems_.begin(); | 193 file_system_map_.find(file_system_id); |
186 it != file_systems_.end(); | 194 if (file_system_it == file_system_map_.end() || |
195 file_system_it->second->GetFileSystemInfo().extension_id() != | |
hashimoto
2014/03/26 03:03:01
Why do you need to check extension ID?
There can b
mtomasz
2014/03/26 05:45:13
You're right, this is wrong. I was comparing exten
| |
196 extension_id) { | |
197 return false; | |
198 } | |
199 | |
200 return file_system_it->second->RequestUnmount( | |
201 base::Bind(&Service::OnRequestUnmountStatus, | |
202 weak_ptr_factory_.GetWeakPtr(), | |
203 file_system_it->second->GetFileSystemInfo())); | |
204 } | |
205 | |
206 std::vector<ProvidedFileSystemInfo> Service::GetProvidedFileSystemInfoList() { | |
207 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
208 | |
209 std::vector<ProvidedFileSystemInfo> result; | |
210 for (ProvidedFileSystemMap::const_iterator it = file_system_map_.begin(); | |
211 it != file_system_map_.end(); | |
187 ++it) { | 212 ++it) { |
188 result.push_back(it->second); | 213 result.push_back(it->second->GetFileSystemInfo()); |
189 } | 214 } |
190 return result; | 215 return result; |
191 } | 216 } |
192 | 217 |
193 bool Service::FulfillRequest(const std::string& extension_id, | 218 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
194 int file_system_id, | 219 const std::string& extension_id, |
195 int request_id, | 220 int file_system_id) { |
196 scoped_ptr<base::DictionaryValue> result, | |
197 bool has_next) { | |
198 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 221 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
199 | 222 |
200 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | 223 ProvidedFileSystemMap::iterator file_system_it = |
201 if (file_system_it == file_systems_.end() || | 224 file_system_map_.find(file_system_id); |
202 file_system_it->second.extension_id() != extension_id) { | 225 if (file_system_it == file_system_map_.end() || |
203 return false; | 226 file_system_it->second->GetFileSystemInfo().extension_id() != |
227 extension_id) { | |
228 return NULL; | |
204 } | 229 } |
205 | 230 |
206 return request_manager_.FulfillRequest( | 231 return file_system_it->second; |
207 file_system_it->second, request_id, result.Pass(), has_next); | |
208 } | |
209 | |
210 bool Service::RejectRequest(const std::string& extension_id, | |
211 int file_system_id, | |
212 int request_id, | |
213 base::File::Error error) { | |
214 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
215 | |
216 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | |
217 if (file_system_it == file_systems_.end() || | |
218 file_system_it->second.extension_id() != extension_id) { | |
219 return false; | |
220 } | |
221 | |
222 return request_manager_.RejectRequest( | |
223 file_system_it->second, request_id, error); | |
224 } | |
225 | |
226 bool Service::RequestUnmount(int file_system_id) { | |
227 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
228 | |
229 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | |
230 if (file_system_it == file_systems_.end()) | |
231 return false; | |
232 | |
233 int request_id = | |
234 request_manager_.CreateRequest(file_system_it->second, | |
235 SuccessCallback(), | |
236 base::Bind(&Service::OnRequestUnmountError, | |
237 weak_ptr_factory_.GetWeakPtr(), | |
238 file_system_it->second)); | |
239 | |
240 if (!request_id) | |
241 return false; | |
242 | |
243 scoped_ptr<base::ListValue> values( | |
244 CreateRequestValues(file_system_id, request_id)); | |
245 | |
246 extensions::EventRouter* event_router = | |
247 extensions::ExtensionSystem::Get(profile_)->event_router(); | |
248 DCHECK(event_router); | |
249 | |
250 event_router->DispatchEventToExtension( | |
251 file_system_it->second.extension_id(), | |
252 make_scoped_ptr(new extensions::Event( | |
253 extensions::api::file_system_provider::OnUnmountRequested::kEventName, | |
254 values.Pass()))); | |
255 | |
256 return true; | |
257 } | 232 } |
258 | 233 |
259 void Service::Shutdown() { RemoveObserver(&request_manager_); } | 234 void Service::Shutdown() { RemoveObserver(&request_manager_); } |
260 | 235 |
261 void Service::OnRequestUnmountError(const ProvidedFileSystem& file_system, | 236 void Service::OnRequestUnmountStatus( |
262 base::File::Error error) { | 237 const ProvidedFileSystemInfo& file_system_info, |
263 FOR_EACH_OBSERVER( | 238 base::File::Error error) { |
264 Observer, observers_, OnProvidedFileSystemUnmount(file_system, error)); | 239 if (error == base::File::FILE_OK) { |
240 // Do not notify observers, since the providing app is supposted to call | |
241 // unmount(), and then the event will be emitted. | |
242 return; | |
243 } | |
244 | |
245 FOR_EACH_OBSERVER(Observer, | |
hashimoto
2014/03/26 03:03:01
nit: Here what you want is to notify observers abo
mtomasz
2014/03/26 05:45:13
Almost. I want to notify observers about failure i
| |
246 observers_, | |
247 OnProvidedFileSystemUnmount(file_system_info, error)); | |
265 } | 248 } |
266 | 249 |
267 } // namespace file_system_provider | 250 } // namespace file_system_provider |
268 } // namespace chromeos | 251 } // namespace chromeos |
OLD | NEW |