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