Chromium Code Reviews| Index: chrome/browser/extensions/api/systeminfo_storage/systeminfo_storage_api.cc |
| diff --git a/chrome/browser/extensions/api/systeminfo_storage/systeminfo_storage_api.cc b/chrome/browser/extensions/api/systeminfo_storage/systeminfo_storage_api.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b4ee856a3d5b3d0f0a2b915e2f3e4435bcda5804 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/systeminfo_storage/systeminfo_storage_api.cc |
| @@ -0,0 +1,91 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +#include "chrome/browser/extensions/api/systeminfo_storage/systeminfo_storage_api.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/string_piece.h" |
| +#include "base/values.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/extensions/api/systeminfo_storage/storage_info_provider.h" |
| + |
| +namespace extensions { |
| +using api::experimental_systeminfo_storage::StorageInfo; |
| +using api::experimental_systeminfo_storage::StorageUnitInfo; |
| +using content::BrowserThread; |
| + |
| +SysteminfoStorageGetFunction::SysteminfoStorageGetFunction() { |
| +} |
| + |
| +SysteminfoStorageGetFunction::~SysteminfoStorageGetFunction() { |
| +} |
| + |
| +bool SysteminfoStorageGetFunction::RunImpl() { |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&SysteminfoStorageGetFunction::WorkOnFileThread, this)); |
| + return true; |
| +} |
| + |
| +void SysteminfoStorageGetFunction::WorkOnFileThread() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + bool success = false; |
| + scoped_ptr<StorageInfoProvider> provider(StorageInfoProvider::Create()); |
| + StorageInfo info; |
| + if (provider.get() && provider->GetStorageInfo(&info)) { |
| + SetResult(info.ToValue().release()); |
| + success = true; |
| + } else { |
| + SetError("Error in querying storage information"); |
| + } |
| + |
| + // Response the result on UI thread. |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&SysteminfoStorageGetFunction::RespondOnUIThread, |
| + this, success)); |
| +} |
| + |
| +void SysteminfoStorageGetFunction::RespondOnUIThread(bool success) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + SendResponse(success); |
| +} |
| + |
| +#if !defined(OS_WIN) |
| +// A Mock implementation for temporary usage. Will be moved out for testing |
| +// once the platform specifc implementations are ready. |
| +const char kStorageTypeUnknownStr[] = "unknown"; |
|
Mihai Parparita -not on Chrome
2012/08/10 20:46:47
BTW, filed http://crbug.com/141940, which, if fixe
|
| +const char kStorageTypeHardDiskStr[] = "harddisk"; |
| +const char kStorageTypeUSBStr[] = "usb"; |
| +const char kStorageTypeMMCStr[] = "mmc"; |
| + |
| +class MockStorageInfoProvider : public StorageInfoProvider { |
| + public: |
| + MockStorageInfoProvider() {} |
| + virtual ~MockStorageInfoProvider() {} |
| + virtual bool GetStorageInfo(StorageInfo* info) OVERRIDE; |
| +}; |
| + |
| +bool MockStorageInfoProvider::GetStorageInfo(StorageInfo* info) { |
| + if (info == NULL) return false; |
| + linked_ptr<StorageUnitInfo> unit(new StorageUnitInfo()); |
| + unit->id = "0xbeaf"; |
| + unit->type = kStorageTypeUnknownStr; |
| + unit->capacity = 4098; |
| + unit->available_capacity = 1000; |
| + |
| + info->units.push_back(unit); |
| + return true; |
| +} |
| + |
| +// static |
| +StorageInfoProvider* StorageInfoProvider::Create() { |
| + return new MockStorageInfoProvider(); |
| +} |
| +#endif // !OS_WIN |
| + |
| +} // namespace extensions |