Chromium Code Reviews| Index: chrome/browser/chromeos/file_system_provider/service.cc |
| diff --git a/chrome/browser/chromeos/file_system_provider/service.cc b/chrome/browser/chromeos/file_system_provider/service.cc |
| index c989766f166c9add902e3a2614e5dde7a879fce8..281a3d3fa69431741eabf95d7714d3f4d10112bc 100644 |
| --- a/chrome/browser/chromeos/file_system_provider/service.cc |
| +++ b/chrome/browser/chromeos/file_system_provider/service.cc |
| @@ -5,6 +5,8 @@ |
| #include "chrome/browser/chromeos/file_system_provider/service.h" |
| #include "base/files/file_path.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/prefs/scoped_user_pref_update.h" |
| #include "base/stl_util.h" |
| #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" |
| #include "chrome/browser/chromeos/file_system_provider/observer.h" |
| @@ -12,6 +14,8 @@ |
| #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h" |
| #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h" |
| #include "chrome/browser/chromeos/file_system_provider/service_factory.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "components/pref_registry/pref_registry_syncable.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "extensions/browser/event_router.h" |
| #include "extensions/browser/extension_registry.h" |
| @@ -38,6 +42,12 @@ ProvidedFileSystemInterface* CreateProvidedFileSystem( |
| } // namespace |
| +void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { |
| + registry->RegisterDictionaryPref( |
| + prefs::kFSPMountedFileSystems, |
| + user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| +} |
| + |
| Service::Service(Profile* profile, |
| extensions::ExtensionRegistry* extension_registry) |
| : profile_(profile), |
| @@ -49,6 +59,7 @@ Service::Service(Profile* profile, |
| Service::~Service() { |
| extension_registry_->RemoveObserver(this); |
| + RememberFileSystems(); |
| ProvidedFileSystemMap::iterator it = file_system_map_.begin(); |
| while (it != file_system_map_.end()) { |
| @@ -251,6 +262,11 @@ void Service::OnExtensionUnloaded( |
| content::BrowserContext* browser_context, |
| const extensions::Extension* extension, |
| extensions::UnloadedExtensionInfo::Reason reason) { |
| + // If the reason is not a profile shutdown, then forget the mounted file |
| + // systems from preferences. |
| + if (reason != extensions::UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN) |
| + ForgetFileSystems(extension->id()); |
| + |
| // Unmount all of the provided file systems associated with this extension. |
| ProvidedFileSystemMap::iterator it = file_system_map_.begin(); |
| while (it != file_system_map_.end()) { |
| @@ -267,6 +283,11 @@ void Service::OnExtensionUnloaded( |
| } |
| } |
| +void Service::OnExtensionLoaded(content::BrowserContext* browser_context, |
| + const extensions::Extension* extension) { |
| + RestoreFileSystems(extension->id()); |
| +} |
| + |
| ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
| const std::string& mount_point_name) { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| @@ -297,5 +318,82 @@ void Service::OnRequestUnmountStatus( |
| } |
| } |
| +void Service::RememberFileSystems() { |
| + base::DictionaryValue extensions; |
| + const std::vector<ProvidedFileSystemInfo> file_system_info_list = |
| + GetProvidedFileSystemInfoList(); |
| + |
| + for (std::vector<ProvidedFileSystemInfo>::const_iterator it = |
| + file_system_info_list.begin(); |
| + it != file_system_info_list.end(); |
| + ++it) { |
| + base::ListValue* file_systems = NULL; |
| + if (!extensions.GetList(it->extension_id(), &file_systems)) { |
| + file_systems = new base::ListValue(); |
| + extensions.Set(it->extension_id(), file_systems); |
| + } |
| + |
| + base::DictionaryValue* file_system = new base::DictionaryValue(); |
| + file_system->SetString("file_system_id", it->file_system_id()); |
| + file_system->SetString("file_system_name", it->file_system_name()); |
|
hashimoto
2014/05/26 08:35:38
These keys should be constants.
mtomasz
2014/05/27 02:10:27
Done.
|
| + file_systems->Append(file_system); |
| + } |
| + |
| + PrefService* pref_service = profile_->GetPrefs(); |
| + DCHECK(pref_service); |
| + pref_service->Set(prefs::kFSPMountedFileSystems, extensions); |
|
hashimoto
2014/05/26 08:35:38
This overwrites the previously saved data.
Doesn't
mtomasz
2014/05/27 02:10:27
Yes, this basically remembers all *currently mount
mtomasz
2014/05/27 02:13:43
This is because loading is synchronous. All instal
|
| + pref_service->CommitPendingWrite(); |
| +} |
| + |
| +void Service::ForgetFileSystems(const std::string& extension_id) { |
| + PrefService* pref_service = profile_->GetPrefs(); |
| + DCHECK(pref_service); |
| + |
| + DictionaryPrefUpdate update(pref_service, prefs::kFSPMountedFileSystems); |
| + base::DictionaryValue* extensions = update.Get(); |
| + DCHECK(extensions); |
| + |
| + extensions->Remove(extension_id, NULL); |
| +} |
| + |
| +void Service::RestoreFileSystems(const std::string& extension_id) { |
| + PrefService* pref_service = profile_->GetPrefs(); |
| + DCHECK(pref_service); |
| + |
| + const base::DictionaryValue* extensions = |
| + pref_service->GetDictionary(prefs::kFSPMountedFileSystems); |
| + DCHECK(extensions); |
| + |
| + const base::ListValue* file_systems; |
|
hashimoto
2014/05/26 08:35:38
nit: Initialize this with NULL.
mtomasz
2014/05/27 02:10:27
Done.
|
| + |
| + if (!extensions->GetList(extension_id, &file_systems)) |
|
hashimoto
2014/05/26 08:35:38
Why other GetXXX calls are only DCHECKing the resu
mtomasz
2014/05/27 01:13:14
Extension doesn't need to be in preferences, eg. i
hashimoto
2014/05/27 11:29:24
Then maybe we should emit LOG for empty file_syste
mtomasz
2014/05/28 02:02:19
Done.
|
| + return; |
| + |
| + for (size_t i = 0; i < file_systems->GetSize(); ++i) { |
| + const base::DictionaryValue* file_system = NULL; |
| + file_systems->GetDictionary(i, &file_system); |
| + DCHECK(file_system); |
| + |
| + std::string file_system_id; |
| + file_system->GetString("file_system_id", &file_system_id); |
| + DCHECK(!file_system_id.empty()); |
| + |
| + std::string file_system_name; |
| + file_system->GetString("file_system_name", &file_system_name); |
| + DCHECK(!file_system_name.empty()); |
| + |
| + if (file_system_id.empty() || file_system_name.empty()) |
| + continue; |
| + |
| + const bool result = |
| + MountFileSystem(extension_id, file_system_id, file_system_name); |
| + if (!result) { |
| + LOG(ERROR) << "Failed to restore a provided file system from " |
| + << "preferences: " << extension_id << ", " << file_system_id |
| + << ", " << file_system_name << "."; |
| + } |
| + } |
| +} |
| + |
| } // namespace file_system_provider |
| } // namespace chromeos |