• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "apps/saved_files_service.h"
6 
7 #include <algorithm>
8 
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"
23 
24 namespace apps {
25 
26 using extensions::APIPermission;
27 using extensions::Extension;
28 using extensions::ExtensionHost;
29 using extensions::ExtensionPrefs;
30 
31 namespace {
32 
33 // Preference keys
34 
35 // The file entries that the app has permission to access.
36 const char kFileEntries[] = "file_entries";
37 
38 // The path to a file entry that the app had permission to access.
39 const char kFileEntryPath[] = "path";
40 
41 // Whether or not the the entry refers to a directory.
42 const char kFileEntryIsDirectory[] = "is_directory";
43 
44 // The sequence number in the LRU of the file entry.
45 const char kFileEntrySequenceNumber[] = "sequence_number";
46 
47 const size_t kMaxSavedFileEntries = 500;
48 const int kMaxSequenceNumber = kint32max;
49 
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;
53 
54 // Persists a SavedFileEntry in ExtensionPrefs.
AddSavedFileEntry(ExtensionPrefs * prefs,const std::string & extension_id,const SavedFileEntry & file_entry)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();
61   if (!file_entries)
62     file_entries = update.Create();
63   DCHECK(!file_entries->GetDictionaryWithoutPathExpansion(file_entry.id, NULL));
64 
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);
71 }
72 
73 // Updates the sequence_number of a SavedFileEntry persisted in ExtensionPrefs.
UpdateSavedFileEntry(ExtensionPrefs * prefs,const std::string & extension_id,const SavedFileEntry & file_entry)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();
80   DCHECK(file_entries);
81   base::DictionaryValue* file_entry_dict = NULL;
82   file_entries->GetDictionaryWithoutPathExpansion(file_entry.id,
83                                                   &file_entry_dict);
84   DCHECK(file_entry_dict);
85   file_entry_dict->SetInteger(kFileEntrySequenceNumber,
86                               file_entry.sequence_number);
87 }
88 
89 // Removes a SavedFileEntry from ExtensionPrefs.
RemoveSavedFileEntry(ExtensionPrefs * prefs,const std::string & extension_id,const std::string & file_entry_id)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();
96   if (!file_entries)
97     file_entries = update.Create();
98   file_entries->RemoveWithoutPathExpansion(file_entry_id, NULL);
99 }
100 
101 // Clears all SavedFileEntry for the app from ExtensionPrefs.
ClearSavedFileEntries(ExtensionPrefs * prefs,const std::string & extension_id)102 void ClearSavedFileEntries(ExtensionPrefs* prefs,
103                            const std::string& extension_id) {
104   prefs->UpdateExtensionPref(extension_id, kFileEntries, NULL);
105 }
106 
107 // Returns all SavedFileEntries for the app.
GetSavedFileEntries(ExtensionPrefs * prefs,const std::string & extension_id)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))
114     return result;
115 
116   for (base::DictionaryValue::Iterator it(*file_entries); !it.IsAtEnd();
117        it.Advance()) {
118     const base::DictionaryValue* file_entry = NULL;
119     if (!it.value().GetAsDictionary(&file_entry))
120       continue;
121     const base::Value* path_value;
122     if (!file_entry->Get(kFileEntryPath, &path_value))
123       continue;
124     base::FilePath file_path;
125     if (!GetValueAsFilePath(*path_value, &file_path))
126       continue;
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))
131       continue;
132     if (!sequence_number)
133       continue;
134     result.push_back(
135         SavedFileEntry(it.key(), file_path, is_directory, sequence_number));
136   }
137   return result;
138 }
139 
140 }  // namespace
141 
SavedFileEntry()142 SavedFileEntry::SavedFileEntry() : is_directory(false), sequence_number(0) {}
143 
SavedFileEntry(const std::string & id,const base::FilePath & path,bool is_directory,int sequence_number)144 SavedFileEntry::SavedFileEntry(const std::string& id,
145                                const base::FilePath& path,
146                                bool is_directory,
147                                int sequence_number)
148     : id(id),
149       path(path),
150       is_directory(is_directory),
151       sequence_number(sequence_number) {}
152 
153 class SavedFilesService::SavedFiles {
154  public:
155   SavedFiles(Profile* profile, const std::string& extension_id);
156   ~SavedFiles();
157 
158   void RegisterFileEntry(const std::string& id,
159                          const base::FilePath& file_path,
160                          bool is_directory);
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;
165 
166  private:
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();
171 
172   void LoadSavedFileEntriesFromPreferences();
173 
174   Profile* profile_;
175   const std::string extension_id_;
176 
177   // Contains all file entries that have been registered, keyed by ID. Owns
178   // values.
179   base::hash_map<std::string, SavedFileEntry*> registered_file_entries_;
180   STLValueDeleter<base::hash_map<std::string, SavedFileEntry*> >
181       registered_file_entries_deleter_;
182 
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_;
187 
188   DISALLOW_COPY_AND_ASSIGN(SavedFiles);
189 };
190 
191 // static
Get(Profile * profile)192 SavedFilesService* SavedFilesService::Get(Profile* profile) {
193   return SavedFilesServiceFactory::GetForProfile(profile);
194 }
195 
SavedFilesService(Profile * profile)196 SavedFilesService::SavedFilesService(Profile* profile)
197     : extension_id_to_saved_files_deleter_(&extension_id_to_saved_files_),
198       profile_(profile) {
199   registrar_.Add(this,
200                  chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
201                  content::NotificationService::AllSources());
202   registrar_.Add(this,
203                  chrome::NOTIFICATION_APP_TERMINATING,
204                  content::NotificationService::AllSources());
205 }
206 
~SavedFilesService()207 SavedFilesService::~SavedFilesService() {}
208 
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)209 void SavedFilesService::Observe(int type,
210                                 const content::NotificationSource& source,
211                                 const content::NotificationDetails& details) {
212   switch (type) {
213     case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
214       ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
215       const Extension* extension = host->extension();
216       if (extension) {
217         ClearQueueIfNoRetainPermission(extension);
218         Clear(extension->id());
219       }
220       break;
221     }
222 
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();
227       break;
228     }
229   }
230 }
231 
RegisterFileEntry(const std::string & extension_id,const std::string & id,const base::FilePath & file_path,bool is_directory)232 void SavedFilesService::RegisterFileEntry(const std::string& extension_id,
233                                           const std::string& id,
234                                           const base::FilePath& file_path,
235                                           bool is_directory) {
236   GetOrInsert(extension_id)->RegisterFileEntry(id, file_path, is_directory);
237 }
238 
EnqueueFileEntry(const std::string & extension_id,const std::string & id)239 void SavedFilesService::EnqueueFileEntry(const std::string& extension_id,
240                                          const std::string& id) {
241   GetOrInsert(extension_id)->EnqueueFileEntry(id);
242 }
243 
GetAllFileEntries(const std::string & extension_id)244 std::vector<SavedFileEntry> SavedFilesService::GetAllFileEntries(
245     const std::string& extension_id) {
246   SavedFiles* saved_files = Get(extension_id);
247   if (saved_files)
248     return saved_files->GetAllFileEntries();
249   return GetSavedFileEntries(ExtensionPrefs::Get(profile_), extension_id);
250 }
251 
IsRegistered(const std::string & extension_id,const std::string & id)252 bool SavedFilesService::IsRegistered(const std::string& extension_id,
253                                      const std::string& id) {
254   return GetOrInsert(extension_id)->IsRegistered(id);
255 }
256 
GetFileEntry(const std::string & extension_id,const std::string & id)257 const SavedFileEntry* SavedFilesService::GetFileEntry(
258     const std::string& extension_id,
259     const std::string& id) {
260   return GetOrInsert(extension_id)->GetFileEntry(id);
261 }
262 
ClearQueueIfNoRetainPermission(const Extension * extension)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);
269   }
270 }
271 
ClearQueue(const extensions::Extension * extension)272 void SavedFilesService::ClearQueue(const extensions::Extension* extension) {
273   ClearSavedFileEntries(ExtensionPrefs::Get(profile_), extension->id());
274   Clear(extension->id());
275 }
276 
Get(const std::string & extension_id) const277 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())
282     return it->second;
283 
284   return NULL;
285 }
286 
GetOrInsert(const std::string & extension_id)287 SavedFilesService::SavedFiles* SavedFilesService::GetOrInsert(
288     const std::string& extension_id) {
289   SavedFiles* saved_files = Get(extension_id);
290   if (saved_files)
291     return saved_files;
292 
293   saved_files = new SavedFiles(profile_, extension_id);
294   extension_id_to_saved_files_.insert(
295       std::make_pair(extension_id, saved_files));
296   return saved_files;
297 }
298 
Clear(const std::string & extension_id)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()) {
303     delete it->second;
304     extension_id_to_saved_files_.erase(it);
305   }
306 }
307 
SavedFiles(Profile * profile,const std::string & extension_id)308 SavedFilesService::SavedFiles::SavedFiles(Profile* profile,
309                                           const std::string& extension_id)
310     : profile_(profile),
311       extension_id_(extension_id),
312       registered_file_entries_deleter_(&registered_file_entries_) {
313   LoadSavedFileEntriesFromPreferences();
314 }
315 
~SavedFiles()316 SavedFilesService::SavedFiles::~SavedFiles() {}
317 
RegisterFileEntry(const std::string & id,const base::FilePath & file_path,bool is_directory)318 void SavedFilesService::SavedFiles::RegisterFileEntry(
319     const std::string& id,
320     const base::FilePath& file_path,
321     bool is_directory) {
322   if (ContainsKey(registered_file_entries_, id))
323     return;
324 
325   registered_file_entries_.insert(
326       std::make_pair(id, new SavedFileEntry(id, file_path, is_directory, 0)));
327 }
328 
EnqueueFileEntry(const std::string & id)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());
333 
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)
341       return;
342 
343     file_entry->sequence_number = it->first + 1;
344   } else {
345     // The first sequence number is 1, as 0 means the entry is not in the LRU.
346     file_entry->sequence_number = 1;
347   }
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);
354   } else {
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);
361     }
362   }
363   MaybeCompactSequenceNumbers();
364 }
365 
IsRegistered(const std::string & id) const366 bool SavedFilesService::SavedFiles::IsRegistered(const std::string& id) const {
367   return ContainsKey(registered_file_entries_, id);
368 }
369 
GetFileEntry(const std::string & id) const370 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())
375     return NULL;
376 
377   return it->second;
378 }
379 
GetAllFileEntries() const380 std::vector<SavedFileEntry> SavedFilesService::SavedFiles::GetAllFileEntries()
381     const {
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();
386        ++it) {
387     result.push_back(*it->second);
388   }
389   return result;
390 }
391 
MaybeCompactSequenceNumbers()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())
399     return;
400 
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)
404     return;
405 
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();
410        ++it) {
411     sequence_number++;
412     if (it->second->sequence_number == sequence_number)
413       continue;
414 
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));
424   }
425 }
426 
LoadSavedFileEntriesFromPreferences()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();
433        ++it) {
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));
438   }
439 }
440 
441 // static
SetMaxSequenceNumberForTest(int max_value)442 void SavedFilesService::SetMaxSequenceNumberForTest(int max_value) {
443   g_max_sequence_number = max_value;
444 }
445 
446 // static
ClearMaxSequenceNumberForTest()447 void SavedFilesService::ClearMaxSequenceNumberForTest() {
448   g_max_sequence_number = kMaxSequenceNumber;
449 }
450 
451 // static
SetLruSizeForTest(int size)452 void SavedFilesService::SetLruSizeForTest(int size) {
453   g_max_saved_file_entries = size;
454 }
455 
456 // static
ClearLruSizeForTest()457 void SavedFilesService::ClearLruSizeForTest() {
458   g_max_saved_file_entries = kMaxSavedFileEntries;
459 }
460 
461 }  // namespace apps
462