| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h" | 5 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" |
| 11 #include "base/callback.h" |
| 10 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 11 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 12 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 14 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/location.h" |
| 18 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/message_loop/message_loop_proxy.h" |
| 16 #include "base/prefs/pref_service.h" | 21 #include "base/prefs/pref_service.h" |
| 22 #include "base/run_loop.h" |
| 23 #include "base/single_thread_task_runner.h" |
| 17 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/test/scoped_path_override.h" | 25 #include "base/test/scoped_path_override.h" |
| 19 #include "base/threading/sequenced_worker_pool.h" | 26 #include "base/threading/sequenced_worker_pool.h" |
| 20 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 21 #include "chrome/browser/apps/app_browsertest_util.h" | 28 #include "chrome/browser/apps/app_browsertest_util.h" |
| 22 #include "chrome/browser/browser_process.h" | 29 #include "chrome/browser/browser_process.h" |
| 23 #include "chrome/browser/browser_process_platform_part.h" | 30 #include "chrome/browser/browser_process_platform_part.h" |
| 24 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h" | 31 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h" |
| 32 #include "chrome/browser/chromeos/system/automatic_reboot_manager_observer.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 33 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/common/chrome_switches.h" | 34 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
| 28 #include "chromeos/chromeos_paths.h" | 36 #include "chromeos/chromeos_paths.h" |
| 29 #include "chromeos/dbus/update_engine_client.h" | 37 #include "chromeos/dbus/update_engine_client.h" |
| 30 #include "content/public/browser/browser_thread.h" | 38 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/test/test_utils.h" | |
| 32 #include "extensions/common/extension.h" | 39 #include "extensions/common/extension.h" |
| 33 #include "extensions/test/extension_test_message_listener.h" | 40 #include "extensions/test/extension_test_message_listener.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 35 | 42 |
| 36 namespace chromeos { | 43 namespace chromeos { |
| 37 | 44 |
| 38 class KioskAppUpdateServiceTest : public extensions::PlatformAppBrowserTest { | 45 namespace { |
| 46 |
| 47 void RunCallback(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 48 const base::Closure& callback) { |
| 49 task_runner->PostTask(FROM_HERE, callback); |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
| 54 class KioskAppUpdateServiceTest |
| 55 : public extensions::PlatformAppBrowserTest, |
| 56 public system::AutomaticRebootManagerObserver { |
| 39 public: | 57 public: |
| 40 KioskAppUpdateServiceTest() : app_(NULL), update_service_(NULL) {} | 58 KioskAppUpdateServiceTest() |
| 41 virtual ~KioskAppUpdateServiceTest() {} | 59 : app_(NULL), |
| 60 update_service_(NULL), |
| 61 automatic_reboot_manager_(NULL) {} |
| 62 |
| 63 ~KioskAppUpdateServiceTest() override {} |
| 42 | 64 |
| 43 // extensions::PlatformAppBrowserTest overrides: | 65 // extensions::PlatformAppBrowserTest overrides: |
| 44 virtual void SetUpOnMainThread() override { | 66 void SetUpInProcessBrowserTestFixture() override { |
| 45 extensions::PlatformAppBrowserTest::SetUpOnMainThread(); | 67 extensions::PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 46 | 68 |
| 47 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 48 const base::FilePath& temp_dir = temp_dir_.path(); | 70 const base::FilePath& temp_dir = temp_dir_.path(); |
| 49 | 71 |
| 50 const base::TimeDelta uptime = base::TimeDelta::FromHours(1); | 72 const base::TimeDelta uptime = base::TimeDelta::FromHours(3); |
| 51 const std::string uptime_seconds = | 73 const std::string uptime_seconds = |
| 52 base::DoubleToString(uptime.InSecondsF()); | 74 base::DoubleToString(uptime.InSecondsF()); |
| 53 const base::FilePath uptime_file = temp_dir.Append("uptime"); | 75 const base::FilePath uptime_file = temp_dir.Append("uptime"); |
| 54 ASSERT_EQ(static_cast<int>(uptime_seconds.size()), | 76 ASSERT_EQ(static_cast<int>(uptime_seconds.size()), |
| 55 base::WriteFile( | 77 base::WriteFile( |
| 56 uptime_file, uptime_seconds.c_str(), uptime_seconds.size())); | 78 uptime_file, uptime_seconds.c_str(), uptime_seconds.size())); |
| 57 uptime_file_override_.reset( | 79 uptime_file_override_.reset( |
| 58 new base::ScopedPathOverride(chromeos::FILE_UPTIME, uptime_file)); | 80 new base::ScopedPathOverride(chromeos::FILE_UPTIME, uptime_file)); |
| 81 } |
| 82 |
| 83 void SetUpOnMainThread() override { |
| 84 extensions::PlatformAppBrowserTest::SetUpOnMainThread(); |
| 59 | 85 |
| 60 app_ = LoadExtension( | 86 app_ = LoadExtension( |
| 61 test_data_dir_.AppendASCII("api_test/runtime/on_restart_required")); | 87 test_data_dir_.AppendASCII("api_test/runtime/on_restart_required")); |
| 62 | 88 |
| 63 // Fake app mode command line. | 89 // Fake app mode command line. |
| 64 CommandLine* command = CommandLine::ForCurrentProcess(); | 90 CommandLine* command = CommandLine::ForCurrentProcess(); |
| 65 command->AppendSwitch(switches::kForceAppMode); | 91 command->AppendSwitch(switches::kForceAppMode); |
| 66 command->AppendSwitchASCII(switches::kAppId, app_->id()); | 92 command->AppendSwitchASCII(switches::kAppId, app_->id()); |
| 67 | 93 |
| 94 automatic_reboot_manager_ = |
| 95 g_browser_process->platform_part()->automatic_reboot_manager(); |
| 96 |
| 97 // Wait for the |automatic_reboot_manager_| to finish initializing. |
| 98 base::RunLoop run_loop; |
| 99 base::SequencedWorkerPool* worker_pool = |
| 100 content::BrowserThread::GetBlockingPool(); |
| 101 // Ensure that the initialization task the |automatic_reboot_manager_| posts |
| 102 // to the blocking pool has finished by posting another task with the same |
| 103 // |SequenceToken| and waiting for it to finish. |
| 104 worker_pool->PostSequencedWorkerTask( |
| 105 worker_pool->GetNamedSequenceToken("automatic-reboot-manager"), |
| 106 FROM_HERE, |
| 107 base::Bind(&RunCallback, |
| 108 base::MessageLoopProxy::current(), |
| 109 run_loop.QuitClosure())); |
| 110 run_loop.Run(); |
| 111 // Ensure that the |automatic_reboot_manager_| has had a chance to fully |
| 112 // process the result of the task posted to the blocking pool. |
| 113 base::RunLoop().RunUntilIdle(); |
| 114 |
| 115 automatic_reboot_manager_->AddObserver(this); |
| 116 } |
| 117 |
| 118 // system::AutomaticRebootManagerObserver: |
| 119 void OnRebootRequested( |
| 120 system::AutomaticRebootManagerObserver::Reason) override { |
| 121 if (run_loop_) |
| 122 run_loop_->Quit(); |
| 123 } |
| 124 |
| 125 void WillDestroyAutomaticRebootManager() override { |
| 126 automatic_reboot_manager_->RemoveObserver(this); |
| 127 } |
| 128 |
| 129 void CreateKioskAppUpdateService() { |
| 130 EXPECT_FALSE(update_service_); |
| 68 update_service_ = KioskAppUpdateServiceFactory::GetForProfile(profile()); | 131 update_service_ = KioskAppUpdateServiceFactory::GetForProfile(profile()); |
| 69 update_service_->set_app_id(app_->id()); | 132 update_service_->Init(app_->id()); |
| 70 | |
| 71 content::RunAllBlockingPoolTasksUntilIdle(); | |
| 72 } | 133 } |
| 73 | 134 |
| 74 void FireAppUpdateAvailable() { | 135 void FireAppUpdateAvailable() { |
| 75 update_service_->OnAppUpdateAvailable(app_); | 136 update_service_->OnAppUpdateAvailable(app_); |
| 76 } | 137 } |
| 77 | 138 |
| 78 void FireUpdatedNeedReboot() { | 139 void FireUpdatedNeedReboot() { |
| 79 UpdateEngineClient::Status status; | 140 UpdateEngineClient::Status status; |
| 80 status.status = UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT; | 141 status.status = UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT; |
| 81 g_browser_process->platform_part()->automatic_reboot_manager()-> | 142 run_loop_.reset(new base::RunLoop); |
| 82 UpdateStatusChanged(status); | 143 automatic_reboot_manager_->UpdateStatusChanged(status); |
| 144 run_loop_->Run(); |
| 145 } |
| 146 |
| 147 void RequestPeriodicReboot() { |
| 148 run_loop_.reset(new base::RunLoop); |
| 149 g_browser_process->local_state()->SetInteger( |
| 150 prefs::kUptimeLimit, base::TimeDelta::FromHours(2).InSeconds()); |
| 151 run_loop_->Run(); |
| 83 } | 152 } |
| 84 | 153 |
| 85 private: | 154 private: |
| 86 base::ScopedTempDir temp_dir_; | 155 base::ScopedTempDir temp_dir_; |
| 87 scoped_ptr<base::ScopedPathOverride> uptime_file_override_; | 156 scoped_ptr<base::ScopedPathOverride> uptime_file_override_; |
| 88 const extensions::Extension* app_; // Not owned. | 157 const extensions::Extension* app_; // Not owned. |
| 89 KioskAppUpdateService* update_service_; // Not owned. | 158 KioskAppUpdateService* update_service_; // Not owned. |
| 159 system::AutomaticRebootManager* automatic_reboot_manager_; // Not owned. |
| 160 scoped_ptr<base::RunLoop> run_loop_; |
| 90 | 161 |
| 91 DISALLOW_COPY_AND_ASSIGN(KioskAppUpdateServiceTest); | 162 DISALLOW_COPY_AND_ASSIGN(KioskAppUpdateServiceTest); |
| 92 }; | 163 }; |
| 93 | 164 |
| 165 // Verifies that the app is notified a reboot is required when an app update |
| 166 // becomes available. |
| 94 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, AppUpdate) { | 167 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, AppUpdate) { |
| 95 FireAppUpdateAvailable(); | 168 CreateKioskAppUpdateService(); |
| 96 | 169 |
| 97 ExtensionTestMessageListener listener("app_update", false); | 170 ExtensionTestMessageListener listener("app_update", false); |
| 171 FireAppUpdateAvailable(); |
| 98 listener.WaitUntilSatisfied(); | 172 listener.WaitUntilSatisfied(); |
| 99 } | 173 } |
| 100 | 174 |
| 175 // Verifies that the app is notified a reboot is required when an OS update is |
| 176 // applied while Chrome is running and the policy to reboot after update is |
| 177 // enabled. |
| 101 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, OsUpdate) { | 178 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, OsUpdate) { |
| 179 CreateKioskAppUpdateService(); |
| 180 |
| 181 g_browser_process->local_state()->SetBoolean(prefs::kRebootAfterUpdate, true); |
| 182 ExtensionTestMessageListener listener("os_update", false); |
| 183 FireUpdatedNeedReboot(); |
| 184 listener.WaitUntilSatisfied(); |
| 185 } |
| 186 |
| 187 // Verifies that the app is notified a reboot is required when a periodic reboot |
| 188 // is requested while Chrome is running. |
| 189 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, Periodic) { |
| 190 CreateKioskAppUpdateService(); |
| 191 |
| 192 ExtensionTestMessageListener listener("periodic", false); |
| 193 RequestPeriodicReboot(); |
| 194 listener.WaitUntilSatisfied(); |
| 195 } |
| 196 |
| 197 // Verifies that the app is notified a reboot is required when an OS update was |
| 198 // applied before Chrome was started and the policy to reboot after update is |
| 199 // enabled. |
| 200 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, StartAfterOsUpdate) { |
| 102 g_browser_process->local_state()->SetBoolean(prefs::kRebootAfterUpdate, true); | 201 g_browser_process->local_state()->SetBoolean(prefs::kRebootAfterUpdate, true); |
| 103 FireUpdatedNeedReboot(); | 202 FireUpdatedNeedReboot(); |
| 104 | 203 |
| 105 ExtensionTestMessageListener listener("os_update", false); | 204 ExtensionTestMessageListener listener("os_update", false); |
| 205 CreateKioskAppUpdateService(); |
| 106 listener.WaitUntilSatisfied(); | 206 listener.WaitUntilSatisfied(); |
| 107 } | 207 } |
| 108 | 208 |
| 109 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, Periodic) { | 209 // Verifies that the app is notified a reboot is required when a periodic reboot |
| 110 g_browser_process->local_state()->SetInteger( | 210 // was requested before Chrome was started. |
| 111 prefs::kUptimeLimit, base::TimeDelta::FromMinutes(30).InSeconds()); | 211 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, StartAfterPeriodic) { |
| 212 RequestPeriodicReboot(); |
| 112 | 213 |
| 113 ExtensionTestMessageListener listener("periodic", false); | 214 ExtensionTestMessageListener listener("periodic", false); |
| 215 CreateKioskAppUpdateService(); |
| 114 listener.WaitUntilSatisfied(); | 216 listener.WaitUntilSatisfied(); |
| 115 } | 217 } |
| 116 | 218 |
| 117 } // namespace chromeos | 219 } // namespace chromeos |
| OLD | NEW |