diff options
author | hongbo.min@intel.com <hongbo.min@intel.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-27 06:10:37 +0000 |
---|---|---|
committer | hongbo.min@intel.com <hongbo.min@intel.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-27 06:10:37 +0000 |
commit | 52bdcdad3d131faf6443b7fa558a474c496d974a (patch) | |
tree | 8c9503d1e8b602ff7741b470d646ee3f9af7624a | |
parent | 7e8f754399801f33da56e4aa062480cf732b105b (diff) | |
download | chromium_src-52bdcdad3d131faf6443b7fa558a474c496d974a.zip chromium_src-52bdcdad3d131faf6443b7fa558a474c496d974a.tar.gz chromium_src-52bdcdad3d131faf6443b7fa558a474c496d974a.tar.bz2 |
Add systemInfo.memory API implementation
BUG=136519
TEST=browser_tests --gtest_filter=SystemInfoMemoryApiTest.*
Review URL: https://chromiumcodereview.appspot.com/11185049
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164518 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/sys_info.h | 4 | ||||
-rw-r--r-- | base/sys_info_linux.cc | 11 | ||||
-rw-r--r-- | base/sys_info_mac.cc | 6 | ||||
-rw-r--r-- | base/sys_info_win.cc | 15 | ||||
-rw-r--r-- | chrome/browser/extensions/api/system_info_memory/memory_info_provider.cc | 30 | ||||
-rw-r--r-- | chrome/browser/extensions/api/system_info_memory/memory_info_provider.h | 37 | ||||
-rw-r--r-- | chrome/browser/extensions/api/system_info_memory/system_info_memory_api.cc | 32 | ||||
-rw-r--r-- | chrome/browser/extensions/api/system_info_memory/system_info_memory_api.h | 28 | ||||
-rw-r--r-- | chrome/browser/extensions/api/system_info_memory/system_info_memory_apitest.cc | 55 | ||||
-rw-r--r-- | chrome/chrome_browser_extensions.gypi | 4 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 | ||||
-rw-r--r-- | chrome/common/extensions/api/api.gyp | 1 | ||||
-rw-r--r-- | chrome/common/extensions/api/experimental_system_info_memory.idl | 21 | ||||
-rw-r--r-- | chrome/test/data/extensions/api_test/systeminfo/memory/manifest.json | 11 | ||||
-rw-r--r-- | chrome/test/data/extensions/api_test/systeminfo/memory/test_memory_api.js | 20 |
15 files changed, 276 insertions, 0 deletions
diff --git a/base/sys_info.h b/base/sys_info.h index e1122b5..e48c5a3 100644 --- a/base/sys_info.h +++ b/base/sys_info.h @@ -22,6 +22,10 @@ class BASE_EXPORT SysInfo { // Return the number of bytes of physical memory on the current machine. static int64 AmountOfPhysicalMemory(); + // Return the number of bytes of current available physical memory on the + // machine. + static int64 AmountOfAvailablePhysicalMemory(); + // Return the number of megabytes of physical memory on the current machine. static int AmountOfPhysicalMemoryMB() { return static_cast<int>(AmountOfPhysicalMemory() / 1024 / 1024); diff --git a/base/sys_info_linux.cc b/base/sys_info_linux.cc index 59dfee0..6683674 100644 --- a/base/sys_info_linux.cc +++ b/base/sys_info_linux.cc @@ -24,6 +24,17 @@ int64 SysInfo::AmountOfPhysicalMemory() { } // static +int64 SysInfo::AmountOfAvailablePhysicalMemory() { + long available_pages = sysconf(_SC_AVPHYS_PAGES); + long page_size = sysconf(_SC_PAGE_SIZE); + if (available_pages == -1 || page_size == -1) { + NOTREACHED(); + return 0; + } + return static_cast<int64>(available_pages) * page_size; +} + +// static size_t SysInfo::MaxSharedMemorySize() { static int64 limit; static bool limit_valid = false; diff --git a/base/sys_info_mac.cc b/base/sys_info_mac.cc index 8ed0290..2393186 100644 --- a/base/sys_info_mac.cc +++ b/base/sys_info_mac.cc @@ -59,6 +59,12 @@ int64 SysInfo::AmountOfPhysicalMemory() { } // static +int64 SysInfo::AmountOfAvailablePhysicalMemory() { + // TODO(hongbo): Add implementation for Mac. + return 0; +} + +// static std::string SysInfo::CPUModelName() { char name[256]; size_t len = arraysize(name); diff --git a/base/sys_info_win.cc b/base/sys_info_win.cc index 8b5e10d..98d2f7c 100644 --- a/base/sys_info_win.cc +++ b/base/sys_info_win.cc @@ -36,6 +36,21 @@ int64 SysInfo::AmountOfPhysicalMemory() { } // static +int64 SysInfo::AmountOfAvailablePhysicalMemory() { + MEMORYSTATUSEX memory_info; + memory_info.dwLength = sizeof(memory_info); + if (!GlobalMemoryStatusEx(&memory_info)) { + NOTREACHED(); + return 0; + } + + int64 rv = static_cast<int64>(memory_info.ullAvailPhys); + if (rv < 0) + rv = kint64max; + return rv; +} + +// static int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) { base::ThreadRestrictions::AssertIOAllowed(); diff --git a/chrome/browser/extensions/api/system_info_memory/memory_info_provider.cc b/chrome/browser/extensions/api/system_info_memory/memory_info_provider.cc new file mode 100644 index 0000000..3586246 --- /dev/null +++ b/chrome/browser/extensions/api/system_info_memory/memory_info_provider.cc @@ -0,0 +1,30 @@ +// 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_memory/memory_info_provider.h" + +#include "base/sys_info.h" + +namespace extensions { + +using api::experimental_system_info_memory::MemoryInfo; + +MemoryInfoProvider::MemoryInfoProvider() {} + +MemoryInfoProvider::~MemoryInfoProvider() {} + +bool MemoryInfoProvider::QueryInfo(MemoryInfo* info) { + info->capacity = static_cast<double>(base::SysInfo::AmountOfPhysicalMemory()); + info->available_capacity = + static_cast<double>(base::SysInfo::AmountOfAvailablePhysicalMemory()); + return true; +} + +// static +MemoryInfoProvider* MemoryInfoProvider::Get() { + return MemoryInfoProvider::GetInstance<MemoryInfoProvider>(); +} + +} // namespace extensions + diff --git a/chrome/browser/extensions/api/system_info_memory/memory_info_provider.h b/chrome/browser/extensions/api/system_info_memory/memory_info_provider.h new file mode 100644 index 0000000..f52154b --- /dev/null +++ b/chrome/browser/extensions/api/system_info_memory/memory_info_provider.h @@ -0,0 +1,37 @@ +// 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_MEMORY_MEMORY_INFO_PROVIDER_H_ +#define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_MEMORY_MEMORY_INFO_PROVIDER_H_ + +#include "chrome/browser/extensions/system_info_provider.h" +#include "chrome/common/extensions/api/experimental_system_info_memory.h" + +namespace extensions { + +class MemoryInfoProvider + : public SystemInfoProvider< + api::experimental_system_info_memory::MemoryInfo> { + public: + static MemoryInfoProvider* Get(); + + // Overriden from SystemInfoProvider<MemoryInfo>. + virtual bool QueryInfo( + api::experimental_system_info_memory::MemoryInfo* info) OVERRIDE; + + private: + friend class SystemInfoProvider< + api::experimental_system_info_memory::MemoryInfo>; + friend class MockMemoryInfoProviderImpl; + + MemoryInfoProvider(); + virtual ~MemoryInfoProvider(); + + DISALLOW_COPY_AND_ASSIGN(MemoryInfoProvider); +}; + +} // namespace extensions + +#endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_MEMORY_MEMORY_INFO_PROVIDER_H_ + diff --git a/chrome/browser/extensions/api/system_info_memory/system_info_memory_api.cc b/chrome/browser/extensions/api/system_info_memory/system_info_memory_api.cc new file mode 100644 index 0000000..3d7d7ff --- /dev/null +++ b/chrome/browser/extensions/api/system_info_memory/system_info_memory_api.cc @@ -0,0 +1,32 @@ +// 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_memory/system_info_memory_api.h" + +#include "chrome/browser/extensions/api/system_info_memory/memory_info_provider.h" + +namespace extensions { + +using api::experimental_system_info_memory::MemoryInfo; + +SystemInfoMemoryGetFunction::SystemInfoMemoryGetFunction() {} + +SystemInfoMemoryGetFunction::~SystemInfoMemoryGetFunction() {} + +bool SystemInfoMemoryGetFunction::RunImpl() { + MemoryInfoProvider::Get()->StartQueryInfo( + base::Bind(&SystemInfoMemoryGetFunction::OnGetMemoryInfoCompleted, this)); + return true; +} + +void SystemInfoMemoryGetFunction::OnGetMemoryInfoCompleted( + const MemoryInfo& info, bool success) { + if (success) + SetResult(info.ToValue().release()); + else + SetError("Error occurred when querying memory information."); + SendResponse(success); +} + +} // namespace extensions diff --git a/chrome/browser/extensions/api/system_info_memory/system_info_memory_api.h b/chrome/browser/extensions/api/system_info_memory/system_info_memory_api.h new file mode 100644 index 0000000..87989e1 --- /dev/null +++ b/chrome/browser/extensions/api/system_info_memory/system_info_memory_api.h @@ -0,0 +1,28 @@ +// 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_MEMORY_SYSTEM_INFO_MEMORY_API_H_ +#define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_MEMORY_SYSTEM_INFO_MEMORY_API_H_ + +#include "chrome/browser/extensions/extension_function.h" +#include "chrome/common/extensions/api/experimental_system_info_memory.h" + +namespace extensions { + +class SystemInfoMemoryGetFunction : public AsyncExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION_NAME("experimental.systemInfo.memory.get"); + SystemInfoMemoryGetFunction(); + + private: + virtual ~SystemInfoMemoryGetFunction(); + virtual bool RunImpl() OVERRIDE; + void OnGetMemoryInfoCompleted( + const api::experimental_system_info_memory::MemoryInfo& info, + bool success); +}; + +} // namespace extensions + +#endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_MEMORY_SYSTEM_INFO_MEMORY_API_H__ diff --git a/chrome/browser/extensions/api/system_info_memory/system_info_memory_apitest.cc b/chrome/browser/extensions/api/system_info_memory/system_info_memory_apitest.cc new file mode 100644 index 0000000..4ffb578 --- /dev/null +++ b/chrome/browser/extensions/api/system_info_memory/system_info_memory_apitest.cc @@ -0,0 +1,55 @@ +// 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 "base/message_loop.h" +#include "chrome/browser/extensions/api/system_info_memory/memory_info_provider.h" +#include "chrome/browser/extensions/extension_apitest.h" +#include "chrome/browser/extensions/extension_test_message_listener.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/test/base/ui_test_utils.h" + +namespace extensions { + +using api::experimental_system_info_memory::MemoryInfo; + +class MockMemoryInfoProviderImpl : public MemoryInfoProvider { + public: + MockMemoryInfoProviderImpl() {} + ~MockMemoryInfoProviderImpl() {} + + virtual bool QueryInfo(MemoryInfo* info) OVERRIDE { + info->capacity = 4096; + info->available_capacity = 1024; + return true; + } +}; + +class SystemInfoMemoryApiTest: public ExtensionApiTest { + public: + SystemInfoMemoryApiTest() {} + ~SystemInfoMemoryApiTest() {} + + virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { + ExtensionApiTest::SetUpCommandLine(command_line); + command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); + } + + virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { + ExtensionApiTest::SetUpInProcessBrowserTestFixture(); + message_loop_.reset(new MessageLoop(MessageLoop::TYPE_UI)); + } + + private: + scoped_ptr<MessageLoop> message_loop_; +}; + +IN_PROC_BROWSER_TEST_F(SystemInfoMemoryApiTest, Memory) { + MemoryInfoProvider* provider = new MockMemoryInfoProviderImpl(); + // The provider is owned by the single MemoryInfoProvider instance. + MemoryInfoProvider::InitializeForTesting(provider); + ASSERT_TRUE(RunExtensionTest("systeminfo/memory")) << message_; +} + +} // namespace extensions diff --git a/chrome/chrome_browser_extensions.gypi b/chrome/chrome_browser_extensions.gypi index c61ed07..a1a5ef1 100644 --- a/chrome/chrome_browser_extensions.gypi +++ b/chrome/chrome_browser_extensions.gypi @@ -281,6 +281,10 @@ 'browser/extensions/api/system_info_cpu/cpu_info_provider_android.cc', '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_memory/memory_info_provider.cc', + 'browser/extensions/api/system_info_memory/memory_info_provider.h', + 'browser/extensions/api/system_info_memory/system_info_memory_api.cc', + 'browser/extensions/api/system_info_memory/system_info_memory_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_linux.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index d69fa45..1a2df29 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -2963,6 +2963,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_memory/system_info_memory_apitest.cc', 'browser/extensions/api/system_info_storage/system_info_storage_apitest.cc', 'browser/extensions/api/sync_file_system/sync_file_system_apitest.cc', 'browser/extensions/api/tab_capture/tab_capture_apitest.cc', diff --git a/chrome/common/extensions/api/api.gyp b/chrome/common/extensions/api/api.gyp index c0a206e..de8ef33 100644 --- a/chrome/common/extensions/api/api.gyp +++ b/chrome/common/extensions/api/api.gyp @@ -54,6 +54,7 @@ 'experimental_media_galleries.idl', 'experimental_notification.idl', 'experimental_system_info_cpu.idl', + 'experimental_system_info_memory.idl', 'experimental_system_info_storage.idl', 'experimental_usb.idl', 'file_system.idl', diff --git a/chrome/common/extensions/api/experimental_system_info_memory.idl b/chrome/common/extensions/api/experimental_system_info_memory.idl new file mode 100644 index 0000000..09f226f --- /dev/null +++ b/chrome/common/extensions/api/experimental_system_info_memory.idl @@ -0,0 +1,21 @@ +// 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. + +// File-level comment to appease parser. Eventually this will not be necessary. +namespace experimental.systemInfo.memory { + + dictionary MemoryInfo { + // The total amount of physical memory capacity, in bytes. + double capacity; + // The amount of available capacity, in bytes. + double availableCapacity; + }; + + callback MemoryInfoCallback = void (MemoryInfo info); + + interface Functions { + // Get physical memory information. + static void get(MemoryInfoCallback callback); + }; +}; diff --git a/chrome/test/data/extensions/api_test/systeminfo/memory/manifest.json b/chrome/test/data/extensions/api_test/systeminfo/memory/manifest.json new file mode 100644 index 0000000..c9a02e8 --- /dev/null +++ b/chrome/test/data/extensions/api_test/systeminfo/memory/manifest.json @@ -0,0 +1,11 @@ +{ + "name": "memoryinfo", + "version": "1.0", + "description": "Test systeminfo.memory API", + "permissions": ["experimental"], + "app": { + "background": { + "scripts": ["test_memory_api.js"] + } + } +} diff --git a/chrome/test/data/extensions/api_test/systeminfo/memory/test_memory_api.js b/chrome/test/data/extensions/api_test/systeminfo/memory/test_memory_api.js new file mode 100644 index 0000000..f936463 --- /dev/null +++ b/chrome/test/data/extensions/api_test/systeminfo/memory/test_memory_api.js @@ -0,0 +1,20 @@ +// 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.memory api test +// browser_tests --gtest_filter=SystemInfoMemoryApiTest.* + +chrome.systemInfo = chrome.experimental.systemInfo; + +chrome.test.runTests([ + function testGet() { + for(var i = 0; i < 10; ++i) { + chrome.systemInfo.memory.get(chrome.test.callbackPass(function(result) { + chrome.test.assertEq(4096, result.capacity); + chrome.test.assertEq(1024, result.availableCapacity); + })); + } + } +]); + |