OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/file_browser_handler_api.h" | 5 #include "chrome/browser/chromeos/extensions/file_browser_handler_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
11 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "chrome/browser/chromeos/extensions/file_handler_util.h" | 14 #include "chrome/browser/chromeos/extensions/file_handler_util.h" |
14 #include "chrome/browser/chromeos/extensions/file_manager_util.h" | 15 #include "chrome/browser/chromeos/extensions/file_manager_util.h" |
| 16 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/browser_tabstrip.h" | 19 #include "chrome/browser/ui/browser_tabstrip.h" |
18 #include "chrome/browser/ui/browser_window.h" | 20 #include "chrome/browser/ui/browser_window.h" |
19 #include "chrome/browser/ui/chrome_select_file_policy.h" | 21 #include "chrome/browser/ui/chrome_select_file_policy.h" |
20 #include "chrome/browser/ui/select_file_dialog.h" | 22 #include "chrome/browser/ui/select_file_dialog.h" |
21 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 23 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
22 #include "chrome/common/extensions/api/file_browser_handler_internal.h" | 24 #include "chrome/common/extensions/api/file_browser_handler_internal.h" |
23 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/browser/child_process_security_policy.h" | 26 #include "content/public/browser/child_process_security_policy.h" |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 if (function_.get()) | 164 if (function_.get()) |
163 function_->OnFilePathSelected(success, selected_path); | 165 function_->OnFilePathSelected(success, selected_path); |
164 function_ = NULL; | 166 function_ = NULL; |
165 } | 167 } |
166 | 168 |
167 typedef base::Callback<void (bool success, | 169 typedef base::Callback<void (bool success, |
168 const std::string& file_system_name, | 170 const std::string& file_system_name, |
169 const GURL& file_system_root)> | 171 const GURL& file_system_root)> |
170 FileSystemOpenCallback; | 172 FileSystemOpenCallback; |
171 | 173 |
172 void RelayOpenFileSystemCallbackToFileThread( | 174 void RunOpenFileSystemCallback( |
173 const FileSystemOpenCallback& callback, | 175 const FileSystemOpenCallback& callback, |
174 base::PlatformFileError error, | 176 base::PlatformFileError error, |
175 const std::string& file_system_name, | 177 const std::string& file_system_name, |
176 const GURL& file_system_root) { | 178 const GURL& file_system_root) { |
177 bool success = (error == base::PLATFORM_FILE_OK); | 179 bool success = (error == base::PLATFORM_FILE_OK); |
178 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | 180 callback.Run(success, file_system_name, file_system_root); |
179 base::Bind(callback, success, file_system_name, file_system_root)); | |
180 } | 181 } |
181 | 182 |
182 } // namespace | 183 } // namespace |
183 | 184 |
184 FileHandlerSelectFileFunction::FileHandlerSelectFileFunction() {} | 185 FileHandlerSelectFileFunction::FileHandlerSelectFileFunction() {} |
185 | 186 |
186 void FileHandlerSelectFileFunction::OnFilePathSelected( | |
187 bool success, | |
188 const FilePath& full_path) { | |
189 if (!success) { | |
190 Respond(false, std::string(), GURL(), FilePath()); | |
191 return; | |
192 } | |
193 | |
194 full_path_ = full_path; | |
195 | |
196 BrowserContext::GetFileSystemContext(profile_)->OpenFileSystem( | |
197 source_url_.GetOrigin(), fileapi::kFileSystemTypeExternal, false, | |
198 base::Bind(&RelayOpenFileSystemCallbackToFileThread, | |
199 base::Bind(&FileHandlerSelectFileFunction::CreateFileOnFileThread, | |
200 this))); | |
201 }; | |
202 | |
203 // static | 187 // static |
204 void FileHandlerSelectFileFunction::set_file_selector_for_test( | 188 void FileHandlerSelectFileFunction::set_file_selector_for_test( |
205 FileSelector* file_selector) { | 189 FileSelector* file_selector) { |
206 FileHandlerSelectFileFunction::file_selector_for_test_ = file_selector; | 190 FileHandlerSelectFileFunction::file_selector_for_test_ = file_selector; |
207 } | 191 } |
208 | 192 |
209 // static | 193 // static |
210 void FileHandlerSelectFileFunction::set_gesture_check_disabled_for_test( | 194 void FileHandlerSelectFileFunction::set_gesture_check_disabled_for_test( |
211 bool disabled) { | 195 bool disabled) { |
212 FileHandlerSelectFileFunction::gesture_check_disabled_for_test_ = disabled; | 196 FileHandlerSelectFileFunction::gesture_check_disabled_for_test_ = disabled; |
(...skipping 11 matching lines...) Expand all Loading... |
224 return false; | 208 return false; |
225 } | 209 } |
226 | 210 |
227 // If |file_selector_| is set (e.g. in test), use it instesad of creating new | 211 // If |file_selector_| is set (e.g. in test), use it instesad of creating new |
228 // file selector. | 212 // file selector. |
229 FileSelector* file_selector = GetFileSelector(); | 213 FileSelector* file_selector = GetFileSelector(); |
230 file_selector->SelectFile(suggested_name.BaseName(), GetCurrentBrowser()); | 214 file_selector->SelectFile(suggested_name.BaseName(), GetCurrentBrowser()); |
231 return true; | 215 return true; |
232 } | 216 } |
233 | 217 |
234 void FileHandlerSelectFileFunction::CreateFileOnFileThread( | 218 void FileHandlerSelectFileFunction::OnFilePathSelected( |
235 bool success, | 219 bool success, |
236 const std::string& file_system_name, | 220 const FilePath& full_path) { |
237 const GURL& file_system_root) { | 221 if (!success) { |
238 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 222 Respond(false, std::string(), GURL(), FilePath()); |
| 223 return; |
| 224 } |
239 | 225 |
240 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 226 full_path_ = full_path; |
241 base::Bind(&FileHandlerSelectFileFunction::OnFileCreated, this, | |
242 success, file_system_name, file_system_root)); | |
243 } | |
244 | 227 |
245 void FileHandlerSelectFileFunction::OnFileCreated( | 228 BrowserContext::GetFileSystemContext(profile_)->OpenFileSystem( |
| 229 source_url_.GetOrigin(), fileapi::kFileSystemTypeExternal, false, |
| 230 base::Bind(&RunOpenFileSystemCallback, |
| 231 base::Bind(&FileHandlerSelectFileFunction::OnFileSystemOpened, |
| 232 this))); |
| 233 }; |
| 234 |
| 235 void FileHandlerSelectFileFunction::OnFileSystemOpened( |
246 bool success, | 236 bool success, |
247 const std::string& file_system_name, | 237 const std::string& file_system_name, |
248 const GURL& file_system_root) { | 238 const GURL& file_system_root) { |
249 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 239 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
250 | 240 |
251 FilePath virtual_path; | 241 if (success) { |
252 if (success) | 242 GrantPermissions(base::Bind( |
253 virtual_path = GrantPermissions(); | 243 &FileHandlerSelectFileFunction::Respond, this, |
254 Respond(success, file_system_name, file_system_root, virtual_path); | 244 success, file_system_name, file_system_root)); |
| 245 return; |
| 246 } |
| 247 Respond(success, file_system_name, file_system_root, FilePath()); |
255 } | 248 } |
256 | 249 |
257 FilePath FileHandlerSelectFileFunction::GrantPermissions() { | 250 void FileHandlerSelectFileFunction::GrantPermissions( |
| 251 const GrantPermissionsCallback& callback) { |
258 fileapi::ExternalFileSystemMountPointProvider* external_provider = | 252 fileapi::ExternalFileSystemMountPointProvider* external_provider = |
259 BrowserContext::GetFileSystemContext(profile_)->external_provider(); | 253 BrowserContext::GetFileSystemContext(profile_)->external_provider(); |
260 DCHECK(external_provider); | 254 DCHECK(external_provider); |
261 | 255 |
262 FilePath virtual_path; | 256 FilePath virtual_path; |
263 external_provider->GetVirtualPath(full_path_, &virtual_path); | 257 external_provider->GetVirtualPath(full_path_, &virtual_path); |
264 DCHECK(!virtual_path.empty()); | 258 DCHECK(!virtual_path.empty()); |
265 | 259 |
266 // Grant access to this particular file to target extension. This will | 260 // Grant access to this particular file to target extension. This will |
267 // ensure that the target extension can access only this FS entry and | 261 // ensure that the target extension can access only this FS entry and |
268 // prevent from traversing FS hierarchy upward. | 262 // prevent from traversing FS hierarchy upward. |
269 external_provider->GrantFileAccessToExtension(extension_id(), virtual_path); | 263 external_provider->GrantFileAccessToExtension(extension_id(), virtual_path); |
270 content::ChildProcessSecurityPolicy::GetInstance()->GrantPermissionsForFile( | 264 |
271 render_view_host()->GetProcess()->GetID(), | 265 // Give read write permissions for the file. |
| 266 permissions_to_grant_.push_back(std::make_pair( |
272 full_path_, | 267 full_path_, |
273 file_handler_util::GetReadWritePermissions()); | 268 file_handler_util::GetReadWritePermissions())); |
274 | 269 |
275 return virtual_path; | 270 if (!gdata::util::IsUnderGDataMountPoint(full_path_)) { |
| 271 OnGotPermissionsToGrant(callback, virtual_path); |
| 272 return; |
| 273 } |
| 274 |
| 275 // For drive files, we also have to grant permissions for cache paths. |
| 276 scoped_ptr<std::vector<FilePath> > gdata_paths(new std::vector<FilePath>()); |
| 277 gdata_paths->push_back(virtual_path); |
| 278 |
| 279 gdata::util::InsertGDataCachePathsPermissions( |
| 280 profile(), |
| 281 gdata_paths.Pass(), |
| 282 &permissions_to_grant_, |
| 283 base::Bind(&FileHandlerSelectFileFunction::OnGotPermissionsToGrant, |
| 284 this, callback, virtual_path)); |
| 285 } |
| 286 |
| 287 void FileHandlerSelectFileFunction::OnGotPermissionsToGrant( |
| 288 const GrantPermissionsCallback& callback, |
| 289 const FilePath& virtual_path) { |
| 290 for (size_t i = 0; i < permissions_to_grant_.size(); i++) { |
| 291 content::ChildProcessSecurityPolicy::GetInstance()->GrantPermissionsForFile( |
| 292 render_view_host()->GetProcess()->GetID(), |
| 293 permissions_to_grant_[i].first, |
| 294 permissions_to_grant_[i].second); |
| 295 } |
| 296 callback.Run(virtual_path); |
276 } | 297 } |
277 | 298 |
278 void FileHandlerSelectFileFunction::Respond( | 299 void FileHandlerSelectFileFunction::Respond( |
279 bool success, | 300 bool success, |
280 const std::string& file_system_name, | 301 const std::string& file_system_name, |
281 const GURL& file_system_root, | 302 const GURL& file_system_root, |
282 const FilePath& virtual_path) { | 303 const FilePath& virtual_path) { |
283 scoped_ptr<SelectFile::Results::Result> result( | 304 scoped_ptr<SelectFile::Results::Result> result( |
284 new SelectFile::Results::Result()); | 305 new SelectFile::Results::Result()); |
285 result->success = success; | 306 result->success = success; |
(...skipping 15 matching lines...) Expand all Loading... |
301 result->set_function_for_test(this); | 322 result->set_function_for_test(this); |
302 return result; | 323 return result; |
303 } | 324 } |
304 return new FileSelectorImpl(this); | 325 return new FileSelectorImpl(this); |
305 } | 326 } |
306 | 327 |
307 FileSelector* FileHandlerSelectFileFunction::file_selector_for_test_ = NULL; | 328 FileSelector* FileHandlerSelectFileFunction::file_selector_for_test_ = NULL; |
308 | 329 |
309 bool FileHandlerSelectFileFunction::gesture_check_disabled_for_test_ = false; | 330 bool FileHandlerSelectFileFunction::gesture_check_disabled_for_test_ = false; |
310 | 331 |
OLD | NEW |