| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/file_system_provider/service.h" | 5 #include "chrome/browser/chromeos/file_system_provider/service.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/prefs/scoped_user_pref_update.h" | 9 #include "base/prefs/scoped_user_pref_update.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 Profile* profile, | 32 Profile* profile, |
| 33 const ProvidedFileSystemInfo& file_system_info) { | 33 const ProvidedFileSystemInfo& file_system_info) { |
| 34 DCHECK(profile); | 34 DCHECK(profile); |
| 35 return new ProvidedFileSystem(profile, file_system_info); | 35 return new ProvidedFileSystem(profile, file_system_info); |
| 36 } | 36 } |
| 37 | 37 |
| 38 } // namespace | 38 } // namespace |
| 39 | 39 |
| 40 const char kPrefKeyFileSystemId[] = "file-system-id"; | 40 const char kPrefKeyFileSystemId[] = "file-system-id"; |
| 41 const char kPrefKeyDisplayName[] = "display-name"; | 41 const char kPrefKeyDisplayName[] = "display-name"; |
| 42 const char kPrefKeyWritable[] = "writable"; | |
| 43 | 42 |
| 44 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { | 43 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { |
| 45 registry->RegisterDictionaryPref( | 44 registry->RegisterDictionaryPref( |
| 46 prefs::kFileSystemProviderMounted, | 45 prefs::kFileSystemProviderMounted, |
| 47 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 46 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 48 } | 47 } |
| 49 | 48 |
| 50 Service::Service(Profile* profile, | 49 Service::Service(Profile* profile, |
| 51 extensions::ExtensionRegistry* extension_registry) | 50 extensions::ExtensionRegistry* extension_registry) |
| 52 : profile_(profile), | 51 : profile_(profile), |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 } | 94 } |
| 96 | 95 |
| 97 void Service::SetFileSystemFactoryForTesting( | 96 void Service::SetFileSystemFactoryForTesting( |
| 98 const FileSystemFactoryCallback& factory_callback) { | 97 const FileSystemFactoryCallback& factory_callback) { |
| 99 DCHECK(!factory_callback.is_null()); | 98 DCHECK(!factory_callback.is_null()); |
| 100 file_system_factory_ = factory_callback; | 99 file_system_factory_ = factory_callback; |
| 101 } | 100 } |
| 102 | 101 |
| 103 bool Service::MountFileSystem(const std::string& extension_id, | 102 bool Service::MountFileSystem(const std::string& extension_id, |
| 104 const std::string& file_system_id, | 103 const std::string& file_system_id, |
| 105 const std::string& display_name, | 104 const std::string& display_name) { |
| 106 bool writable) { | |
| 107 DCHECK(thread_checker_.CalledOnValidThread()); | 105 DCHECK(thread_checker_.CalledOnValidThread()); |
| 108 | 106 |
| 109 // If already exists a file system provided by the same extension with this | 107 // If already exists a file system provided by the same extension with this |
| 110 // id, then abort. | 108 // id, then abort. |
| 111 if (GetProvidedFileSystem(extension_id, file_system_id)) { | 109 if (GetProvidedFileSystem(extension_id, file_system_id)) { |
| 112 FOR_EACH_OBSERVER(Observer, | 110 FOR_EACH_OBSERVER(Observer, |
| 113 observers_, | 111 observers_, |
| 114 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), | 112 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
| 115 base::File::FILE_ERROR_EXISTS)); | 113 base::File::FILE_ERROR_EXISTS)); |
| 116 return false; | 114 return false; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 144 Observer, | 142 Observer, |
| 145 observers_, | 143 observers_, |
| 146 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), | 144 OnProvidedFileSystemMount(ProvidedFileSystemInfo(), |
| 147 base::File::FILE_ERROR_INVALID_OPERATION)); | 145 base::File::FILE_ERROR_INVALID_OPERATION)); |
| 148 return false; | 146 return false; |
| 149 } | 147 } |
| 150 | 148 |
| 151 // Store the file system descriptor. Use the mount point name as the file | 149 // Store the file system descriptor. Use the mount point name as the file |
| 152 // system provider file system id. | 150 // system provider file system id. |
| 153 // Examples: | 151 // Examples: |
| 154 // file_system_id = hello_world | 152 // file_system_id = 41 |
| 155 // mount_point_name = b33f1337-hello_world-5aa5 | 153 // mount_point_name = b33f1337-41-5aa5 |
| 156 // writable = false | 154 // mount_path = /provided/b33f1337-41-5aa5 |
| 157 // mount_path = /provided/b33f1337-hello_world-5aa5 | |
| 158 ProvidedFileSystemInfo file_system_info( | 155 ProvidedFileSystemInfo file_system_info( |
| 159 extension_id, file_system_id, display_name, writable, mount_path); | 156 extension_id, file_system_id, display_name, mount_path); |
| 160 | 157 |
| 161 ProvidedFileSystemInterface* file_system = | 158 ProvidedFileSystemInterface* file_system = |
| 162 file_system_factory_.Run(profile_, file_system_info); | 159 file_system_factory_.Run(profile_, file_system_info); |
| 163 DCHECK(file_system); | 160 DCHECK(file_system); |
| 164 file_system_map_[FileSystemKey(extension_id, file_system_id)] = file_system; | 161 file_system_map_[FileSystemKey(extension_id, file_system_id)] = file_system; |
| 165 mount_point_name_to_key_map_[mount_point_name] = | 162 mount_point_name_to_key_map_[mount_point_name] = |
| 166 FileSystemKey(extension_id, file_system_id); | 163 FileSystemKey(extension_id, file_system_id); |
| 167 RememberFileSystem(file_system_info); | 164 RememberFileSystem(file_system_info); |
| 168 | 165 |
| 169 FOR_EACH_OBSERVER( | 166 FOR_EACH_OBSERVER( |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 } | 324 } |
| 328 } | 325 } |
| 329 | 326 |
| 330 void Service::RememberFileSystem( | 327 void Service::RememberFileSystem( |
| 331 const ProvidedFileSystemInfo& file_system_info) { | 328 const ProvidedFileSystemInfo& file_system_info) { |
| 332 base::DictionaryValue* file_system = new base::DictionaryValue(); | 329 base::DictionaryValue* file_system = new base::DictionaryValue(); |
| 333 file_system->SetStringWithoutPathExpansion(kPrefKeyFileSystemId, | 330 file_system->SetStringWithoutPathExpansion(kPrefKeyFileSystemId, |
| 334 file_system_info.file_system_id()); | 331 file_system_info.file_system_id()); |
| 335 file_system->SetStringWithoutPathExpansion(kPrefKeyDisplayName, | 332 file_system->SetStringWithoutPathExpansion(kPrefKeyDisplayName, |
| 336 file_system_info.display_name()); | 333 file_system_info.display_name()); |
| 337 file_system->SetBooleanWithoutPathExpansion(kPrefKeyWritable, | |
| 338 file_system_info.writable()); | |
| 339 | 334 |
| 340 PrefService* const pref_service = profile_->GetPrefs(); | 335 PrefService* const pref_service = profile_->GetPrefs(); |
| 341 DCHECK(pref_service); | 336 DCHECK(pref_service); |
| 342 | 337 |
| 343 DictionaryPrefUpdate dict_update(pref_service, | 338 DictionaryPrefUpdate dict_update(pref_service, |
| 344 prefs::kFileSystemProviderMounted); | 339 prefs::kFileSystemProviderMounted); |
| 345 | 340 |
| 346 base::DictionaryValue* file_systems_per_extension = NULL; | 341 base::DictionaryValue* file_systems_per_extension = NULL; |
| 347 if (!dict_update->GetDictionaryWithoutPathExpansion( | 342 if (!dict_update->GetDictionaryWithoutPathExpansion( |
| 348 file_system_info.extension_id(), &file_systems_per_extension)) { | 343 file_system_info.extension_id(), &file_systems_per_extension)) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 !it.IsAtEnd(); | 390 !it.IsAtEnd(); |
| 396 it.Advance()) { | 391 it.Advance()) { |
| 397 const base::Value* file_system_value = NULL; | 392 const base::Value* file_system_value = NULL; |
| 398 const base::DictionaryValue* file_system = NULL; | 393 const base::DictionaryValue* file_system = NULL; |
| 399 file_systems_per_extension_copy->GetWithoutPathExpansion( | 394 file_systems_per_extension_copy->GetWithoutPathExpansion( |
| 400 it.key(), &file_system_value); | 395 it.key(), &file_system_value); |
| 401 DCHECK(file_system_value); | 396 DCHECK(file_system_value); |
| 402 | 397 |
| 403 std::string file_system_id; | 398 std::string file_system_id; |
| 404 std::string display_name; | 399 std::string display_name; |
| 405 bool writable; | 400 if (file_system_value->GetAsDictionary(&file_system)) { |
| 401 file_system->GetStringWithoutPathExpansion(kPrefKeyFileSystemId, |
| 402 &file_system_id); |
| 403 file_system->GetStringWithoutPathExpansion(kPrefKeyDisplayName, |
| 404 &display_name); |
| 405 } |
| 406 | 406 |
| 407 if (!file_system_value->GetAsDictionary(&file_system) || | 407 if (file_system_id.empty() || display_name.empty()) { |
| 408 !file_system->GetStringWithoutPathExpansion(kPrefKeyFileSystemId, | |
| 409 &file_system_id) || | |
| 410 !file_system->GetStringWithoutPathExpansion(kPrefKeyDisplayName, | |
| 411 &display_name) || | |
| 412 !file_system->GetBooleanWithoutPathExpansion(kPrefKeyWritable, | |
| 413 &writable) || | |
| 414 file_system_id.empty() || display_name.empty()) { | |
| 415 LOG(ERROR) | 408 LOG(ERROR) |
| 416 << "Malformed provided file system information in preferences."; | 409 << "Malformed provided file system information in preferences."; |
| 417 continue; | 410 continue; |
| 418 } | 411 } |
| 419 | 412 |
| 420 const bool result = | 413 const bool result = |
| 421 MountFileSystem(extension_id, file_system_id, display_name, writable); | 414 MountFileSystem(extension_id, file_system_id, display_name); |
| 422 if (!result) { | 415 if (!result) { |
| 423 LOG(ERROR) << "Failed to restore a provided file system from " | 416 LOG(ERROR) << "Failed to restore a provided file system from " |
| 424 << "preferences: " << extension_id << ", " << file_system_id | 417 << "preferences: " << extension_id << ", " << file_system_id |
| 425 << ", " << display_name << "."; | 418 << ", " << display_name << "."; |
| 426 // Since remounting of the file system failed, then remove it from | 419 // Since remounting of the file system failed, then remove it from |
| 427 // preferences to avoid remounting it over and over again with a failure. | 420 // preferences to avoid remounting it over and over again with a failure. |
| 428 ForgetFileSystem(extension_id, file_system_id); | 421 ForgetFileSystem(extension_id, file_system_id); |
| 429 } | 422 } |
| 430 } | 423 } |
| 431 } | 424 } |
| 432 | 425 |
| 433 } // namespace file_system_provider | 426 } // namespace file_system_provider |
| 434 } // namespace chromeos | 427 } // namespace chromeos |
| OLD | NEW |