Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: chrome/browser/extensions/api/file_handlers/app_file_handler_util.cc

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

Powered by Google App Engine
This is Rietveld 408576698