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

Side by Side Diff: chrome/browser/extensions/api/file_system/file_system_api.cc

Issue 1991083002: Remove ExtensionFunction::SetResult(T*) overload. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: IWYU Created 4 years, 7 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
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/extensions/api/file_system/file_system_api.h" 5 #include "chrome/browser/extensions/api/file_system/file_system_api.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory>
9 #include <set> 10 #include <set>
11 #include <utility>
10 #include <vector> 12 #include <vector>
11 13
12 #include "apps/saved_files_service.h" 14 #include "apps/saved_files_service.h"
13 #include "base/bind.h" 15 #include "base/bind.h"
14 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
15 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
16 #include "base/logging.h" 18 #include "base/logging.h"
17 #include "base/macros.h" 19 #include "base/macros.h"
18 #include "base/memory/linked_ptr.h" 20 #include "base/memory/linked_ptr.h"
19 #include "base/memory/ptr_util.h" 21 #include "base/memory/ptr_util.h"
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name)); 482 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &filesystem_name));
481 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path)); 483 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &filesystem_path));
482 484
483 base::FilePath file_path; 485 base::FilePath file_path;
484 if (!app_file_handler_util::ValidateFileEntryAndGetPath( 486 if (!app_file_handler_util::ValidateFileEntryAndGetPath(
485 filesystem_name, filesystem_path, 487 filesystem_name, filesystem_path,
486 render_frame_host()->GetProcess()->GetID(), &file_path, &error_)) 488 render_frame_host()->GetProcess()->GetID(), &file_path, &error_))
487 return false; 489 return false;
488 490
489 file_path = path_util::PrettifyPath(file_path); 491 file_path = path_util::PrettifyPath(file_path);
490 SetResult(new base::StringValue(file_path.value())); 492 SetResult(base::MakeUnique<base::StringValue>(file_path.value()));
491 return true; 493 return true;
492 } 494 }
493 495
494 FileSystemEntryFunction::FileSystemEntryFunction() 496 FileSystemEntryFunction::FileSystemEntryFunction()
495 : multiple_(false), 497 : multiple_(false), is_directory_(false) {}
496 is_directory_(false),
497 response_(NULL) {}
498 498
499 void FileSystemEntryFunction::PrepareFilesForWritableApp( 499 void FileSystemEntryFunction::PrepareFilesForWritableApp(
500 const std::vector<base::FilePath>& paths) { 500 const std::vector<base::FilePath>& paths) {
501 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 501 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
502 // TODO(cmihail): Path directory set should be initialized only with the 502 // TODO(cmihail): Path directory set should be initialized only with the
503 // paths that are actually directories, but for now we will consider 503 // paths that are actually directories, but for now we will consider
504 // all paths directories in case is_directory_ is true, otherwise 504 // all paths directories in case is_directory_ is true, otherwise
505 // all paths files, as this was the previous logic. 505 // all paths files, as this was the previous logic.
506 std::set<base::FilePath> path_directory_set_ = 506 std::set<base::FilePath> path_directory_set_ =
507 is_directory_ ? std::set<base::FilePath>(paths.begin(), paths.end()) 507 is_directory_ ? std::set<base::FilePath>(paths.begin(), paths.end())
508 : std::set<base::FilePath>{}; 508 : std::set<base::FilePath>{};
509 app_file_handler_util::PrepareFilesForWritableApp( 509 app_file_handler_util::PrepareFilesForWritableApp(
510 paths, GetProfile(), path_directory_set_, 510 paths, GetProfile(), path_directory_set_,
511 base::Bind(&FileSystemEntryFunction::RegisterFileSystemsAndSendResponse, 511 base::Bind(&FileSystemEntryFunction::RegisterFileSystemsAndSendResponse,
512 this, paths), 512 this, paths),
513 base::Bind(&FileSystemEntryFunction::HandleWritableFileError, this)); 513 base::Bind(&FileSystemEntryFunction::HandleWritableFileError, this));
514 } 514 }
515 515
516 void FileSystemEntryFunction::RegisterFileSystemsAndSendResponse( 516 void FileSystemEntryFunction::RegisterFileSystemsAndSendResponse(
517 const std::vector<base::FilePath>& paths) { 517 const std::vector<base::FilePath>& paths) {
518 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 518 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
519 if (!render_frame_host()) 519 if (!render_frame_host())
520 return; 520 return;
521 521
522 CreateResponse(); 522 std::unique_ptr<base::DictionaryValue> result = CreateResult();
523 for (std::vector<base::FilePath>::const_iterator it = paths.begin(); 523 for (const auto& path : paths)
524 it != paths.end(); ++it) { 524 AddEntryToResult(path, std::string(), result.get());
525 AddEntryToResponse(*it, ""); 525 SetResult(std::move(result));
526 }
527 SendResponse(true); 526 SendResponse(true);
528 } 527 }
529 528
530 void FileSystemEntryFunction::CreateResponse() { 529 std::unique_ptr<base::DictionaryValue> FileSystemEntryFunction::CreateResult() {
531 DCHECK(!response_); 530 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
532 response_ = new base::DictionaryValue(); 531 result->Set("entries", base::MakeUnique<base::ListValue>());
533 base::ListValue* list = new base::ListValue(); 532 result->SetBoolean("multiple", multiple_);
534 response_->Set("entries", list); 533 return result;
535 response_->SetBoolean("multiple", multiple_);
536 SetResult(response_);
537 } 534 }
538 535
539 void FileSystemEntryFunction::AddEntryToResponse( 536 void FileSystemEntryFunction::AddEntryToResult(const base::FilePath& path,
540 const base::FilePath& path, 537 const std::string& id_override,
541 const std::string& id_override) { 538 base::DictionaryValue* result) {
542 DCHECK(response_);
543 GrantedFileEntry file_entry = app_file_handler_util::CreateFileEntry( 539 GrantedFileEntry file_entry = app_file_handler_util::CreateFileEntry(
544 GetProfile(), 540 GetProfile(),
545 extension(), 541 extension(),
546 render_frame_host()->GetProcess()->GetID(), 542 render_frame_host()->GetProcess()->GetID(),
547 path, 543 path,
548 is_directory_); 544 is_directory_);
549 base::ListValue* entries; 545 base::ListValue* entries;
550 bool success = response_->GetList("entries", &entries); 546 bool success = result->GetList("entries", &entries);
Devlin 2016/05/20 17:56:54 nit: this is silly, since it means we repeat this
dcheng 2016/05/20 18:11:57 It's a map internally, so lookup is reasonably tim
Devlin 2016/05/20 20:56:18 Ah, I didn't notice it's called from two places.
551 DCHECK(success); 547 DCHECK(success);
552 548
553 base::DictionaryValue* entry = new base::DictionaryValue(); 549 base::DictionaryValue* entry = new base::DictionaryValue();
554 entry->SetString("fileSystemId", file_entry.filesystem_id); 550 entry->SetString("fileSystemId", file_entry.filesystem_id);
555 entry->SetString("baseName", file_entry.registered_name); 551 entry->SetString("baseName", file_entry.registered_name);
556 if (id_override.empty()) 552 if (id_override.empty())
557 entry->SetString("id", file_entry.id); 553 entry->SetString("id", file_entry.id);
558 else 554 else
559 entry->SetString("id", id_override); 555 entry->SetString("id", id_override);
560 entry->SetBoolean("isDirectory", is_directory_); 556 entry->SetBoolean("isDirectory", is_directory_);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 error_ = app_file_handler_util::kInvalidParameters; 624 error_ = app_file_handler_util::kInvalidParameters;
629 return false; 625 return false;
630 } 626 }
631 627
632 content::ChildProcessSecurityPolicy* policy = 628 content::ChildProcessSecurityPolicy* policy =
633 content::ChildProcessSecurityPolicy::GetInstance(); 629 content::ChildProcessSecurityPolicy::GetInstance();
634 int renderer_id = render_frame_host()->GetProcess()->GetID(); 630 int renderer_id = render_frame_host()->GetProcess()->GetID();
635 bool is_writable = policy->CanReadWriteFileSystem(renderer_id, 631 bool is_writable = policy->CanReadWriteFileSystem(renderer_id,
636 filesystem_id); 632 filesystem_id);
637 633
638 SetResult(new base::FundamentalValue(is_writable)); 634 SetResult(base::MakeUnique<base::FundamentalValue>(is_writable));
639 return true; 635 return true;
640 } 636 }
641 637
642 // Handles showing a dialog to the user to ask for the filename for a file to 638 // Handles showing a dialog to the user to ask for the filename for a file to
643 // save or open. 639 // save or open.
644 class FileSystemChooseEntryFunction::FilePicker 640 class FileSystemChooseEntryFunction::FilePicker
645 : public ui::SelectFileDialog::Listener { 641 : public ui::SelectFileDialog::Listener {
646 public: 642 public:
647 FilePicker(FileSystemChooseEntryFunction* function, 643 FilePicker(FileSystemChooseEntryFunction* function,
648 content::WebContents* web_contents, 644 content::WebContents* web_contents,
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after
1194 SavedFilesService* saved_files_service = SavedFilesService::Get(GetProfile()); 1190 SavedFilesService* saved_files_service = SavedFilesService::Get(GetProfile());
1195 saved_files_service->RegisterFileEntry( 1191 saved_files_service->RegisterFileEntry(
1196 extension_->id(), entry_id, path, file_info->is_directory); 1192 extension_->id(), entry_id, path, file_info->is_directory);
1197 saved_files_service->EnqueueFileEntry(extension_->id(), entry_id); 1193 saved_files_service->EnqueueFileEntry(extension_->id(), entry_id);
1198 SendResponse(true); 1194 SendResponse(true);
1199 } 1195 }
1200 1196
1201 bool FileSystemIsRestorableFunction::RunSync() { 1197 bool FileSystemIsRestorableFunction::RunSync() {
1202 std::string entry_id; 1198 std::string entry_id;
1203 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &entry_id)); 1199 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &entry_id));
1204 SetResult(new base::FundamentalValue(SavedFilesService::Get( 1200 SetResult(base::MakeUnique<base::FundamentalValue>(
1205 GetProfile())->IsRegistered(extension_->id(), entry_id))); 1201 SavedFilesService::Get(GetProfile())
1202 ->IsRegistered(extension_->id(), entry_id)));
1206 return true; 1203 return true;
1207 } 1204 }
1208 1205
1209 bool FileSystemRestoreEntryFunction::RunAsync() { 1206 bool FileSystemRestoreEntryFunction::RunAsync() {
1210 std::string entry_id; 1207 std::string entry_id;
1211 bool needs_new_entry; 1208 bool needs_new_entry;
1212 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &entry_id)); 1209 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &entry_id));
1213 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &needs_new_entry)); 1210 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(1, &needs_new_entry));
1214 const SavedFileEntry* file_entry = SavedFilesService::Get( 1211 const SavedFileEntry* file_entry = SavedFilesService::Get(
1215 GetProfile())->GetFileEntry(extension_->id(), entry_id); 1212 GetProfile())->GetFileEntry(extension_->id(), entry_id);
1216 if (!file_entry) { 1213 if (!file_entry) {
1217 error_ = kUnknownIdError; 1214 error_ = kUnknownIdError;
1218 return false; 1215 return false;
1219 } 1216 }
1220 1217
1221 SavedFilesService::Get(GetProfile()) 1218 SavedFilesService::Get(GetProfile())
1222 ->EnqueueFileEntry(extension_->id(), entry_id); 1219 ->EnqueueFileEntry(extension_->id(), entry_id);
1223 1220
1224 // Only create a new file entry if the renderer requests one. 1221 // Only create a new file entry if the renderer requests one.
1225 // |needs_new_entry| will be false if the renderer already has an Entry for 1222 // |needs_new_entry| will be false if the renderer already has an Entry for
1226 // |entry_id|. 1223 // |entry_id|.
1227 if (needs_new_entry) { 1224 if (needs_new_entry) {
1228 is_directory_ = file_entry->is_directory; 1225 is_directory_ = file_entry->is_directory;
1229 CreateResponse(); 1226 std::unique_ptr<base::DictionaryValue> result = CreateResult();
1230 AddEntryToResponse(file_entry->path, file_entry->id); 1227 AddEntryToResult(file_entry->path, file_entry->id, result.get());
1228 SetResult(std::move(result));
1231 } 1229 }
1232 SendResponse(true); 1230 SendResponse(true);
1233 return true; 1231 return true;
1234 } 1232 }
1235 1233
1236 bool FileSystemObserveDirectoryFunction::RunSync() { 1234 bool FileSystemObserveDirectoryFunction::RunSync() {
1237 NOTIMPLEMENTED(); 1235 NOTIMPLEMENTED();
1238 error_ = kUnknownIdError; 1236 error_ = kUnknownIdError;
1239 return false; 1237 return false;
1240 } 1238 }
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
1417 policy->GrantCopyInto(render_frame_host()->GetProcess()->GetID(), 1415 policy->GrantCopyInto(render_frame_host()->GetProcess()->GetID(),
1418 volume->mount_path()); 1416 volume->mount_path());
1419 policy->GrantWriteFileSystem(render_frame_host()->GetProcess()->GetID(), 1417 policy->GrantWriteFileSystem(render_frame_host()->GetProcess()->GetID(),
1420 file_system_id); 1418 file_system_id);
1421 policy->GrantDeleteFromFileSystem( 1419 policy->GrantDeleteFromFileSystem(
1422 render_frame_host()->GetProcess()->GetID(), file_system_id); 1420 render_frame_host()->GetProcess()->GetID(), file_system_id);
1423 policy->GrantCreateFileForFileSystem( 1421 policy->GrantCreateFileForFileSystem(
1424 render_frame_host()->GetProcess()->GetID(), file_system_id); 1422 render_frame_host()->GetProcess()->GetID(), file_system_id);
1425 } 1423 }
1426 1424
1427 base::DictionaryValue* const dict = new base::DictionaryValue(); 1425 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
1428 dict->SetString("file_system_id", file_system_id); 1426 dict->SetString("file_system_id", file_system_id);
1429 dict->SetString("file_system_path", register_name); 1427 dict->SetString("file_system_path", register_name);
1430 1428
1431 SetResult(dict); 1429 SetResult(std::move(dict));
1432 SendResponse(true); 1430 SendResponse(true);
1433 } 1431 }
1434 1432
1435 FileSystemGetVolumeListFunction::FileSystemGetVolumeListFunction() 1433 FileSystemGetVolumeListFunction::FileSystemGetVolumeListFunction()
1436 : chrome_details_(this) { 1434 : chrome_details_(this) {
1437 } 1435 }
1438 1436
1439 FileSystemGetVolumeListFunction::~FileSystemGetVolumeListFunction() { 1437 FileSystemGetVolumeListFunction::~FileSystemGetVolumeListFunction() {
1440 } 1438 }
1441 1439
1442 ExtensionFunction::ResponseAction FileSystemGetVolumeListFunction::Run() { 1440 ExtensionFunction::ResponseAction FileSystemGetVolumeListFunction::Run() {
1443 // Only kiosk apps in kiosk sessions can use this API. 1441 // Only kiosk apps in kiosk sessions can use this API.
1444 // Additionally it is enabled for whitelisted component extensions and apps. 1442 // Additionally it is enabled for whitelisted component extensions and apps.
1445 file_system_api::ConsentProviderDelegate consent_provider_delegate( 1443 file_system_api::ConsentProviderDelegate consent_provider_delegate(
1446 chrome_details_.GetProfile(), render_frame_host()); 1444 chrome_details_.GetProfile(), render_frame_host());
1447 file_system_api::ConsentProvider consent_provider(&consent_provider_delegate); 1445 file_system_api::ConsentProvider consent_provider(&consent_provider_delegate);
1448 1446
1449 if (!consent_provider.IsGrantable(*extension())) 1447 if (!consent_provider.IsGrantable(*extension()))
1450 return RespondNow(Error(kNotSupportedOnNonKioskSessionError)); 1448 return RespondNow(Error(kNotSupportedOnNonKioskSessionError));
1451 std::vector<api::file_system::Volume> result_volume_list; 1449 std::vector<api::file_system::Volume> result_volume_list;
1452 FillVolumeList(chrome_details_.GetProfile(), &result_volume_list); 1450 FillVolumeList(chrome_details_.GetProfile(), &result_volume_list);
1453 1451
1454 return RespondNow(ArgumentList( 1452 return RespondNow(ArgumentList(
1455 api::file_system::GetVolumeList::Results::Create(result_volume_list))); 1453 api::file_system::GetVolumeList::Results::Create(result_volume_list)));
1456 } 1454 }
1457 #endif 1455 #endif
1458 1456
1459 } // namespace extensions 1457 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698