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
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/mojo/keep_alive_impl.h"
#include <utility>
#include "base/run_loop.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extensions_test.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/unloaded_extension_reason.h"
#include "extensions/common/extension_builder.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
namespace extensions {
class KeepAliveTest : public ExtensionsTest {
public:
KeepAliveTest() : mojo_activity_(Activity::MOJO, "") {}
KeepAliveTest(const KeepAliveTest&) = delete;
KeepAliveTest& operator=(const KeepAliveTest&) = delete;
~KeepAliveTest() override {}
void SetUp() override {
ExtensionsTest::SetUp();
extension_ =
ExtensionBuilder()
.SetManifest(
base::Value::Dict()
.Set("name", "app")
.Set("version", "1")
.Set("manifest_version", 2)
.Set("app", base::Value::Dict().Set(
"background",
base::Value::Dict().Set(
"scripts", base::Value::List().Append(
"background.js")))))
.SetID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
.Build();
}
void WaitUntilLazyKeepAliveChanges() {
int initial_keep_alive_count = GetKeepAliveCount();
while (GetKeepAliveCount() == initial_keep_alive_count) {
base::RunLoop().RunUntilIdle();
}
}
void CreateKeepAlive(mojo::PendingReceiver<KeepAlive> receiver) {
KeepAliveImpl::Create(browser_context(), extension_.get(), nullptr,
std::move(receiver));
}
const Extension* extension() { return extension_.get(); }
int GetKeepAliveCount() {
return ProcessManager::Get(browser_context())
->GetLazyKeepaliveCount(extension());
}
using ActivitiesMultiset = ProcessManager::ActivitiesMultiset;
const std::pair<Activity::Type, std::string> mojo_activity_;
ActivitiesMultiset GetActivities() {
return ProcessManager::Get(browser_context())
->GetLazyKeepaliveActivities(extension());
}
private:
scoped_refptr<const Extension> extension_;
};
TEST_F(KeepAliveTest, Basic) {
mojo::PendingRemote<KeepAlive> keep_alive;
CreateKeepAlive(keep_alive.InitWithNewPipeAndPassReceiver());
EXPECT_EQ(1, GetKeepAliveCount());
EXPECT_EQ(1u, GetActivities().count(mojo_activity_));
keep_alive.reset();
WaitUntilLazyKeepAliveChanges();
EXPECT_EQ(0, GetKeepAliveCount());
EXPECT_EQ(0u, GetActivities().count(mojo_activity_));
}
TEST_F(KeepAliveTest, TwoKeepAlives) {
mojo::PendingRemote<KeepAlive> keep_alive;
CreateKeepAlive(keep_alive.InitWithNewPipeAndPassReceiver());
EXPECT_EQ(1, GetKeepAliveCount());
EXPECT_EQ(1u, GetActivities().count(mojo_activity_));
mojo::PendingRemote<KeepAlive> other_keep_alive;
CreateKeepAlive(other_keep_alive.InitWithNewPipeAndPassReceiver());
EXPECT_EQ(2, GetKeepAliveCount());
EXPECT_EQ(2u, GetActivities().count(mojo_activity_));
keep_alive.reset();
WaitUntilLazyKeepAliveChanges();
EXPECT_EQ(1, GetKeepAliveCount());
EXPECT_EQ(1u, GetActivities().count(mojo_activity_));
other_keep_alive.reset();
WaitUntilLazyKeepAliveChanges();
EXPECT_EQ(0, GetKeepAliveCount());
EXPECT_EQ(0u, GetActivities().count(mojo_activity_));
}
TEST_F(KeepAliveTest, UnloadExtension) {
mojo::Remote<KeepAlive> keep_alive;
CreateKeepAlive(keep_alive.BindNewPipeAndPassReceiver());
EXPECT_EQ(1, GetKeepAliveCount());
EXPECT_EQ(1u, GetActivities().count(mojo_activity_));
scoped_refptr<const Extension> other_extension =
ExtensionBuilder()
.SetManifest(
base::Value::Dict()
.Set("name", "app")
.Set("version", "1")
.Set("manifest_version", 2)
.Set("app", base::Value::Dict().Set(
"background",
base::Value::Dict().Set(
"scripts", base::Value::List().Append(
"background.js")))))
.SetID("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
.Build();
ExtensionRegistry::Get(browser_context())
->TriggerOnUnloaded(other_extension.get(),
UnloadedExtensionReason::DISABLE);
EXPECT_EQ(1, GetKeepAliveCount());
EXPECT_EQ(1u, GetActivities().count(mojo_activity_));
ExtensionRegistry::Get(browser_context())
->TriggerOnUnloaded(extension(), UnloadedExtensionReason::DISABLE);
// When its extension is unloaded, the KeepAliveImpl should not modify the
// keep-alive count for its extension. However, ProcessManager resets its
// keep-alive count for an unloaded extension.
EXPECT_EQ(0, GetKeepAliveCount());
EXPECT_EQ(0u, GetActivities().count(mojo_activity_));
// Wait for |keep_alive| to disconnect.
base::RunLoop run_loop;
keep_alive.set_disconnect_handler(run_loop.QuitClosure());
run_loop.Run();
}
TEST_F(KeepAliveTest, ShutdownExtensionRegistry) {
mojo::Remote<KeepAlive> keep_alive;
CreateKeepAlive(keep_alive.BindNewPipeAndPassReceiver());
EXPECT_EQ(1, GetKeepAliveCount());
EXPECT_EQ(1u, GetActivities().count(mojo_activity_));
ExtensionRegistry::Get(browser_context())->Shutdown();
// After a shutdown event, the KeepAliveImpl should not access its
// ProcessManager and so the keep-alive count should remain unchanged.
EXPECT_EQ(1, GetKeepAliveCount());
EXPECT_EQ(1u, GetActivities().count(mojo_activity_));
// Wait for |keep_alive| to disconnect.
base::RunLoop run_loop;
keep_alive.set_disconnect_handler(run_loop.QuitClosure());
run_loop.Run();
}
TEST_F(KeepAliveTest, ShutdownProcessManager) {
mojo::Remote<KeepAlive> keep_alive;
CreateKeepAlive(keep_alive.BindNewPipeAndPassReceiver());
EXPECT_EQ(1, GetKeepAliveCount());
EXPECT_EQ(1u, GetActivities().count(mojo_activity_));
ProcessManager::Get(browser_context())->Shutdown();
// After a shutdown event, the KeepAliveImpl should not access its
// ProcessManager and so the keep-alive count should remain unchanged.
EXPECT_EQ(1, GetKeepAliveCount());
EXPECT_EQ(1u, GetActivities().count(mojo_activity_));
// Wait for |keep_alive| to disconnect.
base::RunLoop run_loop;
keep_alive.set_disconnect_handler(run_loop.QuitClosure());
run_loop.Run();
}
} // namespace extensions
|