[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 5 | #include "chrome/browser/extensions/extension_web_ui.h" |
| 6 | |
Sylvain Defresne | 711ff6b | 2018-10-04 12:33:54 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 8 | #include "base/command_line.h" |
fdoray | cb32419d | 2016-06-23 15:52:55 | [diff] [blame] | 9 | #include "base/run_loop.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 10 | #include "build/build_config.h" |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_service.h" |
rdevlin.cronin | 5fafa5a | 2015-12-30 03:42:02 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_web_ui_override_registrar.h" |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 13 | #include "chrome/browser/extensions/test_extension_system.h" |
Matt Siembor | 8db9de0 | 2019-05-23 04:26:58 | [diff] [blame] | 14 | #include "chrome/common/webui_url_constants.h" |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 15 | #include "chrome/test/base/testing_profile.h" |
Benjamin Ackerman | 001a7f8 | 2018-09-10 14:53:30 | [diff] [blame] | 16 | #include "components/favicon_base/favicon_callback.h" |
| 17 | #include "components/favicon_base/favicon_types.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 18 | #include "content/public/test/browser_task_environment.h" |
[email protected] | 03d2581 | 2014-06-22 19:41:55 | [diff] [blame] | 19 | #include "extensions/browser/extension_system.h" |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 20 | #include "extensions/common/extension.h" |
| 21 | #include "extensions/common/extension_builder.h" |
| 22 | #include "extensions/common/manifest_constants.h" |
| 23 | #include "testing/gtest/include/gtest/gtest.h" |
Benjamin Ackerman | 001a7f8 | 2018-09-10 14:53:30 | [diff] [blame] | 24 | #include "third_party/skia/include/core/SkBitmap.h" |
| 25 | #include "ui/gfx/codec/png_codec.h" |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 26 | |
| 27 | #if defined(OS_CHROMEOS) |
[email protected] | 4d39078 | 2014-08-15 09:22:58 | [diff] [blame] | 28 | #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h" |
A Olsen | c63621c | 2018-08-13 15:32:17 | [diff] [blame] | 29 | #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h" |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | namespace extensions { |
| 33 | |
rdevlin.cronin | 5fafa5a | 2015-12-30 03:42:02 | [diff] [blame] | 34 | namespace { |
| 35 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 36 | std::unique_ptr<KeyedService> BuildOverrideRegistrar( |
rdevlin.cronin | 5fafa5a | 2015-12-30 03:42:02 | [diff] [blame] | 37 | content::BrowserContext* context) { |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 38 | return std::make_unique<ExtensionWebUIOverrideRegistrar>(context); |
rdevlin.cronin | 5fafa5a | 2015-12-30 03:42:02 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 43 | class ExtensionWebUITest : public testing::Test { |
| 44 | public: |
fdoray | 2ce6dc22 | 2017-04-27 14:39:39 | [diff] [blame] | 45 | ExtensionWebUITest() = default; |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 46 | |
| 47 | protected: |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 48 | void SetUp() override { |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 49 | profile_.reset(new TestingProfile()); |
| 50 | TestExtensionSystem* system = |
| 51 | static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_.get())); |
| 52 | extension_service_ = system->CreateExtensionService( |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 53 | base::CommandLine::ForCurrentProcess(), base::FilePath(), false); |
rdevlin.cronin | 5fafa5a | 2015-12-30 03:42:02 | [diff] [blame] | 54 | ExtensionWebUIOverrideRegistrar::GetFactoryInstance()->SetTestingFactory( |
Sylvain Defresne | 711ff6b | 2018-10-04 12:33:54 | [diff] [blame] | 55 | profile_.get(), base::BindRepeating(&BuildOverrideRegistrar)); |
rdevlin.cronin | 5fafa5a | 2015-12-30 03:42:02 | [diff] [blame] | 56 | ExtensionWebUIOverrideRegistrar::GetFactoryInstance()->Get(profile_.get()); |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 57 | } |
| 58 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 59 | void TearDown() override { |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 60 | profile_.reset(); |
fdoray | cb32419d | 2016-06-23 15:52:55 | [diff] [blame] | 61 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 62 | } |
| 63 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 64 | std::unique_ptr<TestingProfile> profile_; |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 65 | ExtensionService* extension_service_; |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 66 | content::BrowserTaskEnvironment task_environment_; |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 67 | |
| 68 | #if defined OS_CHROMEOS |
A Olsen | c63621c | 2018-08-13 15:32:17 | [diff] [blame] | 69 | chromeos::ScopedCrosSettingsTestHelper cros_settings_test_helper_; |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 70 | chromeos::ScopedTestUserManager test_user_manager_; |
| 71 | #endif |
| 72 | }; |
| 73 | |
| 74 | // Test that component extension url overrides have lower priority than |
| 75 | // non-component extension url overrides. |
| 76 | TEST_F(ExtensionWebUITest, ExtensionURLOverride) { |
rdevlin.cronin | 2b7cf6a | 2016-01-27 18:23:52 | [diff] [blame] | 77 | const char kOverrideResource[] = "1.html"; |
[email protected] | 6584f47 | 2014-03-18 17:42:24 | [diff] [blame] | 78 | // Register a non-component extension. |
[email protected] | 03d2581 | 2014-06-22 19:41:55 | [diff] [blame] | 79 | DictionaryBuilder manifest; |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 80 | manifest.Set(manifest_keys::kName, "ext1") |
| 81 | .Set(manifest_keys::kVersion, "0.1") |
Devlin Cronin | 1fa235b6 | 2018-04-12 14:04:07 | [diff] [blame] | 82 | .Set(manifest_keys::kManifestVersion, 2) |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 83 | .Set(std::string(manifest_keys::kChromeURLOverrides), |
dcheng | 794d2bd | 2016-02-27 03:51:32 | [diff] [blame] | 84 | DictionaryBuilder().Set("bookmarks", kOverrideResource).Build()); |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 85 | scoped_refptr<const Extension> ext_unpacked( |
[email protected] | 03d2581 | 2014-06-22 19:41:55 | [diff] [blame] | 86 | ExtensionBuilder() |
dcheng | 794d2bd | 2016-02-27 03:51:32 | [diff] [blame] | 87 | .SetManifest(manifest.Build()) |
[email protected] | 6584f47 | 2014-03-18 17:42:24 | [diff] [blame] | 88 | .SetLocation(Manifest::UNPACKED) |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 89 | .SetID("abcdefghijabcdefghijabcdefghijaa") |
| 90 | .Build()); |
[email protected] | 03d2581 | 2014-06-22 19:41:55 | [diff] [blame] | 91 | extension_service_->AddExtension(ext_unpacked.get()); |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 92 | |
rdevlin.cronin | 2b7cf6a | 2016-01-27 18:23:52 | [diff] [blame] | 93 | const GURL kExpectedUnpackedOverrideUrl = |
| 94 | ext_unpacked->GetResourceURL(kOverrideResource); |
Matt Siembor | 8db9de0 | 2019-05-23 04:26:58 | [diff] [blame] | 95 | const GURL kBookmarksUrl(chrome::kChromeUIBookmarksURL); |
rdevlin.cronin | 2b7cf6a | 2016-01-27 18:23:52 | [diff] [blame] | 96 | GURL changed_url = kBookmarksUrl; |
| 97 | EXPECT_TRUE( |
| 98 | ExtensionWebUI::HandleChromeURLOverride(&changed_url, profile_.get())); |
| 99 | EXPECT_EQ(kExpectedUnpackedOverrideUrl, changed_url); |
| 100 | EXPECT_TRUE(ExtensionWebUI::HandleChromeURLOverrideReverse(&changed_url, |
| 101 | profile_.get())); |
| 102 | EXPECT_EQ(kBookmarksUrl, changed_url); |
| 103 | |
| 104 | GURL url_plus_fragment = kBookmarksUrl.Resolve("#1"); |
| 105 | EXPECT_TRUE(ExtensionWebUI::HandleChromeURLOverride(&url_plus_fragment, |
| 106 | profile_.get())); |
| 107 | EXPECT_EQ(kExpectedUnpackedOverrideUrl.Resolve("#1"), |
| 108 | url_plus_fragment); |
| 109 | EXPECT_TRUE(ExtensionWebUI::HandleChromeURLOverrideReverse(&url_plus_fragment, |
| 110 | profile_.get())); |
| 111 | EXPECT_EQ(kBookmarksUrl.Resolve("#1"), url_plus_fragment); |
[email protected] | 6584f47 | 2014-03-18 17:42:24 | [diff] [blame] | 112 | |
| 113 | // Register a component extension |
rdevlin.cronin | 2b7cf6a | 2016-01-27 18:23:52 | [diff] [blame] | 114 | const char kOverrideResource2[] = "2.html"; |
[email protected] | 03d2581 | 2014-06-22 19:41:55 | [diff] [blame] | 115 | DictionaryBuilder manifest2; |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 116 | manifest2.Set(manifest_keys::kName, "ext2") |
| 117 | .Set(manifest_keys::kVersion, "0.1") |
Devlin Cronin | 1fa235b6 | 2018-04-12 14:04:07 | [diff] [blame] | 118 | .Set(manifest_keys::kManifestVersion, 2) |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 119 | .Set(std::string(manifest_keys::kChromeURLOverrides), |
dcheng | 794d2bd | 2016-02-27 03:51:32 | [diff] [blame] | 120 | DictionaryBuilder().Set("bookmarks", kOverrideResource2).Build()); |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 121 | scoped_refptr<const Extension> ext_component( |
[email protected] | 03d2581 | 2014-06-22 19:41:55 | [diff] [blame] | 122 | ExtensionBuilder() |
dcheng | 794d2bd | 2016-02-27 03:51:32 | [diff] [blame] | 123 | .SetManifest(manifest2.Build()) |
[email protected] | 6584f47 | 2014-03-18 17:42:24 | [diff] [blame] | 124 | .SetLocation(Manifest::COMPONENT) |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 125 | .SetID("bbabcdefghijabcdefghijabcdefghij") |
| 126 | .Build()); |
[email protected] | 03d2581 | 2014-06-22 19:41:55 | [diff] [blame] | 127 | extension_service_->AddComponentExtension(ext_component.get()); |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 128 | |
[email protected] | 6584f47 | 2014-03-18 17:42:24 | [diff] [blame] | 129 | // Despite being registered more recently, the component extension should |
[email protected] | 3b09cec5 | 2014-05-15 00:33:41 | [diff] [blame] | 130 | // not take precedence over the non-component extension. |
rdevlin.cronin | 2b7cf6a | 2016-01-27 18:23:52 | [diff] [blame] | 131 | changed_url = kBookmarksUrl; |
| 132 | EXPECT_TRUE( |
| 133 | ExtensionWebUI::HandleChromeURLOverride(&changed_url, profile_.get())); |
| 134 | EXPECT_EQ(kExpectedUnpackedOverrideUrl, changed_url); |
| 135 | EXPECT_TRUE(ExtensionWebUI::HandleChromeURLOverrideReverse(&changed_url, |
| 136 | profile_.get())); |
| 137 | EXPECT_EQ(kBookmarksUrl, changed_url); |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 138 | |
rdevlin.cronin | 2b7cf6a | 2016-01-27 18:23:52 | [diff] [blame] | 139 | GURL kExpectedComponentOverrideUrl = |
| 140 | ext_component->GetResourceURL(kOverrideResource2); |
[email protected] | 6584f47 | 2014-03-18 17:42:24 | [diff] [blame] | 141 | |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 142 | // Unregister non-component extension. Only component extension remaining. |
| 143 | ExtensionWebUI::UnregisterChromeURLOverrides( |
| 144 | profile_.get(), URLOverrides::GetChromeURLOverrides(ext_unpacked.get())); |
rdevlin.cronin | 2b7cf6a | 2016-01-27 18:23:52 | [diff] [blame] | 145 | changed_url = kBookmarksUrl; |
| 146 | EXPECT_TRUE( |
| 147 | ExtensionWebUI::HandleChromeURLOverride(&changed_url, profile_.get())); |
| 148 | EXPECT_EQ(kExpectedComponentOverrideUrl, changed_url); |
| 149 | EXPECT_TRUE(ExtensionWebUI::HandleChromeURLOverrideReverse(&changed_url, |
| 150 | profile_.get())); |
| 151 | EXPECT_EQ(kBookmarksUrl, changed_url); |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 152 | |
[email protected] | 6584f47 | 2014-03-18 17:42:24 | [diff] [blame] | 153 | // This time the non-component extension was registered more recently and |
| 154 | // should still take precedence. |
rdevlin.cronin | 5fafa5a | 2015-12-30 03:42:02 | [diff] [blame] | 155 | ExtensionWebUI::RegisterOrActivateChromeURLOverrides( |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 156 | profile_.get(), URLOverrides::GetChromeURLOverrides(ext_unpacked.get())); |
rdevlin.cronin | 2b7cf6a | 2016-01-27 18:23:52 | [diff] [blame] | 157 | changed_url = kBookmarksUrl; |
| 158 | EXPECT_TRUE( |
| 159 | ExtensionWebUI::HandleChromeURLOverride(&changed_url, profile_.get())); |
| 160 | EXPECT_EQ(kExpectedUnpackedOverrideUrl, changed_url); |
| 161 | EXPECT_TRUE(ExtensionWebUI::HandleChromeURLOverrideReverse(&changed_url, |
| 162 | profile_.get())); |
| 163 | EXPECT_EQ(kBookmarksUrl, changed_url); |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 164 | } |
| 165 | |
Devlin Cronin | 313da281 | 2017-11-10 17:40:40 | [diff] [blame] | 166 | TEST_F(ExtensionWebUITest, TestRemovingDuplicateEntriesForHosts) { |
| 167 | // Test that duplicate entries for a single extension are removed. This could |
| 168 | // happen because of https://crbug.com/782959. |
Devlin Cronin | 313da281 | 2017-11-10 17:40:40 | [diff] [blame] | 169 | scoped_refptr<const Extension> extension = |
| 170 | ExtensionBuilder("extension") |
Devlin Cronin | 98cd658 | 2018-05-08 19:18:12 | [diff] [blame] | 171 | .SetManifestPath({"chrome_url_overrides", "newtab"}, "newtab.html") |
Devlin Cronin | 313da281 | 2017-11-10 17:40:40 | [diff] [blame] | 172 | .Build(); |
| 173 | |
| 174 | const GURL newtab_url = extension->GetResourceURL("newtab.html"); |
| 175 | |
| 176 | PrefService* prefs = profile_->GetPrefs(); |
| 177 | { |
| 178 | // Add multiple entries for the same extension. |
| 179 | DictionaryPrefUpdate update(prefs, ExtensionWebUI::kExtensionURLOverrides); |
| 180 | base::DictionaryValue* all_overrides = update.Get(); |
| 181 | base::Value newtab_list(base::Value::Type::LIST); |
| 182 | { |
| 183 | base::Value newtab(base::Value::Type::DICTIONARY); |
| 184 | newtab.SetKey("entry", base::Value(newtab_url.spec())); |
| 185 | newtab.SetKey("active", base::Value(true)); |
Jan Wilken Dörrie | 91e4ef0 | 2019-09-11 08:22:12 | [diff] [blame] | 186 | newtab_list.Append(std::move(newtab)); |
Devlin Cronin | 313da281 | 2017-11-10 17:40:40 | [diff] [blame] | 187 | } |
| 188 | { |
| 189 | base::Value newtab(base::Value::Type::DICTIONARY); |
| 190 | newtab.SetKey( |
| 191 | "entry", |
| 192 | base::Value(extension->GetResourceURL("oldtab.html").spec())); |
| 193 | newtab.SetKey("active", base::Value(true)); |
Jan Wilken Dörrie | 91e4ef0 | 2019-09-11 08:22:12 | [diff] [blame] | 194 | newtab_list.Append(std::move(newtab)); |
Devlin Cronin | 313da281 | 2017-11-10 17:40:40 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | all_overrides->SetKey("newtab", std::move(newtab_list)); |
| 198 | } |
| 199 | |
| 200 | extension_service_->AddExtension(extension.get()); |
| 201 | static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_.get())) |
| 202 | ->SetReady(); |
| 203 | base::RunLoop().RunUntilIdle(); |
| 204 | |
| 205 | // Duplicates should be removed (in response to ExtensionSystem::ready()). |
| 206 | // Only a single entry should remain. |
| 207 | const base::DictionaryValue* overrides = |
| 208 | prefs->GetDictionary(ExtensionWebUI::kExtensionURLOverrides); |
| 209 | ASSERT_TRUE(overrides); |
| 210 | const base::Value* newtab_overrides = |
| 211 | overrides->FindKeyOfType("newtab", base::Value::Type::LIST); |
| 212 | ASSERT_TRUE(newtab_overrides); |
| 213 | ASSERT_EQ(1u, newtab_overrides->GetList().size()); |
| 214 | const base::Value& override_dict = newtab_overrides->GetList()[0]; |
| 215 | EXPECT_EQ(newtab_url.spec(), override_dict.FindKey("entry")->GetString()); |
| 216 | EXPECT_TRUE(override_dict.FindKey("active")->GetBool()); |
| 217 | } |
| 218 | |
Benjamin Ackerman | 001a7f8 | 2018-09-10 14:53:30 | [diff] [blame] | 219 | TEST_F(ExtensionWebUITest, TestFaviconAlwaysAvailable) { |
| 220 | scoped_refptr<const Extension> extension = |
| 221 | ExtensionBuilder("extension").Build(); |
| 222 | extension_service_->AddExtension(extension.get()); |
| 223 | static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_.get())) |
| 224 | ->SetReady(); |
| 225 | |
| 226 | const GURL kExtensionManifestURL = extension->GetResourceURL("manifest.json"); |
| 227 | |
| 228 | std::vector<favicon_base::FaviconRawBitmapResult> favicon_results; |
| 229 | auto set_favicon_results = |
| 230 | [](std::vector<favicon_base::FaviconRawBitmapResult>* favicons_out, |
| 231 | base::RepeatingClosure quit_closure, |
| 232 | const std::vector<favicon_base::FaviconRawBitmapResult>& favicons) { |
| 233 | *favicons_out = favicons; |
| 234 | std::move(quit_closure).Run(); |
| 235 | }; |
| 236 | |
| 237 | base::RunLoop run_loop; |
| 238 | ExtensionWebUI::GetFaviconForURL( |
| 239 | profile_.get(), kExtensionManifestURL, |
Jan Wilken Dörrie | 1a7df948 | 2020-04-01 17:34:10 | [diff] [blame] | 240 | base::BindOnce(set_favicon_results, &favicon_results, |
| 241 | run_loop.QuitClosure())); |
Benjamin Ackerman | 001a7f8 | 2018-09-10 14:53:30 | [diff] [blame] | 242 | |
| 243 | run_loop.Run(); |
| 244 | EXPECT_FALSE(favicon_results.empty()); |
| 245 | |
| 246 | // Verify that the favicon bitmaps are not empty and are valid. |
| 247 | for (const auto& favicon : favicon_results) { |
| 248 | EXPECT_TRUE(favicon.is_valid()); |
| 249 | |
| 250 | SkBitmap bitmap; |
| 251 | bool result = |
| 252 | gfx::PNGCodec::Decode(favicon.bitmap_data.get()->front(), |
| 253 | favicon.bitmap_data.get()->size(), &bitmap); |
| 254 | EXPECT_TRUE(result); |
| 255 | EXPECT_FALSE(bitmap.isNull()); |
| 256 | EXPECT_FALSE(bitmap.drawsNothing()); |
| 257 | } |
| 258 | } |
| 259 | |
[email protected] | a7789f2 | 2014-03-12 16:10:38 | [diff] [blame] | 260 | } // namespace extensions |