OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/api/file_handlers/app_file_handler_util.h" | 5 #include "extensions/browser/api/file_handlers/app_file_handler_util.h" |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "content/public/browser/browser_context.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "content/public/browser/child_process_security_policy.h" | 13 #include "content/public/browser/child_process_security_policy.h" |
14 #include "extensions/browser/api/extensions_api_client.h" | 14 #include "extensions/browser/api/extensions_api_client.h" |
15 #include "extensions/browser/entry_info.h" | 15 #include "extensions/browser/entry_info.h" |
16 #include "extensions/browser/extension_prefs.h" | 16 #include "extensions/browser/extension_prefs.h" |
17 #include "extensions/browser/granted_file_entry.h" | 17 #include "extensions/browser/granted_file_entry.h" |
18 #include "extensions/common/permissions/permissions_data.h" | 18 #include "extensions/common/permissions/permissions_data.h" |
19 #include "net/base/mime_util.h" | 19 #include "net/base/mime_util.h" |
20 #include "storage/browser/fileapi/isolated_context.h" | 20 #include "storage/browser/fileapi/isolated_context.h" |
21 #include "storage/common/fileapi/file_system_mount_option.h" | 21 #include "storage/common/fileapi/file_system_mount_option.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 // Checks whether a list of paths are all OK for writing and calls a provided | 95 // Checks whether a list of paths are all OK for writing and calls a provided |
96 // on_success or on_failure callback when done. A path is OK for writing if it | 96 // on_success or on_failure callback when done. A path is OK for writing if it |
97 // is not a symlink, is not in a blacklisted path and can be opened for writing. | 97 // is not a symlink, is not in a blacklisted path and can be opened for writing. |
98 // Creates files if they do not exist, but fails for non-existent directory | 98 // Creates files if they do not exist, but fails for non-existent directory |
99 // paths. On Chrome OS, also fails for non-local files that don't already exist. | 99 // paths. On Chrome OS, also fails for non-local files that don't already exist. |
100 class WritableFileChecker | 100 class WritableFileChecker |
101 : public base::RefCountedThreadSafe<WritableFileChecker> { | 101 : public base::RefCountedThreadSafe<WritableFileChecker> { |
102 public: | 102 public: |
103 WritableFileChecker( | 103 WritableFileChecker( |
104 const std::vector<base::FilePath>& paths, | 104 const std::vector<base::FilePath>& paths, |
105 Profile* profile, | 105 content::BrowserContext* context, |
106 const std::set<base::FilePath>& directory_paths, | 106 const std::set<base::FilePath>& directory_paths, |
107 const base::Closure& on_success, | 107 const base::Closure& on_success, |
108 const base::Callback<void(const base::FilePath&)>& on_failure); | 108 const base::Callback<void(const base::FilePath&)>& on_failure); |
109 | 109 |
110 void Check(); | 110 void Check(); |
111 | 111 |
112 private: | 112 private: |
113 friend class base::RefCountedThreadSafe<WritableFileChecker>; | 113 friend class base::RefCountedThreadSafe<WritableFileChecker>; |
114 virtual ~WritableFileChecker(); | 114 virtual ~WritableFileChecker(); |
115 | 115 |
116 // Called when a work item is completed. If all work items are done, this | 116 // Called when a work item is completed. If all work items are done, this |
117 // calls the success or failure callback. | 117 // calls the success or failure callback. |
118 void TaskDone(); | 118 void TaskDone(); |
119 | 119 |
120 // Reports an error in completing a work item. This may be called more than | 120 // Reports an error in completing a work item. This may be called more than |
121 // once, but only the last message will be retained. | 121 // once, but only the last message will be retained. |
122 void Error(const base::FilePath& error_path); | 122 void Error(const base::FilePath& error_path); |
123 | 123 |
124 void CheckLocalWritableFiles(); | 124 void CheckLocalWritableFiles(); |
125 | 125 |
126 // Called when processing a file is completed with either a success or an | 126 // Called when processing a file is completed with either a success or an |
127 // error. | 127 // error. |
128 void OnPrepareFileDone(const base::FilePath& path, bool success); | 128 void OnPrepareFileDone(const base::FilePath& path, bool success); |
129 | 129 |
130 const std::vector<base::FilePath> paths_; | 130 const std::vector<base::FilePath> paths_; |
131 Profile* profile_; | 131 content::BrowserContext* context_; |
132 const std::set<base::FilePath> directory_paths_; | 132 const std::set<base::FilePath> directory_paths_; |
133 int outstanding_tasks_; | 133 int outstanding_tasks_; |
134 base::FilePath error_path_; | 134 base::FilePath error_path_; |
135 base::Closure on_success_; | 135 base::Closure on_success_; |
136 base::Callback<void(const base::FilePath&)> on_failure_; | 136 base::Callback<void(const base::FilePath&)> on_failure_; |
137 }; | 137 }; |
138 | 138 |
139 WritableFileChecker::WritableFileChecker( | 139 WritableFileChecker::WritableFileChecker( |
140 const std::vector<base::FilePath>& paths, | 140 const std::vector<base::FilePath>& paths, |
141 Profile* profile, | 141 content::BrowserContext* context, |
142 const std::set<base::FilePath>& directory_paths, | 142 const std::set<base::FilePath>& directory_paths, |
143 const base::Closure& on_success, | 143 const base::Closure& on_success, |
144 const base::Callback<void(const base::FilePath&)>& on_failure) | 144 const base::Callback<void(const base::FilePath&)>& on_failure) |
145 : paths_(paths), | 145 : paths_(paths), |
146 profile_(profile), | 146 context_(context), |
147 directory_paths_(directory_paths), | 147 directory_paths_(directory_paths), |
148 outstanding_tasks_(1), | 148 outstanding_tasks_(1), |
149 on_success_(on_success), | 149 on_success_(on_success), |
150 on_failure_(on_failure) {} | 150 on_failure_(on_failure) {} |
151 | 151 |
152 void WritableFileChecker::Check() { | 152 void WritableFileChecker::Check() { |
153 outstanding_tasks_ = paths_.size(); | 153 outstanding_tasks_ = paths_.size(); |
154 for (const auto& path : paths_) { | 154 for (const auto& path : paths_) { |
155 bool is_directory = directory_paths_.find(path) != directory_paths_.end(); | 155 bool is_directory = directory_paths_.find(path) != directory_paths_.end(); |
156 #if defined(OS_CHROMEOS) | 156 #if defined(OS_CHROMEOS) |
157 NonNativeFileSystemDelegate* delegate = | 157 NonNativeFileSystemDelegate* delegate = |
158 ExtensionsAPIClient::Get()->GetNonNativeFileSystemDelegate(); | 158 ExtensionsAPIClient::Get()->GetNonNativeFileSystemDelegate(); |
159 if (delegate && delegate->IsUnderNonNativeLocalPath(profile_, path)) { | 159 if (delegate && delegate->IsUnderNonNativeLocalPath(context_, path)) { |
160 if (is_directory) { | 160 if (is_directory) { |
161 delegate->IsNonNativeLocalPathDirectory( | 161 delegate->IsNonNativeLocalPathDirectory( |
162 profile_, | 162 context_, path, |
163 path, | |
164 base::Bind(&WritableFileChecker::OnPrepareFileDone, this, path)); | 163 base::Bind(&WritableFileChecker::OnPrepareFileDone, this, path)); |
165 } else { | 164 } else { |
166 delegate->PrepareNonNativeLocalFileForWritableApp( | 165 delegate->PrepareNonNativeLocalFileForWritableApp( |
167 profile_, | 166 context_, path, |
168 path, | |
169 base::Bind(&WritableFileChecker::OnPrepareFileDone, this, path)); | 167 base::Bind(&WritableFileChecker::OnPrepareFileDone, this, path)); |
170 } | 168 } |
171 continue; | 169 continue; |
172 } | 170 } |
173 #endif | 171 #endif |
174 content::BrowserThread::PostTaskAndReplyWithResult( | 172 content::BrowserThread::PostTaskAndReplyWithResult( |
175 content::BrowserThread::FILE, FROM_HERE, | 173 content::BrowserThread::FILE, FROM_HERE, |
176 base::Bind(&PrepareNativeLocalFileForWritableApp, path, is_directory), | 174 base::Bind(&PrepareNativeLocalFileForWritableApp, path, is_directory), |
177 base::Bind(&WritableFileChecker::OnPrepareFileDone, this, path)); | 175 base::Bind(&WritableFileChecker::OnPrepareFileDone, this, path)); |
178 } | 176 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 | 250 |
253 bool FileHandlerCanHandleEntry(const FileHandlerInfo& handler, | 251 bool FileHandlerCanHandleEntry(const FileHandlerInfo& handler, |
254 const EntryInfo& entry) { | 252 const EntryInfo& entry) { |
255 if (entry.is_directory) | 253 if (entry.is_directory) |
256 return handler.include_directories; | 254 return handler.include_directories; |
257 | 255 |
258 return FileHandlerCanHandleFileWithMimeType(handler, entry.mime_type) || | 256 return FileHandlerCanHandleFileWithMimeType(handler, entry.mime_type) || |
259 FileHandlerCanHandleFileWithExtension(handler, entry.path); | 257 FileHandlerCanHandleFileWithExtension(handler, entry.path); |
260 } | 258 } |
261 | 259 |
262 GrantedFileEntry CreateFileEntry(Profile* profile, | 260 GrantedFileEntry CreateFileEntry(content::BrowserContext* context, |
263 const Extension* extension, | 261 const Extension* extension, |
264 int renderer_id, | 262 int renderer_id, |
265 const base::FilePath& path, | 263 const base::FilePath& path, |
266 bool is_directory) { | 264 bool is_directory) { |
267 GrantedFileEntry result; | 265 GrantedFileEntry result; |
268 storage::IsolatedContext* isolated_context = | 266 storage::IsolatedContext* isolated_context = |
269 storage::IsolatedContext::GetInstance(); | 267 storage::IsolatedContext::GetInstance(); |
270 DCHECK(isolated_context); | 268 DCHECK(isolated_context); |
271 | 269 |
272 result.filesystem_id = isolated_context->RegisterFileSystemForPath( | 270 result.filesystem_id = isolated_context->RegisterFileSystemForPath( |
(...skipping 11 matching lines...) Expand all Loading... |
284 policy->GrantDeleteFromFileSystem(renderer_id, result.filesystem_id); | 282 policy->GrantDeleteFromFileSystem(renderer_id, result.filesystem_id); |
285 } | 283 } |
286 } | 284 } |
287 | 285 |
288 result.id = result.filesystem_id + ":" + result.registered_name; | 286 result.id = result.filesystem_id + ":" + result.registered_name; |
289 return result; | 287 return result; |
290 } | 288 } |
291 | 289 |
292 void PrepareFilesForWritableApp( | 290 void PrepareFilesForWritableApp( |
293 const std::vector<base::FilePath>& paths, | 291 const std::vector<base::FilePath>& paths, |
294 Profile* profile, | 292 content::BrowserContext* context, |
295 const std::set<base::FilePath>& directory_paths, | 293 const std::set<base::FilePath>& directory_paths, |
296 const base::Closure& on_success, | 294 const base::Closure& on_success, |
297 const base::Callback<void(const base::FilePath&)>& on_failure) { | 295 const base::Callback<void(const base::FilePath&)>& on_failure) { |
298 scoped_refptr<WritableFileChecker> checker(new WritableFileChecker( | 296 scoped_refptr<WritableFileChecker> checker(new WritableFileChecker( |
299 paths, profile, directory_paths, on_success, on_failure)); | 297 paths, context, directory_paths, on_success, on_failure)); |
300 checker->Check(); | 298 checker->Check(); |
301 } | 299 } |
302 | 300 |
303 bool HasFileSystemWritePermission(const Extension* extension) { | 301 bool HasFileSystemWritePermission(const Extension* extension) { |
304 return extension->permissions_data()->HasAPIPermission( | 302 return extension->permissions_data()->HasAPIPermission( |
305 APIPermission::kFileSystemWrite); | 303 APIPermission::kFileSystemWrite); |
306 } | 304 } |
307 | 305 |
308 bool ValidateFileEntryAndGetPath(const std::string& filesystem_name, | 306 bool ValidateFileEntryAndGetPath(const std::string& filesystem_name, |
309 const std::string& filesystem_path, | 307 const std::string& filesystem_path, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 *error = kInvalidParameters; | 350 *error = kInvalidParameters; |
353 return false; | 351 return false; |
354 } | 352 } |
355 | 353 |
356 return true; | 354 return true; |
357 } | 355 } |
358 | 356 |
359 } // namespace app_file_handler_util | 357 } // namespace app_file_handler_util |
360 | 358 |
361 } // namespace extensions | 359 } // namespace extensions |
OLD | NEW |