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/stl_util.h" |
8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
9 #include "base/values.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" | 15 #include "chrome/browser/chromeos/login/user.h" |
14 #include "chrome/browser/chromeos/login/user_manager.h" | 16 #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" | 17 #include "content/public/browser/browser_thread.h" |
17 #include "extensions/browser/event_router.h" | 18 #include "extensions/browser/event_router.h" |
18 #include "extensions/browser/extension_system.h" | 19 #include "extensions/browser/extension_system.h" |
19 #include "webkit/browser/fileapi/external_mount_points.h" | 20 #include "webkit/browser/fileapi/external_mount_points.h" |
20 | 21 |
21 namespace chromeos { | 22 namespace chromeos { |
22 namespace file_system_provider { | 23 namespace file_system_provider { |
23 namespace { | 24 namespace { |
24 | 25 |
25 // Root mount path for all of the provided file systems. | 26 // Root mount path for all of the provided file systems. |
(...skipping 11 matching lines...) Expand all Loading... |
37 chromeos::User* const user = | 38 chromeos::User* const user = |
38 chromeos::UserManager::IsInitialized() | 39 chromeos::UserManager::IsInitialized() |
39 ? chromeos::UserManager::Get()->GetUserByProfile( | 40 ? chromeos::UserManager::Get()->GetUserByProfile( |
40 profile->GetOriginalProfile()) | 41 profile->GetOriginalProfile()) |
41 : NULL; | 42 : NULL; |
42 const std::string user_suffix = user ? "-" + user->username_hash() : ""; | 43 const std::string user_suffix = user ? "-" + user->username_hash() : ""; |
43 return base::FilePath(kProvidedMountPointRoot).AppendASCII( | 44 return base::FilePath(kProvidedMountPointRoot).AppendASCII( |
44 extension_id + "-" + base::IntToString(file_system_id) + user_suffix); | 45 extension_id + "-" + base::IntToString(file_system_id) + user_suffix); |
45 } | 46 } |
46 | 47 |
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 } | |
56 | |
57 } // namespace | 48 } // namespace |
58 | 49 |
59 Service::Service(Profile* profile) | 50 Service::Service(Profile* profile) |
60 : profile_(profile), next_id_(1), weak_ptr_factory_(this) { | 51 : profile_(profile), next_id_(1), weak_ptr_factory_(this) { |
61 AddObserver(&request_manager_); | 52 AddObserver(&request_manager_); |
62 } | 53 } |
63 | 54 |
64 Service::~Service() {} | 55 Service::~Service() { STLDeleteValues(&file_system_map_); } |
65 | 56 |
66 // static | 57 // static |
67 Service* Service::Get(content::BrowserContext* context) { | 58 Service* Service::Get(content::BrowserContext* context) { |
68 return ServiceFactory::Get(context); | 59 return ServiceFactory::Get(context); |
69 } | 60 } |
70 | 61 |
71 void Service::AddObserver(Observer* observer) { | 62 void Service::AddObserver(Observer* observer) { |
72 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 63 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
73 DCHECK(observer); | 64 DCHECK(observer); |
74 observers_.AddObserver(observer); | 65 observers_.AddObserver(observer); |
75 } | 66 } |
76 | 67 |
77 void Service::RemoveObserver(Observer* observer) { | 68 void Service::RemoveObserver(Observer* observer) { |
78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 69 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
79 DCHECK(observer); | 70 DCHECK(observer); |
80 observers_.RemoveObserver(observer); | 71 observers_.RemoveObserver(observer); |
81 } | 72 } |
82 | 73 |
83 int Service::MountFileSystem(const std::string& extension_id, | 74 int Service::MountFileSystem(const std::string& extension_id, |
84 const std::string& file_system_name) { | 75 const std::string& file_system_name) { |
85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 76 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
86 | 77 |
87 // Restrict number of file systems to prevent system abusing. | 78 // Restrict number of file systems to prevent system abusing. |
88 if (file_systems_.size() + 1 > kMaxFileSystems) { | 79 if (file_system_map_.size() + 1 > kMaxFileSystems) { |
89 FOR_EACH_OBSERVER( | 80 FOR_EACH_OBSERVER( |
90 Observer, | 81 Observer, |
91 observers_, | 82 observers_, |
92 OnProvidedFileSystemMount(ProvidedFileSystem(), | 83 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
93 base::File::FILE_ERROR_TOO_MANY_OPENED)); | 84 base::File::FILE_ERROR_TOO_MANY_OPENED)); |
94 return 0; | 85 return 0; |
95 } | 86 } |
96 | 87 |
97 // The file system id is unique per service, so per profile. | 88 // The provided file system id is unique per service, so per profile. |
98 int file_system_id = next_id_; | 89 int file_system_id = next_id_; |
99 | 90 |
100 fileapi::ExternalMountPoints* const mount_points = | 91 fileapi::ExternalMountPoints* const mount_points = |
101 fileapi::ExternalMountPoints::GetSystemInstance(); | 92 fileapi::ExternalMountPoints::GetSystemInstance(); |
102 DCHECK(mount_points); | 93 DCHECK(mount_points); |
103 | 94 |
104 // The mount point path and name are unique per system, since they are system | 95 // The mount point path and name are unique per system, since they are system |
105 // wide. This is necessary for copying between profiles. | 96 // wide. This is necessary for copying between profiles. |
106 const base::FilePath& mount_point_path = | 97 const base::FilePath& mount_point_path = |
107 GetMountPointPath(profile_, extension_id, file_system_id); | 98 GetMountPointPath(profile_, extension_id, file_system_id); |
108 const std::string mount_point_name = | 99 const std::string mount_point_name = |
109 mount_point_path.BaseName().AsUTF8Unsafe(); | 100 mount_point_path.BaseName().AsUTF8Unsafe(); |
110 | 101 |
111 if (!mount_points->RegisterFileSystem(mount_point_name, | 102 if (!mount_points->RegisterFileSystem(mount_point_name, |
112 fileapi::kFileSystemTypeProvided, | 103 fileapi::kFileSystemTypeProvided, |
113 fileapi::FileSystemMountOption(), | 104 fileapi::FileSystemMountOption(), |
114 mount_point_path)) { | 105 mount_point_path)) { |
115 FOR_EACH_OBSERVER( | 106 FOR_EACH_OBSERVER( |
116 Observer, | 107 Observer, |
117 observers_, | 108 observers_, |
118 OnProvidedFileSystemMount(ProvidedFileSystem(), | 109 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
119 base::File::FILE_ERROR_INVALID_OPERATION)); | 110 base::File::FILE_ERROR_INVALID_OPERATION)); |
120 return 0; | 111 return 0; |
121 } | 112 } |
122 | 113 |
123 // Store the file system descriptor. Use the mount point name as the file | 114 // Store the file system descriptor. Use the mount point name as the file |
124 // system provider file system id. | 115 // system provider file system id. |
125 // Examples: | 116 // Examples: |
126 // file_system_id = 41 | 117 // file_system_id = 41 |
127 // mount_point_name = file_system_id = b33f1337-41-5aa5 | 118 // mount_point_name = file_system_id = b33f1337-41-5aa5 |
128 // mount_point_path = /provided/b33f1337-41-5aa5 | 119 // mount_point_path = /provided/b33f1337-41-5aa5 |
129 ProvidedFileSystem file_system( | 120 ProvidedFileSystemInfo file_system_info( |
130 extension_id, file_system_id, file_system_name, mount_point_path); | 121 extension_id, file_system_id, file_system_name, mount_point_path); |
131 file_systems_[file_system_id] = file_system; | 122 |
| 123 // Create and add the provided file system to the internal map. |
| 124 extensions::EventRouter* event_router = |
| 125 extensions::ExtensionSystem::Get(profile_)->event_router(); |
| 126 DCHECK(event_router); |
| 127 |
| 128 ProvidedFileSystem* file_system = |
| 129 new ProvidedFileSystem(event_router, &request_manager_, file_system_info); |
| 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 = |
| 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 delete file_system_it->second; |
| 183 file_system_map_.erase(file_system_it); |
178 return true; | 184 return true; |
179 } | 185 } |
180 | 186 |
181 std::vector<ProvidedFileSystem> Service::GetMountedFileSystems() { | |
182 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
183 | |
184 std::vector<ProvidedFileSystem> result; | |
185 for (FileSystemMap::const_iterator it = file_systems_.begin(); | |
186 it != file_systems_.end(); | |
187 ++it) { | |
188 result.push_back(it->second); | |
189 } | |
190 return result; | |
191 } | |
192 | |
193 bool Service::FulfillRequest(const std::string& extension_id, | |
194 int file_system_id, | |
195 int request_id, | |
196 scoped_ptr<base::DictionaryValue> result, | |
197 bool has_next) { | |
198 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
199 | |
200 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | |
201 if (file_system_it == file_systems_.end() || | |
202 file_system_it->second.extension_id() != extension_id) { | |
203 return false; | |
204 } | |
205 | |
206 return request_manager_.FulfillRequest( | |
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) { | 187 bool Service::RequestUnmount(int file_system_id) { |
227 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 188 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
228 | 189 |
229 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | 190 ProvidedFileSystemMap::iterator file_system_it = |
230 if (file_system_it == file_systems_.end()) | 191 file_system_map_.find(file_system_id); |
| 192 if (file_system_it == file_system_map_.end()) |
231 return false; | 193 return false; |
232 | 194 |
233 int request_id = | 195 return file_system_it->second->RequestUnmount( |
234 request_manager_.CreateRequest(file_system_it->second, | 196 base::Bind(&Service::OnRequestUnmountStatus, |
235 SuccessCallback(), | 197 weak_ptr_factory_.GetWeakPtr(), |
236 base::Bind(&Service::OnRequestUnmountError, | 198 file_system_it->second->GetFileSystemInfo())); |
237 weak_ptr_factory_.GetWeakPtr(), | 199 } |
238 file_system_it->second)); | |
239 | 200 |
240 if (!request_id) | 201 std::vector<ProvidedFileSystemInfo> Service::GetProvidedFileSystemInfoList() { |
241 return false; | 202 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
242 | 203 |
243 scoped_ptr<base::ListValue> values( | 204 std::vector<ProvidedFileSystemInfo> result; |
244 CreateRequestValues(file_system_id, request_id)); | 205 for (ProvidedFileSystemMap::const_iterator it = file_system_map_.begin(); |
| 206 it != file_system_map_.end(); |
| 207 ++it) { |
| 208 result.push_back(it->second->GetFileSystemInfo()); |
| 209 } |
| 210 return result; |
| 211 } |
245 | 212 |
246 extensions::EventRouter* event_router = | 213 ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
247 extensions::ExtensionSystem::Get(profile_)->event_router(); | 214 const std::string& extension_id, |
248 DCHECK(event_router); | 215 int file_system_id) { |
| 216 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
249 | 217 |
250 event_router->DispatchEventToExtension( | 218 ProvidedFileSystemMap::iterator file_system_it = |
251 file_system_it->second.extension_id(), | 219 file_system_map_.find(file_system_id); |
252 make_scoped_ptr(new extensions::Event( | 220 if (file_system_it == file_system_map_.end() || |
253 extensions::api::file_system_provider::OnUnmountRequested::kEventName, | 221 file_system_it->second->GetFileSystemInfo().extension_id() != |
254 values.Pass()))); | 222 extension_id) { |
| 223 return NULL; |
| 224 } |
255 | 225 |
256 return true; | 226 return file_system_it->second; |
257 } | 227 } |
258 | 228 |
259 void Service::Shutdown() { RemoveObserver(&request_manager_); } | 229 void Service::Shutdown() { RemoveObserver(&request_manager_); } |
260 | 230 |
261 void Service::OnRequestUnmountError(const ProvidedFileSystem& file_system, | 231 void Service::OnRequestUnmountStatus( |
262 base::File::Error error) { | 232 const ProvidedFileSystemInfo& file_system_info, |
263 FOR_EACH_OBSERVER( | 233 base::File::Error error) { |
264 Observer, observers_, OnProvidedFileSystemUnmount(file_system, error)); | 234 // Notify observers about failure in unmounting, since mount() will not be |
| 235 // called by the provided file system. In case of success mount() will be |
| 236 // invoked, and observers notified, so there is no need to call them now. |
| 237 if (error != base::File::FILE_OK) { |
| 238 FOR_EACH_OBSERVER(Observer, |
| 239 observers_, |
| 240 OnProvidedFileSystemUnmount(file_system_info, error)); |
| 241 } |
265 } | 242 } |
266 | 243 |
267 } // namespace file_system_provider | 244 } // namespace file_system_provider |
268 } // namespace chromeos | 245 } // namespace chromeos |
OLD | NEW |