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