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 "base/values.h" |
9 #include "chrome/browser/chromeos/file_system_provider/observer.h" | 10 #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/provided_file_system.h" |
11 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" | 12 #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.h" |
13 #include "chrome/browser/chromeos/login/user_manager.h" | 14 #include "chrome/browser/chromeos/login/user_manager.h" |
| 15 #include "chrome/common/extensions/api/file_system_provider.h" |
14 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "extensions/browser/event_router.h" |
| 18 #include "extensions/browser/extension_system.h" |
15 #include "webkit/browser/fileapi/external_mount_points.h" | 19 #include "webkit/browser/fileapi/external_mount_points.h" |
16 | 20 |
17 namespace chromeos { | 21 namespace chromeos { |
18 namespace file_system_provider { | 22 namespace file_system_provider { |
19 namespace { | 23 namespace { |
20 | 24 |
21 // Root mount path for all of the provided file systems. | 25 // Root mount path for all of the provided file systems. |
22 const base::FilePath::CharType kProvidedMountPointRoot[] = | 26 const base::FilePath::CharType kProvidedMountPointRoot[] = |
23 FILE_PATH_LITERAL("/provided"); | 27 FILE_PATH_LITERAL("/provided"); |
24 | 28 |
25 // Maximum number of file systems to be mounted in the same time, per profile. | 29 // Maximum number of file systems to be mounted in the same time, per profile. |
26 const size_t kMaxFileSystems = 16; | 30 const size_t kMaxFileSystems = 16; |
27 | 31 |
28 // Constructs a safe mount point path for the provided file system represented | 32 // 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. | 33 // by |file_system_handle|. The handle is a numeric part of the file system id. |
30 base::FilePath GetMountPointPath(Profile* profile, | 34 base::FilePath GetMountPointPath(Profile* profile, |
31 std::string extension_id, | 35 std::string extension_id, |
32 int file_system_id) { | 36 int file_system_id) { |
33 chromeos::User* const user = | 37 chromeos::User* const user = |
34 chromeos::UserManager::IsInitialized() | 38 chromeos::UserManager::IsInitialized() |
35 ? chromeos::UserManager::Get()->GetUserByProfile( | 39 ? chromeos::UserManager::Get()->GetUserByProfile( |
36 profile->GetOriginalProfile()) | 40 profile->GetOriginalProfile()) |
37 : NULL; | 41 : NULL; |
38 const std::string user_suffix = user ? "-" + user->username_hash() : ""; | 42 const std::string user_suffix = user ? "-" + user->username_hash() : ""; |
39 return base::FilePath(kProvidedMountPointRoot).AppendASCII( | 43 return base::FilePath(kProvidedMountPointRoot).AppendASCII( |
40 extension_id + "-" + base::IntToString(file_system_id) + user_suffix); | 44 extension_id + "-" + base::IntToString(file_system_id) + user_suffix); |
41 } | 45 } |
42 | 46 |
| 47 // Creates values to be passed to request events. These values can be extended |
| 48 // by additional fields. |
| 49 scoped_ptr<base::ListValue> CreateRequestValues(int file_system_id, |
| 50 int request_id) { |
| 51 scoped_ptr<base::ListValue> values(new base::ListValue()); |
| 52 values->AppendInteger(file_system_id); |
| 53 values->AppendInteger(request_id); |
| 54 return values.Pass(); |
| 55 } |
| 56 |
43 } // namespace | 57 } // namespace |
44 | 58 |
45 Service::Service(Profile* profile) : profile_(profile), next_id_(1) {} | 59 Service::Service(Profile* profile) |
| 60 : profile_(profile), next_id_(1), weak_ptr_factory_(this) { |
| 61 AddObserver(&request_manager_); |
| 62 } |
46 | 63 |
47 Service::~Service() {} | 64 Service::~Service() {} |
48 | 65 |
49 // static | 66 // static |
50 Service* Service::Get(content::BrowserContext* context) { | 67 Service* Service::Get(content::BrowserContext* context) { |
51 return ServiceFactory::Get(context); | 68 return ServiceFactory::Get(context); |
52 } | 69 } |
53 | 70 |
54 void Service::AddObserver(Observer* observer) { | 71 void Service::AddObserver(Observer* observer) { |
55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 72 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
56 DCHECK(observer); | 73 DCHECK(observer); |
57 observers_.AddObserver(observer); | 74 observers_.AddObserver(observer); |
58 } | 75 } |
59 | 76 |
60 void Service::RemoveObserver(Observer* observer) { | 77 void Service::RemoveObserver(Observer* observer) { |
61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 78 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
62 DCHECK(observer); | 79 DCHECK(observer); |
63 observers_.RemoveObserver(observer); | 80 observers_.RemoveObserver(observer); |
64 } | 81 } |
65 | 82 |
66 int Service::RegisterFileSystem(const std::string& extension_id, | 83 int Service::MountFileSystem(const std::string& extension_id, |
67 const std::string& file_system_name) { | 84 const std::string& file_system_name) { |
68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
69 | 86 |
70 // Restrict number of file systems to prevent system abusing. | 87 // Restrict number of file systems to prevent system abusing. |
71 if (file_systems_.size() + 1 > kMaxFileSystems) | 88 if (file_systems_.size() + 1 > kMaxFileSystems) { |
| 89 FOR_EACH_OBSERVER( |
| 90 Observer, |
| 91 observers_, |
| 92 OnProvidedFileSystemMount(ProvidedFileSystem(), |
| 93 base::File::FILE_ERROR_TOO_MANY_OPENED)); |
72 return 0; | 94 return 0; |
| 95 } |
73 | 96 |
74 // The file system id is unique per service, so per profile. | 97 // The file system id is unique per service, so per profile. |
75 int file_system_id = next_id_; | 98 int file_system_id = next_id_; |
76 | 99 |
77 fileapi::ExternalMountPoints* const mount_points = | 100 fileapi::ExternalMountPoints* const mount_points = |
78 fileapi::ExternalMountPoints::GetSystemInstance(); | 101 fileapi::ExternalMountPoints::GetSystemInstance(); |
79 DCHECK(mount_points); | 102 DCHECK(mount_points); |
80 | 103 |
81 // The mount point path and name are unique per system, since they are system | 104 // The mount point path and name are unique per system, since they are system |
82 // wide. This is necessary for copying between profiles. | 105 // wide. This is necessary for copying between profiles. |
83 const base::FilePath& mount_point_path = | 106 const base::FilePath& mount_point_path = |
84 GetMountPointPath(profile_, extension_id, file_system_id); | 107 GetMountPointPath(profile_, extension_id, file_system_id); |
85 const std::string mount_point_name = | 108 const std::string mount_point_name = |
86 mount_point_path.BaseName().AsUTF8Unsafe(); | 109 mount_point_path.BaseName().AsUTF8Unsafe(); |
87 | 110 |
88 if (!mount_points->RegisterFileSystem(mount_point_name, | 111 if (!mount_points->RegisterFileSystem(mount_point_name, |
89 fileapi::kFileSystemTypeProvided, | 112 fileapi::kFileSystemTypeProvided, |
90 fileapi::FileSystemMountOption(), | 113 fileapi::FileSystemMountOption(), |
91 mount_point_path)) { | 114 mount_point_path)) { |
| 115 FOR_EACH_OBSERVER( |
| 116 Observer, |
| 117 observers_, |
| 118 OnProvidedFileSystemMount(ProvidedFileSystem(), |
| 119 base::File::FILE_ERROR_INVALID_OPERATION)); |
92 return 0; | 120 return 0; |
93 } | 121 } |
94 | 122 |
95 // Store the file system descriptor. Use the mount point name as the file | 123 // Store the file system descriptor. Use the mount point name as the file |
96 // system provider file system id. | 124 // system provider file system id. |
97 // Examples: | 125 // Examples: |
98 // file_system_id = 41 | 126 // file_system_id = 41 |
99 // mount_point_name = file_system_id = b33f1337-41-5aa5 | 127 // mount_point_name = file_system_id = b33f1337-41-5aa5 |
100 // mount_point_path = /provided/b33f1337-41-5aa5 | 128 // mount_point_path = /provided/b33f1337-41-5aa5 |
101 ProvidedFileSystem file_system( | 129 ProvidedFileSystem file_system( |
102 extension_id, file_system_id, file_system_name, mount_point_path); | 130 extension_id, file_system_id, file_system_name, mount_point_path); |
103 file_systems_[file_system_id] = file_system; | 131 file_systems_[file_system_id] = file_system; |
104 | 132 |
105 FOR_EACH_OBSERVER( | 133 FOR_EACH_OBSERVER( |
106 Observer, observers_, OnProvidedFileSystemRegistered(file_system)); | 134 Observer, |
| 135 observers_, |
| 136 OnProvidedFileSystemMount(file_system, base::File::FILE_OK)); |
107 | 137 |
108 next_id_++; | 138 next_id_++; |
109 return file_system_id; | 139 return file_system_id; |
110 } | 140 } |
111 | 141 |
112 bool Service::UnregisterFileSystem(const std::string& extension_id, | 142 bool Service::UnmountFileSystem(const std::string& extension_id, |
113 int file_system_id) { | 143 int file_system_id) { |
114 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 144 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
115 | 145 |
116 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); | 146 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); |
117 if (file_system_it == file_systems_.end() || | 147 if (file_system_it == file_systems_.end() || |
118 file_system_it->second.extension_id() != extension_id) { | 148 file_system_it->second.extension_id() != extension_id) { |
| 149 const ProvidedFileSystem empty_file_system; |
| 150 FOR_EACH_OBSERVER(Observer, |
| 151 observers_, |
| 152 OnProvidedFileSystemUnmount( |
| 153 empty_file_system, base::File::FILE_ERROR_NOT_FOUND)); |
119 return false; | 154 return false; |
120 } | 155 } |
121 | 156 |
122 fileapi::ExternalMountPoints* const mount_points = | 157 fileapi::ExternalMountPoints* const mount_points = |
123 fileapi::ExternalMountPoints::GetSystemInstance(); | 158 fileapi::ExternalMountPoints::GetSystemInstance(); |
124 DCHECK(mount_points); | 159 DCHECK(mount_points); |
125 | 160 |
126 const std::string mount_point_name = | 161 const std::string mount_point_name = |
127 file_system_it->second.mount_path().BaseName().value(); | 162 file_system_it->second.mount_path().BaseName().value(); |
128 if (!mount_points->RevokeFileSystem(mount_point_name)) | 163 if (!mount_points->RevokeFileSystem(mount_point_name)) { |
| 164 FOR_EACH_OBSERVER( |
| 165 Observer, |
| 166 observers_, |
| 167 OnProvidedFileSystemUnmount(file_system_it->second, |
| 168 base::File::FILE_ERROR_INVALID_OPERATION)); |
129 return false; | 169 return false; |
| 170 } |
130 | 171 |
131 FOR_EACH_OBSERVER(Observer, | 172 FOR_EACH_OBSERVER( |
132 observers_, | 173 Observer, |
133 OnProvidedFileSystemUnregistered(file_system_it->second)); | 174 observers_, |
| 175 OnProvidedFileSystemUnmount(file_system_it->second, base::File::FILE_OK)); |
134 | 176 |
135 file_systems_.erase(file_system_it); | 177 file_systems_.erase(file_system_it); |
136 return true; | 178 return true; |
137 } | 179 } |
138 | 180 |
139 std::vector<ProvidedFileSystem> Service::GetRegisteredFileSystems() { | 181 std::vector<ProvidedFileSystem> Service::GetMountedFileSystems() { |
140 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 182 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
141 | 183 |
142 std::vector<ProvidedFileSystem> result; | 184 std::vector<ProvidedFileSystem> result; |
143 for (FileSystemMap::const_iterator it = file_systems_.begin(); | 185 for (FileSystemMap::const_iterator it = file_systems_.begin(); |
144 it != file_systems_.end(); | 186 it != file_systems_.end(); |
145 ++it) { | 187 ++it) { |
146 result.push_back(it->second); | 188 result.push_back(it->second); |
147 } | 189 } |
148 return result; | 190 return result; |
149 } | 191 } |
150 | 192 |
| 193 bool Service::FulfillRequest(const std::string& extension_id, |
| 194 int file_system_id, |
| 195 int request_id, |
| 196 scoped_ptr<base::DictionaryValue> result, |
| 197 bool has_next) { |
| 198 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 199 |
| 200 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); |
| 201 if (file_system_it == file_systems_.end() || |
| 202 file_system_it->second.extension_id() != extension_id) { |
| 203 return false; |
| 204 } |
| 205 |
| 206 return request_manager_.FulfillRequest( |
| 207 file_system_it->second, request_id, result.Pass(), has_next); |
| 208 } |
| 209 |
| 210 bool Service::RejectRequest(const std::string& extension_id, |
| 211 int file_system_id, |
| 212 int request_id, |
| 213 base::File::Error error) { |
| 214 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 215 |
| 216 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); |
| 217 if (file_system_it == file_systems_.end() || |
| 218 file_system_it->second.extension_id() != extension_id) { |
| 219 return false; |
| 220 } |
| 221 |
| 222 return request_manager_.RejectRequest( |
| 223 file_system_it->second, request_id, error); |
| 224 } |
| 225 |
| 226 bool Service::RequestUnmount(int file_system_id) { |
| 227 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 228 |
| 229 FileSystemMap::iterator file_system_it = file_systems_.find(file_system_id); |
| 230 if (file_system_it == file_systems_.end()) |
| 231 return false; |
| 232 |
| 233 int request_id = |
| 234 request_manager_.CreateRequest(file_system_it->second, |
| 235 SuccessCallback(), |
| 236 base::Bind(&Service::OnRequestUnmountError, |
| 237 weak_ptr_factory_.GetWeakPtr(), |
| 238 file_system_it->second)); |
| 239 |
| 240 if (!request_id) |
| 241 return false; |
| 242 |
| 243 scoped_ptr<base::ListValue> values( |
| 244 CreateRequestValues(file_system_id, request_id)); |
| 245 |
| 246 extensions::EventRouter* event_router = |
| 247 extensions::ExtensionSystem::Get(profile_)->event_router(); |
| 248 DCHECK(event_router); |
| 249 |
| 250 event_router->DispatchEventToExtension( |
| 251 file_system_it->second.extension_id(), |
| 252 make_scoped_ptr(new extensions::Event( |
| 253 extensions::api::file_system_provider::OnUnmountRequested::kEventName, |
| 254 values.Pass()))); |
| 255 |
| 256 return true; |
| 257 } |
| 258 |
| 259 void Service::Shutdown() { RemoveObserver(&request_manager_); } |
| 260 |
| 261 void Service::OnRequestUnmountError(const ProvidedFileSystem& file_system, |
| 262 base::File::Error error) { |
| 263 FOR_EACH_OBSERVER( |
| 264 Observer, observers_, OnProvidedFileSystemUnmount(file_system, error)); |
| 265 } |
| 266 |
151 } // namespace file_system_provider | 267 } // namespace file_system_provider |
152 } // namespace chromeos | 268 } // namespace chromeos |
OLD | NEW |