Chromium Code Reviews| Index: chrome/browser/chromeos/file_system_provider/service.cc |
| diff --git a/chrome/browser/chromeos/file_system_provider/service.cc b/chrome/browser/chromeos/file_system_provider/service.cc |
| index c6876c1ac6d5da666b83aa7d3f5166240d5ef975..3b5430ef9dd4bd8abebd841e05929f669641c4ca 100644 |
| --- a/chrome/browser/chromeos/file_system_provider/service.cc |
| +++ b/chrome/browser/chromeos/file_system_provider/service.cc |
| @@ -5,14 +5,14 @@ |
| #include "chrome/browser/chromeos/file_system_provider/service.h" |
| #include "base/files/file_path.h" |
| +#include "base/stl_util.h" |
| #include "base/strings/string_number_conversions.h" |
|
hashimoto
2014/04/09 06:18:23
nit: No need to include this?
mtomasz
2014/04/09 19:51:15
Done.
|
| -#include "base/values.h" |
| #include "chrome/browser/chromeos/file_system_provider/observer.h" |
| #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" |
| +#include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h" |
| +#include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h" |
| #include "chrome/browser/chromeos/file_system_provider/service_factory.h" |
| -#include "chrome/browser/chromeos/login/user.h" |
| -#include "chrome/browser/chromeos/login/user_manager.h" |
| -#include "chrome/common/extensions/api/file_system_provider.h" |
| +#include "chrome/browser/chromeos/file_system_provider/util.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "extensions/browser/event_router.h" |
| #include "extensions/browser/extension_system.h" |
| @@ -22,38 +22,9 @@ namespace chromeos { |
| namespace file_system_provider { |
| namespace { |
| -// Root mount path for all of the provided file systems. |
| -const base::FilePath::CharType kProvidedMountPointRoot[] = |
| - FILE_PATH_LITERAL("/provided"); |
| - |
| // Maximum number of file systems to be mounted in the same time, per profile. |
| const size_t kMaxFileSystems = 16; |
| -// Constructs a safe mount point path for the provided file system represented |
| -// by |file_system_handle|. The handle is a numeric part of the file system id. |
| -base::FilePath GetMountPointPath(Profile* profile, |
| - std::string extension_id, |
| - int file_system_id) { |
| - chromeos::User* const user = |
| - chromeos::UserManager::IsInitialized() |
| - ? chromeos::UserManager::Get()->GetUserByProfile( |
| - profile->GetOriginalProfile()) |
| - : NULL; |
| - const std::string user_suffix = user ? "-" + user->username_hash() : ""; |
| - return base::FilePath(kProvidedMountPointRoot).AppendASCII( |
| - extension_id + "-" + base::IntToString(file_system_id) + user_suffix); |
| -} |
| - |
| -// Creates values to be passed to request events. These values can be extended |
| -// by additional fields. |
| -scoped_ptr<base::ListValue> CreateRequestValues(int file_system_id, |
| - int request_id) { |
| - scoped_ptr<base::ListValue> values(new base::ListValue()); |
| - values->AppendInteger(file_system_id); |
| - values->AppendInteger(request_id); |
| - return values.Pass(); |
| -} |
| - |
| } // namespace |
| Service::Service(Profile* profile) |
| @@ -61,7 +32,7 @@ Service::Service(Profile* profile) |
| AddObserver(&request_manager_); |
| } |
| -Service::~Service() {} |
| +Service::~Service() { STLDeleteValues(&file_system_map_); } |
| // static |
| Service* Service::Get(content::BrowserContext* context) { |
| @@ -85,16 +56,16 @@ int Service::MountFileSystem(const std::string& extension_id, |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| // Restrict number of file systems to prevent system abusing. |
| - if (file_systems_.size() + 1 > kMaxFileSystems) { |
| + if (file_system_map_.size() + 1 > kMaxFileSystems) { |
| FOR_EACH_OBSERVER( |
| Observer, |
| observers_, |
| - OnProvidedFileSystemMount(ProvidedFileSystem(), |
| + OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
| base::File::FILE_ERROR_TOO_MANY_OPENED)); |
| return 0; |
| } |
| - // The file system id is unique per service, so per profile. |
| + // The provided file system id is unique per service, so per profile. |
| int file_system_id = next_id_; |
| fileapi::ExternalMountPoints* const mount_points = |
| @@ -104,7 +75,7 @@ int Service::MountFileSystem(const std::string& extension_id, |
| // The mount point path and name are unique per system, since they are system |
| // wide. This is necessary for copying between profiles. |
| const base::FilePath& mount_point_path = |
| - GetMountPointPath(profile_, extension_id, file_system_id); |
| + util::GetMountPointPath(profile_, extension_id, file_system_id); |
| const std::string mount_point_name = |
| mount_point_path.BaseName().AsUTF8Unsafe(); |
| @@ -115,7 +86,7 @@ int Service::MountFileSystem(const std::string& extension_id, |
| FOR_EACH_OBSERVER( |
| Observer, |
| observers_, |
| - OnProvidedFileSystemMount(ProvidedFileSystem(), |
| + OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
| base::File::FILE_ERROR_INVALID_OPERATION)); |
| return 0; |
| } |
| @@ -126,14 +97,22 @@ int Service::MountFileSystem(const std::string& extension_id, |
| // file_system_id = 41 |
| // mount_point_name = file_system_id = b33f1337-41-5aa5 |
| // mount_point_path = /provided/b33f1337-41-5aa5 |
| - ProvidedFileSystem file_system( |
| + ProvidedFileSystemInfo file_system_info( |
| extension_id, file_system_id, file_system_name, mount_point_path); |
| - file_systems_[file_system_id] = file_system; |
| + |
| + // Create and add the provided file system to the internal map. |
| + extensions::EventRouter* event_router = |
| + extensions::ExtensionSystem::Get(profile_)->event_router(); |
| + DCHECK(event_router); |
| + |
| + ProvidedFileSystem* file_system = |
| + new ProvidedFileSystem(event_router, &request_manager_, file_system_info); |
| + file_system_map_[file_system_id] = file_system; |
| FOR_EACH_OBSERVER( |
| Observer, |
| observers_, |
| - OnProvidedFileSystemMount(file_system, base::File::FILE_OK)); |
| + OnProvidedFileSystemMount(file_system_info, base::File::FILE_OK)); |
| next_id_++; |
| return file_system_id; |
| @@ -143,14 +122,17 @@ bool Service::UnmountFileSystem(const std::string& extension_id, |
| int file_system_id) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| - FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); |
| - if (file_system_it == file_systems_.end() || |
| - file_system_it->second.extension_id() != extension_id) { |
| - const ProvidedFileSystem empty_file_system; |
| - FOR_EACH_OBSERVER(Observer, |
| - observers_, |
| - OnProvidedFileSystemUnmount( |
| - empty_file_system, base::File::FILE_ERROR_NOT_FOUND)); |
| + ProvidedFileSystemMap::iterator file_system_it = |
| + file_system_map_.find(file_system_id); |
| + if (file_system_it == file_system_map_.end() || |
| + file_system_it->second->GetFileSystemInfo().extension_id() != |
| + extension_id) { |
| + const ProvidedFileSystemInfo empty_file_system_info; |
| + FOR_EACH_OBSERVER( |
| + Observer, |
| + observers_, |
| + OnProvidedFileSystemUnmount(empty_file_system_info, |
| + base::File::FILE_ERROR_NOT_FOUND)); |
| return false; |
| } |
| @@ -158,13 +140,16 @@ bool Service::UnmountFileSystem(const std::string& extension_id, |
| fileapi::ExternalMountPoints::GetSystemInstance(); |
| DCHECK(mount_points); |
| + const ProvidedFileSystemInfo& file_system_info = |
| + file_system_it->second->GetFileSystemInfo(); |
| + |
| const std::string mount_point_name = |
| - file_system_it->second.mount_path().BaseName().value(); |
| + file_system_info.mount_path().BaseName().value(); |
| if (!mount_points->RevokeFileSystem(mount_point_name)) { |
| FOR_EACH_OBSERVER( |
| Observer, |
| observers_, |
| - OnProvidedFileSystemUnmount(file_system_it->second, |
| + OnProvidedFileSystemUnmount(file_system_info, |
| base::File::FILE_ERROR_INVALID_OPERATION)); |
| return false; |
| } |
| @@ -172,96 +157,68 @@ bool Service::UnmountFileSystem(const std::string& extension_id, |
| FOR_EACH_OBSERVER( |
| Observer, |
| observers_, |
| - OnProvidedFileSystemUnmount(file_system_it->second, base::File::FILE_OK)); |
| + OnProvidedFileSystemUnmount(file_system_info, base::File::FILE_OK)); |
| - file_systems_.erase(file_system_it); |
| + delete file_system_it->second; |
| + file_system_map_.erase(file_system_it); |
| return true; |
| } |
| -std::vector<ProvidedFileSystem> Service::GetMountedFileSystems() { |
| - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| - |
| - std::vector<ProvidedFileSystem> result; |
| - for (FileSystemMap::const_iterator it = file_systems_.begin(); |
| - it != file_systems_.end(); |
| - ++it) { |
| - result.push_back(it->second); |
| - } |
| - return result; |
| -} |
| - |
| -bool Service::FulfillRequest(const std::string& extension_id, |
| - int file_system_id, |
| - int request_id, |
| - scoped_ptr<base::DictionaryValue> result, |
| - bool has_next) { |
| +bool Service::RequestUnmount(int file_system_id) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| - FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); |
| - if (file_system_it == file_systems_.end() || |
| - file_system_it->second.extension_id() != extension_id) { |
| + ProvidedFileSystemMap::iterator file_system_it = |
| + file_system_map_.find(file_system_id); |
| + if (file_system_it == file_system_map_.end()) |
| return false; |
| - } |
| - return request_manager_.FulfillRequest( |
| - file_system_it->second, request_id, result.Pass(), has_next); |
| + return file_system_it->second->RequestUnmount( |
| + base::Bind(&Service::OnRequestUnmountStatus, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + file_system_it->second->GetFileSystemInfo())); |
| } |
| -bool Service::RejectRequest(const std::string& extension_id, |
| - int file_system_id, |
| - int request_id, |
| - base::File::Error error) { |
| +std::vector<ProvidedFileSystemInfo> Service::GetProvidedFileSystemInfoList() { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| - FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); |
| - if (file_system_it == file_systems_.end() || |
| - file_system_it->second.extension_id() != extension_id) { |
| - return false; |
| + std::vector<ProvidedFileSystemInfo> result; |
| + for (ProvidedFileSystemMap::const_iterator it = file_system_map_.begin(); |
| + it != file_system_map_.end(); |
| + ++it) { |
| + result.push_back(it->second->GetFileSystemInfo()); |
| } |
| - |
| - return request_manager_.RejectRequest( |
| - file_system_it->second, request_id, error); |
| + return result; |
| } |
| -bool Service::RequestUnmount(int file_system_id) { |
| +ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
| + const std::string& extension_id, |
| + int file_system_id) { |
| DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| - FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); |
| - if (file_system_it == file_systems_.end()) |
| - return false; |
| - |
| - int request_id = |
| - request_manager_.CreateRequest(file_system_it->second, |
| - SuccessCallback(), |
| - base::Bind(&Service::OnRequestUnmountError, |
| - weak_ptr_factory_.GetWeakPtr(), |
| - file_system_it->second)); |
| - |
| - if (!request_id) |
| - return false; |
| - |
| - scoped_ptr<base::ListValue> values( |
| - CreateRequestValues(file_system_id, request_id)); |
| - |
| - extensions::EventRouter* event_router = |
| - extensions::ExtensionSystem::Get(profile_)->event_router(); |
| - DCHECK(event_router); |
| - |
| - event_router->DispatchEventToExtension( |
| - file_system_it->second.extension_id(), |
| - make_scoped_ptr(new extensions::Event( |
| - extensions::api::file_system_provider::OnUnmountRequested::kEventName, |
| - values.Pass()))); |
| + ProvidedFileSystemMap::iterator file_system_it = |
| + file_system_map_.find(file_system_id); |
| + if (file_system_it == file_system_map_.end() || |
| + file_system_it->second->GetFileSystemInfo().extension_id() != |
| + extension_id) { |
| + return NULL; |
| + } |
| - return true; |
| + return file_system_it->second; |
| } |
| void Service::Shutdown() { RemoveObserver(&request_manager_); } |
| -void Service::OnRequestUnmountError(const ProvidedFileSystem& file_system, |
| - base::File::Error error) { |
| - FOR_EACH_OBSERVER( |
| - Observer, observers_, OnProvidedFileSystemUnmount(file_system, error)); |
| +void Service::OnRequestUnmountStatus( |
| + const ProvidedFileSystemInfo& file_system_info, |
| + base::File::Error error) { |
| + // Notify observers about failure in unmounting, since mount() will not be |
| + // called by the provided file system. In case of success mount() will be |
| + // invoked, and observers notified, so there is no need to call them now. |
| + if (error != base::File::FILE_OK) { |
| + FOR_EACH_OBSERVER(Observer, |
| + observers_, |
| + OnProvidedFileSystemUnmount(file_system_info, error)); |
| + } |
| } |
| } // namespace file_system_provider |