| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/api/system_storage/system_storage_api.h" | 5 #include "extensions/browser/api/system_storage/system_storage_api.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 | 8 |
| 9 using storage_monitor::StorageMonitor; | 9 using storage_monitor::StorageMonitor; |
| 10 | 10 |
| 11 namespace extensions { | 11 namespace extensions { |
| 12 | 12 |
| 13 using api::system_storage::StorageUnitInfo; | 13 using api::system_storage::StorageUnitInfo; |
| 14 namespace EjectDevice = api::system_storage::EjectDevice; | 14 namespace EjectDevice = api::system_storage::EjectDevice; |
| 15 namespace GetAvailableCapacity = api::system_storage::GetAvailableCapacity; | 15 namespace GetAvailableCapacity = api::system_storage::GetAvailableCapacity; |
| 16 | 16 |
| 17 SystemStorageGetInfoFunction::SystemStorageGetInfoFunction() { | 17 SystemStorageGetInfoFunction::SystemStorageGetInfoFunction() { |
| 18 } | 18 } |
| 19 | 19 |
| 20 SystemStorageGetInfoFunction::~SystemStorageGetInfoFunction() { | 20 SystemStorageGetInfoFunction::~SystemStorageGetInfoFunction() { |
| 21 } | 21 } |
| 22 | 22 |
| 23 bool SystemStorageGetInfoFunction::RunAsync() { | 23 ExtensionFunction::ResponseAction SystemStorageGetInfoFunction::Run() { |
| 24 StorageInfoProvider::Get()->StartQueryInfo(base::Bind( | 24 StorageInfoProvider::Get()->StartQueryInfo(base::Bind( |
| 25 &SystemStorageGetInfoFunction::OnGetStorageInfoCompleted, this)); | 25 &SystemStorageGetInfoFunction::OnGetStorageInfoCompleted, this)); |
| 26 return true; | 26 return RespondLater(); |
| 27 } | 27 } |
| 28 | 28 |
| 29 void SystemStorageGetInfoFunction::OnGetStorageInfoCompleted(bool success) { | 29 void SystemStorageGetInfoFunction::OnGetStorageInfoCompleted(bool success) { |
| 30 if (success) { | 30 if (success) { |
| 31 results_ = api::system_storage::GetInfo::Results::Create( | 31 Respond(ArgumentList(api::system_storage::GetInfo::Results::Create( |
| 32 StorageInfoProvider::Get()->storage_unit_info_list()); | 32 StorageInfoProvider::Get()->storage_unit_info_list()))); |
| 33 } else { | 33 } else { |
| 34 SetError("Error occurred when querying storage information."); | 34 Respond(Error("Error occurred when querying storage information.")); |
| 35 } | 35 } |
| 36 | |
| 37 SendResponse(success); | |
| 38 } | 36 } |
| 39 | 37 |
| 40 SystemStorageEjectDeviceFunction::~SystemStorageEjectDeviceFunction() { | 38 SystemStorageEjectDeviceFunction::~SystemStorageEjectDeviceFunction() { |
| 41 } | 39 } |
| 42 | 40 |
| 43 bool SystemStorageEjectDeviceFunction::RunAsync() { | 41 ExtensionFunction::ResponseAction SystemStorageEjectDeviceFunction::Run() { |
| 44 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 45 | 43 |
| 46 std::unique_ptr<EjectDevice::Params> params( | 44 std::unique_ptr<EjectDevice::Params> params( |
| 47 EjectDevice::Params::Create(*args_)); | 45 EjectDevice::Params::Create(*args_)); |
| 48 EXTENSION_FUNCTION_VALIDATE(params.get()); | 46 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 49 | 47 |
| 50 StorageMonitor::GetInstance()->EnsureInitialized( | 48 StorageMonitor::GetInstance()->EnsureInitialized( |
| 51 base::Bind(&SystemStorageEjectDeviceFunction::OnStorageMonitorInit, | 49 base::Bind(&SystemStorageEjectDeviceFunction::OnStorageMonitorInit, |
| 52 this, | 50 this, |
| 53 params->id)); | 51 params->id)); |
| 54 return true; | 52 // EnsureInitialized() above can result in synchronous Respond(). |
| 53 return did_respond() ? AlreadyResponded() : RespondLater(); |
| 55 } | 54 } |
| 56 | 55 |
| 57 void SystemStorageEjectDeviceFunction::OnStorageMonitorInit( | 56 void SystemStorageEjectDeviceFunction::OnStorageMonitorInit( |
| 58 const std::string& transient_device_id) { | 57 const std::string& transient_device_id) { |
| 59 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); | 58 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); |
| 60 StorageMonitor* monitor = StorageMonitor::GetInstance(); | 59 StorageMonitor* monitor = StorageMonitor::GetInstance(); |
| 61 std::string device_id_str = | 60 std::string device_id_str = |
| 62 StorageMonitor::GetInstance()->GetDeviceIdForTransientId( | 61 StorageMonitor::GetInstance()->GetDeviceIdForTransientId( |
| 63 transient_device_id); | 62 transient_device_id); |
| 64 | 63 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 83 case StorageMonitor::EJECT_IN_USE: | 82 case StorageMonitor::EJECT_IN_USE: |
| 84 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_IN_USE; | 83 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_IN_USE; |
| 85 break; | 84 break; |
| 86 case StorageMonitor::EJECT_NO_SUCH_DEVICE: | 85 case StorageMonitor::EJECT_NO_SUCH_DEVICE: |
| 87 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_NO_SUCH_DEVICE; | 86 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_NO_SUCH_DEVICE; |
| 88 break; | 87 break; |
| 89 case StorageMonitor::EJECT_FAILURE: | 88 case StorageMonitor::EJECT_FAILURE: |
| 90 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE; | 89 result = api::system_storage::EJECT_DEVICE_RESULT_CODE_FAILURE; |
| 91 } | 90 } |
| 92 | 91 |
| 93 SetResult(base::MakeUnique<base::StringValue>( | 92 Respond(OneArgument(base::MakeUnique<base::StringValue>( |
| 94 api::system_storage::ToString(result))); | 93 api::system_storage::ToString(result)))); |
| 95 SendResponse(true); | |
| 96 } | 94 } |
| 97 | 95 |
| 98 SystemStorageGetAvailableCapacityFunction:: | 96 SystemStorageGetAvailableCapacityFunction:: |
| 99 SystemStorageGetAvailableCapacityFunction() { | 97 SystemStorageGetAvailableCapacityFunction() { |
| 100 } | 98 } |
| 101 | 99 |
| 102 SystemStorageGetAvailableCapacityFunction:: | 100 SystemStorageGetAvailableCapacityFunction:: |
| 103 ~SystemStorageGetAvailableCapacityFunction() { | 101 ~SystemStorageGetAvailableCapacityFunction() { |
| 104 } | 102 } |
| 105 | 103 |
| 106 bool SystemStorageGetAvailableCapacityFunction::RunAsync() { | 104 ExtensionFunction::ResponseAction |
| 105 SystemStorageGetAvailableCapacityFunction::Run() { |
| 107 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 108 | 107 |
| 109 std::unique_ptr<GetAvailableCapacity::Params> params( | 108 std::unique_ptr<GetAvailableCapacity::Params> params( |
| 110 GetAvailableCapacity::Params::Create(*args_)); | 109 GetAvailableCapacity::Params::Create(*args_)); |
| 111 EXTENSION_FUNCTION_VALIDATE(params.get()); | 110 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 112 | 111 |
| 113 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( | 112 StorageMonitor::GetInstance()->EnsureInitialized(base::Bind( |
| 114 &SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit, | 113 &SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit, |
| 115 this, | 114 this, |
| 116 params->id)); | 115 params->id)); |
| 117 return true; | 116 return RespondLater(); |
| 118 } | 117 } |
| 119 | 118 |
| 120 void SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit( | 119 void SystemStorageGetAvailableCapacityFunction::OnStorageMonitorInit( |
| 121 const std::string& transient_id) { | 120 const std::string& transient_id) { |
| 122 content::BrowserThread::PostTaskAndReplyWithResult( | 121 content::BrowserThread::PostTaskAndReplyWithResult( |
| 123 content::BrowserThread::FILE, | 122 content::BrowserThread::FILE, |
| 124 FROM_HERE, | 123 FROM_HERE, |
| 125 base::Bind( | 124 base::Bind( |
| 126 &StorageInfoProvider::GetStorageFreeSpaceFromTransientIdOnFileThread, | 125 &StorageInfoProvider::GetStorageFreeSpaceFromTransientIdOnFileThread, |
| 127 StorageInfoProvider::Get(), | 126 StorageInfoProvider::Get(), |
| 128 transient_id), | 127 transient_id), |
| 129 base::Bind(&SystemStorageGetAvailableCapacityFunction::OnQueryCompleted, | 128 base::Bind(&SystemStorageGetAvailableCapacityFunction::OnQueryCompleted, |
| 130 this, | 129 this, |
| 131 transient_id)); | 130 transient_id)); |
| 132 } | 131 } |
| 133 | 132 |
| 134 void SystemStorageGetAvailableCapacityFunction::OnQueryCompleted( | 133 void SystemStorageGetAvailableCapacityFunction::OnQueryCompleted( |
| 135 const std::string& transient_id, | 134 const std::string& transient_id, |
| 136 double available_capacity) { | 135 double available_capacity) { |
| 137 bool success = available_capacity >= 0; | 136 bool success = available_capacity >= 0; |
| 138 if (success) { | 137 if (success) { |
| 139 api::system_storage::StorageAvailableCapacityInfo result; | 138 api::system_storage::StorageAvailableCapacityInfo result; |
| 140 result.id = transient_id; | 139 result.id = transient_id; |
| 141 result.available_capacity = available_capacity; | 140 result.available_capacity = available_capacity; |
| 142 SetResult(result.ToValue()); | 141 Respond(OneArgument(result.ToValue())); |
| 143 } else { | 142 } else { |
| 144 SetError("Error occurred when querying available capacity."); | 143 Respond(Error("Error occurred when querying available capacity.")); |
| 145 } | 144 } |
| 146 SendResponse(success); | |
| 147 } | 145 } |
| 148 | 146 |
| 149 } // namespace extensions | 147 } // namespace extensions |
| OLD | NEW |