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/common/permissions/api_permission.h"
19 #include "extensions/common/permissions/permission_set.h"
23 using extensions::APIPermission
;
24 using extensions::Extension
;
25 using extensions::ExtensionHost
;
26 using extensions::ExtensionPrefs
;
32 // The file entries that the app has permission to access.
33 const char kFileEntries
[] = "file_entries";
35 // The path to a file entry that the app had permission to access.
36 const char kFileEntryPath
[] = "path";
38 // Whether or not the the entry refers to a directory.
39 const char kFileEntryIsDirectory
[] = "is_directory";
41 // The sequence number in the LRU of the file entry.
42 const char kFileEntrySequenceNumber
[] = "sequence_number";
44 const size_t kMaxSavedFileEntries
= 500;
45 const int kMaxSequenceNumber
= kint32max
;
47 // These might be different to the constant values in tests.
48 size_t g_max_saved_file_entries
= kMaxSavedFileEntries
;
49 int g_max_sequence_number
= kMaxSequenceNumber
;
51 // Persists a SavedFileEntry in ExtensionPrefs.
52 void AddSavedFileEntry(ExtensionPrefs
* prefs
,
53 const std::string
& extension_id
,
54 const SavedFileEntry
& file_entry
) {
55 ExtensionPrefs::ScopedDictionaryUpdate
update(
56 prefs
, extension_id
, kFileEntries
);
57 base::DictionaryValue
* file_entries
= update
.Get();
59 file_entries
= update
.Create();
60 DCHECK(!file_entries
->GetDictionaryWithoutPathExpansion(file_entry
.id
, NULL
));
62 base::DictionaryValue
* file_entry_dict
= new base::DictionaryValue();
63 file_entry_dict
->Set(kFileEntryPath
, CreateFilePathValue(file_entry
.path
));
64 file_entry_dict
->SetBoolean(kFileEntryIsDirectory
, file_entry
.is_directory
);
65 file_entry_dict
->SetInteger(kFileEntrySequenceNumber
,
66 file_entry
.sequence_number
);
67 file_entries
->SetWithoutPathExpansion(file_entry
.id
, file_entry_dict
);
70 // Updates the sequence_number of a SavedFileEntry persisted in ExtensionPrefs.
71 void UpdateSavedFileEntry(ExtensionPrefs
* prefs
,
72 const std::string
& extension_id
,
73 const SavedFileEntry
& file_entry
) {
74 ExtensionPrefs::ScopedDictionaryUpdate
update(
75 prefs
, extension_id
, kFileEntries
);
76 base::DictionaryValue
* file_entries
= update
.Get();
78 base::DictionaryValue
* file_entry_dict
= NULL
;
79 file_entries
->GetDictionaryWithoutPathExpansion(file_entry
.id
,
81 DCHECK(file_entry_dict
);
82 file_entry_dict
->SetInteger(kFileEntrySequenceNumber
,
83 file_entry
.sequence_number
);
86 // Removes a SavedFileEntry from ExtensionPrefs.
87 void RemoveSavedFileEntry(ExtensionPrefs
* prefs
,
88 const std::string
& extension_id
,
89 const std::string
& file_entry_id
) {
90 ExtensionPrefs::ScopedDictionaryUpdate
update(
91 prefs
, extension_id
, kFileEntries
);
92 base::DictionaryValue
* file_entries
= update
.Get();
94 file_entries
= update
.Create();
95 file_entries
->RemoveWithoutPathExpansion(file_entry_id
, NULL
);
98 // Clears all SavedFileEntry for the app from ExtensionPrefs.
99 void ClearSavedFileEntries(ExtensionPrefs
* prefs
,
100 const std::string
& extension_id
) {
101 prefs
->UpdateExtensionPref(extension_id
, kFileEntries
, NULL
);
104 // Returns all SavedFileEntries for the app.
105 std::vector
<SavedFileEntry
> GetSavedFileEntries(
106 ExtensionPrefs
* prefs
,
107 const std::string
& extension_id
) {
108 std::vector
<SavedFileEntry
> result
;
109 const base::DictionaryValue
* file_entries
= NULL
;
110 if (!prefs
->ReadPrefAsDictionary(extension_id
, kFileEntries
, &file_entries
))
113 for (base::DictionaryValue::Iterator
it(*file_entries
); !it
.IsAtEnd();
115 const base::DictionaryValue
* file_entry
= NULL
;
116 if (!it
.value().GetAsDictionary(&file_entry
))
118 const base::Value
* path_value
;
119 if (!file_entry
->Get(kFileEntryPath
, &path_value
))
121 base::FilePath file_path
;
122 if (!GetValueAsFilePath(*path_value
, &file_path
))
124 bool is_directory
= false;
125 file_entry
->GetBoolean(kFileEntryIsDirectory
, &is_directory
);
126 int sequence_number
= 0;
127 if (!file_entry
->GetInteger(kFileEntrySequenceNumber
, &sequence_number
))
129 if (!sequence_number
)
132 SavedFileEntry(it
.key(), file_path
, is_directory
, sequence_number
));
139 SavedFileEntry::SavedFileEntry() : is_directory(false), sequence_number(0) {}
141 SavedFileEntry::SavedFileEntry(const std::string
& id
,
142 const base::FilePath
& path
,
147 is_directory(is_directory
),
148 sequence_number(sequence_number
) {}
150 class SavedFilesService::SavedFiles
{
152 SavedFiles(Profile
* profile
, const std::string
& extension_id
);
155 void RegisterFileEntry(const std::string
& id
,
156 const base::FilePath
& file_path
,
158 void EnqueueFileEntry(const std::string
& id
);
159 bool IsRegistered(const std::string
& id
) const;
160 const SavedFileEntry
* GetFileEntry(const std::string
& id
) const;
161 std::vector
<SavedFileEntry
> GetAllFileEntries() const;
164 // Compacts sequence numbers if the largest sequence number is
165 // g_max_sequence_number. Outside of testing, it is set to kint32max, so this
166 // will almost never do any real work.
167 void MaybeCompactSequenceNumbers();
169 void LoadSavedFileEntriesFromPreferences();
172 const std::string extension_id_
;
174 // Contains all file entries that have been registered, keyed by ID. Owns
176 base::hash_map
<std::string
, SavedFileEntry
*> registered_file_entries_
;
177 STLValueDeleter
<base::hash_map
<std::string
, SavedFileEntry
*> >
178 registered_file_entries_deleter_
;
180 // The queue of file entries that have been retained, keyed by
181 // sequence_number. Values are a subset of values in registered_file_entries_.
182 // This should be kept in sync with file entries stored in extension prefs.
183 std::map
<int, SavedFileEntry
*> saved_file_lru_
;
185 DISALLOW_COPY_AND_ASSIGN(SavedFiles
);
189 SavedFilesService
* SavedFilesService::Get(Profile
* profile
) {
190 return SavedFilesServiceFactory::GetForProfile(profile
);
193 SavedFilesService::SavedFilesService(Profile
* profile
)
194 : extension_id_to_saved_files_deleter_(&extension_id_to_saved_files_
),
197 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
198 content::NotificationService::AllSources());
200 chrome::NOTIFICATION_APP_TERMINATING
,
201 content::NotificationService::AllSources());
204 SavedFilesService::~SavedFilesService() {}
206 void SavedFilesService::Observe(int type
,
207 const content::NotificationSource
& source
,
208 const content::NotificationDetails
& details
) {
210 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
: {
211 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
212 const Extension
* extension
= host
->extension();
214 ClearQueueIfNoRetainPermission(extension
);
215 Clear(extension
->id());
220 case chrome::NOTIFICATION_APP_TERMINATING
: {
221 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular
222 // as all extension hosts will be destroyed as a result of shutdown.
223 registrar_
.RemoveAll();
229 void SavedFilesService::RegisterFileEntry(const std::string
& extension_id
,
230 const std::string
& id
,
231 const base::FilePath
& file_path
,
233 GetOrInsert(extension_id
)->RegisterFileEntry(id
, file_path
, is_directory
);
236 void SavedFilesService::EnqueueFileEntry(const std::string
& extension_id
,
237 const std::string
& id
) {
238 GetOrInsert(extension_id
)->EnqueueFileEntry(id
);
241 std::vector
<SavedFileEntry
> SavedFilesService::GetAllFileEntries(
242 const std::string
& extension_id
) {
243 SavedFiles
* saved_files
= Get(extension_id
);
245 return saved_files
->GetAllFileEntries();
246 return GetSavedFileEntries(ExtensionPrefs::Get(profile_
), extension_id
);
249 bool SavedFilesService::IsRegistered(const std::string
& extension_id
,
250 const std::string
& id
) {
251 return GetOrInsert(extension_id
)->IsRegistered(id
);
254 const SavedFileEntry
* SavedFilesService::GetFileEntry(
255 const std::string
& extension_id
,
256 const std::string
& id
) {
257 return GetOrInsert(extension_id
)->GetFileEntry(id
);
260 void SavedFilesService::ClearQueueIfNoRetainPermission(
261 const Extension
* extension
) {
262 if (!extension
->GetActivePermissions()->HasAPIPermission(
263 APIPermission::kFileSystemRetainEntries
)) {
264 ClearQueue(extension
);
268 void SavedFilesService::ClearQueue(const extensions::Extension
* extension
) {
269 ClearSavedFileEntries(ExtensionPrefs::Get(profile_
), extension
->id());
270 Clear(extension
->id());
273 SavedFilesService::SavedFiles
* SavedFilesService::Get(
274 const std::string
& extension_id
) const {
275 std::map
<std::string
, SavedFiles
*>::const_iterator it
=
276 extension_id_to_saved_files_
.find(extension_id
);
277 if (it
!= extension_id_to_saved_files_
.end())
283 SavedFilesService::SavedFiles
* SavedFilesService::GetOrInsert(
284 const std::string
& extension_id
) {
285 SavedFiles
* saved_files
= Get(extension_id
);
289 saved_files
= new SavedFiles(profile_
, extension_id
);
290 extension_id_to_saved_files_
.insert(
291 std::make_pair(extension_id
, saved_files
));
295 void SavedFilesService::Clear(const std::string
& extension_id
) {
296 std::map
<std::string
, SavedFiles
*>::iterator it
=
297 extension_id_to_saved_files_
.find(extension_id
);
298 if (it
!= extension_id_to_saved_files_
.end()) {
300 extension_id_to_saved_files_
.erase(it
);
304 SavedFilesService::SavedFiles::SavedFiles(Profile
* profile
,
305 const std::string
& extension_id
)
307 extension_id_(extension_id
),
308 registered_file_entries_deleter_(®istered_file_entries_
) {
309 LoadSavedFileEntriesFromPreferences();
312 SavedFilesService::SavedFiles::~SavedFiles() {}
314 void SavedFilesService::SavedFiles::RegisterFileEntry(
315 const std::string
& id
,
316 const base::FilePath
& file_path
,
318 if (ContainsKey(registered_file_entries_
, id
))
321 registered_file_entries_
.insert(
322 std::make_pair(id
, new SavedFileEntry(id
, file_path
, is_directory
, 0)));
325 void SavedFilesService::SavedFiles::EnqueueFileEntry(const std::string
& id
) {
326 base::hash_map
<std::string
, SavedFileEntry
*>::iterator it
=
327 registered_file_entries_
.find(id
);
328 DCHECK(it
!= registered_file_entries_
.end());
330 SavedFileEntry
* file_entry
= it
->second
;
331 int old_sequence_number
= file_entry
->sequence_number
;
332 if (!saved_file_lru_
.empty()) {
333 // Get the sequence number after the last file entry in the LRU.
334 std::map
<int, SavedFileEntry
*>::reverse_iterator it
=
335 saved_file_lru_
.rbegin();
336 if (it
->second
== file_entry
)
339 file_entry
->sequence_number
= it
->first
+ 1;
341 // The first sequence number is 1, as 0 means the entry is not in the LRU.
342 file_entry
->sequence_number
= 1;
344 saved_file_lru_
.insert(
345 std::make_pair(file_entry
->sequence_number
, file_entry
));
346 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
347 if (old_sequence_number
) {
348 saved_file_lru_
.erase(old_sequence_number
);
349 UpdateSavedFileEntry(prefs
, extension_id_
, *file_entry
);
351 AddSavedFileEntry(prefs
, extension_id_
, *file_entry
);
352 if (saved_file_lru_
.size() > g_max_saved_file_entries
) {
353 std::map
<int, SavedFileEntry
*>::iterator it
= saved_file_lru_
.begin();
354 it
->second
->sequence_number
= 0;
355 RemoveSavedFileEntry(prefs
, extension_id_
, it
->second
->id
);
356 saved_file_lru_
.erase(it
);
359 MaybeCompactSequenceNumbers();
362 bool SavedFilesService::SavedFiles::IsRegistered(const std::string
& id
) const {
363 return ContainsKey(registered_file_entries_
, id
);
366 const SavedFileEntry
* SavedFilesService::SavedFiles::GetFileEntry(
367 const std::string
& id
) const {
368 base::hash_map
<std::string
, SavedFileEntry
*>::const_iterator it
=
369 registered_file_entries_
.find(id
);
370 if (it
== registered_file_entries_
.end())
376 std::vector
<SavedFileEntry
> SavedFilesService::SavedFiles::GetAllFileEntries()
378 std::vector
<SavedFileEntry
> result
;
379 for (base::hash_map
<std::string
, SavedFileEntry
*>::const_iterator it
=
380 registered_file_entries_
.begin();
381 it
!= registered_file_entries_
.end();
383 result
.push_back(*it
->second
);
388 void SavedFilesService::SavedFiles::MaybeCompactSequenceNumbers() {
389 DCHECK_GE(g_max_sequence_number
, 0);
390 DCHECK_GE(static_cast<size_t>(g_max_sequence_number
),
391 g_max_saved_file_entries
);
392 std::map
<int, SavedFileEntry
*>::reverse_iterator it
=
393 saved_file_lru_
.rbegin();
394 if (it
== saved_file_lru_
.rend())
397 // Only compact sequence numbers if the last entry's sequence number is the
398 // maximum value. This should almost never be the case.
399 if (it
->first
< g_max_sequence_number
)
402 int sequence_number
= 0;
403 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
404 for (std::map
<int, SavedFileEntry
*>::iterator it
= saved_file_lru_
.begin();
405 it
!= saved_file_lru_
.end();
408 if (it
->second
->sequence_number
== sequence_number
)
411 SavedFileEntry
* file_entry
= it
->second
;
412 file_entry
->sequence_number
= sequence_number
;
413 UpdateSavedFileEntry(prefs
, extension_id_
, *file_entry
);
414 saved_file_lru_
.erase(it
++);
415 // Provide the following element as an insert hint. While optimized
416 // insertion time with the following element as a hint is only supported by
417 // the spec in C++11, the implementations do support this.
418 it
= saved_file_lru_
.insert(
419 it
, std::make_pair(file_entry
->sequence_number
, file_entry
));
423 void SavedFilesService::SavedFiles::LoadSavedFileEntriesFromPreferences() {
424 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile_
);
425 std::vector
<SavedFileEntry
> saved_entries
=
426 GetSavedFileEntries(prefs
, extension_id_
);
427 for (std::vector
<SavedFileEntry
>::iterator it
= saved_entries
.begin();
428 it
!= saved_entries
.end();
430 SavedFileEntry
* file_entry
= new SavedFileEntry(*it
);
431 registered_file_entries_
.insert(std::make_pair(file_entry
->id
, file_entry
));
432 saved_file_lru_
.insert(
433 std::make_pair(file_entry
->sequence_number
, file_entry
));
438 void SavedFilesService::SetMaxSequenceNumberForTest(int max_value
) {
439 g_max_sequence_number
= max_value
;
443 void SavedFilesService::ClearMaxSequenceNumberForTest() {
444 g_max_sequence_number
= kMaxSequenceNumber
;
448 void SavedFilesService::SetLruSizeForTest(int size
) {
449 g_max_saved_file_entries
= size
;
453 void SavedFilesService::ClearLruSizeForTest() {
454 g_max_saved_file_entries
= kMaxSavedFileEntries
;