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, | |
|
Matt Giuca
2013/05/22 08:26:14
Nit: One-line comment on each of these.
Sam McNally
2013/05/23 03:47:28
Done.
| |
| 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 RetainFileUntilAppSuspend(const std::string& id, | |
| 138 const base::FilePath& file_path, | |
| 139 bool writable); | |
| 140 void MoveEntryToBackOfQueue(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(); | |
|
Matt Giuca
2013/05/22 08:26:14
Document this. In particular, what is the "Maybe"
Sam McNally
2013/05/23 03:47:28
Done.
| |
| 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_; | |
|
Matt Giuca
2013/05/22 08:26:14
What does it mean to be in this map? It seems to m
| |
| 153 STLValueDeleter<base::hash_map<std::string, SavedFileEntry*> > | |
| 154 file_id_to_file_entry_map_deleter_; | |
|
Matt Giuca
2013/05/22 08:26:14
Hmm, so I see you manually deleting these later on
| |
| 155 | |
| 156 // Values are a subset of values in file_id_to_file_entry_map_. | |
|
Matt Giuca
2013/05/22 08:26:14
What are the keys? What does it mean to be in this
Sam McNally
2013/05/23 03:47:28
Done.
| |
| 157 std::map<int, SavedFileEntry*> saved_file_lru_; | |
| 158 | |
| 159 DISALLOW_COPY_AND_ASSIGN(SavedFiles); | |
| 160 }; | |
| 161 | |
| 162 // static | |
| 163 SavedFilesService* SavedFilesService::Get(Profile* profile) { | |
| 164 return SavedFilesServiceFactory::GetForProfile(profile); | |
| 165 } | |
| 166 | |
| 167 SavedFilesService::SavedFilesService(Profile* profile) | |
| 168 : extension_id_to_saved_files_deleter_(&extension_id_to_saved_files_), | |
| 169 profile_(profile) { | |
| 170 registrar_.Add(this, | |
| 171 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
| 172 content::NotificationService::AllSources()); | |
| 173 registrar_.Add(this, | |
| 174 chrome::NOTIFICATION_APP_TERMINATING, | |
| 175 content::NotificationService::AllSources()); | |
| 176 } | |
| 177 | |
| 178 SavedFilesService::~SavedFilesService() {} | |
| 179 | |
| 180 void SavedFilesService::Observe(int type, | |
| 181 const content::NotificationSource& source, | |
| 182 const content::NotificationDetails& details) { | |
| 183 switch (type) { | |
| 184 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | |
| 185 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); | |
| 186 const Extension* extension = host->extension(); | |
| 187 if (extension) { | |
| 188 MaybeClearQueue(extension); | |
| 189 Clear(extension->id()); | |
| 190 } | |
| 191 break; | |
| 192 } | |
| 193 | |
| 194 case chrome::NOTIFICATION_APP_TERMINATING: { | |
| 195 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular | |
| 196 // as all extension hosts will be destroyed as a result of shutdown. | |
| 197 registrar_.RemoveAll(); | |
| 198 break; | |
| 199 } | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 void SavedFilesService::RetainFileUntilAppSuspend( | |
| 204 const std::string& extension_id, | |
| 205 const std::string& id, | |
| 206 const base::FilePath& file_path, | |
| 207 bool writable) { | |
| 208 GetOrInsert(extension_id)->RetainFileUntilAppSuspend(id, file_path, writable); | |
| 209 } | |
| 210 | |
| 211 void SavedFilesService::MoveEntryToBackOfQueue(const std::string& extension_id, | |
| 212 const std::string& id) { | |
| 213 GetOrInsert(extension_id)->MoveEntryToBackOfQueue(id); | |
| 214 } | |
| 215 | |
| 216 std::vector<SavedFileEntry> SavedFilesService::GetAllFileEntries( | |
| 217 const std::string& extension_id) { | |
| 218 return GetOrInsert(extension_id)->GetAllFileEntries(); | |
| 219 } | |
| 220 | |
| 221 bool SavedFilesService::IsRetained(const std::string& extension_id, | |
| 222 const std::string& id) { | |
| 223 return GetOrInsert(extension_id)->IsRetained(id); | |
| 224 } | |
| 225 | |
| 226 bool SavedFilesService::GetFileEntry(const std::string& extension_id, | |
| 227 const std::string& id, | |
| 228 SavedFileEntry* out) { | |
| 229 return GetOrInsert(extension_id)->GetFileEntry(id, out); | |
| 230 } | |
| 231 | |
| 232 void SavedFilesService::MaybeClearQueue(const Extension* extension) { | |
| 233 if (!extension->GetActivePermissions()->HasAPIPermission( | |
| 234 APIPermission::kFileSystemRetainFiles)) { | |
| 235 ClearSavedFileEntries(ExtensionPrefs::Get(profile_), extension->id()); | |
| 236 Clear(extension->id()); | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 SavedFilesService::SavedFiles* SavedFilesService::GetOrInsert( | |
| 241 const std::string& extension_id) { | |
| 242 std::map<std::string, SavedFiles*>::iterator it = | |
| 243 extension_id_to_saved_files_.find(extension_id); | |
| 244 if (it != extension_id_to_saved_files_.end()) | |
| 245 return it->second; | |
| 246 | |
| 247 SavedFiles* saved_files = new SavedFiles(profile_, extension_id); | |
| 248 extension_id_to_saved_files_.insert( | |
| 249 std::make_pair(extension_id, saved_files)); | |
| 250 return saved_files; | |
| 251 } | |
| 252 | |
| 253 void SavedFilesService::Clear(const std::string& extension_id) { | |
| 254 std::map<std::string, SavedFiles*>::iterator it = | |
| 255 extension_id_to_saved_files_.find(extension_id); | |
| 256 if (it != extension_id_to_saved_files_.end()) { | |
| 257 delete it->second; | |
| 258 extension_id_to_saved_files_.erase(it); | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 SavedFilesService::SavedFiles::SavedFiles(Profile* profile, | |
| 263 const std::string& extension_id) | |
| 264 : profile_(profile), | |
| 265 extension_id_(extension_id), | |
| 266 file_id_to_file_entry_map_deleter_(&file_id_to_file_entry_map_) { | |
| 267 std::vector<SavedFileEntry> saved_entries; | |
| 268 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | |
| 269 GetSavedFileEntries(prefs, extension_id_, &saved_entries); | |
| 270 for (std::vector<SavedFileEntry>::iterator it = saved_entries.begin(); | |
| 271 it != saved_entries.end(); ++it) { | |
| 272 SavedFileEntry* file_entry = new SavedFileEntry(*it); | |
| 273 file_id_to_file_entry_map_.insert( | |
| 274 std::make_pair(file_entry->id, file_entry)); | |
| 275 saved_file_lru_.insert( | |
| 276 std::make_pair(file_entry->sequence_number, file_entry)); | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 SavedFilesService::SavedFiles::~SavedFiles() {} | |
| 281 | |
| 282 void SavedFilesService::SavedFiles::RetainFileUntilAppSuspend( | |
| 283 const std::string& id, | |
| 284 const base::FilePath& file_path, | |
| 285 bool writable) { | |
| 286 if (ContainsKey(file_id_to_file_entry_map_, id)) | |
| 287 return; | |
| 288 | |
| 289 file_id_to_file_entry_map_.insert( | |
| 290 std::make_pair(id, new SavedFileEntry(id, file_path, writable, 0))); | |
| 291 } | |
| 292 | |
| 293 void SavedFilesService::SavedFiles::MoveEntryToBackOfQueue( | |
| 294 const std::string& id) { | |
| 295 base::hash_map<std::string, SavedFileEntry*>::iterator it = | |
| 296 file_id_to_file_entry_map_.find(id); | |
| 297 if (it == file_id_to_file_entry_map_.end()) | |
| 298 return; | |
|
Matt Giuca
2013/05/22 08:26:14
Doesn't this mean that you will need to call Retai
Sam McNally
2013/05/23 03:47:28
Done.
| |
| 299 | |
| 300 SavedFileEntry* file_entry = it->second; | |
| 301 int old_sequence_number = file_entry->sequence_number; | |
| 302 if (!saved_file_lru_.empty()) { | |
|
Matt Giuca
2013/05/22 08:26:14
// Get the sequence number after the last file ent
| |
| 303 std::map<int, SavedFileEntry*>::reverse_iterator it = | |
| 304 saved_file_lru_.rbegin(); | |
| 305 if (it->second == file_entry) | |
| 306 return; | |
| 307 | |
| 308 file_entry->sequence_number = it->first + 1; | |
| 309 } else { | |
| 310 file_entry->sequence_number = 1; | |
|
Matt Giuca
2013/05/22 08:26:14
Why not 0?
Oh, I think I get it ... reading into
Sam McNally
2013/05/23 03:47:28
Done.
| |
| 311 } | |
| 312 saved_file_lru_.insert( | |
| 313 std::make_pair(file_entry->sequence_number, file_entry)); | |
| 314 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | |
| 315 if (old_sequence_number) { | |
| 316 saved_file_lru_.erase(old_sequence_number); | |
| 317 UpdateSavedFileEntry(prefs, extension_id_, *file_entry); | |
| 318 } else { | |
| 319 AddSavedFileEntry(prefs, extension_id_, *file_entry); | |
| 320 if (saved_file_lru_.size() > g_max_saved_file_entries) { | |
| 321 std::map<int, SavedFileEntry*>::iterator it = saved_file_lru_.begin(); | |
| 322 it->second->sequence_number = 0; | |
| 323 RemoveSavedFileEntry(prefs, extension_id_, it->second->id); | |
| 324 saved_file_lru_.erase(it); | |
| 325 } | |
| 326 } | |
| 327 MaybeCompactSequenceNumbers(); | |
| 328 } | |
| 329 | |
| 330 bool SavedFilesService::SavedFiles::IsRetained(const std::string& id) const { | |
| 331 return ContainsKey(file_id_to_file_entry_map_, id); | |
| 332 } | |
| 333 | |
| 334 bool SavedFilesService::SavedFiles::GetFileEntry(const std::string& id, | |
|
Matt Giuca
2013/05/22 08:26:14
Wait, this doesn't do what its documentation descr
Sam McNally
2013/05/23 03:47:28
Done.
| |
| 335 SavedFileEntry* out) const { | |
| 336 base::hash_map<std::string, SavedFileEntry*>::const_iterator it = | |
| 337 file_id_to_file_entry_map_.find(id); | |
| 338 if (it == file_id_to_file_entry_map_.end()) | |
| 339 return false; | |
| 340 | |
| 341 *out = *it->second; | |
| 342 return true; | |
| 343 } | |
| 344 | |
| 345 std::vector<SavedFileEntry> | |
| 346 SavedFilesService::SavedFiles::GetAllFileEntries() const { | |
| 347 std::vector<SavedFileEntry> result; | |
| 348 for (base::hash_map<std::string, SavedFileEntry*>::const_iterator it = | |
| 349 file_id_to_file_entry_map_.begin(); | |
| 350 it != file_id_to_file_entry_map_.end(); ++it) { | |
| 351 result.push_back(*it->second); | |
| 352 } | |
| 353 return result; | |
| 354 } | |
| 355 | |
| 356 void SavedFilesService::SavedFiles::MaybeCompactSequenceNumbers() { | |
|
Matt Giuca
2013/05/22 08:26:14
DCHECK(g_max_sequence_number >= g_max_saved_file_e
Sam McNally
2013/05/23 03:47:28
Done.
| |
| 357 std::map<int, SavedFileEntry*>::reverse_iterator it = | |
| 358 saved_file_lru_.rbegin(); | |
| 359 if (it == saved_file_lru_.rend()) | |
| 360 return; | |
| 361 | |
| 362 if (it->first < g_max_sequence_number) | |
|
Matt Giuca
2013/05/22 08:26:14
// Only compact sequence numbers if the last entry
Sam McNally
2013/05/23 03:47:28
Done.
| |
| 363 return; | |
| 364 | |
| 365 int sequence_number = 0; | |
| 366 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | |
| 367 for (std::map<int, SavedFileEntry*>::iterator it = saved_file_lru_.begin(); | |
| 368 it != saved_file_lru_.end(); ++it) { | |
| 369 sequence_number++; | |
|
Matt Giuca
2013/05/22 08:26:14
Nit: ++sequence_number (style)
| |
| 370 if (it->second->sequence_number == sequence_number) | |
| 371 continue; | |
| 372 | |
| 373 SavedFileEntry* file_entry = it->second; | |
| 374 file_entry->sequence_number = sequence_number; | |
| 375 UpdateSavedFileEntry(prefs, extension_id_, *file_entry); | |
| 376 if (it == saved_file_lru_.begin()) { | |
|
Matt Giuca
2013/05/22 08:26:14
You don't (really) need this special case. The ove
Sam McNally
2013/05/23 03:47:28
Done.
| |
| 377 saved_file_lru_.erase(it); | |
| 378 it = saved_file_lru_.insert(std::make_pair(file_entry->sequence_number, | |
| 379 file_entry)).first; | |
| 380 } else { | |
| 381 saved_file_lru_.erase(it--); | |
|
Matt Giuca
2013/05/22 08:26:14
This relies on undefined behaviour.
"Iterators ...
Sam McNally
2013/05/23 03:47:28
Switched to hint following.
| |
| 382 it = saved_file_lru_.insert( | |
| 383 it, std::make_pair(file_entry->sequence_number, file_entry)); | |
| 384 } | |
| 385 } | |
| 386 } | |
| 387 | |
| 388 // static | |
| 389 void SavedFilesService::SetMaxSequenceNumberForTest(int max_value) { | |
| 390 g_max_sequence_number = max_value; | |
| 391 } | |
| 392 | |
| 393 // static | |
| 394 void SavedFilesService::ClearMaxSequenceNumberForTest() { | |
| 395 g_max_sequence_number = kMaxSequenceNumber; | |
| 396 } | |
| 397 | |
| 398 // static | |
| 399 void SavedFilesService::SetLruSizeForTest(int size) { | |
| 400 g_max_saved_file_entries = size; | |
| 401 } | |
| 402 | |
| 403 // static | |
| 404 void SavedFilesService::ClearLruSizeForTest() { | |
| 405 g_max_saved_file_entries = kMaxSavedFileEntries; | |
| 406 } | |
| 407 | |
| 408 } // namespace apps | |
| OLD | NEW |