| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "apps/saved_files_service.h" | 5 #include "apps/saved_files_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "apps/saved_files_service_factory.h" | 9 #include "apps/saved_files_service_factory.h" |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // Preference keys | 31 // Preference keys |
| 32 | 32 |
| 33 // The file entries that the app has permission to access. | 33 // The file entries that the app has permission to access. |
| 34 const char kFileEntries[] = "file_entries"; | 34 const char kFileEntries[] = "file_entries"; |
| 35 | 35 |
| 36 // The path to a file entry that the app had permission to access. | 36 // The path to a file entry that the app had permission to access. |
| 37 const char kFileEntryPath[] = "path"; | 37 const char kFileEntryPath[] = "path"; |
| 38 | 38 |
| 39 // Whether or not the the entry refers to a directory. |
| 40 const char kFileEntryIsDirectory[] = "is_directory"; |
| 41 |
| 39 // The sequence number in the LRU of the file entry. | 42 // The sequence number in the LRU of the file entry. |
| 40 const char kFileEntrySequenceNumber[] = "sequence_number"; | 43 const char kFileEntrySequenceNumber[] = "sequence_number"; |
| 41 | 44 |
| 42 const size_t kMaxSavedFileEntries = 500; | 45 const size_t kMaxSavedFileEntries = 500; |
| 43 const int kMaxSequenceNumber = kint32max; | 46 const int kMaxSequenceNumber = kint32max; |
| 44 | 47 |
| 45 // These might be different to the constant values in tests. | 48 // These might be different to the constant values in tests. |
| 46 size_t g_max_saved_file_entries = kMaxSavedFileEntries; | 49 size_t g_max_saved_file_entries = kMaxSavedFileEntries; |
| 47 int g_max_sequence_number = kMaxSequenceNumber; | 50 int g_max_sequence_number = kMaxSequenceNumber; |
| 48 | 51 |
| 49 // Persists a SavedFileEntry in ExtensionPrefs. | 52 // Persists a SavedFileEntry in ExtensionPrefs. |
| 50 void AddSavedFileEntry(ExtensionPrefs* prefs, | 53 void AddSavedFileEntry(ExtensionPrefs* prefs, |
| 51 const std::string& extension_id, | 54 const std::string& extension_id, |
| 52 const SavedFileEntry& file_entry) { | 55 const SavedFileEntry& file_entry) { |
| 53 ExtensionPrefs::ScopedDictionaryUpdate update( | 56 ExtensionPrefs::ScopedDictionaryUpdate update( |
| 54 prefs, extension_id, kFileEntries); | 57 prefs, extension_id, kFileEntries); |
| 55 DictionaryValue* file_entries = update.Get(); | 58 DictionaryValue* file_entries = update.Get(); |
| 56 if (!file_entries) | 59 if (!file_entries) |
| 57 file_entries = update.Create(); | 60 file_entries = update.Create(); |
| 58 DCHECK(!file_entries->GetDictionaryWithoutPathExpansion(file_entry.id, NULL)); | 61 DCHECK(!file_entries->GetDictionaryWithoutPathExpansion(file_entry.id, NULL)); |
| 59 | 62 |
| 60 DictionaryValue* file_entry_dict = new DictionaryValue(); | 63 DictionaryValue* file_entry_dict = new DictionaryValue(); |
| 61 file_entry_dict->Set(kFileEntryPath, CreateFilePathValue(file_entry.path)); | 64 file_entry_dict->Set(kFileEntryPath, CreateFilePathValue(file_entry.path)); |
| 65 file_entry_dict->SetBoolean(kFileEntryIsDirectory, file_entry.is_directory); |
| 62 file_entry_dict->SetInteger(kFileEntrySequenceNumber, | 66 file_entry_dict->SetInteger(kFileEntrySequenceNumber, |
| 63 file_entry.sequence_number); | 67 file_entry.sequence_number); |
| 64 file_entries->SetWithoutPathExpansion(file_entry.id, file_entry_dict); | 68 file_entries->SetWithoutPathExpansion(file_entry.id, file_entry_dict); |
| 65 } | 69 } |
| 66 | 70 |
| 67 // Updates the sequence_number of a SavedFileEntry persisted in ExtensionPrefs. | 71 // Updates the sequence_number of a SavedFileEntry persisted in ExtensionPrefs. |
| 68 void UpdateSavedFileEntry(ExtensionPrefs* prefs, | 72 void UpdateSavedFileEntry(ExtensionPrefs* prefs, |
| 69 const std::string& extension_id, | 73 const std::string& extension_id, |
| 70 const SavedFileEntry& file_entry) { | 74 const SavedFileEntry& file_entry) { |
| 71 ExtensionPrefs::ScopedDictionaryUpdate update( | 75 ExtensionPrefs::ScopedDictionaryUpdate update( |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 it.Advance()) { | 115 it.Advance()) { |
| 112 const DictionaryValue* file_entry = NULL; | 116 const DictionaryValue* file_entry = NULL; |
| 113 if (!it.value().GetAsDictionary(&file_entry)) | 117 if (!it.value().GetAsDictionary(&file_entry)) |
| 114 continue; | 118 continue; |
| 115 const base::Value* path_value; | 119 const base::Value* path_value; |
| 116 if (!file_entry->Get(kFileEntryPath, &path_value)) | 120 if (!file_entry->Get(kFileEntryPath, &path_value)) |
| 117 continue; | 121 continue; |
| 118 base::FilePath file_path; | 122 base::FilePath file_path; |
| 119 if (!GetValueAsFilePath(*path_value, &file_path)) | 123 if (!GetValueAsFilePath(*path_value, &file_path)) |
| 120 continue; | 124 continue; |
| 125 bool is_directory = false; |
| 126 file_entry->GetBoolean(kFileEntryIsDirectory, &is_directory); |
| 121 int sequence_number = 0; | 127 int sequence_number = 0; |
| 122 if (!file_entry->GetInteger(kFileEntrySequenceNumber, &sequence_number)) | 128 if (!file_entry->GetInteger(kFileEntrySequenceNumber, &sequence_number)) |
| 123 continue; | 129 continue; |
| 124 if (!sequence_number) | 130 if (!sequence_number) |
| 125 continue; | 131 continue; |
| 126 result.push_back(SavedFileEntry(it.key(), file_path, sequence_number)); | 132 result.push_back( |
| 133 SavedFileEntry(it.key(), file_path, is_directory, sequence_number)); |
| 127 } | 134 } |
| 128 return result; | 135 return result; |
| 129 } | 136 } |
| 130 | 137 |
| 131 } // namespace | 138 } // namespace |
| 132 | 139 |
| 133 SavedFileEntry::SavedFileEntry() : sequence_number(0) {} | 140 SavedFileEntry::SavedFileEntry() : is_directory(false), sequence_number(0) {} |
| 134 | 141 |
| 135 SavedFileEntry::SavedFileEntry(const std::string& id, | 142 SavedFileEntry::SavedFileEntry(const std::string& id, |
| 136 const base::FilePath& path, | 143 const base::FilePath& path, |
| 144 bool is_directory, |
| 137 int sequence_number) | 145 int sequence_number) |
| 138 : id(id), | 146 : id(id), |
| 139 path(path), | 147 path(path), |
| 148 is_directory(is_directory), |
| 140 sequence_number(sequence_number) {} | 149 sequence_number(sequence_number) {} |
| 141 | 150 |
| 142 class SavedFilesService::SavedFiles { | 151 class SavedFilesService::SavedFiles { |
| 143 public: | 152 public: |
| 144 SavedFiles(Profile* profile, const std::string& extension_id); | 153 SavedFiles(Profile* profile, const std::string& extension_id); |
| 145 ~SavedFiles(); | 154 ~SavedFiles(); |
| 146 | 155 |
| 147 void RegisterFileEntry(const std::string& id, | 156 void RegisterFileEntry(const std::string& id, |
| 148 const base::FilePath& file_path); | 157 const base::FilePath& file_path, |
| 158 bool is_directory); |
| 149 void EnqueueFileEntry(const std::string& id); | 159 void EnqueueFileEntry(const std::string& id); |
| 150 bool IsRegistered(const std::string& id) const; | 160 bool IsRegistered(const std::string& id) const; |
| 151 const SavedFileEntry* GetFileEntry(const std::string& id) const; | 161 const SavedFileEntry* GetFileEntry(const std::string& id) const; |
| 152 std::vector<SavedFileEntry> GetAllFileEntries() const; | 162 std::vector<SavedFileEntry> GetAllFileEntries() const; |
| 153 | 163 |
| 154 private: | 164 private: |
| 155 // Compacts sequence numbers if the largest sequence number is | 165 // Compacts sequence numbers if the largest sequence number is |
| 156 // g_max_sequence_number. Outside of testing, it is set to kint32max, so this | 166 // g_max_sequence_number. Outside of testing, it is set to kint32max, so this |
| 157 // will almost never do any real work. | 167 // will almost never do any real work. |
| 158 void MaybeCompactSequenceNumbers(); | 168 void MaybeCompactSequenceNumbers(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular | 222 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular |
| 213 // as all extension hosts will be destroyed as a result of shutdown. | 223 // as all extension hosts will be destroyed as a result of shutdown. |
| 214 registrar_.RemoveAll(); | 224 registrar_.RemoveAll(); |
| 215 break; | 225 break; |
| 216 } | 226 } |
| 217 } | 227 } |
| 218 } | 228 } |
| 219 | 229 |
| 220 void SavedFilesService::RegisterFileEntry(const std::string& extension_id, | 230 void SavedFilesService::RegisterFileEntry(const std::string& extension_id, |
| 221 const std::string& id, | 231 const std::string& id, |
| 222 const base::FilePath& file_path) { | 232 const base::FilePath& file_path, |
| 223 GetOrInsert(extension_id)->RegisterFileEntry(id, file_path); | 233 bool is_directory) { |
| 234 GetOrInsert(extension_id)->RegisterFileEntry(id, file_path, is_directory); |
| 224 } | 235 } |
| 225 | 236 |
| 226 void SavedFilesService::EnqueueFileEntry(const std::string& extension_id, | 237 void SavedFilesService::EnqueueFileEntry(const std::string& extension_id, |
| 227 const std::string& id) { | 238 const std::string& id) { |
| 228 GetOrInsert(extension_id)->EnqueueFileEntry(id); | 239 GetOrInsert(extension_id)->EnqueueFileEntry(id); |
| 229 } | 240 } |
| 230 | 241 |
| 231 std::vector<SavedFileEntry> SavedFilesService::GetAllFileEntries( | 242 std::vector<SavedFileEntry> SavedFilesService::GetAllFileEntries( |
| 232 const std::string& extension_id) { | 243 const std::string& extension_id) { |
| 233 SavedFiles* saved_files = Get(extension_id); | 244 SavedFiles* saved_files = Get(extension_id); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 : profile_(profile), | 307 : profile_(profile), |
| 297 extension_id_(extension_id), | 308 extension_id_(extension_id), |
| 298 registered_file_entries_deleter_(®istered_file_entries_) { | 309 registered_file_entries_deleter_(®istered_file_entries_) { |
| 299 LoadSavedFileEntriesFromPreferences(); | 310 LoadSavedFileEntriesFromPreferences(); |
| 300 } | 311 } |
| 301 | 312 |
| 302 SavedFilesService::SavedFiles::~SavedFiles() {} | 313 SavedFilesService::SavedFiles::~SavedFiles() {} |
| 303 | 314 |
| 304 void SavedFilesService::SavedFiles::RegisterFileEntry( | 315 void SavedFilesService::SavedFiles::RegisterFileEntry( |
| 305 const std::string& id, | 316 const std::string& id, |
| 306 const base::FilePath& file_path) { | 317 const base::FilePath& file_path, |
| 318 bool is_directory) { |
| 307 if (ContainsKey(registered_file_entries_, id)) | 319 if (ContainsKey(registered_file_entries_, id)) |
| 308 return; | 320 return; |
| 309 | 321 |
| 310 registered_file_entries_.insert( | 322 registered_file_entries_.insert( |
| 311 std::make_pair(id, new SavedFileEntry(id, file_path, 0))); | 323 std::make_pair(id, new SavedFileEntry(id, file_path, is_directory, 0))); |
| 312 } | 324 } |
| 313 | 325 |
| 314 void SavedFilesService::SavedFiles::EnqueueFileEntry(const std::string& id) { | 326 void SavedFilesService::SavedFiles::EnqueueFileEntry(const std::string& id) { |
| 315 base::hash_map<std::string, SavedFileEntry*>::iterator it = | 327 base::hash_map<std::string, SavedFileEntry*>::iterator it = |
| 316 registered_file_entries_.find(id); | 328 registered_file_entries_.find(id); |
| 317 DCHECK(it != registered_file_entries_.end()); | 329 DCHECK(it != registered_file_entries_.end()); |
| 318 | 330 |
| 319 SavedFileEntry* file_entry = it->second; | 331 SavedFileEntry* file_entry = it->second; |
| 320 int old_sequence_number = file_entry->sequence_number; | 332 int old_sequence_number = file_entry->sequence_number; |
| 321 if (!saved_file_lru_.empty()) { | 333 if (!saved_file_lru_.empty()) { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 void SavedFilesService::SetLruSizeForTest(int size) { | 449 void SavedFilesService::SetLruSizeForTest(int size) { |
| 438 g_max_saved_file_entries = size; | 450 g_max_saved_file_entries = size; |
| 439 } | 451 } |
| 440 | 452 |
| 441 // static | 453 // static |
| 442 void SavedFilesService::ClearLruSizeForTest() { | 454 void SavedFilesService::ClearLruSizeForTest() { |
| 443 g_max_saved_file_entries = kMaxSavedFileEntries; | 455 g_max_saved_file_entries = kMaxSavedFileEntries; |
| 444 } | 456 } |
| 445 | 457 |
| 446 } // namespace apps | 458 } // namespace apps |
| OLD | NEW |