| 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/strings/string_number_conversions.h" |
| 9 #include "chrome/browser/chromeos/file_system_provider/observer.h" | 9 #include "chrome/browser/chromeos/file_system_provider/observer.h" |
| 10 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" | 10 #include "chrome/browser/chromeos/file_system_provider/provided_file_system.h" |
| 11 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" | 11 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" |
| 12 #include "chrome/browser/chromeos/login/user.h" | 12 #include "chrome/browser/chromeos/login/user.h" |
| 13 #include "chrome/browser/chromeos/login/user_manager.h" | 13 #include "chrome/browser/chromeos/login/user_manager.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "webkit/browser/fileapi/external_mount_points.h" | 15 #include "webkit/browser/fileapi/external_mount_points.h" |
| 16 | 16 |
| 17 namespace chromeos { | 17 namespace chromeos { |
| 18 namespace file_system_provider { | 18 namespace file_system_provider { |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // Root mount path for all of the provided file systems. | 21 // Root mount path for all of the provided file systems. |
| 22 static const base::FilePath::CharType kProvidedMountPointRoot[] = | 22 static const base::FilePath::CharType kProvidedMountPointRoot[] = |
| 23 FILE_PATH_LITERAL("/provided"); | 23 FILE_PATH_LITERAL("/provided"); |
| 24 | 24 |
| 25 // Maximum number of file systems to be mounted in the same time, per profile. | 25 // Maximum number of file systems to be mounted in the same time, per profile. |
| 26 static const size_t kMaxFileSystems = 16; | 26 static const size_t kMaxFileSystems = 16; |
| 27 | 27 |
| 28 base::FilePath GetMountPointPath(Profile* profile, | 28 base::FilePath GetMountPointPath(Profile* profile, |
| 29 std::string extension_id, | 29 std::string extension_id, |
| 30 int file_system_handle) { | 30 int file_system_id) { |
| 31 chromeos::User* const user = | 31 chromeos::User* const user = |
| 32 chromeos::UserManager::IsInitialized() | 32 chromeos::UserManager::IsInitialized() |
| 33 ? chromeos::UserManager::Get()->GetUserByProfile( | 33 ? chromeos::UserManager::Get()->GetUserByProfile( |
| 34 profile->GetOriginalProfile()) | 34 profile->GetOriginalProfile()) |
| 35 : NULL; | 35 : NULL; |
| 36 const std::string user_suffix = user ? "-" + user->username_hash() : ""; | 36 const std::string user_suffix = user ? "-" + user->username_hash() : ""; |
| 37 return base::FilePath(kProvidedMountPointRoot).AppendASCII( | 37 return base::FilePath(kProvidedMountPointRoot).AppendASCII( |
| 38 extension_id + "-" + base::IntToString(file_system_handle) + user_suffix); | 38 extension_id + "-" + base::IntToString(file_system_id) + user_suffix); |
| 39 } | 39 } |
| 40 | 40 |
| 41 } // namespace | 41 } // namespace |
| 42 | 42 |
| 43 Service::Service(Profile* profile) : profile_(profile), next_handle_(1) {} | 43 Service::Service(Profile* profile) : profile_(profile), next_id_(1) {} |
| 44 | 44 |
| 45 Service::~Service() {} | 45 Service::~Service() {} |
| 46 | 46 |
| 47 // static | 47 // static |
| 48 Service* Service::Get(content::BrowserContext* context) { | 48 Service* Service::Get(content::BrowserContext* context) { |
| 49 return ServiceFactory::Get(context); | 49 return ServiceFactory::Get(context); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void Service::AddObserver(Observer* observer) { | 52 void Service::AddObserver(Observer* observer) { |
| 53 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 53 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 54 DCHECK(observer); | 54 DCHECK(observer); |
| 55 observers_.AddObserver(observer); | 55 observers_.AddObserver(observer); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void Service::RemoveObserver(Observer* observer) { | 58 void Service::RemoveObserver(Observer* observer) { |
| 59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 60 DCHECK(observer); | 60 DCHECK(observer); |
| 61 observers_.RemoveObserver(observer); | 61 observers_.RemoveObserver(observer); |
| 62 } | 62 } |
| 63 | 63 |
| 64 std::string Service::RegisterFileSystem(const std::string& extension_id, | 64 int Service::RegisterFileSystem(const std::string& extension_id, |
| 65 const std::string& file_system_name) { | 65 const std::string& file_system_name) { |
| 66 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 66 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 67 | 67 |
| 68 // Restrict number of file systems to prevent system abusing. | 68 // Restrict number of file systems to prevent system abusing. |
| 69 if (file_systems_.size() + 1 > kMaxFileSystems) | 69 if (file_systems_.size() + 1 > kMaxFileSystems) |
| 70 return std::string(); | 70 return 0; |
| 71 | 71 |
| 72 // The file system handle is unique per service, so per profile. | 72 // The file system id is unique per service, so per profile. |
| 73 int file_system_handle = next_handle_; | 73 int file_system_id = next_id_; |
| 74 | 74 |
| 75 fileapi::ExternalMountPoints* const mount_points = | 75 fileapi::ExternalMountPoints* const mount_points = |
| 76 fileapi::ExternalMountPoints::GetSystemInstance(); | 76 fileapi::ExternalMountPoints::GetSystemInstance(); |
| 77 DCHECK(mount_points); | 77 DCHECK(mount_points); |
| 78 | 78 |
| 79 // The mount point path and name are unique per system, since they are system | 79 // The mount point path and name are unique per system, since they are system |
| 80 // wide. This is necessary for copying between profiles. | 80 // wide. This is necessary for copying between profiles. |
| 81 const base::FilePath& mount_point_path = | 81 const base::FilePath& mount_point_path = |
| 82 GetMountPointPath(profile_, extension_id, file_system_handle); | 82 GetMountPointPath(profile_, extension_id, file_system_id); |
| 83 const std::string mount_point_name = | 83 const std::string mount_point_name = |
| 84 mount_point_path.BaseName().AsUTF8Unsafe(); | 84 mount_point_path.BaseName().AsUTF8Unsafe(); |
| 85 | 85 |
| 86 if (!mount_points->RegisterFileSystem(mount_point_name, | 86 if (!mount_points->RegisterFileSystem(mount_point_name, |
| 87 fileapi::kFileSystemTypeProvided, | 87 fileapi::kFileSystemTypeProvided, |
| 88 fileapi::FileSystemMountOption(), | 88 fileapi::FileSystemMountOption(), |
| 89 mount_point_path)) { | 89 mount_point_path)) { |
| 90 return std::string(); | 90 return 0; |
| 91 } | 91 } |
| 92 | 92 |
| 93 // Store the file system descriptor. Use the mount point name as the file | 93 // Store the file system descriptor. Use the mount point name as the file |
| 94 // system provider file system id. | 94 // system provider file system id. |
| 95 // Examples: | 95 // Examples: |
| 96 // file_system_handle = 41 | 96 // file_system_id = 41 |
| 97 // mount_point_name = file_system_id = b33f1337-41-5aa5 | 97 // mount_point_name = file_system_id = b33f1337-41-5aa5 |
| 98 // mount_point_path = /provided/b33f1337-41-5aa5 | 98 // mount_point_path = /provided/b33f1337-41-5aa5 |
| 99 const std::string file_system_id = mount_point_name; | |
| 100 ProvidedFileSystem file_system( | 99 ProvidedFileSystem file_system( |
| 101 extension_id, file_system_id, file_system_name, mount_point_path); | 100 extension_id, file_system_id, file_system_name, mount_point_path); |
| 102 file_systems_[file_system_id] = file_system; | 101 file_systems_[file_system_id] = file_system; |
| 103 | 102 |
| 104 FOR_EACH_OBSERVER( | 103 FOR_EACH_OBSERVER( |
| 105 Observer, observers_, OnProvidedFileSystemRegistered(file_system)); | 104 Observer, observers_, OnProvidedFileSystemRegistered(file_system)); |
| 106 | 105 |
| 107 next_handle_++; | 106 next_id_++; |
| 108 return file_system_id; | 107 return file_system_id; |
| 109 } | 108 } |
| 110 | 109 |
| 111 bool Service::UnregisterFileSystem(const std::string& extension_id, | 110 bool Service::UnregisterFileSystem(const std::string& extension_id, |
| 112 const std::string& file_system_id) { | 111 int file_system_id) { |
| 113 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 112 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 114 | 113 |
| 115 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | 114 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); |
| 116 if (file_system_it == file_systems_.end() || | 115 if (file_system_it == file_systems_.end() || |
| 117 file_system_it->second.extension_id() != extension_id) { | 116 file_system_it->second.extension_id() != extension_id) { |
| 118 return false; | 117 return false; |
| 119 } | 118 } |
| 120 | 119 |
| 121 fileapi::ExternalMountPoints* const mount_points = | 120 fileapi::ExternalMountPoints* const mount_points = |
| 122 fileapi::ExternalMountPoints::GetSystemInstance(); | 121 fileapi::ExternalMountPoints::GetSystemInstance(); |
| 123 DCHECK(mount_points); | 122 DCHECK(mount_points); |
| 124 | 123 |
| 125 if (!mount_points->RevokeFileSystem(file_system_it->second.file_system_id())) | 124 const std::string mount_point_name = |
| 125 file_system_it->second.mount_path().BaseName().value(); |
| 126 if (!mount_points->RevokeFileSystem(mount_point_name)) |
| 126 return false; | 127 return false; |
| 127 | 128 |
| 128 FOR_EACH_OBSERVER(Observer, | 129 FOR_EACH_OBSERVER(Observer, |
| 129 observers_, | 130 observers_, |
| 130 OnProvidedFileSystemUnregistered(file_system_it->second)); | 131 OnProvidedFileSystemUnregistered(file_system_it->second)); |
| 131 | 132 |
| 132 file_systems_.erase(file_system_it); | 133 file_systems_.erase(file_system_it); |
| 133 return true; | 134 return true; |
| 134 } | 135 } |
| 135 | 136 |
| 136 std::vector<ProvidedFileSystem> Service::GetRegisteredFileSystems() { | 137 std::vector<ProvidedFileSystem> Service::GetRegisteredFileSystems() { |
| 137 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 138 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 138 | 139 |
| 139 std::vector<ProvidedFileSystem> result; | 140 std::vector<ProvidedFileSystem> result; |
| 140 for (FileSystemMap::const_iterator it = file_systems_.begin(); | 141 for (FileSystemMap::const_iterator it = file_systems_.begin(); |
| 141 it != file_systems_.end(); | 142 it != file_systems_.end(); |
| 142 ++it) { | 143 ++it) { |
| 143 result.push_back(it->second); | 144 result.push_back(it->second); |
| 144 } | 145 } |
| 145 return result; | 146 return result; |
| 146 } | 147 } |
| 147 | 148 |
| 148 } // namespace file_system_provider | 149 } // namespace file_system_provider |
| 149 } // namespace chromeos | 150 } // namespace chromeos |
| OLD | NEW |