Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: chrome/browser/chromeos/extensions/file_browser_handler_api.cc

Issue 10664002: Add support for drive files to chrome.fileBrowserHandler.selectFile (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 if (success) 226 full_path_ = full_path;
241 success = DoCreateFile();
242 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
243 base::Bind(&FileHandlerSelectFileFunction::OnFileCreated, this,
244 success, file_system_name, file_system_root));
245 }
246 227
247 bool FileHandlerSelectFileFunction::DoCreateFile() { 228 BrowserContext::GetFileSystemContext(profile_)->OpenFileSystem(
248 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 229 source_url_.GetOrigin(), fileapi::kFileSystemTypeExternal, false,
230 base::Bind(&RunOpenFileSystemCallback,
231 base::Bind(&FileHandlerSelectFileFunction::OnFileSystemOpened,
232 this)));
233 };
249 234
250 // Don't allow links. 235 void FileHandlerSelectFileFunction::OnFileSystemOpened(
251 if (file_util::PathExists(full_path_) && file_util::IsLink(full_path_))
252 return false;
253
254 bool created = false;
255 base::PlatformFileError error = base::PLATFORM_FILE_OK;
256 int creation_flags = base::PLATFORM_FILE_CREATE_ALWAYS |
257 base::PLATFORM_FILE_READ |
258 base::PLATFORM_FILE_WRITE;
259 base::CreatePlatformFile(full_path_, creation_flags, &created, &error);
260 return error == base::PLATFORM_FILE_OK;
261 }
262
263 void FileHandlerSelectFileFunction::OnFileCreated(
264 bool success, 236 bool success,
265 const std::string& file_system_name, 237 const std::string& file_system_name,
266 const GURL& file_system_root) { 238 const GURL& file_system_root) {
267 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 239 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
268 240
269 FilePath virtual_path; 241 if (success) {
270 if (success) 242 GrantPermissions(base::Bind(
271 virtual_path = GrantPermissions(); 243 &FileHandlerSelectFileFunction::Respond, this,
272 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());
273 } 248 }
274 249
275 FilePath FileHandlerSelectFileFunction::GrantPermissions() { 250 void FileHandlerSelectFileFunction::GrantPermissions(
251 const GrantPermissionsCallback& callback) {
276 fileapi::ExternalFileSystemMountPointProvider* external_provider = 252 fileapi::ExternalFileSystemMountPointProvider* external_provider =
277 BrowserContext::GetFileSystemContext(profile_)->external_provider(); 253 BrowserContext::GetFileSystemContext(profile_)->external_provider();
278 DCHECK(external_provider); 254 DCHECK(external_provider);
279 255
280 FilePath virtual_path; 256 FilePath virtual_path;
281 external_provider->GetVirtualPath(full_path_, &virtual_path); 257 external_provider->GetVirtualPath(full_path_, &virtual_path);
282 DCHECK(!virtual_path.empty()); 258 DCHECK(!virtual_path.empty());
283 259
284 // Grant access to this particular file to target extension. This will 260 // Grant access to this particular file to target extension. This will
285 // 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
286 // prevent from traversing FS hierarchy upward. 262 // prevent from traversing FS hierarchy upward.
287 external_provider->GrantFileAccessToExtension(extension_id(), virtual_path); 263 external_provider->GrantFileAccessToExtension(extension_id(), virtual_path);
288 content::ChildProcessSecurityPolicy::GetInstance()->GrantPermissionsForFile( 264
289 render_view_host()->GetProcess()->GetID(), 265 // Give read write permissions for the file.
266 permissions_to_grant_.push_back(std::make_pair(
290 full_path_, 267 full_path_,
291 file_handler_util::GetReadWritePermissions()); 268 file_handler_util::GetReadWritePermissions()));
292 269
293 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);
294 } 297 }
295 298
296 void FileHandlerSelectFileFunction::Respond( 299 void FileHandlerSelectFileFunction::Respond(
297 bool success, 300 bool success,
298 const std::string& file_system_name, 301 const std::string& file_system_name,
299 const GURL& file_system_root, 302 const GURL& file_system_root,
300 const FilePath& virtual_path) { 303 const FilePath& virtual_path) {
301 scoped_ptr<SelectFile::Results::Result> result( 304 scoped_ptr<SelectFile::Results::Result> result(
302 new SelectFile::Results::Result()); 305 new SelectFile::Results::Result());
303 result->success = success; 306 result->success = success;
(...skipping 15 matching lines...) Expand all
319 result->set_function_for_test(this); 322 result->set_function_for_test(this);
320 return result; 323 return result;
321 } 324 }
322 return new FileSelectorImpl(this); 325 return new FileSelectorImpl(this);
323 } 326 }
324 327
325 FileSelector* FileHandlerSelectFileFunction::file_selector_for_test_ = NULL; 328 FileSelector* FileHandlerSelectFileFunction::file_selector_for_test_ = NULL;
326 329
327 bool FileHandlerSelectFileFunction::gesture_check_disabled_for_test_ = false; 330 bool FileHandlerSelectFileFunction::gesture_check_disabled_for_test_ = false;
328 331
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/extensions/file_browser_handler_api.h ('k') | chrome/common/extensions/api/file_browser_handler.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698