Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/hash_tables.h" | |
| 12 #include "base/value_conversions.h" | |
| 13 #include "chrome/browser/extensions/extension_host.h" | |
| 14 #include "chrome/browser/extensions/extension_prefs.h" | |
| 15 #include "chrome/browser/extensions/extension_service.h" | |
| 16 #include "chrome/browser/extensions/extension_system.h" | |
| 17 #include "chrome/common/extensions/permissions/api_permission.h" | |
| 18 #include "chrome/common/extensions/permissions/permission_set.h" | |
| 19 | |
| 20 namespace apps { | |
| 21 | |
| 22 using extensions::APIPermission; | |
| 23 using extensions::Extension; | |
| 24 using extensions::ExtensionHost; | |
| 25 using extensions::ExtensionPrefs; | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 // Preference keys | |
| 30 | |
| 31 // The file entries that an extension has permission to access. | |
| 32 const char kFileEntries[] = "file_entries"; | |
| 33 | |
| 34 // The path to a file entry that an extension had permission to access. | |
| 35 const char kFileEntryPath[] = "path"; | |
| 36 | |
| 37 // Whether or not an extension had write access to a file entry. | |
| 38 const char kFileEntryWritable[] = "writable"; | |
| 39 | |
| 40 // The sequence number in the LRU of the file entry. | |
| 41 const char kFileEntrySequenceNumber[] = "sequence_number"; | |
| 42 | |
| 43 const size_t kMaxSavedFileEntries = 500; | |
| 44 const int kMaxSequenceNumber = kint32max; | |
| 45 | |
| 46 // These might be different to the constant values in tests. | |
| 47 size_t g_max_saved_file_entries = kMaxSavedFileEntries; | |
| 48 int g_max_sequence_number = kMaxSequenceNumber; | |
| 49 | |
| 50 void AddSavedFileEntry(ExtensionPrefs* prefs, | |
| 51 const std::string& extension_id, | |
| 52 const SavedFileEntry& file_entry) { | |
| 53 ExtensionPrefs::ScopedDictionaryUpdate update( | |
| 54 prefs, extension_id, kFileEntries); | |
| 55 DictionaryValue* file_entries = update.Get(); | |
| 56 if (!file_entries) | |
| 57 file_entries = update.Create(); | |
| 58 DCHECK(!file_entries->GetDictionaryWithoutPathExpansion(file_entry.id, NULL)); | |
| 59 | |
| 60 DictionaryValue* file_entry_dict = new DictionaryValue(); | |
| 61 file_entry_dict->Set(kFileEntryPath, CreateFilePathValue(file_entry.path)); | |
| 62 file_entry_dict->SetBoolean(kFileEntryWritable, file_entry.writable); | |
| 63 file_entry_dict->SetInteger(kFileEntrySequenceNumber, | |
| 64 file_entry.sequence_number); | |
| 65 file_entries->SetWithoutPathExpansion(file_entry.id, file_entry_dict); | |
| 66 } | |
| 67 | |
| 68 void UpdateSavedFileEntry(ExtensionPrefs* prefs, | |
| 69 const std::string& extension_id, | |
| 70 const SavedFileEntry& file_entry) { | |
| 71 ExtensionPrefs::ScopedDictionaryUpdate update( | |
| 72 prefs, extension_id, kFileEntries); | |
| 73 DictionaryValue* file_entries = update.Get(); | |
| 74 DCHECK(file_entries); | |
| 75 DictionaryValue* file_entry_dict = NULL; | |
| 76 file_entries->GetDictionaryWithoutPathExpansion(file_entry.id, | |
| 77 &file_entry_dict); | |
| 78 DCHECK(file_entry_dict); | |
| 79 file_entry_dict->SetInteger(kFileEntrySequenceNumber, | |
| 80 file_entry.sequence_number); | |
| 81 } | |
| 82 | |
| 83 void RemoveSavedFileEntry(ExtensionPrefs* prefs, | |
| 84 const std::string& extension_id, | |
| 85 const std::string& file_entry_id) { | |
| 86 ExtensionPrefs::ScopedDictionaryUpdate update( | |
| 87 prefs, extension_id, kFileEntries); | |
| 88 DictionaryValue* file_entries = update.Get(); | |
| 89 if (!file_entries) | |
| 90 file_entries = update.Create(); | |
| 91 file_entries->RemoveWithoutPathExpansion(file_entry_id, NULL); | |
| 92 } | |
| 93 | |
| 94 void ClearSavedFileEntries(ExtensionPrefs* prefs, | |
| 95 const std::string& extension_id) { | |
| 96 prefs->UpdateExtensionPref(extension_id, kFileEntries, NULL); | |
| 97 } | |
| 98 | |
| 99 void GetSavedFileEntries(ExtensionPrefs* prefs, | |
| 100 const std::string& extension_id, | |
| 101 std::vector<SavedFileEntry>* out) { | |
| 102 const DictionaryValue* file_entries = NULL; | |
| 103 if (!prefs->ReadPrefAsDictionary(extension_id, kFileEntries, &file_entries)) | |
| 104 return; | |
| 105 | |
| 106 for (DictionaryValue::Iterator it(*file_entries); !it.IsAtEnd(); | |
| 107 it.Advance()) { | |
| 108 const DictionaryValue* file_entry = NULL; | |
| 109 if (!it.value().GetAsDictionary(&file_entry)) | |
| 110 continue; | |
| 111 const base::Value* path_value; | |
| 112 if (!file_entry->Get(kFileEntryPath, &path_value)) | |
| 113 continue; | |
| 114 base::FilePath file_path; | |
| 115 if (!GetValueAsFilePath(*path_value, &file_path)) | |
| 116 continue; | |
| 117 bool writable = false; | |
| 118 if (!file_entry->GetBoolean(kFileEntryWritable, &writable)) | |
| 119 continue; | |
| 120 int sequence_number = 0; | |
| 121 if (!file_entry->GetInteger(kFileEntrySequenceNumber, &sequence_number)) | |
| 122 continue; | |
| 123 if (!sequence_number) | |
| 124 continue; | |
| 125 out->push_back( | |
| 126 SavedFileEntry(it.key(), file_path, writable, sequence_number)); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 } // namespace | |
| 131 | |
| 132 class SavedFilesService::SavedFiles { | |
| 133 public: | |
| 134 SavedFiles(Profile* profile, const std::string& extension_id); | |
| 135 ~SavedFiles(); | |
| 136 | |
| 137 void RetainFileEntry(const std::string& id, | |
| 138 const base::FilePath& file_path, | |
| 139 bool writable); | |
| 140 void MoveEntryToFrontOfQueue(const std::string& id); | |
| 141 bool IsRetained(const std::string& id) const; | |
| 142 bool GetFileEntry(const std::string& id, SavedFileEntry* out) const; | |
| 143 std::vector<SavedFileEntry> GetAllFileEntries() const; | |
| 144 | |
| 145 private: | |
| 146 void MaybeCompactSequenceNumbers(); | |
| 147 | |
| 148 Profile* profile_; | |
| 149 const std::string extension_id_; | |
| 150 | |
| 151 // Owns values. | |
| 152 base::hash_map<std::string, SavedFileEntry*> file_id_to_file_entry_map_; | |
| 153 | |
|
Matt Giuca
2013/05/20 05:06:33
Nit: Remove this blank line.
| |
| 154 STLValueDeleter<base::hash_map<std::string, SavedFileEntry*> > | |
| 155 file_id_to_file_entry_map_deleter_; | |
| 156 | |
| 157 // Values are a subset of values in file_id_to_file_entry_map_. | |
| 158 std::map<int, SavedFileEntry*> saved_file_lru_; | |
| 159 | |
| 160 DISALLOW_COPY_AND_ASSIGN(SavedFiles); | |
| 161 }; | |
| 162 | |
| 163 // static | |
| 164 SavedFilesService* SavedFilesService::Get(Profile* profile) { | |
| 165 return SavedFilesServiceFactory::GetForProfile(profile); | |
| 166 } | |
| 167 | |
| 168 SavedFilesService::SavedFilesService(Profile* profile) | |
| 169 : extension_id_to_saved_files_deleter_(&extension_id_to_saved_files_), | |
| 170 profile_(profile) { | |
| 171 registrar_.Add(this, | |
| 172 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
| 173 content::NotificationService::AllSources()); | |
| 174 registrar_.Add(this, | |
| 175 chrome::NOTIFICATION_APP_TERMINATING, | |
| 176 content::NotificationService::AllSources()); | |
| 177 } | |
| 178 | |
| 179 SavedFilesService::~SavedFilesService() {} | |
| 180 | |
| 181 void SavedFilesService::Observe(int type, | |
| 182 const content::NotificationSource& source, | |
| 183 const content::NotificationDetails& details) { | |
| 184 switch (type) { | |
| 185 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | |
| 186 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); | |
| 187 const Extension* extension = host->extension(); | |
| 188 if (extension) | |
| 189 FlushRetainedEntries(extension); | |
| 190 break; | |
| 191 } | |
| 192 | |
| 193 case chrome::NOTIFICATION_APP_TERMINATING: { | |
| 194 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular | |
| 195 // as all extension hosts will be destroyed as a result of shutdown. | |
| 196 registrar_.RemoveAll(); | |
| 197 break; | |
| 198 } | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 void SavedFilesService::RetainFileEntry(const std::string& extension_id, | |
| 203 const std::string& id, | |
| 204 const base::FilePath& file_path, | |
| 205 bool writable) { | |
| 206 GetOrInsert(extension_id)->RetainFileEntry(id, file_path, writable); | |
| 207 } | |
| 208 | |
| 209 void SavedFilesService::MoveEntryToFrontOfQueue(const std::string& extension_id, | |
| 210 const std::string& id) { | |
| 211 GetOrInsert(extension_id)->MoveEntryToFrontOfQueue(id); | |
| 212 } | |
| 213 | |
| 214 std::vector<SavedFileEntry> SavedFilesService::GetAllFileEntries( | |
| 215 const std::string& extension_id) { | |
| 216 return GetOrInsert(extension_id)->GetAllFileEntries(); | |
| 217 } | |
| 218 | |
| 219 bool SavedFilesService::IsRetained(const std::string& extension_id, | |
| 220 const std::string& id) { | |
| 221 return GetOrInsert(extension_id)->IsRetained(id); | |
| 222 } | |
| 223 | |
| 224 bool SavedFilesService::GetFileEntry(const std::string& extension_id, | |
| 225 const std::string& id, | |
| 226 SavedFileEntry* out) { | |
| 227 return GetOrInsert(extension_id)->GetFileEntry(id, out); | |
| 228 } | |
| 229 | |
| 230 void SavedFilesService::FlushRetainedEntries(const Extension* extension) { | |
| 231 if (!extension->GetActivePermissions()->HasAPIPermission( | |
| 232 APIPermission::kFileSystemRetainFiles)) { | |
| 233 ClearSavedFileEntries(ExtensionPrefs::Get(profile_), extension->id()); | |
| 234 } | |
| 235 std::map<std::string, SavedFiles*>::iterator it = | |
| 236 extension_id_to_saved_files_.find(extension->id()); | |
| 237 if (it != extension_id_to_saved_files_.end()) { | |
| 238 delete it->second; | |
| 239 extension_id_to_saved_files_.erase(it); | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 SavedFilesService::SavedFiles* SavedFilesService::GetOrInsert( | |
| 244 const std::string& extension_id) { | |
| 245 std::map<std::string, SavedFiles*>::iterator it = | |
| 246 extension_id_to_saved_files_.find(extension_id); | |
| 247 if (it != extension_id_to_saved_files_.end()) | |
| 248 return it->second; | |
| 249 | |
| 250 SavedFiles* saved_files = new SavedFiles(profile_, extension_id); | |
| 251 extension_id_to_saved_files_.insert( | |
| 252 std::make_pair(extension_id, saved_files)); | |
| 253 return saved_files; | |
| 254 } | |
| 255 | |
| 256 SavedFilesService::SavedFiles::SavedFiles(Profile* profile, | |
| 257 const std::string& extension_id) | |
| 258 : profile_(profile), | |
| 259 extension_id_(extension_id), | |
| 260 file_id_to_file_entry_map_deleter_(&file_id_to_file_entry_map_) { | |
| 261 std::vector<SavedFileEntry> saved_entries; | |
| 262 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | |
| 263 GetSavedFileEntries(prefs, extension_id_, &saved_entries); | |
| 264 for (std::vector<SavedFileEntry>::iterator it = saved_entries.begin(); | |
| 265 it != saved_entries.end(); ++it) { | |
| 266 SavedFileEntry* file_entry = new SavedFileEntry(*it); | |
| 267 file_id_to_file_entry_map_.insert( | |
| 268 std::make_pair(file_entry->id, file_entry)); | |
| 269 saved_file_lru_.insert( | |
| 270 std::make_pair(file_entry->sequence_number, file_entry)); | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 SavedFilesService::SavedFiles::~SavedFiles() {} | |
| 275 | |
| 276 void SavedFilesService::SavedFiles::RetainFileEntry( | |
| 277 const std::string& id, | |
| 278 const base::FilePath& file_path, | |
| 279 bool writable) { | |
| 280 if (ContainsKey(file_id_to_file_entry_map_, id)) | |
| 281 return; | |
| 282 | |
| 283 file_id_to_file_entry_map_.insert( | |
| 284 std::make_pair(id, new SavedFileEntry(id, file_path, writable, 0))); | |
| 285 } | |
| 286 | |
| 287 void SavedFilesService::SavedFiles::MoveEntryToFrontOfQueue( | |
| 288 const std::string& id) { | |
| 289 base::hash_map<std::string, SavedFileEntry*>::iterator it = | |
| 290 file_id_to_file_entry_map_.find(id); | |
| 291 if (it == file_id_to_file_entry_map_.end()) | |
| 292 return; | |
| 293 | |
| 294 SavedFileEntry* file_entry = it->second; | |
| 295 int old_sequence_number = file_entry->sequence_number; | |
| 296 if (!saved_file_lru_.empty()) { | |
| 297 std::map<int, SavedFileEntry*>::reverse_iterator it = | |
| 298 saved_file_lru_.rbegin(); | |
| 299 if (it->second == file_entry) | |
| 300 return; | |
| 301 | |
| 302 file_entry->sequence_number = it->first + 1; | |
| 303 } else { | |
| 304 file_entry->sequence_number = 1; | |
| 305 } | |
| 306 saved_file_lru_.insert( | |
| 307 std::make_pair(file_entry->sequence_number, file_entry)); | |
| 308 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | |
| 309 if (old_sequence_number) { | |
| 310 saved_file_lru_.erase(old_sequence_number); | |
| 311 UpdateSavedFileEntry(prefs, extension_id_, *file_entry); | |
| 312 } else { | |
| 313 AddSavedFileEntry(prefs, extension_id_, *file_entry); | |
| 314 if (saved_file_lru_.size() > g_max_saved_file_entries) { | |
| 315 std::map<int, SavedFileEntry*>::iterator it = saved_file_lru_.begin(); | |
| 316 it->second->sequence_number = 0; | |
| 317 RemoveSavedFileEntry(prefs, extension_id_, it->second->id); | |
| 318 saved_file_lru_.erase(it); | |
| 319 } | |
| 320 } | |
| 321 MaybeCompactSequenceNumbers(); | |
| 322 } | |
| 323 | |
| 324 bool SavedFilesService::SavedFiles::IsRetained(const std::string& id) const { | |
| 325 return ContainsKey(file_id_to_file_entry_map_, id); | |
| 326 } | |
| 327 | |
| 328 bool SavedFilesService::SavedFiles::GetFileEntry(const std::string& id, | |
| 329 SavedFileEntry* out) const { | |
| 330 base::hash_map<std::string, SavedFileEntry*>::const_iterator it = | |
| 331 file_id_to_file_entry_map_.find(id); | |
| 332 if (it == file_id_to_file_entry_map_.end()) | |
| 333 return false; | |
| 334 | |
| 335 *out = *it->second; | |
| 336 return true; | |
| 337 } | |
| 338 | |
| 339 std::vector<SavedFileEntry> | |
| 340 SavedFilesService::SavedFiles::GetAllFileEntries() const { | |
| 341 std::vector<SavedFileEntry> result; | |
| 342 for (base::hash_map<std::string, SavedFileEntry*>::const_iterator it = | |
| 343 file_id_to_file_entry_map_.begin(); | |
| 344 it != file_id_to_file_entry_map_.end(); ++it) { | |
| 345 result.push_back(*it->second); | |
| 346 } | |
| 347 return result; | |
| 348 } | |
| 349 | |
| 350 void SavedFilesService::SavedFiles::MaybeCompactSequenceNumbers() { | |
| 351 std::map<int, SavedFileEntry*>::reverse_iterator it = | |
| 352 saved_file_lru_.rbegin(); | |
| 353 if (it == saved_file_lru_.rend()) | |
| 354 return; | |
| 355 | |
| 356 if (it->first < g_max_sequence_number) | |
| 357 return; | |
| 358 | |
| 359 int sequence_number = 0; | |
| 360 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | |
| 361 for (std::map<int, SavedFileEntry*>::iterator it = saved_file_lru_.begin(); | |
| 362 it != saved_file_lru_.end(); ++it) { | |
| 363 sequence_number++; | |
| 364 if (it->second->sequence_number == sequence_number) | |
| 365 continue; | |
| 366 | |
| 367 SavedFileEntry* file_entry = it->second; | |
| 368 file_entry->sequence_number = sequence_number; | |
| 369 UpdateSavedFileEntry(prefs, extension_id_, *file_entry); | |
| 370 if (it == saved_file_lru_.begin()) { | |
| 371 saved_file_lru_.erase(it); | |
| 372 it = saved_file_lru_.insert(std::make_pair(file_entry->sequence_number, | |
| 373 file_entry)).first; | |
| 374 } else { | |
| 375 saved_file_lru_.erase(it--); | |
| 376 it = saved_file_lru_.insert( | |
| 377 it, std::make_pair(file_entry->sequence_number, file_entry)); | |
| 378 } | |
| 379 } | |
| 380 } | |
| 381 | |
| 382 // static | |
| 383 void SavedFilesService::SetMaxSequenceNumberForTest(int max_value) { | |
| 384 g_max_sequence_number = max_value; | |
| 385 } | |
| 386 | |
| 387 // static | |
| 388 void SavedFilesService::ClearMaxSequenceNumberForTest() { | |
| 389 g_max_sequence_number = kMaxSequenceNumber; | |
| 390 } | |
| 391 | |
| 392 // static | |
| 393 void SavedFilesService::SetLruSizeForTest(int size) { | |
| 394 g_max_saved_file_entries = size; | |
| 395 } | |
| 396 | |
| 397 // static | |
| 398 void SavedFilesService::ClearLruSizeForTest() { | |
| 399 g_max_saved_file_entries = kMaxSavedFileEntries; | |
| 400 } | |
| 401 | |
| 402 } // namespace apps | |
| OLD | NEW |