| Index: apps/saved_files_service_impl.cc
|
| diff --git a/apps/saved_files_service.cc b/apps/saved_files_service_impl.cc
|
| similarity index 69%
|
| rename from apps/saved_files_service.cc
|
| rename to apps/saved_files_service_impl.cc
|
| index 14e25ffabe07ed29662380947c5dc6e9ef0ab573..8d9df9e7d1431a58ac6a900faaa872206bdc1711 100644
|
| --- a/apps/saved_files_service.cc
|
| +++ b/apps/saved_files_service_impl.cc
|
| @@ -2,7 +2,7 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "apps/saved_files_service.h"
|
| +#include "apps/saved_files_service_impl.h"
|
|
|
| #include <stdint.h>
|
|
|
| @@ -30,6 +30,7 @@ using extensions::APIPermission;
|
| using extensions::Extension;
|
| using extensions::ExtensionHost;
|
| using extensions::ExtensionPrefs;
|
| +using extensions::SavedFilesService;
|
|
|
| namespace {
|
|
|
| @@ -54,12 +55,12 @@ const int kMaxSequenceNumber = INT32_MAX;
|
| size_t g_max_saved_file_entries = kMaxSavedFileEntries;
|
| int g_max_sequence_number = kMaxSequenceNumber;
|
|
|
| -// Persists a SavedFileEntry in ExtensionPrefs.
|
| +// Persists a saved file entry in ExtensionPrefs.
|
| void AddSavedFileEntry(ExtensionPrefs* prefs,
|
| const std::string& extension_id,
|
| - const SavedFileEntry& file_entry) {
|
| - ExtensionPrefs::ScopedDictionaryUpdate update(
|
| - prefs, extension_id, kFileEntries);
|
| + const SavedFilesService::Entry& file_entry) {
|
| + ExtensionPrefs::ScopedDictionaryUpdate update(prefs, extension_id,
|
| + kFileEntries);
|
| auto file_entries = update.Create();
|
| DCHECK(!file_entries->GetDictionaryWithoutPathExpansion(file_entry.id, NULL));
|
|
|
| @@ -73,12 +74,13 @@ void AddSavedFileEntry(ExtensionPrefs* prefs,
|
| std::move(file_entry_dict));
|
| }
|
|
|
| -// Updates the sequence_number of a SavedFileEntry persisted in ExtensionPrefs.
|
| +// Updates the sequence_number of a saved file entry persisted in
|
| +// ExtensionPrefs.
|
| void UpdateSavedFileEntry(ExtensionPrefs* prefs,
|
| const std::string& extension_id,
|
| - const SavedFileEntry& file_entry) {
|
| - ExtensionPrefs::ScopedDictionaryUpdate update(
|
| - prefs, extension_id, kFileEntries);
|
| + const SavedFilesService::Entry& file_entry) {
|
| + ExtensionPrefs::ScopedDictionaryUpdate update(prefs, extension_id,
|
| + kFileEntries);
|
| auto file_entries = update.Get();
|
| DCHECK(file_entries);
|
| std::unique_ptr<prefs::DictionaryValueUpdate> file_entry_dict;
|
| @@ -89,27 +91,27 @@ void UpdateSavedFileEntry(ExtensionPrefs* prefs,
|
| file_entry.sequence_number);
|
| }
|
|
|
| -// Removes a SavedFileEntry from ExtensionPrefs.
|
| +// Removes a saved file entry from ExtensionPrefs.
|
| void RemoveSavedFileEntry(ExtensionPrefs* prefs,
|
| const std::string& extension_id,
|
| const std::string& file_entry_id) {
|
| - ExtensionPrefs::ScopedDictionaryUpdate update(
|
| - prefs, extension_id, kFileEntries);
|
| + ExtensionPrefs::ScopedDictionaryUpdate update(prefs, extension_id,
|
| + kFileEntries);
|
| auto file_entries = update.Create();
|
| file_entries->RemoveWithoutPathExpansion(file_entry_id, NULL);
|
| }
|
|
|
| -// Clears all SavedFileEntry for the app from ExtensionPrefs.
|
| +// Clears all saved file entries for the app from ExtensionPrefs.
|
| void ClearSavedFileEntries(ExtensionPrefs* prefs,
|
| const std::string& extension_id) {
|
| prefs->UpdateExtensionPref(extension_id, kFileEntries, nullptr);
|
| }
|
|
|
| -// Returns all SavedFileEntries for the app.
|
| -std::vector<SavedFileEntry> GetSavedFileEntries(
|
| +// Returns all saved file entries for the app.
|
| +std::vector<SavedFilesService::Entry> GetSavedFileEntries(
|
| ExtensionPrefs* prefs,
|
| const std::string& extension_id) {
|
| - std::vector<SavedFileEntry> result;
|
| + std::vector<SavedFilesService::Entry> result;
|
| const base::DictionaryValue* file_entries = NULL;
|
| if (!prefs->ReadPrefAsDictionary(extension_id, kFileEntries, &file_entries))
|
| return result;
|
| @@ -132,26 +134,15 @@ std::vector<SavedFileEntry> GetSavedFileEntries(
|
| continue;
|
| if (!sequence_number)
|
| continue;
|
| - result.push_back(
|
| - SavedFileEntry(it.key(), file_path, is_directory, sequence_number));
|
| + result.push_back(SavedFilesService::Entry(it.key(), file_path, is_directory,
|
| + sequence_number));
|
| }
|
| return result;
|
| }
|
|
|
| } // namespace
|
|
|
| -SavedFileEntry::SavedFileEntry() : is_directory(false), sequence_number(0) {}
|
| -
|
| -SavedFileEntry::SavedFileEntry(const std::string& id,
|
| - const base::FilePath& path,
|
| - bool is_directory,
|
| - int sequence_number)
|
| - : id(id),
|
| - path(path),
|
| - is_directory(is_directory),
|
| - sequence_number(sequence_number) {}
|
| -
|
| -class SavedFilesService::SavedFiles {
|
| +class SavedFilesServiceImpl::SavedFiles {
|
| public:
|
| SavedFiles(content::BrowserContext* context, const std::string& extension_id);
|
| ~SavedFiles();
|
| @@ -161,8 +152,8 @@ class SavedFilesService::SavedFiles {
|
| bool is_directory);
|
| void EnqueueFileEntry(const std::string& id);
|
| bool IsRegistered(const std::string& id) const;
|
| - const SavedFileEntry* GetFileEntry(const std::string& id) const;
|
| - std::vector<SavedFileEntry> GetAllFileEntries() const;
|
| + const Entry* GetFileEntry(const std::string& id) const;
|
| + std::vector<Entry> GetAllFileEntries() const;
|
|
|
| private:
|
| // Compacts sequence numbers if the largest sequence number is
|
| @@ -176,34 +167,35 @@ class SavedFilesService::SavedFiles {
|
| const std::string extension_id_;
|
|
|
| // Contains all file entries that have been registered, keyed by ID.
|
| - std::unordered_map<std::string, std::unique_ptr<SavedFileEntry>>
|
| + std::unordered_map<std::string, std::unique_ptr<Entry>>
|
| registered_file_entries_;
|
|
|
| // The queue of file entries that have been retained, keyed by
|
| // sequence_number. Values are a subset of values in registered_file_entries_.
|
| // This should be kept in sync with file entries stored in extension prefs.
|
| - std::map<int, SavedFileEntry*> saved_file_lru_;
|
| + std::map<int, Entry*> saved_file_lru_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(SavedFiles);
|
| };
|
|
|
| // static
|
| -SavedFilesService* SavedFilesService::Get(content::BrowserContext* context) {
|
| - return SavedFilesServiceFactory::GetForBrowserContext(context);
|
| +SavedFilesServiceImpl* SavedFilesServiceImpl::Get(
|
| + content::BrowserContext* context) {
|
| + return SavedFilesServiceImplFactory::GetForBrowserContext(context);
|
| }
|
|
|
| -SavedFilesService::SavedFilesService(content::BrowserContext* context)
|
| +SavedFilesServiceImpl::SavedFilesServiceImpl(content::BrowserContext* context)
|
| : context_(context) {
|
| - registrar_.Add(this,
|
| - extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
|
| + registrar_.Add(this, extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
|
| content::NotificationService::AllSources());
|
| }
|
|
|
| -SavedFilesService::~SavedFilesService() {}
|
| +SavedFilesServiceImpl::~SavedFilesServiceImpl() {}
|
|
|
| -void SavedFilesService::Observe(int type,
|
| - const content::NotificationSource& source,
|
| - const content::NotificationDetails& details) {
|
| +void SavedFilesServiceImpl::Observe(
|
| + int type,
|
| + const content::NotificationSource& source,
|
| + const content::NotificationDetails& details) {
|
| DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, type);
|
| ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
|
| const Extension* extension = host->extension();
|
| @@ -213,19 +205,19 @@ void SavedFilesService::Observe(int type,
|
| }
|
| }
|
|
|
| -void SavedFilesService::RegisterFileEntry(const std::string& extension_id,
|
| - const std::string& id,
|
| - const base::FilePath& file_path,
|
| - bool is_directory) {
|
| +void SavedFilesServiceImpl::RegisterFileEntry(const std::string& extension_id,
|
| + const std::string& id,
|
| + const base::FilePath& file_path,
|
| + bool is_directory) {
|
| GetOrInsert(extension_id)->RegisterFileEntry(id, file_path, is_directory);
|
| }
|
|
|
| -void SavedFilesService::EnqueueFileEntry(const std::string& extension_id,
|
| - const std::string& id) {
|
| +void SavedFilesServiceImpl::EnqueueFileEntry(const std::string& extension_id,
|
| + const std::string& id) {
|
| GetOrInsert(extension_id)->EnqueueFileEntry(id);
|
| }
|
|
|
| -std::vector<SavedFileEntry> SavedFilesService::GetAllFileEntries(
|
| +std::vector<SavedFilesService::Entry> SavedFilesServiceImpl::GetAllFileEntries(
|
| const std::string& extension_id) {
|
| SavedFiles* saved_files = Get(extension_id);
|
| if (saved_files)
|
| @@ -233,18 +225,18 @@ std::vector<SavedFileEntry> SavedFilesService::GetAllFileEntries(
|
| return GetSavedFileEntries(ExtensionPrefs::Get(context_), extension_id);
|
| }
|
|
|
| -bool SavedFilesService::IsRegistered(const std::string& extension_id,
|
| - const std::string& id) {
|
| +bool SavedFilesServiceImpl::IsRegistered(const std::string& extension_id,
|
| + const std::string& id) {
|
| return GetOrInsert(extension_id)->IsRegistered(id);
|
| }
|
|
|
| -const SavedFileEntry* SavedFilesService::GetFileEntry(
|
| +const SavedFilesService::Entry* SavedFilesServiceImpl::GetFileEntry(
|
| const std::string& extension_id,
|
| const std::string& id) {
|
| return GetOrInsert(extension_id)->GetFileEntry(id);
|
| }
|
|
|
| -void SavedFilesService::ClearQueueIfNoRetainPermission(
|
| +void SavedFilesServiceImpl::ClearQueueIfNoRetainPermission(
|
| const Extension* extension) {
|
| if (!extension->permissions_data()->active_permissions().HasAPIPermission(
|
| APIPermission::kFileSystemRetainEntries)) {
|
| @@ -252,18 +244,18 @@ void SavedFilesService::ClearQueueIfNoRetainPermission(
|
| }
|
| }
|
|
|
| -void SavedFilesService::ClearQueue(const extensions::Extension* extension) {
|
| +void SavedFilesServiceImpl::ClearQueue(const extensions::Extension* extension) {
|
| ClearSavedFileEntries(ExtensionPrefs::Get(context_), extension->id());
|
| Clear(extension->id());
|
| }
|
|
|
| -void SavedFilesService::OnApplicationTerminating() {
|
| +void SavedFilesServiceImpl::OnApplicationTerminating() {
|
| // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular
|
| // as all extension hosts will be destroyed as a result of shutdown.
|
| registrar_.RemoveAll();
|
| }
|
|
|
| -SavedFilesService::SavedFiles* SavedFilesService::Get(
|
| +SavedFilesServiceImpl::SavedFiles* SavedFilesServiceImpl::Get(
|
| const std::string& extension_id) const {
|
| auto it = extension_id_to_saved_files_.find(extension_id);
|
| if (it != extension_id_to_saved_files_.end())
|
| @@ -272,7 +264,7 @@ SavedFilesService::SavedFiles* SavedFilesService::Get(
|
| return NULL;
|
| }
|
|
|
| -SavedFilesService::SavedFiles* SavedFilesService::GetOrInsert(
|
| +SavedFilesServiceImpl::SavedFiles* SavedFilesServiceImpl::GetOrInsert(
|
| const std::string& extension_id) {
|
| SavedFiles* saved_files = Get(extension_id);
|
| if (saved_files)
|
| @@ -286,19 +278,19 @@ SavedFilesService::SavedFiles* SavedFilesService::GetOrInsert(
|
| return saved_files;
|
| }
|
|
|
| -void SavedFilesService::Clear(const std::string& extension_id) {
|
| +void SavedFilesServiceImpl::Clear(const std::string& extension_id) {
|
| extension_id_to_saved_files_.erase(extension_id);
|
| }
|
|
|
| -SavedFilesService::SavedFiles::SavedFiles(content::BrowserContext* context,
|
| - const std::string& extension_id)
|
| +SavedFilesServiceImpl::SavedFiles::SavedFiles(content::BrowserContext* context,
|
| + const std::string& extension_id)
|
| : context_(context), extension_id_(extension_id) {
|
| LoadSavedFileEntriesFromPreferences();
|
| }
|
|
|
| -SavedFilesService::SavedFiles::~SavedFiles() {}
|
| +SavedFilesServiceImpl::SavedFiles::~SavedFiles() {}
|
|
|
| -void SavedFilesService::SavedFiles::RegisterFileEntry(
|
| +void SavedFilesServiceImpl::SavedFiles::RegisterFileEntry(
|
| const std::string& id,
|
| const base::FilePath& file_path,
|
| bool is_directory) {
|
| @@ -307,19 +299,19 @@ void SavedFilesService::SavedFiles::RegisterFileEntry(
|
| return;
|
|
|
| registered_file_entries_[id] =
|
| - base::MakeUnique<SavedFileEntry>(id, file_path, is_directory, 0);
|
| + base::MakeUnique<Entry>(id, file_path, is_directory, 0);
|
| }
|
|
|
| -void SavedFilesService::SavedFiles::EnqueueFileEntry(const std::string& id) {
|
| +void SavedFilesServiceImpl::SavedFiles::EnqueueFileEntry(
|
| + const std::string& id) {
|
| auto it = registered_file_entries_.find(id);
|
| DCHECK(it != registered_file_entries_.end());
|
|
|
| - SavedFileEntry* file_entry = it->second.get();
|
| + Entry* file_entry = it->second.get();
|
| int old_sequence_number = file_entry->sequence_number;
|
| if (!saved_file_lru_.empty()) {
|
| // Get the sequence number after the last file entry in the LRU.
|
| - std::map<int, SavedFileEntry*>::reverse_iterator it =
|
| - saved_file_lru_.rbegin();
|
| + std::map<int, Entry*>::reverse_iterator it = saved_file_lru_.rbegin();
|
| if (it->second == file_entry)
|
| return;
|
|
|
| @@ -337,7 +329,7 @@ void SavedFilesService::SavedFiles::EnqueueFileEntry(const std::string& id) {
|
| } else {
|
| AddSavedFileEntry(prefs, extension_id_, *file_entry);
|
| if (saved_file_lru_.size() > g_max_saved_file_entries) {
|
| - std::map<int, SavedFileEntry*>::iterator it = saved_file_lru_.begin();
|
| + std::map<int, Entry*>::iterator it = saved_file_lru_.begin();
|
| it->second->sequence_number = 0;
|
| RemoveSavedFileEntry(prefs, extension_id_, it->second->id);
|
| saved_file_lru_.erase(it);
|
| @@ -346,12 +338,13 @@ void SavedFilesService::SavedFiles::EnqueueFileEntry(const std::string& id) {
|
| MaybeCompactSequenceNumbers();
|
| }
|
|
|
| -bool SavedFilesService::SavedFiles::IsRegistered(const std::string& id) const {
|
| +bool SavedFilesServiceImpl::SavedFiles::IsRegistered(
|
| + const std::string& id) const {
|
| auto it = registered_file_entries_.find(id);
|
| return it != registered_file_entries_.end();
|
| }
|
|
|
| -const SavedFileEntry* SavedFilesService::SavedFiles::GetFileEntry(
|
| +const SavedFilesService::Entry* SavedFilesServiceImpl::SavedFiles::GetFileEntry(
|
| const std::string& id) const {
|
| auto it = registered_file_entries_.find(id);
|
| if (it == registered_file_entries_.end())
|
| @@ -360,9 +353,9 @@ const SavedFileEntry* SavedFilesService::SavedFiles::GetFileEntry(
|
| return it->second.get();
|
| }
|
|
|
| -std::vector<SavedFileEntry> SavedFilesService::SavedFiles::GetAllFileEntries()
|
| - const {
|
| - std::vector<SavedFileEntry> result;
|
| +std::vector<SavedFilesService::Entry>
|
| +SavedFilesServiceImpl::SavedFiles::GetAllFileEntries() const {
|
| + std::vector<Entry> result;
|
| for (auto it = registered_file_entries_.begin();
|
| it != registered_file_entries_.end(); ++it) {
|
| result.push_back(*it->second.get());
|
| @@ -370,12 +363,11 @@ std::vector<SavedFileEntry> SavedFilesService::SavedFiles::GetAllFileEntries()
|
| return result;
|
| }
|
|
|
| -void SavedFilesService::SavedFiles::MaybeCompactSequenceNumbers() {
|
| +void SavedFilesServiceImpl::SavedFiles::MaybeCompactSequenceNumbers() {
|
| DCHECK_GE(g_max_sequence_number, 0);
|
| DCHECK_GE(static_cast<size_t>(g_max_sequence_number),
|
| g_max_saved_file_entries);
|
| - std::map<int, SavedFileEntry*>::reverse_iterator it =
|
| - saved_file_lru_.rbegin();
|
| + std::map<int, Entry*>::reverse_iterator it = saved_file_lru_.rbegin();
|
| if (it == saved_file_lru_.rend())
|
| return;
|
|
|
| @@ -386,14 +378,13 @@ void SavedFilesService::SavedFiles::MaybeCompactSequenceNumbers() {
|
|
|
| int sequence_number = 0;
|
| ExtensionPrefs* prefs = ExtensionPrefs::Get(context_);
|
| - for (std::map<int, SavedFileEntry*>::iterator it = saved_file_lru_.begin();
|
| - it != saved_file_lru_.end();
|
| - ++it) {
|
| + for (std::map<int, Entry*>::iterator it = saved_file_lru_.begin();
|
| + it != saved_file_lru_.end(); ++it) {
|
| sequence_number++;
|
| if (it->second->sequence_number == sequence_number)
|
| continue;
|
|
|
| - SavedFileEntry* file_entry = it->second;
|
| + Entry* file_entry = it->second;
|
| file_entry->sequence_number = sequence_number;
|
| UpdateSavedFileEntry(prefs, extension_id_, *file_entry);
|
| saved_file_lru_.erase(it++);
|
| @@ -405,14 +396,12 @@ void SavedFilesService::SavedFiles::MaybeCompactSequenceNumbers() {
|
| }
|
| }
|
|
|
| -void SavedFilesService::SavedFiles::LoadSavedFileEntriesFromPreferences() {
|
| +void SavedFilesServiceImpl::SavedFiles::LoadSavedFileEntriesFromPreferences() {
|
| ExtensionPrefs* prefs = ExtensionPrefs::Get(context_);
|
| - std::vector<SavedFileEntry> saved_entries =
|
| - GetSavedFileEntries(prefs, extension_id_);
|
| - for (std::vector<SavedFileEntry>::iterator it = saved_entries.begin();
|
| - it != saved_entries.end();
|
| - ++it) {
|
| - std::unique_ptr<SavedFileEntry> file_entry(new SavedFileEntry(*it));
|
| + std::vector<Entry> saved_entries = GetSavedFileEntries(prefs, extension_id_);
|
| + for (std::vector<Entry>::iterator it = saved_entries.begin();
|
| + it != saved_entries.end(); ++it) {
|
| + std::unique_ptr<Entry> file_entry(new Entry(*it));
|
| const std::string& id = file_entry->id;
|
| saved_file_lru_.insert(
|
| std::make_pair(file_entry->sequence_number, file_entry.get()));
|
| @@ -421,22 +410,22 @@ void SavedFilesService::SavedFiles::LoadSavedFileEntriesFromPreferences() {
|
| }
|
|
|
| // static
|
| -void SavedFilesService::SetMaxSequenceNumberForTest(int max_value) {
|
| +void SavedFilesServiceImpl::SetMaxSequenceNumberForTest(int max_value) {
|
| g_max_sequence_number = max_value;
|
| }
|
|
|
| // static
|
| -void SavedFilesService::ClearMaxSequenceNumberForTest() {
|
| +void SavedFilesServiceImpl::ClearMaxSequenceNumberForTest() {
|
| g_max_sequence_number = kMaxSequenceNumber;
|
| }
|
|
|
| // static
|
| -void SavedFilesService::SetLruSizeForTest(int size) {
|
| +void SavedFilesServiceImpl::SetLruSizeForTest(int size) {
|
| g_max_saved_file_entries = size;
|
| }
|
|
|
| // static
|
| -void SavedFilesService::ClearLruSizeForTest() {
|
| +void SavedFilesServiceImpl::ClearLruSizeForTest() {
|
| g_max_saved_file_entries = kMaxSavedFileEntries;
|
| }
|
|
|
|
|