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..453d2a1b2bf4c0d1ddd30ce92ad90aeb835826e9 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,14 +14,13 @@ |
#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 "content/public/browser/browser_thread.h" |
+#include "chrome/common/pref_names.h" |
+#include "components/pref_registry/pref_registry_syncable.h" |
#include "extensions/browser/event_router.h" |
#include "extensions/browser/extension_registry.h" |
#include "extensions/browser/extension_system.h" |
#include "webkit/browser/fileapi/external_mount_points.h" |
-using content::BrowserThread; |
- |
namespace chromeos { |
namespace file_system_provider { |
namespace { |
@@ -38,6 +39,15 @@ ProvidedFileSystemInterface* CreateProvidedFileSystem( |
} // namespace |
+const char kPrefKeyFileSystemId[] = "file-system-id"; |
+const char kPrefKeyFileSystemName[] = "file-system-name"; |
+ |
+void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { |
+ registry->RegisterDictionaryPref( |
+ prefs::kFileSystemProviderMounted, |
+ 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()) { |
@@ -88,7 +99,7 @@ void Service::SetFileSystemFactoryForTests( |
bool Service::MountFileSystem(const std::string& extension_id, |
const std::string& file_system_id, |
const std::string& file_system_name) { |
- DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
// If already exists a file system provided by the same extension with this |
// id, then abort. |
@@ -161,7 +172,7 @@ bool Service::MountFileSystem(const std::string& extension_id, |
bool Service::UnmountFileSystem(const std::string& extension_id, |
const std::string& file_system_id) { |
- DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
const ProvidedFileSystemMap::iterator file_system_it = |
file_system_map_.find(FileSystemKey(extension_id, file_system_id)); |
@@ -208,7 +219,7 @@ bool Service::UnmountFileSystem(const std::string& extension_id, |
bool Service::RequestUnmount(const std::string& extension_id, |
const std::string& file_system_id) { |
- DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
ProvidedFileSystemMap::iterator file_system_it = |
file_system_map_.find(FileSystemKey(extension_id, file_system_id)); |
@@ -223,7 +234,7 @@ bool Service::RequestUnmount(const std::string& extension_id, |
} |
std::vector<ProvidedFileSystemInfo> Service::GetProvidedFileSystemInfoList() { |
- DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
std::vector<ProvidedFileSystemInfo> result; |
for (ProvidedFileSystemMap::const_iterator it = file_system_map_.begin(); |
@@ -237,7 +248,7 @@ std::vector<ProvidedFileSystemInfo> Service::GetProvidedFileSystemInfoList() { |
ProvidedFileSystemInterface* Service::GetProvidedFileSystem( |
const std::string& extension_id, |
const std::string& file_system_id) { |
- DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
const ProvidedFileSystemMap::const_iterator file_system_it = |
file_system_map_.find(FileSystemKey(extension_id, file_system_id)); |
@@ -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,9 +283,14 @@ 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); |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
const MountPointNameToKeyMap::const_iterator mapping_it = |
mount_point_name_to_key_map_.find(mount_point_name); |
@@ -297,5 +318,85 @@ 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(kPrefKeyFileSystemId, it->file_system_id()); |
+ file_system->SetString(kPrefKeyFileSystemName, it->file_system_name()); |
+ file_systems->Append(file_system); |
+ } |
+ |
+ PrefService* pref_service = profile_->GetPrefs(); |
+ DCHECK(pref_service); |
+ pref_service->Set(prefs::kFileSystemProviderMounted, extensions); |
+ pref_service->CommitPendingWrite(); |
+} |
+ |
+void Service::ForgetFileSystems(const std::string& extension_id) { |
+ PrefService* pref_service = profile_->GetPrefs(); |
+ DCHECK(pref_service); |
+ |
+ DictionaryPrefUpdate update(pref_service, prefs::kFileSystemProviderMounted); |
+ 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::kFileSystemProviderMounted); |
+ DCHECK(extensions); |
+ |
+ const base::ListValue* file_systems = NULL; |
+ |
+ if (!extensions->GetList(extension_id, &file_systems)) |
+ 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(kPrefKeyFileSystemId, &file_system_id); |
+ DCHECK(!file_system_id.empty()); |
+ |
+ std::string file_system_name; |
+ file_system->GetString(kPrefKeyFileSystemName, &file_system_name); |
+ DCHECK(!file_system_name.empty()); |
+ |
+ if (file_system_id.empty() || file_system_name.empty()) { |
+ LOG(ERROR) |
+ << "Malformed provided file system information in preferences."; |
+ 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 |