1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/power/extension_event_observer.h"
#include <stdint.h>
#include <memory>
#include <string>
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/values.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/ash/settings/scoped_cros_settings_test_helper.h"
#include "chrome/common/extensions/api/gcm.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "chromeos/dbus/power_manager/suspend.pb.h"
#include "components/user_manager/scoped_user_manager.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_renderer_host.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_host_observer.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
class ExtensionEventObserverTest : public ChromeRenderViewHostTestHarness {
public:
ExtensionEventObserverTest()
: fake_user_manager_(new FakeChromeUserManager()),
scoped_user_manager_enabler_(
base::WrapUnique(fake_user_manager_.get())) {}
ExtensionEventObserverTest(const ExtensionEventObserverTest&) = delete;
ExtensionEventObserverTest& operator=(const ExtensionEventObserverTest&) =
delete;
~ExtensionEventObserverTest() override = default;
// ChromeRenerViewHostTestHarness overrides:
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
chromeos::PowerManagerClient::InitializeFake();
testing_local_state_ = std::make_unique<ScopedTestingLocalState>(
TestingBrowserProcess::GetGlobal());
profile_manager_ = std::make_unique<TestingProfileManager>(
TestingBrowserProcess::GetGlobal());
// Must be called from ::testing::Test::SetUp.
ASSERT_TRUE(profile_manager_->SetUp());
extension_event_observer_ = std::make_unique<ExtensionEventObserver>();
test_api_ = extension_event_observer_->CreateTestApi();
const char kUserProfile[] = "profile1@example.com";
const AccountId account_id(AccountId::FromUserEmail(kUserProfile));
fake_user_manager_->AddUser(account_id);
fake_user_manager_->LoginUser(account_id);
profile_ =
profile_manager_->CreateTestingProfile(account_id.GetUserEmail());
}
void TearDown() override {
extension_event_observer_.reset();
profile_ = nullptr;
profile_manager_->DeleteAllTestingProfiles();
profile_manager_.reset();
chromeos::PowerManagerClient::Shutdown();
ChromeRenderViewHostTestHarness::TearDown();
}
protected:
scoped_refptr<const extensions::Extension> CreateApp(const std::string& name,
bool uses_gcm) {
scoped_refptr<const extensions::Extension> app =
extensions::ExtensionBuilder()
.SetManifest(
base::Value::Dict()
.Set("name", name)
.Set("version", "1.0.0")
.Set("manifest_version", 2)
.Set("app", base::Value::Dict().Set(
"background",
base::Value::Dict().Set(
"scripts", base::Value::List().Append(
"background.js"))))
.Set("permissions",
base::Value::List().Append(uses_gcm ? "gcm" : "")))
.Build();
created_apps_.push_back(app);
return app;
}
extensions::ExtensionHost* CreateHostForApp(
Profile* profile,
const extensions::Extension* app) {
extensions::ProcessManager::Get(profile)->CreateBackgroundHost(
app, extensions::BackgroundInfo::GetBackgroundURL(app));
base::RunLoop().RunUntilIdle();
return extensions::ProcessManager::Get(profile)
->GetBackgroundHostForExtension(app->id());
}
std::unique_ptr<ExtensionEventObserver> extension_event_observer_;
std::unique_ptr<ExtensionEventObserver::TestApi> test_api_;
// Owned by |profile_manager_|.
raw_ptr<TestingProfile> profile_;
std::unique_ptr<TestingProfileManager> profile_manager_;
private:
// Chrome OS needs the CrosSettings test helper.
ScopedCrosSettingsTestHelper cros_settings_test_helper_;
// Owned by |scoped_user_manager_enabler_|.
raw_ptr<FakeChromeUserManager, DanglingUntriaged> fake_user_manager_;
user_manager::ScopedUserManager scoped_user_manager_enabler_;
std::vector<scoped_refptr<const extensions::Extension>> created_apps_;
std::unique_ptr<ScopedTestingLocalState> testing_local_state_;
};
// Tests that the ExtensionEventObserver reports readiness for suspend when
// there is nothing interesting going on.
TEST_F(ExtensionEventObserverTest, BasicSuspendAndDarkSuspend) {
chromeos::FakePowerManagerClient::Get()->SendSuspendImminent(
power_manager::SuspendImminent_Reason_OTHER);
EXPECT_EQ(1, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
EXPECT_TRUE(test_api_->MaybeRunSuspendReadinessCallback());
EXPECT_EQ(0, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
chromeos::FakePowerManagerClient::Get()->SendDarkSuspendImminent();
EXPECT_EQ(1, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
EXPECT_TRUE(test_api_->MaybeRunSuspendReadinessCallback());
EXPECT_EQ(0, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
}
// Tests that the ExtensionEventObserver properly handles a canceled suspend
// attempt.
TEST_F(ExtensionEventObserverTest, CanceledSuspend) {
chromeos::FakePowerManagerClient::Get()->SendSuspendImminent(
power_manager::SuspendImminent_Reason_OTHER);
EXPECT_EQ(1, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
chromeos::FakePowerManagerClient::Get()->SendSuspendDone();
EXPECT_FALSE(test_api_->MaybeRunSuspendReadinessCallback());
}
// Tests that the ExtensionEventObserver delays suspends and dark suspends while
// there is a push message pending for an app that uses GCM.
TEST_F(ExtensionEventObserverTest, PushMessagesDelaySuspend) {
scoped_refptr<const extensions::Extension> gcm_app =
CreateApp("DelaysSuspendForPushMessages", true /* uses_gcm */);
extensions::ExtensionHost* host = CreateHostForApp(profile_, gcm_app.get());
ASSERT_TRUE(host);
EXPECT_TRUE(test_api_->WillDelaySuspendForExtensionHost(host));
// Test that a push message received before a suspend attempt delays the
// attempt.
const int kSuspendPushId = 23874;
extension_event_observer_->OnBackgroundEventDispatched(
host, extensions::api::gcm::OnMessage::kEventName, kSuspendPushId);
chromeos::FakePowerManagerClient::Get()->SendSuspendImminent(
power_manager::SuspendImminent_Reason_OTHER);
EXPECT_TRUE(test_api_->MaybeRunSuspendReadinessCallback());
EXPECT_EQ(1, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
extension_event_observer_->OnBackgroundEventAcked(host, kSuspendPushId);
EXPECT_EQ(0, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
// Now test receiving the suspend attempt before the push message.
const int kDarkSuspendPushId = 56674;
chromeos::FakePowerManagerClient::Get()->SendDarkSuspendImminent();
extension_event_observer_->OnBackgroundEventDispatched(
host, extensions::api::gcm::OnMessage::kEventName, kDarkSuspendPushId);
EXPECT_TRUE(test_api_->MaybeRunSuspendReadinessCallback());
EXPECT_EQ(1, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
extension_event_observer_->OnBackgroundEventAcked(host, kDarkSuspendPushId);
EXPECT_EQ(0, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
// Test that non-push messages do not delay the suspend.
const int kNonPushId = 5687;
chromeos::FakePowerManagerClient::Get()->SendDarkSuspendImminent();
extension_event_observer_->OnBackgroundEventDispatched(host, "FakeMessage",
kNonPushId);
EXPECT_TRUE(test_api_->MaybeRunSuspendReadinessCallback());
EXPECT_EQ(0, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
}
// Tests that messages sent for apps that don't use GCM are ignored.
TEST_F(ExtensionEventObserverTest, IgnoresNonGCMApps) {
scoped_refptr<const extensions::Extension> app = CreateApp("Non-GCM", false);
extensions::ExtensionHost* host = CreateHostForApp(profile_, app.get());
ASSERT_TRUE(host);
EXPECT_FALSE(test_api_->WillDelaySuspendForExtensionHost(host));
chromeos::FakePowerManagerClient::Get()->SendSuspendImminent(
power_manager::SuspendImminent_Reason_OTHER);
EXPECT_TRUE(test_api_->MaybeRunSuspendReadinessCallback());
EXPECT_EQ(0, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
}
// Tests that network requests started by an app while it is processing a push
// message delay any suspend attempt.
TEST_F(ExtensionEventObserverTest, NetworkRequestsMayDelaySuspend) {
scoped_refptr<const extensions::Extension> app =
CreateApp("NetworkRequests", true);
extensions::ExtensionHost* host = CreateHostForApp(profile_, app.get());
ASSERT_TRUE(host);
EXPECT_TRUE(test_api_->WillDelaySuspendForExtensionHost(host));
// Test that network requests started while there is no pending push message
// are ignored.
const uint64_t kNonPushRequestId = 5170725;
extension_event_observer_->OnNetworkRequestStarted(host, kNonPushRequestId);
chromeos::FakePowerManagerClient::Get()->SendSuspendImminent(
power_manager::SuspendImminent_Reason_OTHER);
EXPECT_TRUE(test_api_->MaybeRunSuspendReadinessCallback());
EXPECT_EQ(0, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
// Test that network requests started while a push message is pending delay
// the suspend even after the push message has been acked.
const int kPushMessageId = 178674;
const uint64_t kNetworkRequestId = 78917089;
chromeos::FakePowerManagerClient::Get()->SendDarkSuspendImminent();
extension_event_observer_->OnBackgroundEventDispatched(
host, extensions::api::gcm::OnMessage::kEventName, kPushMessageId);
EXPECT_TRUE(test_api_->MaybeRunSuspendReadinessCallback());
EXPECT_EQ(1, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
extension_event_observer_->OnNetworkRequestStarted(host, kNetworkRequestId);
extension_event_observer_->OnBackgroundEventAcked(host, kPushMessageId);
EXPECT_EQ(1, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
extension_event_observer_->OnNetworkRequestDone(host, kNetworkRequestId);
EXPECT_EQ(0, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
}
// Tests that any outstanding push messages or network requests for an
// ExtensionHost that is destroyed do not end up blocking system suspend.
TEST_F(ExtensionEventObserverTest, DeletedExtensionHostDoesNotBlockSuspend) {
scoped_refptr<const extensions::Extension> app =
CreateApp("DeletedExtensionHost", true);
extensions::ExtensionHost* host = CreateHostForApp(profile_, app.get());
ASSERT_TRUE(host);
EXPECT_TRUE(test_api_->WillDelaySuspendForExtensionHost(host));
const int kPushId = 156178;
const uint64_t kNetworkId = 791605;
extension_event_observer_->OnBackgroundEventDispatched(
host, extensions::api::gcm::OnMessage::kEventName, kPushId);
extension_event_observer_->OnNetworkRequestStarted(host, kNetworkId);
// Now delete the ExtensionHosts.
host->Close();
chromeos::FakePowerManagerClient::Get()->SendSuspendImminent(
power_manager::SuspendImminent_Reason_OTHER);
EXPECT_TRUE(test_api_->MaybeRunSuspendReadinessCallback());
EXPECT_EQ(0, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
}
// Tests that the ExtensionEventObserver does not delay suspend attempts when it
// is disabled.
TEST_F(ExtensionEventObserverTest, DoesNotDelaySuspendWhenDisabled) {
scoped_refptr<const extensions::Extension> app =
CreateApp("NoDelayWhenDisabled", true);
extensions::ExtensionHost* host = CreateHostForApp(profile_, app.get());
ASSERT_TRUE(host);
EXPECT_TRUE(test_api_->WillDelaySuspendForExtensionHost(host));
// Test that disabling the suspend delay while a suspend is pending will cause
// the ExtensionEventObserver to immediately report readiness.
const int kPushId = 416753;
extension_event_observer_->OnBackgroundEventDispatched(
host, extensions::api::gcm::OnMessage::kEventName, kPushId);
chromeos::FakePowerManagerClient::Get()->SendSuspendImminent(
power_manager::SuspendImminent_Reason_OTHER);
EXPECT_EQ(1, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
extension_event_observer_->SetShouldDelaySuspend(false);
EXPECT_FALSE(test_api_->MaybeRunSuspendReadinessCallback());
EXPECT_EQ(0, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
// Test that the ExtensionEventObserver does not delay suspend attempts when
// it is disabled.
chromeos::FakePowerManagerClient::Get()->SendDarkSuspendImminent();
EXPECT_EQ(0, chromeos::FakePowerManagerClient::Get()
->num_pending_suspend_readiness_callbacks());
}
} // namespace ash
|