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/extensions/extension_host.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "content/public/browser/notification_service.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
->GetActivePermissions()->HasAPIPermission(
264 APIPermission::kFileSystemRetainEntries
)) {
265 ClearQueue(extension
);
269 void SavedFilesService::ClearQueue(const extensions::Extension
* extension
) {
270 ClearSavedFileEntries(ExtensionPrefs::Get(profile_
), extension
->id());
271 Clear(extension
->id());
274 SavedFilesService::SavedFiles
* SavedFilesService::Get(
275 const std::string
& extension_id
) const {
276 std::map
<std::string
, SavedFiles
*>::const_iterator it
=
277 extension_id_to_saved_files_
.find(extension_id
);
278 if (it
!= extension_id_to_saved_files_
.end())
284 SavedFilesService::SavedFiles
* SavedFilesService::GetOrInsert(
285 const std::string
& extension_id
) {
286 SavedFiles
* saved_files
= Get(extension_id
);
290 saved_files
= new SavedFiles(profile_
, extension_id
);
291 extension_id_to_saved_files_
.insert(
292 std::make_pair(extension_id
, saved_files
));
296 void SavedFilesService::Clear(const std::string
& extension_id
) {
297 std::map
<std::string
, SavedFiles
*>::iterator it
=
298 extension_id_to_saved_files_
.find(extension_id
);
299 if (it
!= extension_id_to_saved_files_
.end()) {
301 extension_id_to_saved_files_
.erase(it
);
305 SavedFilesService::SavedFiles::SavedFiles(Profile
* profile
,
306 const std::string
& extension_id
)
308 extension_id_(extension_id
),
309 registered_file_entries_deleter_(®istered_file_entries_
) {
310 LoadSavedFileEntriesFromPreferences();
313 SavedFilesService::SavedFiles::~SavedFiles() {}
315 void SavedFilesService::SavedFiles::RegisterFileEntry(
316 const std::string
& id
,
317 const base::FilePath
& file_path
,
319 if (ContainsKey(registered_file_entries_
, id
))
322 registered_file_entries_
.insert(
323 std::make_pair(id
, new SavedFileEntry(id
, file_path
, is_directory
, 0)));
326 void SavedFilesService::SavedFiles::EnqueueFileEntry(const std::string
& id
) {
327 base::hash_map
<std::string
, SavedFileEntry
*>::iterator it
=
328 registered_file_entries_
.find(id
);
329 DCHECK(it
!= registered_file_entries_
.end());
331 SavedFileEntry
* file_entry
= it
->second
;
332 int old_sequence_number
= file_entry
->sequence_number
;
333 if (!saved_file_lru_
.empty()) {
334 // Get the sequence number after the last file entry in the LRU.
335 std::map
<int, SavedFileEntry
*>::reverse_iterator it
=
336 saved_file_lru_
.rbegin();
337 if (it
->second
== file_entry
)
340 file_entry
->sequence_number
= it
->first
+ 1;
342 // The first sequence number is 1, as 0 means the entry is not in the LRU.
343 file_entry
->sequence_number
= 1;
345 saved_file_lru_
.insert(
346 std::make_pair(file_entry
->sequence_number
, file_entry
));
347 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
348 if (old_sequence_number
) {
349 saved_file_lru_
.erase(old_sequence_number
);
350 UpdateSavedFileEntry(prefs
, extension_id_
, *file_entry
);
352 AddSavedFileEntry(prefs
, extension_id_
, *file_entry
);
353 if (saved_file_lru_
.size() > g_max_saved_file_entries
) {
354 std::map
<int, SavedFileEntry
*>::iterator it
= saved_file_lru_
.begin();
355 it
->second
->sequence_number
= 0;
356 RemoveSavedFileEntry(prefs
, extension_id_
, it
->second
->id
);
357 saved_file_lru_
.erase(it
);
360 MaybeCompactSequenceNumbers();
363 bool SavedFilesService::SavedFiles::IsRegistered(const std::string
& id
) const {
364 return ContainsKey(registered_file_entries_
, id
);
367 const SavedFileEntry
* SavedFilesService::SavedFiles::GetFileEntry(
368 const std::string
& id
) const {
369 base::hash_map
<std::string
, SavedFileEntry
*>::const_iterator it
=
370 registered_file_entries_
.find(id
);
371 if (it
== registered_file_entries_
.end())
377 std::vector
<SavedFileEntry
> SavedFilesService::SavedFiles::GetAllFileEntries()
379 std::vector
<SavedFileEntry
> result
;
380 for (base::hash_map
<std::string
, SavedFileEntry
*>::const_iterator it
=
381 registered_file_entries_
.begin();
382 it
!= registered_file_entries_
.end();
384 result
.push_back(*it
->second
);
389 void SavedFilesService::SavedFiles::MaybeCompactSequenceNumbers() {
390 DCHECK_GE(g_max_sequence_number
, 0);
391 DCHECK_GE(static_cast<size_t>(g_max_sequence_number
),
392 g_max_saved_file_entries
);
393 std::map
<int, SavedFileEntry
*>::reverse_iterator it
=
394 saved_file_lru_
.rbegin();
395 if (it
== saved_file_lru_
.rend())
398 // Only compact sequence numbers if the last entry's sequence number is the
399 // maximum value. This should almost never be the case.
400 if (it
->first
< g_max_sequence_number
)
403 int sequence_number
= 0;
404 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
405 for (std::map
<int, SavedFileEntry
*>::iterator it
= saved_file_lru_
.begin();
406 it
!= saved_file_lru_
.end();
409 if (it
->second
->sequence_number
== sequence_number
)
412 SavedFileEntry
* file_entry
= it
->second
;
413 file_entry
->sequence_number
= sequence_number
;
414 UpdateSavedFileEntry(prefs
, extension_id_
, *file_entry
);
415 saved_file_lru_
.erase(it
++);
416 // Provide the following element as an insert hint. While optimized
417 // insertion time with the following element as a hint is only supported by
418 // the spec in C++11, the implementations do support this.
419 it
= saved_file_lru_
.insert(
420 it
, std::make_pair(file_entry
->sequence_number
, file_entry
));
424 void SavedFilesService::SavedFiles::LoadSavedFileEntriesFromPreferences() {
425 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
426 std::vector
<SavedFileEntry
> saved_entries
=
427 GetSavedFileEntries(prefs
, extension_id_
);
428 for (std::vector
<SavedFileEntry
>::iterator it
= saved_entries
.begin();
429 it
!= saved_entries
.end();
431 SavedFileEntry
* file_entry
= new SavedFileEntry(*it
);
432 registered_file_entries_
.insert(std::make_pair(file_entry
->id
, file_entry
));
433 saved_file_lru_
.insert(
434 std::make_pair(file_entry
->sequence_number
, file_entry
));
439 void SavedFilesService::SetMaxSequenceNumberForTest(int max_value
) {
440 g_max_sequence_number
= max_value
;
444 void SavedFilesService::ClearMaxSequenceNumberForTest() {
445 g_max_sequence_number
= kMaxSequenceNumber
;
449 void SavedFilesService::SetLruSizeForTest(int size
) {
450 g_max_saved_file_entries
= size
;
454 void SavedFilesService::ClearLruSizeForTest() {
455 g_max_saved_file_entries
= kMaxSavedFileEntries
;