Commit e07a3cdb authored by hongbo.min@intel.com's avatar hongbo.min@intel.com

Add systemInfo.storage API implementation skeleton

BUG=141229, 136519
TEST=browser_tests --gtest_filter=SystemInfoStorageApiTest.*


Review URL: https://chromiumcodereview.appspot.com/10824232

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@152046 0039d316-1c4b-4281-b951-d872f2087c98
parent 13934f8e
hongbo.min@intel.com
ningxin.hu@intel.com
// 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/system_info_storage/storage_info_provider.h"
namespace extensions {
namespace systeminfo {
const char kStorageTypeUnknown[] = "unknown";
const char kStorageTypeHardDisk[] = "harddisk";
const char kStorageTypeRemovable[] = "removable";
} // namespace systeminfo
} // namespace extensions
// 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.
#ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_STORAGE_STORAGE_INFO_PROVIDER_H_
#define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_STORAGE_STORAGE_INFO_PROVIDER_H_
#include "chrome/common/extensions/api/experimental_system_info_storage.h"
namespace extensions {
namespace systeminfo {
// TODO(hmin): Here the storage type is declared as string type due to the IDL
// doesn't support enum type yet. Once http://crbug.com/141940 is fixed, we
// could reuse enum values generated by IDL compiler.
// Unknown storage type.
extern const char kStorageTypeUnknown[];
// Hard disk.
extern const char kStorageTypeHardDisk[];
// Removable storage, e.g. USB device, flash card reader.
extern const char kStorageTypeRemovable[];
} // namespace systeminfo
// An interface for retrieving storage information on different platforms.
class StorageInfoProvider {
public:
// Return a StorageInfoProvider instance. The caller is responsible for
// destroying it.
static StorageInfoProvider* Create();
virtual ~StorageInfoProvider() {}
// Return true if succeed to get storage information. Should be implemented
// on different platforms.
virtual bool GetStorageInfo(
api::experimental_system_info_storage::StorageInfo* info) = 0;
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_STORAGE_STORAGE_INFO_PROVIDER_H_
// 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/system_info_storage/storage_info_provider.h"
#include "base/string_util.h"
#include <windows.h>
namespace extensions {
using api::experimental_system_info_storage::StorageInfo;
using api::experimental_system_info_storage::StorageUnitInfo;
// Logical drive string length is 4. It contains one driver letter character,
// L":", L"\" and a terminating null character.
const unsigned long kMaxLogicalDriveString = 4 * 26;
// StorageInfoProvider implementation on Windows platform.
class StorageInfoProviderWin : public StorageInfoProvider {
public:
StorageInfoProviderWin();
virtual ~StorageInfoProviderWin();
virtual bool GetStorageInfo(StorageInfo* info) OVERRIDE;
};
// static
StorageInfoProvider* StorageInfoProvider::Create() {
return new StorageInfoProviderWin();
}
// StorageInfoProviderWin implementation.
StorageInfoProviderWin::StorageInfoProviderWin() {
}
StorageInfoProviderWin::~StorageInfoProviderWin() {
}
bool StorageInfoProviderWin::GetStorageInfo(StorageInfo* info) {
if (info == NULL) return false;
WCHAR logical_drive_strings[kMaxLogicalDriveString];
// The total length of drive string returned from GetLogicalDriveStrings
// must be divided exactly by 4 since each drive is represented by 4 wchars.
DWORD string_length = GetLogicalDriveStrings(
kMaxLogicalDriveString - 1, logical_drive_strings);
DCHECK(string_length % 4 == 0);
// No drive found, return false.
if (string_length == 0)
return false;
// Iterate the drive string by 4 wchars each step
for (unsigned int i = 0; i < string_length; i += 4) {
std::string type;
DWORD ret = GetDriveType(&logical_drive_strings[i]);
switch (ret) {
case DRIVE_FIXED:
type = systeminfo::kStorageTypeHardDisk;
break;
case DRIVE_REMOVABLE:
type = systeminfo::kStorageTypeRemovable;
break;
case DRIVE_UNKNOWN:
type = systeminfo::kStorageTypeUnknown;
break;
case DRIVE_CDROM: // CD ROM
case DRIVE_REMOTE: // Remote network drive
case DRIVE_NO_ROOT_DIR: // Invalid root path
case DRIVE_RAMDISK: // RAM disk
// TODO(hmin): Do we need to care about these drive types?
continue;
}
ULARGE_INTEGER available_bytes;
ULARGE_INTEGER free_bytes;
BOOL status = GetDiskFreeSpaceEx(&logical_drive_strings[i], NULL,
&available_bytes,
&free_bytes);
if (status) {
linked_ptr<StorageUnitInfo> unit(new StorageUnitInfo());
unit->id = WideToASCII(string16(&logical_drive_strings[i]));
unit->type = type;
unit->capacity = static_cast<double>(available_bytes.QuadPart);
unit->available_capacity = static_cast<double>(free_bytes.QuadPart);
info->units.push_back(unit);
}
}
return true;
}
} // namespace extensions
// 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/system_info_storage/system_info_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/system_info_storage/storage_info_provider.h"
namespace extensions {
using api::experimental_system_info_storage::StorageInfo;
using api::experimental_system_info_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");
}
// Return 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";
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
// 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.
#ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_STORAGE_SYSTEM_INFO_STORAGE_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_STORAGE_SYSTEM_INFO_STORAGE_API_H_
#include "chrome/browser/extensions/extension_function.h"
namespace extensions {
// Implementation of the systeminfo.storage.get API. It is an asynchronous
// call relative to browser UI thread.
class SystemInfoStorageGetFunction : public AsyncExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION_NAME("experimental.systemInfo.storage.get");
SystemInfoStorageGetFunction();
private:
virtual ~SystemInfoStorageGetFunction();
// AsyncExtensionFunction implementation.
virtual bool RunImpl() OVERRIDE;
// Called on FILE thread to get storage information
void WorkOnFileThread();
// Responds the result back to UI thread
void RespondOnUIThread(bool success);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_STORAGE_SYSTEM_INFO_STORAGE_API_H_
// 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 "base/command_line.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/common/chrome_switches.h"
namespace extensions {
class SystemInfoStorageApiTest: public ExtensionApiTest {
public:
SystemInfoStorageApiTest() {}
~SystemInfoStorageApiTest() {}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
ExtensionApiTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
}
};
IN_PROC_BROWSER_TEST_F(SystemInfoStorageApiTest, Storage) {
ASSERT_TRUE(RunExtensionTest("systeminfo/storage")) << message_;
}
} // namespace extensions
......@@ -234,6 +234,11 @@
'browser/extensions/api/system_info_cpu/cpu_info_provider.h',
'browser/extensions/api/system_info_cpu/system_info_cpu_api.cc',
'browser/extensions/api/system_info_cpu/system_info_cpu_api.h',
'browser/extensions/api/system_info_storage/system_info_storage_api.cc',
'browser/extensions/api/system_info_storage/system_info_storage_api.h',
'browser/extensions/api/system_info_storage/storage_info_provider.cc',
'browser/extensions/api/system_info_storage/storage_info_provider.h',
'browser/extensions/api/system_info_storage/storage_info_provider_win.cc',
'browser/extensions/api/tabs/execute_code_in_tab_function.cc',
'browser/extensions/api/tabs/execute_code_in_tab_function.h',
'browser/extensions/api/tabs/tabs.cc',
......@@ -688,6 +693,7 @@
['include', '^browser/extensions/'],
# Other excluded stuff.
['exclude', '^browser/extensions/api/system_info_storage/storage_info_provider_win.cc'],
['exclude', '^browser/extensions/browser_action_test_util_gtk.cc'],
['exclude', '^browser/extensions/extension_host_mac.h'],
['exclude', '^browser/extensions/extension_host_mac.mm'],
......
......@@ -2779,6 +2779,7 @@
'browser/extensions/api/serial/serial_apitest.cc',
'browser/extensions/api/socket/socket_apitest.cc',
'browser/extensions/api/system_info_cpu/system_info_cpu_apitest.cc',
'browser/extensions/api/system_info_storage/system_info_storage_apitest.cc',
'browser/extensions/api/tabs/tabs_test.cc',
'browser/extensions/api/terminal/terminal_private_apitest.cc',
'browser/extensions/api/test/apitest_apitest.cc',
......
......@@ -50,6 +50,7 @@
'experimental_media_galleries.idl',
'experimental_push_messaging.idl',
'experimental_system_info_cpu.idl',
'experimental_system_info_storage.idl',
'experimental_usb.idl',
'file_system.idl',
'serial.idl',
......
// 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.
namespace experimental.systemInfo.storage {
enum StorageUnitType {
// Unknow storage type.
unknown,
// Hard disk.
harddisk,
// Removable storage, e.g.USB device.
removable
};
dictionary StorageUnitInfo {
// The unique id of the storage unit.
DOMString id;
// The type of storage device. The value is one of the constants defined
// for this type.
StorageUnitType type;
// The total amount of the storage space, in bytes.
double capacity;
// The available amount of the storage space, in bytes.
double availableCapacity;
};
dictionary StorageInfo {
// The array of storage units connected to the system.
StorageUnitInfo[] units;
};
callback StorageInfoCallback = void (StorageInfo info);
interface Functions {
// Get the storage information from the system.
static void get(StorageInfoCallback callback);
};
};
{
"name": "storage info",
"version": "0.1",
"manifest_version": 2,
"description": "Test systeminfo.cpu API",
"permissions": ["experimental"],
"app": {
"background": {
"scripts": ["test_storage_api.js"]
}
}
}
// 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.
// systemInfo.storage api test
// browser_tests.exe --gtest_filter=SystemInfoStorageApiTest.Storage
chrome.systemInfo = chrome.experimental.systemInfo;
chrome.test.runTests([
function testGet() {
chrome.systemInfo.storage.get(chrome.test.callbackPass(function(info) {
// TODO(hmin): Need to check the value from Mock implementation
chrome.test.assertTrue(info.units.length > 0);
}));
}
]);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment