From 11c823cbb31d113dd3841b31f325b0054c515913 Mon Sep 17 00:00:00 2001 From: "xiyuan@chromium.org" Date: Tue, 8 Oct 2013 06:48:50 +0000 Subject: [PATCH] Implement chrome.runtime.restart for cros kiosk app. This allows an app running in ChromeOS kiosk mode to restart the device. BUG=287727 Review URL: https://codereview.chromium.org/25944003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@227474 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/apps/app_browsertest.cc | 79 ++++++++++++++++++++++ .../browser/extensions/api/runtime/runtime_api.cc | 19 ++++++ .../browser/extensions/api/runtime/runtime_api.h | 9 +++ .../extension_function_histogram_value.h | 1 + .../extensions/extension_function_registry.cc | 1 + .../extensions/extension_messages_apitest.cc | 1 + chrome/common/extensions/api/runtime.json | 7 ++ .../platform_apps/restart_device/main.html | 10 +++ .../platform_apps/restart_device/main.js | 10 +++ .../platform_apps/restart_device/manifest.json | 10 +++ .../platform_apps/restart_device/test.js | 7 ++ 11 files changed, 154 insertions(+) create mode 100644 chrome/test/data/extensions/platform_apps/restart_device/main.html create mode 100644 chrome/test/data/extensions/platform_apps/restart_device/main.js create mode 100644 chrome/test/data/extensions/platform_apps/restart_device/manifest.json create mode 100644 chrome/test/data/extensions/platform_apps/restart_device/test.js diff --git a/chrome/browser/apps/app_browsertest.cc b/chrome/browser/apps/app_browsertest.cc index 2f38d17bff6c..6a1d9f1437a2 100644 --- a/chrome/browser/apps/app_browsertest.cc +++ b/chrome/browser/apps/app_browsertest.cc @@ -46,6 +46,15 @@ #include "net/test/embedded_test_server/embedded_test_server.h" #include "url/gurl.h" +#if defined(OS_CHROMEOS) +#include "base/memory/scoped_ptr.h" +#include "chrome/browser/chromeos/login/mock_user_manager.h" +#include "chrome/browser/chromeos/login/user_manager.h" +#include "chromeos/dbus/dbus_thread_manager.h" +#include "chromeos/dbus/fake_power_manager_client.h" +#include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h" +#endif + using apps::ShellWindow; using apps::ShellWindowRegistry; using content::WebContents; @@ -1140,6 +1149,76 @@ IN_PROC_BROWSER_TEST_F(PlatformAppIncognitoBrowserTest, IncognitoComponentApp) { } } +class RestartDeviceTest : public PlatformAppBrowserTest { + public: + RestartDeviceTest() + : power_manager_client_(NULL), + mock_user_manager_(NULL) {} + virtual ~RestartDeviceTest() {} + + // PlatformAppBrowserTest overrides + virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { + PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture(); + + chromeos::MockDBusThreadManagerWithoutGMock* dbus_manager = + new chromeos::MockDBusThreadManagerWithoutGMock; + chromeos::DBusThreadManager::InitializeForTesting(dbus_manager); + power_manager_client_ = dbus_manager->fake_power_manager_client(); + } + + virtual void SetUpOnMainThread() OVERRIDE { + PlatformAppBrowserTest::SetUpOnMainThread(); + + mock_user_manager_ = new chromeos::MockUserManager; + user_manager_enabler_.reset( + new chromeos::ScopedUserManagerEnabler(mock_user_manager_)); + + EXPECT_CALL(*mock_user_manager_, IsUserLoggedIn()) + .WillRepeatedly(testing::Return(true)); + EXPECT_CALL(*mock_user_manager_, IsLoggedInAsKioskApp()) + .WillRepeatedly(testing::Return(true)); + } + + virtual void CleanUpOnMainThread() OVERRIDE { + user_manager_enabler_.reset(); + PlatformAppBrowserTest::CleanUpOnMainThread(); + } + + virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { + chromeos::DBusThreadManager::Shutdown(); + PlatformAppBrowserTest::TearDownInProcessBrowserTestFixture(); + } + + int request_restart_call_count() const { + return power_manager_client_->request_restart_call_count(); + } + + private: + chromeos::FakePowerManagerClient* power_manager_client_; + chromeos::MockUserManager* mock_user_manager_; + scoped_ptr user_manager_enabler_; + + DISALLOW_COPY_AND_ASSIGN(RestartDeviceTest); +}; + +// Tests that chrome.runtime.restart would request device restart in +// ChromeOS kiosk mode. +IN_PROC_BROWSER_TEST_F(RestartDeviceTest, Restart) { + ASSERT_EQ(0, request_restart_call_count()); + + ExtensionTestMessageListener launched_listener("Launched", true); + const Extension* extension = LoadAndLaunchPlatformApp("restart_device"); + ASSERT_TRUE(extension); + ASSERT_TRUE(launched_listener.WaitUntilSatisfied()); + + launched_listener.Reply("restart"); + ExtensionTestMessageListener restart_requested_listener("restartRequested", + false); + ASSERT_TRUE(restart_requested_listener.WaitUntilSatisfied()); + + EXPECT_EQ(1, request_restart_call_count()); +} + #endif // defined(OS_CHROMEOS) diff --git a/chrome/browser/extensions/api/runtime/runtime_api.cc b/chrome/browser/extensions/api/runtime/runtime_api.cc index 5966e8a17d28..1af8642a2bf6 100644 --- a/chrome/browser/extensions/api/runtime/runtime_api.cc +++ b/chrome/browser/extensions/api/runtime/runtime_api.cc @@ -35,6 +35,12 @@ #include "url/gurl.h" #include "webkit/browser/fileapi/isolated_context.h" +#if defined(OS_CHROMEOS) +#include "chrome/browser/chromeos/login/user_manager.h" +#include "chromeos/dbus/dbus_thread_manager.h" +#include "chromeos/dbus/power_manager_client.h" +#endif + namespace GetPlatformInfo = extensions::api::runtime::GetPlatformInfo; namespace extensions { @@ -356,6 +362,19 @@ void RuntimeRequestUpdateCheckFunction::ReplyUpdateFound( SendResponse(true); } +bool RuntimeRestartFunction::RunImpl() { +#if defined(OS_CHROMEOS) + if (chromeos::UserManager::Get()->IsLoggedInAsKioskApp()) { + chromeos::DBusThreadManager::Get() + ->GetPowerManagerClient() + ->RequestRestart(); + return true; + } +#endif + SetError("Function available only for ChromeOS kiosk mode."); + return false; +} + bool RuntimeGetPlatformInfoFunction::RunImpl() { GetPlatformInfo::Results::PlatformInfo info; diff --git a/chrome/browser/extensions/api/runtime/runtime_api.h b/chrome/browser/extensions/api/runtime/runtime_api.h index d9369454ead1..10f35f2f0861 100644 --- a/chrome/browser/extensions/api/runtime/runtime_api.h +++ b/chrome/browser/extensions/api/runtime/runtime_api.h @@ -109,6 +109,15 @@ class RuntimeRequestUpdateCheckFunction : public AsyncExtensionFunction, bool did_reply_; }; +class RuntimeRestartFunction : public SyncExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) + + protected: + virtual ~RuntimeRestartFunction() {} + virtual bool RunImpl() OVERRIDE; +}; + class RuntimeGetPlatformInfoFunction : public SyncExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", diff --git a/chrome/browser/extensions/extension_function_histogram_value.h b/chrome/browser/extensions/extension_function_histogram_value.h index 50f49eb6a4e1..541b9dd220d0 100644 --- a/chrome/browser/extensions/extension_function_histogram_value.h +++ b/chrome/browser/extensions/extension_function_histogram_value.h @@ -646,6 +646,7 @@ enum HistogramValue { CAST_CHANNEL_OPEN, CAST_CHANNEL_SEND, CAST_CHANNEL_CLOSE, + RUNTIME_RESTART, ENUM_BOUNDARY // Last entry: Add new entries above. }; diff --git a/chrome/browser/extensions/extension_function_registry.cc b/chrome/browser/extensions/extension_function_registry.cc index 7b476d96fb7d..48202d91f4e1 100644 --- a/chrome/browser/extensions/extension_function_registry.cc +++ b/chrome/browser/extensions/extension_function_registry.cc @@ -48,6 +48,7 @@ void ExtensionFunctionRegistry::ResetFunctions() { RegisterFunction(); RegisterFunction(); RegisterFunction(); + RegisterFunction(); // ExperimentalIdentity. RegisterFunction(); diff --git a/chrome/browser/extensions/extension_messages_apitest.cc b/chrome/browser/extensions/extension_messages_apitest.cc index 6222ec5b9baa..e6557df4b064 100644 --- a/chrome/browser/extensions/extension_messages_apitest.cc +++ b/chrome/browser/extensions/extension_messages_apitest.cc @@ -200,6 +200,7 @@ class ExternallyConnectableMessagingTest : public ExtensionApiTest { "getURL", "reload", "requestUpdateCheck", + "restart", "connectNative", "sendNativeMessage", "onStartup", diff --git a/chrome/common/extensions/api/runtime.json b/chrome/common/extensions/api/runtime.json index 26fc7acef7bb..69f53e82e0aa 100644 --- a/chrome/common/extensions/api/runtime.json +++ b/chrome/common/extensions/api/runtime.json @@ -162,6 +162,13 @@ ] }, { + "name": "restart", + "description": "Restart the ChromeOS device when the app runs in kiosk mode. Otherwise, it's no-op.", + "type": "function", + "nocompile": true, + "parameters": [] + }, + { "name": "connect", "type": "function", "nocompile": true, diff --git a/chrome/test/data/extensions/platform_apps/restart_device/main.html b/chrome/test/data/extensions/platform_apps/restart_device/main.html new file mode 100644 index 000000000000..8fe0840e5ef4 --- /dev/null +++ b/chrome/test/data/extensions/platform_apps/restart_device/main.html @@ -0,0 +1,10 @@ + + + + + + diff --git a/chrome/test/data/extensions/platform_apps/restart_device/main.js b/chrome/test/data/extensions/platform_apps/restart_device/main.js new file mode 100644 index 000000000000..1592361127ec --- /dev/null +++ b/chrome/test/data/extensions/platform_apps/restart_device/main.js @@ -0,0 +1,10 @@ +// Copyright 2013 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. + +chrome.test.sendMessage('Launched', function(response) { + if (response == "restart") { + chrome.runtime.restart(); + chrome.test.sendMessage('restartRequested'); + } +}); diff --git a/chrome/test/data/extensions/platform_apps/restart_device/manifest.json b/chrome/test/data/extensions/platform_apps/restart_device/manifest.json new file mode 100644 index 000000000000..4b964cf33914 --- /dev/null +++ b/chrome/test/data/extensions/platform_apps/restart_device/manifest.json @@ -0,0 +1,10 @@ +{ + "name": "Platform App Test: runtime.restart API", + "version": "1", + "kiosk_enabled": true, + "app": { + "background": { + "scripts": ["test.js"] + } + } +} diff --git a/chrome/test/data/extensions/platform_apps/restart_device/test.js b/chrome/test/data/extensions/platform_apps/restart_device/test.js new file mode 100644 index 000000000000..2f9f855f2d12 --- /dev/null +++ b/chrome/test/data/extensions/platform_apps/restart_device/test.js @@ -0,0 +1,7 @@ +// Copyright 2013 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. + +chrome.app.runtime.onLaunched.addListener(function() { + chrome.app.window.create('main.html', {}, function () {}); +}); -- 2.11.4.GIT