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/common/permissions/api_permission.h"
20 #include "extensions/common/permissions/permission_set.h"
24 using extensions::APIPermission
;
25 using extensions::Extension
;
26 using extensions::ExtensionHost
;
27 using extensions::ExtensionPrefs
;
33 // The file entries that the app has permission to access.
34 const char kFileEntries
[] = "file_entries";
36 // The path to a file entry that the app had permission to access.
37 const char kFileEntryPath
[] = "path";
39 // Whether or not the the entry refers to a directory.
40 const char kFileEntryIsDirectory
[] = "is_directory";
42 // The sequence number in the LRU of the file entry.
43 const char kFileEntrySequenceNumber
[] = "sequence_number";
45 const size_t kMaxSavedFileEntries
= 500;
46 const int kMaxSequenceNumber
= kint32max
;
48 // These might be different to the constant values in tests.
49 size_t g_max_saved_file_entries
= kMaxSavedFileEntries
;
50 int g_max_sequence_number
= kMaxSequenceNumber
;
52 // Persists a SavedFileEntry in ExtensionPrefs.
53 void AddSavedFileEntry(ExtensionPrefs
* prefs
,
54 const std::string
& extension_id
,
55 const SavedFileEntry
& file_entry
) {
56 ExtensionPrefs::ScopedDictionaryUpdate
update(
57 prefs
, extension_id
, kFileEntries
);
58 base::DictionaryValue
* file_entries
= update
.Get();
60 file_entries
= update
.Create();
61 DCHECK(!file_entries
->GetDictionaryWithoutPathExpansion(file_entry
.id
, NULL
));
63 base::DictionaryValue
* file_entry_dict
= new base::DictionaryValue();
64 file_entry_dict
->Set(kFileEntryPath
, CreateFilePathValue(file_entry
.path
));
65 file_entry_dict
->SetBoolean(kFileEntryIsDirectory
, file_entry
.is_directory
);
66 file_entry_dict
->SetInteger(kFileEntrySequenceNumber
,
67 file_entry
.sequence_number
);
68 file_entries
->SetWithoutPathExpansion(file_entry
.id
, file_entry_dict
);
71 // Updates the sequence_number of a SavedFileEntry persisted in ExtensionPrefs.
72 void UpdateSavedFileEntry(ExtensionPrefs
* prefs
,
73 const std::string
& extension_id
,
74 const SavedFileEntry
& file_entry
) {
75 ExtensionPrefs::ScopedDictionaryUpdate
update(
76 prefs
, extension_id
, kFileEntries
);
77 base::DictionaryValue
* file_entries
= update
.Get();
79 base::DictionaryValue
* file_entry_dict
= NULL
;
80 file_entries
->GetDictionaryWithoutPathExpansion(file_entry
.id
,
82 DCHECK(file_entry_dict
);
83 file_entry_dict
->SetInteger(kFileEntrySequenceNumber
,
84 file_entry
.sequence_number
);
87 // Removes a SavedFileEntry from ExtensionPrefs.
88 void RemoveSavedFileEntry(ExtensionPrefs
* prefs
,
89 const std::string
& extension_id
,
90 const std::string
& file_entry_id
) {
91 ExtensionPrefs::ScopedDictionaryUpdate
update(
92 prefs
, extension_id
, kFileEntries
);
93 base::DictionaryValue
* file_entries
= update
.Get();
95 file_entries
= update
.Create();
96 file_entries
->RemoveWithoutPathExpansion(file_entry_id
, NULL
);
99 // Clears all SavedFileEntry for the app from ExtensionPrefs.
100 void ClearSavedFileEntries(ExtensionPrefs
* prefs
,
101 const std::string
& extension_id
) {
102 prefs
->UpdateExtensionPref(extension_id
, kFileEntries
, NULL
);
105 // Returns all SavedFileEntries for the app.
106 std::vector
<SavedFileEntry
> GetSavedFileEntries(
107 ExtensionPrefs
* prefs
,
108 const std::string
& extension_id
) {
109 std::vector
<SavedFileEntry
> result
;
110 const base::DictionaryValue
* file_entries
= NULL
;
111 if (!prefs
->ReadPrefAsDictionary(extension_id
, kFileEntries
, &file_entries
))
114 for (base::DictionaryValue::Iterator
it(*file_entries
); !it
.IsAtEnd();
116 const base::DictionaryValue
* file_entry
= NULL
;
117 if (!it
.value().GetAsDictionary(&file_entry
))
119 const base::Value
* path_value
;
120 if (!file_entry
->Get(kFileEntryPath
, &path_value
))
122 base::FilePath file_path
;
123 if (!GetValueAsFilePath(*path_value
, &file_path
))
125 bool is_directory
= false;
126 file_entry
->GetBoolean(kFileEntryIsDirectory
, &is_directory
);
127 int sequence_number
= 0;
128 if (!file_entry
->GetInteger(kFileEntrySequenceNumber
, &sequence_number
))
130 if (!sequence_number
)
133 SavedFileEntry(it
.key(), file_path
, is_directory
, sequence_number
));
140 SavedFileEntry::SavedFileEntry() : is_directory(false), sequence_number(0) {}
142 SavedFileEntry::SavedFileEntry(const std::string
& id
,
143 const base::FilePath
& path
,
148 is_directory(is_directory
),
149 sequence_number(sequence_number
) {}
151 class SavedFilesService::SavedFiles
{
153 SavedFiles(Profile
* profile
, const std::string
& extension_id
);
156 void RegisterFileEntry(const std::string
& id
,
157 const base::FilePath
& file_path
,
159 void EnqueueFileEntry(const std::string
& id
);
160 bool IsRegistered(const std::string
& id
) const;
161 const SavedFileEntry
* GetFileEntry(const std::string
& id
) const;
162 std::vector
<SavedFileEntry
> GetAllFileEntries() const;
165 // Compacts sequence numbers if the largest sequence number is
166 // g_max_sequence_number. Outside of testing, it is set to kint32max, so this
167 // will almost never do any real work.
168 void MaybeCompactSequenceNumbers();
170 void LoadSavedFileEntriesFromPreferences();
173 const std::string extension_id_
;
175 // Contains all file entries that have been registered, keyed by ID. Owns
177 base::hash_map
<std::string
, SavedFileEntry
*> registered_file_entries_
;
178 STLValueDeleter
<base::hash_map
<std::string
, SavedFileEntry
*> >
179 registered_file_entries_deleter_
;
181 // The queue of file entries that have been retained, keyed by
182 // sequence_number. Values are a subset of values in registered_file_entries_.
183 // This should be kept in sync with file entries stored in extension prefs.
184 std::map
<int, SavedFileEntry
*> saved_file_lru_
;
186 DISALLOW_COPY_AND_ASSIGN(SavedFiles
);
190 SavedFilesService
* SavedFilesService::Get(Profile
* profile
) {
191 return SavedFilesServiceFactory::GetForProfile(profile
);
194 SavedFilesService::SavedFilesService(Profile
* profile
)
195 : extension_id_to_saved_files_deleter_(&extension_id_to_saved_files_
),
198 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
199 content::NotificationService::AllSources());
201 chrome::NOTIFICATION_APP_TERMINATING
,
202 content::NotificationService::AllSources());
205 SavedFilesService::~SavedFilesService() {}
207 void SavedFilesService::Observe(int type
,
208 const content::NotificationSource
& source
,
209 const content::NotificationDetails
& details
) {
211 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
: {
212 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
213 const Extension
* extension
= host
->extension();
215 ClearQueueIfNoRetainPermission(extension
);
216 Clear(extension
->id());
221 case chrome::NOTIFICATION_APP_TERMINATING
: {
222 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular
223 // as all extension hosts will be destroyed as a result of shutdown.
224 registrar_
.RemoveAll();
230 void SavedFilesService::RegisterFileEntry(const std::string
& extension_id
,
231 const std::string
& id
,
232 const base::FilePath
& file_path
,
234 GetOrInsert(extension_id
)->RegisterFileEntry(id
, file_path
, is_directory
);
237 void SavedFilesService::EnqueueFileEntry(const std::string
& extension_id
,
238 const std::string
& id
) {
239 GetOrInsert(extension_id
)->EnqueueFileEntry(id
);
242 std::vector
<SavedFileEntry
> SavedFilesService::GetAllFileEntries(
243 const std::string
& extension_id
) {
244 SavedFiles
* saved_files
= Get(extension_id
);
246 return saved_files
->GetAllFileEntries();
247 return GetSavedFileEntries(ExtensionPrefs::Get(profile_
), extension_id
);
250 bool SavedFilesService::IsRegistered(const std::string
& extension_id
,
251 const std::string
& id
) {
252 return GetOrInsert(extension_id
)->IsRegistered(id
);
255 const SavedFileEntry
* SavedFilesService::GetFileEntry(
256 const std::string
& extension_id
,
257 const std::string
& id
) {
258 return GetOrInsert(extension_id
)->GetFileEntry(id
);
261 void SavedFilesService::ClearQueueIfNoRetainPermission(
262 const Extension
* extension
) {
263 if (extension
->is_ephemeral() ||
264 !extension
->GetActivePermissions()->HasAPIPermission(
265 APIPermission::kFileSystemRetainEntries
)) {
266 ClearQueue(extension
);
270 void SavedFilesService::ClearQueue(const extensions::Extension
* extension
) {
271 ClearSavedFileEntries(ExtensionPrefs::Get(profile_
), extension
->id());
272 Clear(extension
->id());
275 SavedFilesService::SavedFiles
* SavedFilesService::Get(
276 const std::string
& extension_id
) const {
277 std::map
<std::string
, SavedFiles
*>::const_iterator it
=
278 extension_id_to_saved_files_
.find(extension_id
);
279 if (it
!= extension_id_to_saved_files_
.end())
285 SavedFilesService::SavedFiles
* SavedFilesService::GetOrInsert(
286 const std::string
& extension_id
) {
287 SavedFiles
* saved_files
= Get(extension_id
);
291 saved_files
= new SavedFiles(profile_
, extension_id
);
292 extension_id_to_saved_files_
.insert(
293 std::make_pair(extension_id
, saved_files
));
297 void SavedFilesService::Clear(const std::string
& extension_id
) {
298 std::map
<std::string
, SavedFiles
*>::iterator it
=
299 extension_id_to_saved_files_
.find(extension_id
);
300 if (it
!= extension_id_to_saved_files_
.end()) {
302 extension_id_to_saved_files_
.erase(it
);
306 SavedFilesService::SavedFiles::SavedFiles(Profile
* profile
,
307 const std::string
& extension_id
)
309 extension_id_(extension_id
),
310 registered_file_entries_deleter_(®istered_file_entries_
) {
311 LoadSavedFileEntriesFromPreferences();
314 SavedFilesService::SavedFiles::~SavedFiles() {}
316 void SavedFilesService::SavedFiles::RegisterFileEntry(
317 const std::string
& id
,
318 const base::FilePath
& file_path
,
320 if (ContainsKey(registered_file_entries_
, id
))
323 registered_file_entries_
.insert(
324 std::make_pair(id
, new SavedFileEntry(id
, file_path
, is_directory
, 0)));
327 void SavedFilesService::SavedFiles::EnqueueFileEntry(const std::string
& id
) {
328 base::hash_map
<std::string
, SavedFileEntry
*>::iterator it
=
329 registered_file_entries_
.find(id
);
330 DCHECK(it
!= registered_file_entries_
.end());
332 SavedFileEntry
* file_entry
= it
->second
;
333 int old_sequence_number
= file_entry
->sequence_number
;
334 if (!saved_file_lru_
.empty()) {
335 // Get the sequence number after the last file entry in the LRU.
336 std::map
<int, SavedFileEntry
*>::reverse_iterator it
=
337 saved_file_lru_
.rbegin();
338 if (it
->second
== file_entry
)
341 file_entry
->sequence_number
= it
->first
+ 1;
343 // The first sequence number is 1, as 0 means the entry is not in the LRU.
344 file_entry
->sequence_number
= 1;
346 saved_file_lru_
.insert(
347 std::make_pair(file_entry
->sequence_number
, file_entry
));
348 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
349 if (old_sequence_number
) {
350 saved_file_lru_
.erase(old_sequence_number
);
351 UpdateSavedFileEntry(prefs
, extension_id_
, *file_entry
);
353 AddSavedFileEntry(prefs
, extension_id_
, *file_entry
);
354 if (saved_file_lru_
.size() > g_max_saved_file_entries
) {
355 std::map
<int, SavedFileEntry
*>::iterator it
= saved_file_lru_
.begin();
356 it
->second
->sequence_number
= 0;
357 RemoveSavedFileEntry(prefs
, extension_id_
, it
->second
->id
);
358 saved_file_lru_
.erase(it
);
361 MaybeCompactSequenceNumbers();
364 bool SavedFilesService::SavedFiles::IsRegistered(const std::string
& id
) const {
365 return ContainsKey(registered_file_entries_
, id
);
368 const SavedFileEntry
* SavedFilesService::SavedFiles::GetFileEntry(
369 const std::string
& id
) const {
370 base::hash_map
<std::string
, SavedFileEntry
*>::const_iterator it
=
371 registered_file_entries_
.find(id
);
372 if (it
== registered_file_entries_
.end())
378 std::vector
<SavedFileEntry
> SavedFilesService::SavedFiles::GetAllFileEntries()
380 std::vector
<SavedFileEntry
> result
;
381 for (base::hash_map
<std::string
, SavedFileEntry
*>::const_iterator it
=
382 registered_file_entries_
.begin();
383 it
!= registered_file_entries_
.end();
385 result
.push_back(*it
->second
);
390 void SavedFilesService::SavedFiles::MaybeCompactSequenceNumbers() {
391 DCHECK_GE(g_max_sequence_number
, 0);
392 DCHECK_GE(static_cast<size_t>(g_max_sequence_number
),
393 g_max_saved_file_entries
);
394 std::map
<int, SavedFileEntry
*>::reverse_iterator it
=
395 saved_file_lru_
.rbegin();
396 if (it
== saved_file_lru_
.rend())
399 // Only compact sequence numbers if the last entry's sequence number is the
400 // maximum value. This should almost never be the case.
401 if (it
->first
< g_max_sequence_number
)
404 int sequence_number
= 0;
405 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
406 for (std::map
<int, SavedFileEntry
*>::iterator it
= saved_file_lru_
.begin();
407 it
!= saved_file_lru_
.end();
410 if (it
->second
->sequence_number
== sequence_number
)
413 SavedFileEntry
* file_entry
= it
->second
;
414 file_entry
->sequence_number
= sequence_number
;
415 UpdateSavedFileEntry(prefs
, extension_id_
, *file_entry
);
416 saved_file_lru_
.erase(it
++);
417 // Provide the following element as an insert hint. While optimized
418 // insertion time with the following element as a hint is only supported by
419 // the spec in C++11, the implementations do support this.
420 it
= saved_file_lru_
.insert(
421 it
, std::make_pair(file_entry
->sequence_number
, file_entry
));
425 void SavedFilesService::SavedFiles::LoadSavedFileEntriesFromPreferences() {
426 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
427 std::vector
<SavedFileEntry
> saved_entries
=
428 GetSavedFileEntries(prefs
, extension_id_
);
429 for (std::vector
<SavedFileEntry
>::iterator it
= saved_entries
.begin();
430 it
!= saved_entries
.end();
432 SavedFileEntry
* file_entry
= new SavedFileEntry(*it
);
433 registered_file_entries_
.insert(std::make_pair(file_entry
->id
, file_entry
));
434 saved_file_lru_
.insert(
435 std::make_pair(file_entry
->sequence_number
, file_entry
));
440 void SavedFilesService::SetMaxSequenceNumberForTest(int max_value
) {
441 g_max_sequence_number
= max_value
;
445 void SavedFilesService::ClearMaxSequenceNumberForTest() {
446 g_max_sequence_number
= kMaxSequenceNumber
;
450 void SavedFilesService::SetLruSizeForTest(int size
) {
451 g_max_saved_file_entries
= size
;
455 void SavedFilesService::ClearLruSizeForTest() {
456 g_max_saved_file_entries
= kMaxSavedFileEntries
;