Add private system extension API to get system update status

BUG=chromium-os:17585
TEST=browser_tests --gtest_filter=SystemPrivateChromeOsApiTest.GetUpdateStatus

Review URL: http://codereview.chromium.org/8747003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114596 0039d316-1c4b-4281-b951-d872f2087c98
parent 458b365c
...@@ -490,6 +490,7 @@ void FactoryRegistry::ResetFunctions() { ...@@ -490,6 +490,7 @@ void FactoryRegistry::ResetFunctions() {
// System // System
RegisterFunction<extensions::GetIncognitoModeAvailabilityFunction>(); RegisterFunction<extensions::GetIncognitoModeAvailabilityFunction>();
RegisterFunction<extensions::GetUpdateStatusFunction>();
} }
void FactoryRegistry::GetAllNames(std::vector<std::string>* names) { void FactoryRegistry::GetAllNames(std::vector<std::string>* names) {
......
...@@ -9,6 +9,13 @@ ...@@ -9,6 +9,13 @@
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/dbus/dbus_thread_manager.h"
#include "chrome/browser/chromeos/dbus/update_engine_client.h"
#else
#include "chrome/browser/upgrade_detector.h"
#endif
namespace { namespace {
// Maps prefs::kIncognitoModeAvailability values (0 = enabled, ...) // Maps prefs::kIncognitoModeAvailability values (0 = enabled, ...)
...@@ -19,13 +26,19 @@ const char* kIncognitoModeAvailabilityStrings[] = { ...@@ -19,13 +26,19 @@ const char* kIncognitoModeAvailabilityStrings[] = {
"forced" "forced"
}; };
// Property keys.
const char kStateKey[] = "state";
const char kDownloadProgressKey[] = "download_progress";
// System update states.
const char kNotAvailableState[] = "NotAvailable";
const char kUpdatingState[] = "Updating";
const char kNeedRestartState[] = "NeedRestart";
} // namespace } // namespace
namespace extensions { namespace extensions {
GetIncognitoModeAvailabilityFunction::~GetIncognitoModeAvailabilityFunction() {
}
bool GetIncognitoModeAvailabilityFunction::RunImpl() { bool GetIncognitoModeAvailabilityFunction::RunImpl() {
PrefService* prefs = profile_->GetPrefs(); PrefService* prefs = profile_->GetPrefs();
int value = prefs->GetInteger(prefs::kIncognitoModeAvailability); int value = prefs->GetInteger(prefs::kIncognitoModeAvailability);
...@@ -37,4 +50,65 @@ bool GetIncognitoModeAvailabilityFunction::RunImpl() { ...@@ -37,4 +50,65 @@ bool GetIncognitoModeAvailabilityFunction::RunImpl() {
return true; return true;
} }
bool GetUpdateStatusFunction::RunImpl() {
std::string state;
double download_progress = 0;
#if defined(OS_CHROMEOS)
// With UpdateEngineClient, we can provide more detailed information about
// system updates on ChromeOS.
const chromeos::UpdateEngineClient::Status status =
chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()->
GetLastStatus();
// |download_progress| is set to 1 after download finishes
// (i.e. verify, finalize and need-reboot phase) to indicate the progress
// even though |status.download_progress| is 0 in these phases.
switch (status.status) {
case chromeos::UpdateEngineClient::UPDATE_STATUS_ERROR:
state = kNotAvailableState;
break;
case chromeos::UpdateEngineClient::UPDATE_STATUS_IDLE:
state = kNotAvailableState;
break;
case chromeos::UpdateEngineClient::UPDATE_STATUS_CHECKING_FOR_UPDATE:
state = kNotAvailableState;
break;
case chromeos::UpdateEngineClient::UPDATE_STATUS_UPDATE_AVAILABLE:
state = kUpdatingState;
break;
case chromeos::UpdateEngineClient::UPDATE_STATUS_DOWNLOADING:
state = kUpdatingState;
download_progress = status.download_progress;
break;
case chromeos::UpdateEngineClient::UPDATE_STATUS_VERIFYING:
state = kUpdatingState;
download_progress = 1;
break;
case chromeos::UpdateEngineClient::UPDATE_STATUS_FINALIZING:
state = kUpdatingState;
download_progress = 1;
break;
case chromeos::UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT:
state = kNeedRestartState;
download_progress = 1;
break;
case chromeos::UpdateEngineClient::UPDATE_STATUS_REPORTING_ERROR_EVENT:
state = kNotAvailableState;
break;
}
#else
if (UpgradeDetector::GetInstance()->notify_upgrade()) {
state = kNeedRestartState;
download_progress = 1;
} else {
state = kNotAvailableState;
}
#endif
DictionaryValue* dict = new DictionaryValue();
dict->SetString(kStateKey, state);
dict->SetDouble(kDownloadProgressKey, download_progress);
result_.reset(dict);
return true;
}
} // namespace extensions } // namespace extensions
...@@ -8,23 +8,25 @@ ...@@ -8,23 +8,25 @@
#ifndef CHROME_BROWSER_EXTENSIONS_SYSTEM_SYSTEM_API_H_ #ifndef CHROME_BROWSER_EXTENSIONS_SYSTEM_SYSTEM_API_H_
#define CHROME_BROWSER_EXTENSIONS_SYSTEM_SYSTEM_API_H_ #define CHROME_BROWSER_EXTENSIONS_SYSTEM_SYSTEM_API_H_
#include "chrome/browser/extensions/extension_preference_api.h" #include "chrome/browser/extensions/extension_function.h"
#include <string>
namespace base {
class Value;
}
namespace extensions { namespace extensions {
class GetIncognitoModeAvailabilityFunction : public SyncExtensionFunction { class GetIncognitoModeAvailabilityFunction : public SyncExtensionFunction {
public: public:
virtual ~GetIncognitoModeAvailabilityFunction(); virtual ~GetIncognitoModeAvailabilityFunction() {}
virtual bool RunImpl() OVERRIDE; virtual bool RunImpl() OVERRIDE;
DECLARE_EXTENSION_FUNCTION_NAME("systemPrivate.getIncognitoModeAvailability") DECLARE_EXTENSION_FUNCTION_NAME("systemPrivate.getIncognitoModeAvailability")
}; };
// API function which returns the status of system update.
class GetUpdateStatusFunction : public SyncExtensionFunction {
public:
virtual ~GetUpdateStatusFunction() {}
virtual bool RunImpl() OVERRIDE;
DECLARE_EXTENSION_FUNCTION_NAME("systemPrivate.getUpdateStatus")
};
} // namespace extensions } // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_SYSTEM_SYSTEM_API_H_ #endif // CHROME_BROWSER_EXTENSIONS_SYSTEM_SYSTEM_API_H_
...@@ -8,9 +8,65 @@ ...@@ -8,9 +8,65 @@
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, SystemApi) { #if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/dbus/mock_dbus_thread_manager.h"
#include "chrome/browser/chromeos/dbus/mock_update_engine_client.h"
using chromeos::UpdateEngineClient;
using ::testing::Return;
#endif
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, GetIncognitoModeAvailability) {
PrefService* pref_service = browser()->profile()->GetPrefs(); PrefService* pref_service = browser()->profile()->GetPrefs();
pref_service->SetInteger(prefs::kIncognitoModeAvailability, 1); pref_service->SetInteger(prefs::kIncognitoModeAvailability, 1);
EXPECT_TRUE(RunComponentExtensionTest("system")) << message_; EXPECT_TRUE(RunComponentExtensionTest(
"system/get_incognito_mode_availability")) << message_;
}
#if defined(OS_CHROMEOS)
class GetUpdateStatusApiTest : public ExtensionApiTest {
public:
GetUpdateStatusApiTest() : mock_update_engine_client_(NULL) {}
virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
ExtensionApiTest::SetUpInProcessBrowserTestFixture();
chromeos::MockDBusThreadManager* mock_dbus_thread_manager =
new chromeos::MockDBusThreadManager;
chromeos::DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager);
mock_update_engine_client_ =
mock_dbus_thread_manager->mock_update_engine_client();
}
virtual void TearDownInProcessBrowserTestFixture() OVERRIDE {
chromeos::DBusThreadManager::Shutdown();
ExtensionApiTest::TearDownInProcessBrowserTestFixture();
}
protected:
chromeos::MockUpdateEngineClient* mock_update_engine_client_;
private:
DISALLOW_COPY_AND_ASSIGN(GetUpdateStatusApiTest);
};
IN_PROC_BROWSER_TEST_F(GetUpdateStatusApiTest, Progress) {
UpdateEngineClient::Status status_not_available;
status_not_available.status = UpdateEngineClient::UPDATE_STATUS_IDLE;
UpdateEngineClient::Status status_updating;
status_updating.status = UpdateEngineClient::UPDATE_STATUS_DOWNLOADING;
status_updating.download_progress = 0.5;
UpdateEngineClient::Status status_boot_needed;
status_boot_needed.status =
UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT;
EXPECT_CALL(*mock_update_engine_client_, GetLastStatus())
.WillOnce(Return(status_not_available))
.WillOnce(Return(status_updating))
.WillOnce(Return(status_boot_needed));
ASSERT_TRUE(RunComponentExtensionTest(
"system/get_update_status")) << message_;
} }
#endif
...@@ -9233,7 +9233,24 @@ ...@@ -9233,7 +9233,24 @@
{ {
"namespace": "systemPrivate", "namespace": "systemPrivate",
"nodoc": true, "nodoc": true,
"types": [], "types": [
{
"id": "UpdateStatus",
"type": "object",
"description": "Information about the system update.",
"properties": {
"state": {
"type": "string",
"enum": ["NotAvailable", "Updating", "NeedRestart"],
"description": "State of system update. NotAvailable when there is no available update or the update system is in error state, Updating when a system update is in progress, NeedRestart when a system update is finished and restart is needed."
},
"download_progress": {
"type": "number",
"description": "Value between 0 and 1 describing the progress of system update download. This value will be set to 0 when |state| is NotAvailable, 1 when NeedRestart."
}
}
}
],
"functions": [ "functions": [
{ {
"name": "getIncognitoModeAvailability", "name": "getIncognitoModeAvailability",
...@@ -9254,6 +9271,24 @@ ...@@ -9254,6 +9271,24 @@
] ]
} }
] ]
},
{
"name": "getUpdateStatus",
"type": "function",
"description": "Gets information about the system update.",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"$ref": "UpdateStatus",
"name": "status",
"description": "Details of the system update"
}
]
}
]
} }
] ]
} }
......
{ {
"key": "MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDFFBqmJf6+xgNeQhSwunB7Vdi+peXwR6uf09DKBmStju73Cjhggl3x+i7jfeRvGguJA1nnxK45dHktx5ppyy2w16nFKFcfIAN9dP6RrfPWuHVxw1AzNCRm/VutRLje1e9Kk3xtXAw9Vj3N0/txZ3u8HOr62YUDIyFcS87+Yo/a9QIBIw==", "key": "MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDFFBqmJf6+xgNeQhSwunB7Vdi+peXwR6uf09DKBmStju73Cjhggl3x+i7jfeRvGguJA1nnxK45dHktx5ppyy2w16nFKFcfIAN9dP6RrfPWuHVxw1AzNCRm/VutRLje1e9Kk3xtXAw9Vj3N0/txZ3u8HOr62YUDIyFcS87+Yo/a9QIBIw==",
"name" : "System API Test Extension", "name" : "chrome.systemPrivate.getIncognitoModeAvailability",
"version" : "0.1", "version" : "0.1",
"description" : "System API Test Extension", "description" : "browser test for chrome.systemPrivate.getIncognitoModeVailability",
"permissions": ["systemPrivate"], "permissions": ["systemPrivate"],
"background_page": "test.html" "background_page": "test.html"
} }
<script> <script>
// System API test // System API test
// Run with browser_tests --gtest_filter=ExtensionApiTest.SystemApi // Run with browser_tests --gtest_filter=ExtensionApiTest.GetIncognitoModeAvailability
function expect(expected, message) { function expect(expected, message) {
return chrome.test.callbackPass(function(value) { return chrome.test.callbackPass(function(value) {
......
// Copyright (c) 2011 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.
// systemPrivate.getUpdateStatus test for Chrome
// browser_tests --gtest_filter="GetUpdateStatusApiTest.Progress"
chrome.test.runTests([
function notAvailable() {
chrome.systemPrivate.getUpdateStatus(function(status) {
chrome.test.assertEq(status.state, "NotAvailable");
chrome.test.assertEq(status.download_progress, 0.0);
chrome.test.succeed();
});
},
function updating() {
chrome.systemPrivate.getUpdateStatus(function(status) {
chrome.test.assertEq(status.state, "Updating");
chrome.test.assertEq(status.download_progress, 0.5);
chrome.test.succeed();
});
},
function needRestart() {
chrome.systemPrivate.getUpdateStatus(function(status) {
chrome.test.assertEq(status.state, "NeedRestart");
chrome.test.assertEq(status.download_progress, 1);
chrome.test.succeed();
});
}
]);
{
"key": "MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDFFBqmJf6+xgNeQhSwunB7Vdi+peXwR6uf09DKBmStju73Cjhggl3x+i7jfeRvGguJA1nnxK45dHktx5ppyy2w16nFKFcfIAN9dP6RrfPWuHVxw1AzNCRm/VutRLje1e9Kk3xtXAw9Vj3N0/txZ3u8HOr62YUDIyFcS87+Yo/a9QIBIw==",
"name": "chrome.systemPrivate.getUpdateStatus",
"version": "0.1",
"description": "browser test for chrome.systemPriavte.getUpdateStatus API",
"background_page": "background.html",
"permissions": [
"systemPrivate"
]
}
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