| 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 "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/extension_prefs.h" | 7 #include "chrome/browser/extensions/extension_prefs.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_system.h" | 9 #include "chrome/browser/extensions/extension_system.h" |
| 10 #include "content/public/browser/child_process_security_policy.h" | 10 #include "content/public/browser/child_process_security_policy.h" |
| 11 #include "net/base/mime_util.h" | 11 #include "net/base/mime_util.h" |
| 12 #include "webkit/fileapi/file_system_types.h" | 12 #include "webkit/fileapi/file_system_types.h" |
| 13 #include "webkit/fileapi/isolated_context.h" | 13 #include "webkit/fileapi/isolated_context.h" |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 | 16 |
| 17 namespace app_file_handler_util { | 17 namespace app_file_handler_util { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 // Preference keys | |
| 21 | |
| 22 // The file entries that an extension has permission to access. | |
| 23 const char kFileEntries[] = "file_entries"; | |
| 24 | |
| 25 // The path to a file entry that an extension had permission to access. | |
| 26 const char kFileEntryPath[] = "path"; | |
| 27 | |
| 28 // Whether or not an extension had write access to a file entry. | |
| 29 const char kFileEntryWritable[] = "writable"; | |
| 30 | 20 |
| 31 bool FileHandlerCanHandleFileWithExtension( | 21 bool FileHandlerCanHandleFileWithExtension( |
| 32 const FileHandlerInfo& handler, | 22 const FileHandlerInfo& handler, |
| 33 const base::FilePath& path) { | 23 const base::FilePath& path) { |
| 34 for (std::set<std::string>::const_iterator extension = | 24 for (std::set<std::string>::const_iterator extension = |
| 35 handler.extensions.begin(); | 25 handler.extensions.begin(); |
| 36 extension != handler.extensions.end(); ++extension) { | 26 extension != handler.extensions.end(); ++extension) { |
| 37 if (*extension == "*") | 27 if (*extension == "*") |
| 38 return true; | 28 return true; |
| 39 | 29 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 if (writable) | 144 if (writable) |
| 155 policy->GrantWriteFileSystem(renderer_id, result.filesystem_id); | 145 policy->GrantWriteFileSystem(renderer_id, result.filesystem_id); |
| 156 | 146 |
| 157 result.id = result.filesystem_id + ":" + result.registered_name; | 147 result.id = result.filesystem_id + ":" + result.registered_name; |
| 158 | 148 |
| 159 // We only need file level access for reading FileEntries. Saving FileEntries | 149 // We only need file level access for reading FileEntries. Saving FileEntries |
| 160 // just needs the file system to have read/write access, which is granted | 150 // just needs the file system to have read/write access, which is granted |
| 161 // above if required. | 151 // above if required. |
| 162 if (!policy->CanReadFile(renderer_id, path)) | 152 if (!policy->CanReadFile(renderer_id, path)) |
| 163 policy->GrantReadFile(renderer_id, path); | 153 policy->GrantReadFile(renderer_id, path); |
| 164 | |
| 165 // Save this file entry in the prefs. | |
| 166 AddSavedFileEntry(ExtensionSystem::Get(profile)->extension_prefs(), | |
| 167 extension_id, | |
| 168 result.id, | |
| 169 path, | |
| 170 writable); | |
| 171 return result; | 154 return result; |
| 172 } | 155 } |
| 173 | 156 |
| 174 void AddSavedFileEntry(ExtensionPrefs* prefs, | |
| 175 const std::string& extension_id, | |
| 176 const std::string& file_entry_id, | |
| 177 const base::FilePath& file_path, | |
| 178 bool writable) { | |
| 179 ExtensionPrefs::ScopedDictionaryUpdate update( | |
| 180 prefs, | |
| 181 extension_id, | |
| 182 kFileEntries); | |
| 183 DictionaryValue* file_entries = update.Get(); | |
| 184 if (!file_entries) | |
| 185 file_entries = update.Create(); | |
| 186 | |
| 187 // Once a file's permissions are set, they can't be changed. | |
| 188 DictionaryValue* file_entry_dict = NULL; | |
| 189 if (file_entries->GetDictionary(file_entry_id, &file_entry_dict)) | |
| 190 return; | |
| 191 | |
| 192 file_entry_dict = new DictionaryValue(); | |
| 193 file_entry_dict->SetString(kFileEntryPath, file_path.value()); | |
| 194 file_entry_dict->SetBoolean(kFileEntryWritable, writable); | |
| 195 file_entries->SetWithoutPathExpansion(file_entry_id, file_entry_dict); | |
| 196 } | |
| 197 | |
| 198 void GetSavedFileEntries( | |
| 199 const ExtensionPrefs* prefs, | |
| 200 const std::string& extension_id, | |
| 201 std::vector<SavedFileEntry>* out) { | |
| 202 const DictionaryValue* file_entries = NULL; | |
| 203 if (!prefs || !prefs->ReadPrefAsDictionary(extension_id, | |
| 204 kFileEntries, | |
| 205 &file_entries)) { | |
| 206 return; | |
| 207 } | |
| 208 | |
| 209 for (DictionaryValue::Iterator iter(*file_entries); | |
| 210 !iter.IsAtEnd(); iter.Advance()) { | |
| 211 const DictionaryValue* file_entry = NULL; | |
| 212 if (!iter.value().GetAsDictionary(&file_entry)) | |
| 213 continue; | |
| 214 base::FilePath::StringType path_string; | |
| 215 if (!file_entry->GetString(kFileEntryPath, &path_string)) | |
| 216 continue; | |
| 217 bool writable = false; | |
| 218 if (!file_entry->GetBoolean(kFileEntryWritable, &writable)) | |
| 219 continue; | |
| 220 base::FilePath file_path(path_string); | |
| 221 out->push_back(SavedFileEntry(iter.key(), file_path, writable)); | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 void ClearSavedFileEntries(ExtensionPrefs* prefs, | |
| 226 const std::string& extension_id) { | |
| 227 prefs->UpdateExtensionPref(extension_id, kFileEntries, NULL); | |
| 228 } | |
| 229 | |
| 230 } // namespace app_file_handler_util | 157 } // namespace app_file_handler_util |
| 231 | 158 |
| 232 } // namespace extensions | 159 } // namespace extensions |
| OLD | NEW |