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 338 339 340 341 342 343
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <memory>
#include <utility>
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_web_ui.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/chrome_manifest_url_handlers.h"
#include "chrome/common/url_constants.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/extension_creator.h"
#include "extensions/common/constants.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
using content::WebContents;
namespace extensions {
class ExtensionOverrideTest : public ExtensionApiTest {
protected:
void SetUpOnMainThread() override {
ExtensionApiTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
}
bool CheckHistoryOverridesContainsNoDupes() {
// There should be no duplicate entries in the preferences.
const base::Value::Dict& overrides =
profile()->GetPrefs()->GetDict(ExtensionWebUI::kExtensionURLOverrides);
const base::Value::List* values = overrides.FindList("history");
if (!values)
return false;
std::set<std::string> seen_overrides;
for (const auto& val : *values) {
if (!val.is_dict()) {
return false;
}
const base::Value::Dict& dict = val.GetDict();
const std::string* entry = dict.FindString("entry");
if (!entry || seen_overrides.count(*entry) != 0)
return false;
seen_overrides.insert(*entry);
}
return true;
}
// Returns AssertionSuccess() if the given |web_contents| is being actively
// controlled by the extension with |extension_id|.
testing::AssertionResult ExtensionControlsPage(
content::WebContents* web_contents,
const std::string& extension_id) {
if (!web_contents->GetController().GetLastCommittedEntry())
return testing::AssertionFailure() << "No last committed entry.";
// We can't just use WebContents::GetLastCommittedURL() here because
// trickiness makes it think that it committed chrome://newtab.
GURL gurl = web_contents->GetController().GetLastCommittedEntry()->GetURL();
if (!gurl.SchemeIs(kExtensionScheme))
return testing::AssertionFailure() << gurl;
if (gurl.host_piece() != extension_id)
return testing::AssertionFailure() << gurl;
return testing::AssertionSuccess();
}
base::FilePath data_dir() {
return test_data_dir_.AppendASCII("override");
}
};
// Basic test for overriding the NTP.
IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, OverrideNewTab) {
const Extension* extension = LoadExtension(data_dir().AppendASCII("newtab"));
{
// Navigate to the new tab page. The overridden new tab page
// will call chrome.test.sendMessage('controlled by first').
ExtensionTestMessageListener listener;
ASSERT_TRUE(NavigateToURL(GURL("chrome://newtab/")));
EXPECT_TRUE(ExtensionControlsPage(GetActiveWebContents(), extension->id()));
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_EQ("controlled by first", listener.message());
}
}
// Check having multiple extensions with the same override.
IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, OverrideNewTabMultiple) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
base::FilePath temp_path = temp_dir.GetPath();
base::FilePath extension1_path = temp_path.AppendASCII("extension1.crx");
base::FilePath extension1_pem_path = temp_path.AppendASCII("extension1.pem");
base::FilePath extension1_unpacked_path = data_dir().AppendASCII("newtab");
ASSERT_TRUE(ExtensionCreator().Run(extension1_unpacked_path, extension1_path,
base::FilePath(), extension1_pem_path, 0));
base::FilePath extension2_path = temp_path.AppendASCII("extension2.crx");
base::FilePath extension2_pem_path = temp_path.AppendASCII("extension2.pem");
base::FilePath extension2_unpacked_path = data_dir().AppendASCII("newtab2");
ASSERT_TRUE(ExtensionCreator().Run(extension2_unpacked_path, extension2_path,
base::FilePath(), extension2_pem_path, 0));
// Prefer IDs because loading/unloading invalidates the extension ptrs.
const std::string extension1_id =
InstallExtensionWithPermissionsGranted(extension1_path, 1)->id();
const std::string extension2_id =
InstallExtensionWithPermissionsGranted(extension2_path, 1)->id();
{
// Navigate to the new tab page. Last extension installed wins, so
// the new tab page should be controlled by the second extension.
ExtensionTestMessageListener listener;
ASSERT_TRUE(NavigateToURL(GURL("chrome://newtab/")));
EXPECT_TRUE(ExtensionControlsPage(GetActiveWebContents(), extension2_id));
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_EQ("controlled by second", listener.message());
}
// Unload and reload the first extension. This should *not* result in the
// first extension moving to the front of the line.
ReloadExtension(extension1_id);
{
// The page should still be controlled by the second extension.
ExtensionTestMessageListener listener;
ASSERT_TRUE(NavigateToURL(GURL("chrome://newtab/")));
EXPECT_TRUE(ExtensionControlsPage(GetActiveWebContents(), extension2_id));
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_EQ("controlled by second", listener.message());
}
{
// Upgrade the first extension to a version that uses a different NTP url.
// This should *not* result in the first extension moving to the front of
// the line.
base::FilePath update_path = temp_path.AppendASCII("extension1_update.crx");
base::FilePath update_pem_path = extension1_pem_path;
base::FilePath update_unpacked_path =
data_dir().AppendASCII("newtab_upgrade");
ASSERT_TRUE(ExtensionCreator().Run(update_unpacked_path, update_path,
update_pem_path, base::FilePath(), 0));
const Extension* updated_extension =
UpdateExtension(extension1_id, update_path, 0);
ASSERT_TRUE(updated_extension);
EXPECT_EQ(extension1_id, updated_extension->id());
}
{
// The page should still be controlled by the second extension.
ExtensionTestMessageListener listener;
ASSERT_TRUE(NavigateToURL(GURL("chrome://newtab/")));
EXPECT_TRUE(ExtensionControlsPage(GetActiveWebContents(), extension2_id));
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_EQ("controlled by second", listener.message());
}
// Unload the (controlling) second extension. Now, and only now, should
// extension1 take over.
UnloadExtension(extension2_id);
{
ExtensionTestMessageListener listener;
ASSERT_TRUE(NavigateToURL(GURL("chrome://newtab/")));
EXPECT_TRUE(ExtensionControlsPage(GetActiveWebContents(), extension1_id));
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_EQ("controlled by first upgrade", listener.message());
}
}
// Test that unloading an extension overriding the page reloads the page with
// the proper url.
IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest,
OverridingExtensionUnloadedWithPageOpen) {
// Prefer IDs because loading/unloading invalidates the extension ptrs.
const std::string extension1_id =
LoadExtension(data_dir().AppendASCII("newtab"))->id();
const std::string extension2_id =
LoadExtension(data_dir().AppendASCII("newtab2"))->id();
{
// Navigate to the new tab page. Last extension installed wins, so
// the new tab page should be controlled by the second extension.
ExtensionTestMessageListener listener;
ASSERT_TRUE(NavigateToURL(GURL("chrome://newtab/")));
EXPECT_TRUE(ExtensionControlsPage(GetActiveWebContents(), extension2_id));
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_EQ("controlled by second", listener.message());
}
{
// Unload the controlling extension. The page should be automatically
// reloaded with the new controlling extension.
ExtensionTestMessageListener listener;
UnloadExtension(extension2_id);
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_EQ("controlled by first", listener.message());
EXPECT_TRUE(ExtensionControlsPage(GetActiveWebContents(), extension1_id));
}
UnloadExtension(extension1_id);
content::WebContents* active_tab = GetActiveWebContents();
EXPECT_TRUE(content::WaitForLoadStop(active_tab));
EXPECT_FALSE(ExtensionControlsPage(active_tab, extension1_id));
}
IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, OverrideNewTabIncognito) {
LoadExtension(data_dir().AppendASCII("newtab"));
// Navigate an incognito tab to the new tab page. We should get the actual
// new tab page because we can't load chrome-extension URLs in incognito.
WebContents* tab =
PlatformOpenURLOffTheRecord(profile(), GURL("chrome://newtab/"));
ASSERT_TRUE(tab->GetController().GetVisibleEntry());
EXPECT_FALSE(tab->GetController().GetVisibleEntry()->GetURL().
SchemeIs(kExtensionScheme));
}
// Check that when an overridden new tab page has focus, a subframe navigation
// on that page does not steal the focus away by focusing the omnibox.
// See https://crbug.com/700124.
// TODO(crbug.com/40804036): Flaky on Linux.
#if BUILDFLAG(IS_LINUX)
#define MAYBE_SubframeNavigationInOverridenNTPDoesNotAffectFocus \
DISABLED_SubframeNavigationInOverridenNTPDoesNotAffectFocus
#else
#define MAYBE_SubframeNavigationInOverridenNTPDoesNotAffectFocus \
SubframeNavigationInOverridenNTPDoesNotAffectFocus
#endif // BUILDFLAG(IS_LINUX)
IN_PROC_BROWSER_TEST_F(
ExtensionOverrideTest,
MAYBE_SubframeNavigationInOverridenNTPDoesNotAffectFocus) {
// Load an extension that overrides the new tab page.
const Extension* extension = LoadExtension(data_dir().AppendASCII("newtab"));
// Navigate to the new tab page. The overridden new tab page
// will call chrome.test.sendMessage('controlled by first').
ExtensionTestMessageListener listener("controlled by first");
ASSERT_TRUE(NavigateToURL(GURL(chrome::kChromeUINewTabURL)));
WebContents* contents = GetActiveWebContents();
EXPECT_TRUE(ExtensionControlsPage(contents, extension->id()));
EXPECT_TRUE(listener.WaitUntilSatisfied());
// Start off with the main page focused.
contents->Focus();
EXPECT_TRUE(contents->GetRenderWidgetHostView()->HasFocus());
// Inject an iframe and navigate it to a cross-site URL. With
// --site-per-process, this will go into a separate process.
GURL cross_site_url(embedded_test_server()->GetURL("a.com", "/title1.html"));
std::string script = "var f = document.createElement('iframe');\n"
"f.src = '" + cross_site_url.spec() + "';\n"
"document.body.appendChild(f);\n";
EXPECT_TRUE(ExecJs(contents, script));
EXPECT_TRUE(WaitForLoadStop(contents));
// The page should still have focus. The cross-process subframe navigation
// above should not try to focus the omnibox, which would make this false.
EXPECT_TRUE(contents->GetRenderWidgetHostView()->HasFocus());
}
// Times out consistently on Win, http://crbug.com/45173.
#if BUILDFLAG(IS_WIN)
#define MAYBE_OverrideHistory DISABLED_OverrideHistory
#else
#define MAYBE_OverrideHistory OverrideHistory
#endif // BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, MAYBE_OverrideHistory) {
ASSERT_TRUE(RunExtensionTest("override/history")) << message_;
{
ResultCatcher catcher;
// Navigate to the history page. The overridden history page
// will call chrome.test.notifyPass() .
ASSERT_TRUE(NavigateToURL(GURL("chrome://history/")));
ASSERT_TRUE(catcher.GetNextResult());
}
}
// Regression test for http://crbug.com/41442.
IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, ShouldNotCreateDuplicateEntries) {
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("override/history"));
ASSERT_TRUE(extension);
// Simulate several LoadExtension() calls happening over the lifetime of
// a preferences file without corresponding UnloadExtension() calls.
for (size_t i = 0; i < 3; ++i) {
ExtensionWebUI::RegisterOrActivateChromeURLOverrides(
profile(), URLOverrides::GetChromeURLOverrides(extension));
}
ASSERT_TRUE(CheckHistoryOverridesContainsNoDupes());
}
// TODO(devlin): This test seems a bit contrived. How would we end up with
// duplicate entries created?
IN_PROC_BROWSER_TEST_F(ExtensionOverrideTest, ShouldCleanUpDuplicateEntries) {
// Simulate several LoadExtension() calls happening over the lifetime of
// a preferences file without corresponding UnloadExtension() calls. This is
// the same as the above test, except for that it is testing the case where
// the file already contains dupes when an extension is loaded.
base::Value::List list;
for (size_t i = 0; i < 3; ++i) {
base::Value::Dict dict;
dict.Set("entry", "http://www.google.com/");
dict.Set("active", true);
list.Append(std::move(dict));
}
{
ScopedDictPrefUpdate update(profile()->GetPrefs(),
ExtensionWebUI::kExtensionURLOverrides);
update->Set("history", std::move(list));
}
ASSERT_FALSE(CheckHistoryOverridesContainsNoDupes());
ExtensionWebUI::InitializeChromeURLOverrides(profile());
ASSERT_TRUE(CheckHistoryOverridesContainsNoDupes());
}
} // namespace extensions
|