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