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/extensions/extension_prefs.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/extension_system.h"
18 #include "chrome/common/extensions/permissions/api_permission.h"
19 #include "chrome/common/extensions/permissions/permission_set.h"
20 #include "content/public/browser/notification_service.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 app had write access to a file entry.
40 const char kFileEntryWritable
[] = "writable";
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 DictionaryValue
* file_entries
= update
.Get();
60 file_entries
= update
.Create();
61 DCHECK(!file_entries
->GetDictionaryWithoutPathExpansion(file_entry
.id
, NULL
));
63 DictionaryValue
* file_entry_dict
= new DictionaryValue();
64 file_entry_dict
->Set(kFileEntryPath
, CreateFilePathValue(file_entry
.path
));
65 file_entry_dict
->SetBoolean(kFileEntryWritable
, file_entry
.writable
);
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 DictionaryValue
* file_entries
= update
.Get();
79 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 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 DictionaryValue
* file_entries
= NULL
;
111 if (!prefs
->ReadPrefAsDictionary(extension_id
, kFileEntries
, &file_entries
))
114 for (DictionaryValue::Iterator
it(*file_entries
); !it
.IsAtEnd();
116 const 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 writable
= false;
126 if (!file_entry
->GetBoolean(kFileEntryWritable
, &writable
))
128 int sequence_number
= 0;
129 if (!file_entry
->GetInteger(kFileEntrySequenceNumber
, &sequence_number
))
131 if (!sequence_number
)
134 SavedFileEntry(it
.key(), file_path
, writable
, sequence_number
));
141 SavedFileEntry::SavedFileEntry() : writable(false), sequence_number(0) {}
143 SavedFileEntry::SavedFileEntry(const std::string
& id
,
144 const base::FilePath
& path
,
150 sequence_number(sequence_number
) {}
152 class SavedFilesService::SavedFiles
{
154 SavedFiles(Profile
* profile
, const std::string
& extension_id
);
157 void RegisterFileEntry(const std::string
& id
,
158 const base::FilePath
& file_path
,
160 void EnqueueFileEntry(const std::string
& id
);
161 bool IsRegistered(const std::string
& id
) const;
162 const SavedFileEntry
* GetFileEntry(const std::string
& id
) const;
163 std::vector
<SavedFileEntry
> GetAllFileEntries() const;
166 // Compacts sequence numbers if the largest sequence number is
167 // g_max_sequence_number. Outside of testing, it is set to kint32max, so this
168 // will almost never do any real work.
169 void MaybeCompactSequenceNumbers();
171 void LoadSavedFileEntriesFromPreferences();
174 const std::string extension_id_
;
176 // Contains all file entries that have been registered, keyed by ID. Owns
178 base::hash_map
<std::string
, SavedFileEntry
*> registered_file_entries_
;
179 STLValueDeleter
<base::hash_map
<std::string
, SavedFileEntry
*> >
180 registered_file_entries_deleter_
;
182 // The queue of file entries that have been retained, keyed by
183 // sequence_number. Values are a subset of values in registered_file_entries_.
184 // This should be kept in sync with file entries stored in extension prefs.
185 std::map
<int, SavedFileEntry
*> saved_file_lru_
;
187 DISALLOW_COPY_AND_ASSIGN(SavedFiles
);
191 SavedFilesService
* SavedFilesService::Get(Profile
* profile
) {
192 return SavedFilesServiceFactory::GetForProfile(profile
);
195 SavedFilesService::SavedFilesService(Profile
* profile
)
196 : extension_id_to_saved_files_deleter_(&extension_id_to_saved_files_
),
199 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
200 content::NotificationService::AllSources());
202 chrome::NOTIFICATION_APP_TERMINATING
,
203 content::NotificationService::AllSources());
206 SavedFilesService::~SavedFilesService() {}
208 void SavedFilesService::Observe(int type
,
209 const content::NotificationSource
& source
,
210 const content::NotificationDetails
& details
) {
212 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
: {
213 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
214 const Extension
* extension
= host
->extension();
216 ClearQueueIfNoRetainPermission(extension
);
217 Clear(extension
->id());
222 case chrome::NOTIFICATION_APP_TERMINATING
: {
223 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular
224 // as all extension hosts will be destroyed as a result of shutdown.
225 registrar_
.RemoveAll();
231 void SavedFilesService::RegisterFileEntry(const std::string
& extension_id
,
232 const std::string
& id
,
233 const base::FilePath
& file_path
,
235 GetOrInsert(extension_id
)->RegisterFileEntry(id
, file_path
, writable
);
238 void SavedFilesService::EnqueueFileEntry(const std::string
& extension_id
,
239 const std::string
& id
) {
240 GetOrInsert(extension_id
)->EnqueueFileEntry(id
);
243 std::vector
<SavedFileEntry
> SavedFilesService::GetAllFileEntries(
244 const std::string
& extension_id
) {
245 SavedFiles
* saved_files
= Get(extension_id
);
247 return saved_files
->GetAllFileEntries();
248 return GetSavedFileEntries(ExtensionPrefs::Get(profile_
), extension_id
);
251 bool SavedFilesService::IsRegistered(const std::string
& extension_id
,
252 const std::string
& id
) {
253 return GetOrInsert(extension_id
)->IsRegistered(id
);
256 const SavedFileEntry
* SavedFilesService::GetFileEntry(
257 const std::string
& extension_id
,
258 const std::string
& id
) {
259 return GetOrInsert(extension_id
)->GetFileEntry(id
);
262 void SavedFilesService::ClearQueueIfNoRetainPermission(
263 const Extension
* extension
) {
264 if (!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
, writable
, 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
;