blob: 402c5fd7d09bc8ba66c99d71d513b38838d84fbf [file] [log] [blame]
Avi Drissman60039d42022-09-13 21:49:051// Copyright 2013 The Chromium Authors
[email protected]6f371442011-11-09 06:45:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]50de9aa22013-11-14 06:30:345#include "extensions/browser/process_map.h"
[email protected]6f371442011-11-09 06:45:466
tbarzic8e89b0b12017-06-10 03:25:517#include "base/memory/ref_counted.h"
8#include "base/values.h"
9#include "extensions/common/extension.h"
10#include "extensions/common/extension_builder.h"
11#include "extensions/common/features/feature.h"
12#include "extensions/common/value_builder.h"
[email protected]6f371442011-11-09 06:45:4613#include "testing/gtest/include/gtest/gtest.h"
14
tbarzic8e89b0b12017-06-10 03:25:5115namespace extensions {
16namespace {
17
18enum class TypeToCreate { kExtension, kHostedApp, kPlatformApp };
19
20scoped_refptr<const Extension> CreateExtensionWithFlags(TypeToCreate type,
21 const std::string& id) {
22 DictionaryBuilder manifest_builder;
23 manifest_builder.Set("name", "Test extension")
24 .Set("version", "1.0")
25 .Set("manifest_version", 2);
26
27 switch (type) {
28 case TypeToCreate::kExtension:
29 manifest_builder.Set(
30 "background",
31 DictionaryBuilder()
32 .Set("scripts", ListBuilder().Append("background.js").Build())
33 .Build());
34 break;
35 case TypeToCreate::kHostedApp:
36 manifest_builder.Set(
37 "app", DictionaryBuilder()
38 .Set("launch", DictionaryBuilder()
39 .Set("web_url", "https://www.foo.bar")
40 .Build())
41 .Build());
42 break;
43 case TypeToCreate::kPlatformApp:
44 manifest_builder.Set(
45 "app",
46 DictionaryBuilder()
47 .Set("background",
48 DictionaryBuilder()
49 .Set("scripts",
50 ListBuilder().Append("background.js").Build())
51 .Build())
52 .Build());
53 break;
54 }
55
56 return ExtensionBuilder()
57 .SetID(id)
58 .SetManifest(manifest_builder.Build())
59 .Build();
60}
61
62} // namespace
63} // namespace extensions
64
[email protected]6f371442011-11-09 06:45:4665using extensions::ProcessMap;
66
67TEST(ExtensionProcessMapTest, Test) {
68 ProcessMap map;
69
70 // Test behavior when empty.
71 EXPECT_FALSE(map.Contains("a", 1));
Lukasz Anforowicz1de0a222021-07-26 22:02:3272 EXPECT_FALSE(map.Remove("a", 1, content::SiteInstanceId(1)));
[email protected]6f371442011-11-09 06:45:4673 EXPECT_EQ(0u, map.size());
74
75 // Test insertion and behavior with one item.
Lukasz Anforowicz1de0a222021-07-26 22:02:3276 EXPECT_TRUE(map.Insert("a", 1, content::SiteInstanceId(1)));
[email protected]6f371442011-11-09 06:45:4677 EXPECT_TRUE(map.Contains("a", 1));
78 EXPECT_FALSE(map.Contains("a", 2));
79 EXPECT_FALSE(map.Contains("b", 1));
80 EXPECT_EQ(1u, map.size());
81
82 // Test inserting a duplicate item.
Lukasz Anforowicz1de0a222021-07-26 22:02:3283 EXPECT_FALSE(map.Insert("a", 1, content::SiteInstanceId(1)));
[email protected]6f371442011-11-09 06:45:4684 EXPECT_TRUE(map.Contains("a", 1));
85 EXPECT_EQ(1u, map.size());
86
87 // Insert some more items.
Lukasz Anforowicz1de0a222021-07-26 22:02:3288 EXPECT_TRUE(map.Insert("a", 2, content::SiteInstanceId(2)));
89 EXPECT_TRUE(map.Insert("b", 1, content::SiteInstanceId(3)));
90 EXPECT_TRUE(map.Insert("b", 2, content::SiteInstanceId(4)));
[email protected]6f371442011-11-09 06:45:4691 EXPECT_EQ(4u, map.size());
92
93 EXPECT_TRUE(map.Contains("a", 1));
94 EXPECT_TRUE(map.Contains("a", 2));
95 EXPECT_TRUE(map.Contains("b", 1));
96 EXPECT_TRUE(map.Contains("b", 2));
97 EXPECT_FALSE(map.Contains("a", 3));
98
[email protected]6bc04fd82011-12-04 02:29:3599 // Note that this only differs from an existing item because of the site
100 // instance id.
Lukasz Anforowicz1de0a222021-07-26 22:02:32101 EXPECT_TRUE(map.Insert("a", 1, content::SiteInstanceId(5)));
[email protected]6bc04fd82011-12-04 02:29:35102 EXPECT_TRUE(map.Contains("a", 1));
[email protected]6f371442011-11-09 06:45:46103
[email protected]6bc04fd82011-12-04 02:29:35104 // Test removal.
Lukasz Anforowicz1de0a222021-07-26 22:02:32105 EXPECT_TRUE(map.Remove("a", 1, content::SiteInstanceId(1)));
106 EXPECT_FALSE(map.Remove("a", 1, content::SiteInstanceId(1)));
[email protected]6bc04fd82011-12-04 02:29:35107 EXPECT_EQ(4u, map.size());
108
109 // Should still return true because there were two site instances for this
110 // extension/process pair.
111 EXPECT_TRUE(map.Contains("a", 1));
112
Lukasz Anforowicz1de0a222021-07-26 22:02:32113 EXPECT_TRUE(map.Remove("a", 1, content::SiteInstanceId(5)));
[email protected]6bc04fd82011-12-04 02:29:35114 EXPECT_EQ(3u, map.size());
115 EXPECT_FALSE(map.Contains("a", 1));
116
117 EXPECT_EQ(2, map.RemoveAllFromProcess(2));
[email protected]6f371442011-11-09 06:45:46118 EXPECT_EQ(1u, map.size());
[email protected]6bc04fd82011-12-04 02:29:35119 EXPECT_EQ(0, map.RemoveAllFromProcess(2));
[email protected]6f371442011-11-09 06:45:46120 EXPECT_EQ(1u, map.size());
121}
tbarzic8e89b0b12017-06-10 03:25:51122
123TEST(ExtensionProcessMapTest, GetMostLikelyContextType) {
124 ProcessMap map;
Giovanni Ortuño Urquidi7b657232020-03-01 12:08:46125 const GURL web_url("https://foo.example");
126 const GURL extension_url("chrome-extension://foobar");
127 const GURL untrusted_webui_url("chrome-untrusted://foo/index.html");
tbarzic8e89b0b12017-06-10 03:25:51128
129 EXPECT_EQ(extensions::Feature::WEB_PAGE_CONTEXT,
Giovanni Ortuño Urquidi7b657232020-03-01 12:08:46130 map.GetMostLikelyContextType(nullptr, 1, &web_url));
tbarzic8e89b0b12017-06-10 03:25:51131
132 scoped_refptr<const extensions::Extension> extension =
133 CreateExtensionWithFlags(extensions::TypeToCreate::kExtension, "a");
134
135 EXPECT_EQ(extensions::Feature::CONTENT_SCRIPT_CONTEXT,
Giovanni Ortuño Urquidi7b657232020-03-01 12:08:46136 map.GetMostLikelyContextType(extension.get(), 2, &extension_url));
137
138 EXPECT_EQ(extensions::Feature::CONTENT_SCRIPT_CONTEXT,
139 map.GetMostLikelyContextType(extension.get(), 2, &web_url));
140
141 EXPECT_EQ(
142 extensions::Feature::CONTENT_SCRIPT_CONTEXT,
143 map.GetMostLikelyContextType(extension.get(), 2, &untrusted_webui_url));
tbarzic8e89b0b12017-06-10 03:25:51144
Lukasz Anforowicz1de0a222021-07-26 22:02:32145 map.Insert("b", 3, content::SiteInstanceId(1));
tbarzic8e89b0b12017-06-10 03:25:51146 extension =
147 CreateExtensionWithFlags(extensions::TypeToCreate::kExtension, "b");
148 EXPECT_EQ(extensions::Feature::BLESSED_EXTENSION_CONTEXT,
Giovanni Ortuño Urquidi7b657232020-03-01 12:08:46149 map.GetMostLikelyContextType(extension.get(), 3, &extension_url));
tbarzic8e89b0b12017-06-10 03:25:51150
Lukasz Anforowicz1de0a222021-07-26 22:02:32151 map.Insert("c", 4, content::SiteInstanceId(2));
tbarzic8e89b0b12017-06-10 03:25:51152 extension =
153 CreateExtensionWithFlags(extensions::TypeToCreate::kPlatformApp, "c");
154 EXPECT_EQ(extensions::Feature::BLESSED_EXTENSION_CONTEXT,
Giovanni Ortuño Urquidi7b657232020-03-01 12:08:46155 map.GetMostLikelyContextType(extension.get(), 4, &extension_url));
tbarzic8e89b0b12017-06-10 03:25:51156
157 map.set_is_lock_screen_context(true);
158
Lukasz Anforowicz1de0a222021-07-26 22:02:32159 map.Insert("d", 5, content::SiteInstanceId(3));
tbarzic8e89b0b12017-06-10 03:25:51160 extension =
161 CreateExtensionWithFlags(extensions::TypeToCreate::kPlatformApp, "d");
162 EXPECT_EQ(extensions::Feature::LOCK_SCREEN_EXTENSION_CONTEXT,
Giovanni Ortuño Urquidi7b657232020-03-01 12:08:46163 map.GetMostLikelyContextType(extension.get(), 5, &extension_url));
tbarzic8e89b0b12017-06-10 03:25:51164
Lukasz Anforowicz1de0a222021-07-26 22:02:32165 map.Insert("e", 6, content::SiteInstanceId(4));
tbarzic8e89b0b12017-06-10 03:25:51166 extension =
167 CreateExtensionWithFlags(extensions::TypeToCreate::kExtension, "e");
168 EXPECT_EQ(extensions::Feature::LOCK_SCREEN_EXTENSION_CONTEXT,
Giovanni Ortuño Urquidi7b657232020-03-01 12:08:46169 map.GetMostLikelyContextType(extension.get(), 6, &extension_url));
tbarzic8e89b0b12017-06-10 03:25:51170
Lukasz Anforowicz1de0a222021-07-26 22:02:32171 map.Insert("f", 7, content::SiteInstanceId(5));
tbarzic8e89b0b12017-06-10 03:25:51172 extension =
173 CreateExtensionWithFlags(extensions::TypeToCreate::kHostedApp, "f");
174 EXPECT_EQ(extensions::Feature::BLESSED_WEB_PAGE_CONTEXT,
Giovanni Ortuño Urquidi7b657232020-03-01 12:08:46175 map.GetMostLikelyContextType(extension.get(), 7, &web_url));
176
Lukasz Anforowicz1de0a222021-07-26 22:02:32177 map.Insert("g", 8, content::SiteInstanceId(6));
Giovanni Ortuño Urquidi7b657232020-03-01 12:08:46178 EXPECT_EQ(extensions::Feature::WEBUI_UNTRUSTED_CONTEXT,
179 map.GetMostLikelyContextType(/*extension=*/nullptr, 8,
180 &untrusted_webui_url));
181
Lukasz Anforowicz1de0a222021-07-26 22:02:32182 map.Insert("h", 9, content::SiteInstanceId(7));
Giovanni Ortuño Urquidi7b657232020-03-01 12:08:46183 EXPECT_EQ(extensions::Feature::WEB_PAGE_CONTEXT,
184 map.GetMostLikelyContextType(/*extension=*/nullptr, 9, &web_url));
tbarzic8e89b0b12017-06-10 03:25:51185}