1 // Copyright 2013 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.
5 #include "apps/saved_files_service.h"
9 #include "apps/saved_files_service_factory.h"
10 #include "base/basictypes.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/value_conversions.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "content/public/browser/notification_service.h"
16 #include "extensions/browser/extension_host.h"
17 #include "extensions/browser/extension_prefs.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/extension_util.h"
20 #include "extensions/common/permissions/api_permission.h"
21 #include "extensions/common/permissions/permission_set.h"
22 #include "extensions/common/permissions/permissions_data.h"
26 using extensions::APIPermission
;
27 using extensions::Extension
;
28 using extensions::ExtensionHost
;
29 using extensions::ExtensionPrefs
;
35 // The file entries that the app has permission to access.
36 const char kFileEntries
[] = "file_entries";
38 // The path to a file entry that the app had permission to access.
39 const char kFileEntryPath
[] = "path";
41 // Whether or not the the entry refers to a directory.
42 const char kFileEntryIsDirectory
[] = "is_directory";
44 // The sequence number in the LRU of the file entry.
45 const char kFileEntrySequenceNumber
[] = "sequence_number";
47 const size_t kMaxSavedFileEntries
= 500;
48 const int kMaxSequenceNumber
= kint32max
;
50 // These might be different to the constant values in tests.
51 size_t g_max_saved_file_entries
= kMaxSavedFileEntries
;
52 int g_max_sequence_number
= kMaxSequenceNumber
;
54 // Persists a SavedFileEntry in ExtensionPrefs.
55 void AddSavedFileEntry(ExtensionPrefs
* prefs
,
56 const std::string
& extension_id
,
57 const SavedFileEntry
& file_entry
) {
58 ExtensionPrefs::ScopedDictionaryUpdate
update(
59 prefs
, extension_id
, kFileEntries
);
60 base::DictionaryValue
* file_entries
= update
.Get();
62 file_entries
= update
.Create();
63 DCHECK(!file_entries
->GetDictionaryWithoutPathExpansion(file_entry
.id
, NULL
));
65 base::DictionaryValue
* file_entry_dict
= new base::DictionaryValue();
66 file_entry_dict
->Set(kFileEntryPath
, CreateFilePathValue(file_entry
.path
));
67 file_entry_dict
->SetBoolean(kFileEntryIsDirectory
, file_entry
.is_directory
);
68 file_entry_dict
->SetInteger(kFileEntrySequenceNumber
,
69 file_entry
.sequence_number
);
70 file_entries
->SetWithoutPathExpansion(file_entry
.id
, file_entry_dict
);
73 // Updates the sequence_number of a SavedFileEntry persisted in ExtensionPrefs.
74 void UpdateSavedFileEntry(ExtensionPrefs
* prefs
,
75 const std::string
& extension_id
,
76 const SavedFileEntry
& file_entry
) {
77 ExtensionPrefs::ScopedDictionaryUpdate
update(
78 prefs
, extension_id
, kFileEntries
);
79 base::DictionaryValue
* file_entries
= update
.Get();
81 base::DictionaryValue
* file_entry_dict
= NULL
;
82 file_entries
->GetDictionaryWithoutPathExpansion(file_entry
.id
,
84 DCHECK(file_entry_dict
);
85 file_entry_dict
->SetInteger(kFileEntrySequenceNumber
,
86 file_entry
.sequence_number
);
89 // Removes a SavedFileEntry from ExtensionPrefs.
90 void RemoveSavedFileEntry(ExtensionPrefs
* prefs
,
91 const std::string
& extension_id
,
92 const std::string
& file_entry_id
) {
93 ExtensionPrefs::ScopedDictionaryUpdate
update(
94 prefs
, extension_id
, kFileEntries
);
95 base::DictionaryValue
* file_entries
= update
.Get();
97 file_entries
= update
.Create();
98 file_entries
->RemoveWithoutPathExpansion(file_entry_id
, NULL
);
101 // Clears all SavedFileEntry for the app from ExtensionPrefs.
102 void ClearSavedFileEntries(ExtensionPrefs
* prefs
,
103 const std::string
& extension_id
) {
104 prefs
->UpdateExtensionPref(extension_id
, kFileEntries
, NULL
);
107 // Returns all SavedFileEntries for the app.
108 std::vector
<SavedFileEntry
> GetSavedFileEntries(
109 ExtensionPrefs
* prefs
,
110 const std::string
& extension_id
) {
111 std::vector
<SavedFileEntry
> result
;
112 const base::DictionaryValue
* file_entries
= NULL
;
113 if (!prefs
->ReadPrefAsDictionary(extension_id
, kFileEntries
, &file_entries
))
116 for (base::DictionaryValue::Iterator
it(*file_entries
); !it
.IsAtEnd();
118 const base::DictionaryValue
* file_entry
= NULL
;
119 if (!it
.value().GetAsDictionary(&file_entry
))
121 const base::Value
* path_value
;
122 if (!file_entry
->Get(kFileEntryPath
, &path_value
))
124 base::FilePath file_path
;
125 if (!GetValueAsFilePath(*path_value
, &file_path
))
127 bool is_directory
= false;
128 file_entry
->GetBoolean(kFileEntryIsDirectory
, &is_directory
);
129 int sequence_number
= 0;
130 if (!file_entry
->GetInteger(kFileEntrySequenceNumber
, &sequence_number
))
132 if (!sequence_number
)
135 SavedFileEntry(it
.key(), file_path
, is_directory
, sequence_number
));
142 SavedFileEntry::SavedFileEntry() : is_directory(false), sequence_number(0) {}
144 SavedFileEntry::SavedFileEntry(const std::string
& id
,
145 const base::FilePath
& path
,
150 is_directory(is_directory
),
151 sequence_number(sequence_number
) {}
153 class SavedFilesService::SavedFiles
{
155 SavedFiles(Profile
* profile
, const std::string
& extension_id
);
158 void RegisterFileEntry(const std::string
& id
,
159 const base::FilePath
& file_path
,
161 void EnqueueFileEntry(const std::string
& id
);
162 bool IsRegistered(const std::string
& id
) const;
163 const SavedFileEntry
* GetFileEntry(const std::string
& id
) const;
164 std::vector
<SavedFileEntry
> GetAllFileEntries() const;
167 // Compacts sequence numbers if the largest sequence number is
168 // g_max_sequence_number. Outside of testing, it is set to kint32max, so this
169 // will almost never do any real work.
170 void MaybeCompactSequenceNumbers();
172 void LoadSavedFileEntriesFromPreferences();
175 const std::string extension_id_
;
177 // Contains all file entries that have been registered, keyed by ID. Owns
179 base::hash_map
<std::string
, SavedFileEntry
*> registered_file_entries_
;
180 STLValueDeleter
<base::hash_map
<std::string
, SavedFileEntry
*> >
181 registered_file_entries_deleter_
;
183 // The queue of file entries that have been retained, keyed by
184 // sequence_number. Values are a subset of values in registered_file_entries_.
185 // This should be kept in sync with file entries stored in extension prefs.
186 std::map
<int, SavedFileEntry
*> saved_file_lru_
;
188 DISALLOW_COPY_AND_ASSIGN(SavedFiles
);
192 SavedFilesService
* SavedFilesService::Get(Profile
* profile
) {
193 return SavedFilesServiceFactory::GetForProfile(profile
);
196 SavedFilesService::SavedFilesService(Profile
* profile
)
197 : extension_id_to_saved_files_deleter_(&extension_id_to_saved_files_
),
200 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
201 content::NotificationService::AllSources());
203 chrome::NOTIFICATION_APP_TERMINATING
,
204 content::NotificationService::AllSources());
207 SavedFilesService::~SavedFilesService() {}
209 void SavedFilesService::Observe(int type
,
210 const content::NotificationSource
& source
,
211 const content::NotificationDetails
& details
) {
213 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
: {
214 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
215 const Extension
* extension
= host
->extension();
217 ClearQueueIfNoRetainPermission(extension
);
218 Clear(extension
->id());
223 case chrome::NOTIFICATION_APP_TERMINATING
: {
224 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular
225 // as all extension hosts will be destroyed as a result of shutdown.
226 registrar_
.RemoveAll();
232 void SavedFilesService::RegisterFileEntry(const std::string
& extension_id
,
233 const std::string
& id
,
234 const base::FilePath
& file_path
,
236 GetOrInsert(extension_id
)->RegisterFileEntry(id
, file_path
, is_directory
);
239 void SavedFilesService::EnqueueFileEntry(const std::string
& extension_id
,
240 const std::string
& id
) {
241 GetOrInsert(extension_id
)->EnqueueFileEntry(id
);
244 std::vector
<SavedFileEntry
> SavedFilesService::GetAllFileEntries(
245 const std::string
& extension_id
) {
246 SavedFiles
* saved_files
= Get(extension_id
);
248 return saved_files
->GetAllFileEntries();
249 return GetSavedFileEntries(ExtensionPrefs::Get(profile_
), extension_id
);
252 bool SavedFilesService::IsRegistered(const std::string
& extension_id
,
253 const std::string
& id
) {
254 return GetOrInsert(extension_id
)->IsRegistered(id
);
257 const SavedFileEntry
* SavedFilesService::GetFileEntry(
258 const std::string
& extension_id
,
259 const std::string
& id
) {
260 return GetOrInsert(extension_id
)->GetFileEntry(id
);
263 void SavedFilesService::ClearQueueIfNoRetainPermission(
264 const Extension
* extension
) {
265 if (extensions::util::IsEphemeralApp(extension
->id(), profile_
) ||
266 !extension
->permissions_data()->active_permissions()->HasAPIPermission(
267 APIPermission::kFileSystemRetainEntries
)) {
268 ClearQueue(extension
);
272 void SavedFilesService::ClearQueue(const extensions::Extension
* extension
) {
273 ClearSavedFileEntries(ExtensionPrefs::Get(profile_
), extension
->id());
274 Clear(extension
->id());
277 SavedFilesService::SavedFiles
* SavedFilesService::Get(
278 const std::string
& extension_id
) const {
279 std::map
<std::string
, SavedFiles
*>::const_iterator it
=
280 extension_id_to_saved_files_
.find(extension_id
);
281 if (it
!= extension_id_to_saved_files_
.end())
287 SavedFilesService::SavedFiles
* SavedFilesService::GetOrInsert(
288 const std::string
& extension_id
) {
289 SavedFiles
* saved_files
= Get(extension_id
);
293 saved_files
= new SavedFiles(profile_
, extension_id
);
294 extension_id_to_saved_files_
.insert(
295 std::make_pair(extension_id
, saved_files
));
299 void SavedFilesService::Clear(const std::string
& extension_id
) {
300 std::map
<std::string
, SavedFiles
*>::iterator it
=
301 extension_id_to_saved_files_
.find(extension_id
);
302 if (it
!= extension_id_to_saved_files_
.end()) {
304 extension_id_to_saved_files_
.erase(it
);
308 SavedFilesService::SavedFiles::SavedFiles(Profile
* profile
,
309 const std::string
& extension_id
)
311 extension_id_(extension_id
),
312 registered_file_entries_deleter_(®istered_file_entries_
) {
313 LoadSavedFileEntriesFromPreferences();
316 SavedFilesService::SavedFiles::~SavedFiles() {}
318 void SavedFilesService::SavedFiles::RegisterFileEntry(
319 const std::string
& id
,
320 const base::FilePath
& file_path
,
322 if (ContainsKey(registered_file_entries_
, id
))
325 registered_file_entries_
.insert(
326 std::make_pair(id
, new SavedFileEntry(id
, file_path
, is_directory
, 0)));
329 void SavedFilesService::SavedFiles::EnqueueFileEntry(const std::string
& id
) {
330 base::hash_map
<std::string
, SavedFileEntry
*>::iterator it
=
331 registered_file_entries_
.find(id
);
332 DCHECK(it
!= registered_file_entries_
.end());
334 SavedFileEntry
* file_entry
= it
->second
;
335 int old_sequence_number
= file_entry
->sequence_number
;
336 if (!saved_file_lru_
.empty()) {
337 // Get the sequence number after the last file entry in the LRU.
338 std::map
<int, SavedFileEntry
*>::reverse_iterator it
=
339 saved_file_lru_
.rbegin();
340 if (it
->second
== file_entry
)
343 file_entry
->sequence_number
= it
->first
+ 1;
345 // The first sequence number is 1, as 0 means the entry is not in the LRU.
346 file_entry
->sequence_number
= 1;
348 saved_file_lru_
.insert(
349 std::make_pair(file_entry
->sequence_number
, file_entry
));
350 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
351 if (old_sequence_number
) {
352 saved_file_lru_
.erase(old_sequence_number
);
353 UpdateSavedFileEntry(prefs
, extension_id_
, *file_entry
);
355 AddSavedFileEntry(prefs
, extension_id_
, *file_entry
);
356 if (saved_file_lru_
.size() > g_max_saved_file_entries
) {
357 std::map
<int, SavedFileEntry
*>::iterator it
= saved_file_lru_
.begin();
358 it
->second
->sequence_number
= 0;
359 RemoveSavedFileEntry(prefs
, extension_id_
, it
->second
->id
);
360 saved_file_lru_
.erase(it
);
363 MaybeCompactSequenceNumbers();
366 bool SavedFilesService::SavedFiles::IsRegistered(const std::string
& id
) const {
367 return ContainsKey(registered_file_entries_
, id
);
370 const SavedFileEntry
* SavedFilesService::SavedFiles::GetFileEntry(
371 const std::string
& id
) const {
372 base::hash_map
<std::string
, SavedFileEntry
*>::const_iterator it
=
373 registered_file_entries_
.find(id
);
374 if (it
== registered_file_entries_
.end())
380 std::vector
<SavedFileEntry
> SavedFilesService::SavedFiles::GetAllFileEntries()
382 std::vector
<SavedFileEntry
> result
;
383 for (base::hash_map
<std::string
, SavedFileEntry
*>::const_iterator it
=
384 registered_file_entries_
.begin();
385 it
!= registered_file_entries_
.end();
387 result
.push_back(*it
->second
);
392 void SavedFilesService::SavedFiles::MaybeCompactSequenceNumbers() {
393 DCHECK_GE(g_max_sequence_number
, 0);
394 DCHECK_GE(static_cast<size_t>(g_max_sequence_number
),
395 g_max_saved_file_entries
);
396 std::map
<int, SavedFileEntry
*>::reverse_iterator it
=
397 saved_file_lru_
.rbegin();
398 if (it
== saved_file_lru_
.rend())
401 // Only compact sequence numbers if the last entry's sequence number is the
402 // maximum value. This should almost never be the case.
403 if (it
->first
< g_max_sequence_number
)
406 int sequence_number
= 0;
407 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
408 for (std::map
<int, SavedFileEntry
*>::iterator it
= saved_file_lru_
.begin();
409 it
!= saved_file_lru_
.end();
412 if (it
->second
->sequence_number
== sequence_number
)
415 SavedFileEntry
* file_entry
= it
->second
;
416 file_entry
->sequence_number
= sequence_number
;
417 UpdateSavedFileEntry(prefs
, extension_id_
, *file_entry
);
418 saved_file_lru_
.erase(it
++);
419 // Provide the following element as an insert hint. While optimized
420 // insertion time with the following element as a hint is only supported by
421 // the spec in C++11, the implementations do support this.
422 it
= saved_file_lru_
.insert(
423 it
, std::make_pair(file_entry
->sequence_number
, file_entry
));
427 void SavedFilesService::SavedFiles::LoadSavedFileEntriesFromPreferences() {
428 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
429 std::vector
<SavedFileEntry
> saved_entries
=
430 GetSavedFileEntries(prefs
, extension_id_
);
431 for (std::vector
<SavedFileEntry
>::iterator it
= saved_entries
.begin();
432 it
!= saved_entries
.end();
434 SavedFileEntry
* file_entry
= new SavedFileEntry(*it
);
435 registered_file_entries_
.insert(std::make_pair(file_entry
->id
, file_entry
));
436 saved_file_lru_
.insert(
437 std::make_pair(file_entry
->sequence_number
, file_entry
));
442 void SavedFilesService::SetMaxSequenceNumberForTest(int max_value
) {
443 g_max_sequence_number
= max_value
;
447 void SavedFilesService::ClearMaxSequenceNumberForTest() {
448 g_max_sequence_number
= kMaxSequenceNumber
;
452 void SavedFilesService::SetLruSizeForTest(int size
) {
453 g_max_saved_file_entries
= size
;
457 void SavedFilesService::ClearLruSizeForTest() {
458 g_max_saved_file_entries
= kMaxSavedFileEntries
;