File: extension_incognito_apitest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (261 lines) | stat: -rw-r--r-- 10,247 bytes parent folder | download | duplicates (3)
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
// 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 "base/test/run_until.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/extensions/extension_action_test_helper.h"
#include "chrome/common/url_constants.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/event_router.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_extension_dir.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/ui_test_utils.h"
#endif

using content::WebContents;
using extensions::ResultCatcher;

namespace extensions {

class IncognitoApiTest : public ExtensionApiTest {
 public:
  void SetUpOnMainThread() override {
    ExtensionApiTest::SetUpOnMainThread();
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(StartEmbeddedTestServer());
  }
};

IN_PROC_BROWSER_TEST_F(IncognitoApiTest, IncognitoNoScript) {
  // Loads a simple extension which attempts to change the title of every page
  // that loads to "modified".
  ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("incognito")
      .AppendASCII("content_scripts")));

  // Open incognito window and navigate to test page.
  GURL test_url = embedded_test_server()->GetURL("/extensions/test_file.html");
  WebContents* tab = PlatformOpenURLOffTheRecord(profile(), test_url);

  // Verify the script didn't run.
  EXPECT_EQ(true, content::EvalJs(tab, "document.title == 'Unmodified'"));
}

IN_PROC_BROWSER_TEST_F(IncognitoApiTest, IncognitoYesScript) {
  // Loads a simple extension which attempts to change the title of every page
  // that loads to "modified".
  ASSERT_TRUE(LoadExtension(
      test_data_dir_.AppendASCII("incognito").AppendASCII("content_scripts"),
      {.allow_in_incognito = true}));

  // Open incognito window and navigate to test page.
  GURL test_url = embedded_test_server()->GetURL("/extensions/test_file.html");
  WebContents* tab = PlatformOpenURLOffTheRecord(profile(), test_url);

  // Verify the script ran.
  EXPECT_EQ(true, content::EvalJs(tab, "document.title == 'modified'"));
}

IN_PROC_BROWSER_TEST_F(IncognitoApiTest, NoCrashWithMultipleExtensions) {
  // Load a dummy extension. This just tests that we don't regress a
  // crash fix when multiple incognito- and non-incognito-enabled extensions
  // are mixed.
  ASSERT_TRUE(LoadExtension(
      test_data_dir_.AppendASCII("content_scripts").AppendASCII("inject_div")));

  // Load an incognito extension.
  ASSERT_TRUE(LoadExtension(
      test_data_dir_.AppendASCII("incognito").AppendASCII("content_scripts"),
      {.allow_in_incognito = true}));

  // Dummy extension #2.
  ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("content_scripts")
                                .AppendASCII("css_injection")));

  // No crash.
}

// Tests that when listeners in the `incognito` service worker are not removed
// when the `regular` service worker stops.
IN_PROC_BROWSER_TEST_F(IncognitoApiTest, IncognitoSplitKeepListener) {
  constexpr char kEvent[] = "tabs.onCreated";

  // Prepare a test extension.
  TestExtensionDir test_dir;
  constexpr char kManifest[] =
      R"({
           "name": "Test Extension",
           "version": "0.1",
           "manifest_version": 3,
           "background": {
             "service_worker": "background.js"
           },
           "incognito": "split",
           "permissions": ["tabs"]
         })";
  test_dir.WriteManifest(kManifest);
  constexpr char kBackgroundJs[] =
      R"(
        chrome.tabs.onCreated.addListener(() => {});

        self.addEventListener('install', e => e.waitUntil(skipWaiting()));
        self.addEventListener('activate', e => {
          chrome.test.sendMessage(
              chrome.extension.inIncognitoContext ? "waiting_incognito"
                                                  : "waiting");
        });
      )";
  test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundJs);

  ExtensionTestMessageListener listener("waiting", ReplyBehavior::kWontReply);
  ExtensionTestMessageListener listener_incognito("waiting_incognito",
                                                  ReplyBehavior::kWontReply);

  PlatformOpenURLOffTheRecord(
      profile(), embedded_test_server()->GetURL("/extensions/test_file.html"));

  const Extension* extension = LoadExtension(
      test_dir.UnpackedPath(),
      {.allow_in_incognito = true, .wait_for_registration_stored = true});
  ASSERT_TRUE(extension);

  // Waits for both `regular` and `incognito` instances.
  EXPECT_TRUE(listener.WaitUntilSatisfied());
  EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());

  EventRouter* event_router = EventRouter::Get(profile());
  ASSERT_TRUE(base::test::RunUntil([&] {
    return event_router->ExtensionHasEventListener(extension->id(), kEvent);
  }));

  // Stops the `regular` service worker.
  browsertest_util::StopServiceWorkerForExtensionGlobalScope(profile(),
                                                             extension->id());
  // The `incognito` service worker should have active listeners.
  EXPECT_TRUE(event_router->HasNonLazyEventListenerForTesting(kEvent));
}

#if !BUILDFLAG(IS_ANDROID)
// Tests that an extension which is enabled for incognito mode doesn't
// accidentally create an incognito profile.
// TODO(https://crbug.com/390226690): Enable on Android when chrome.windows
// is supported.
IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DontCreateIncognitoProfile) {
  ASSERT_FALSE(browser()->profile()->HasPrimaryOTRProfile());
  ASSERT_TRUE(RunExtensionTest("incognito/dont_create_profile", {},
                               {.allow_in_incognito = true}))
      << message_;
  ASSERT_FALSE(browser()->profile()->HasPrimaryOTRProfile());
}

// TODO(https://crbug.com/390226690): Enable on Android when chrome.windows
// and chrome.tabs are supported.
IN_PROC_BROWSER_TEST_F(IncognitoApiTest, Incognito) {
  ResultCatcher catcher;

  // Open incognito window and navigate to test page.
  OpenURLOffTheRecord(
      browser()->profile(),
      embedded_test_server()->GetURL("/extensions/test_file.html"));

  ASSERT_TRUE(
      LoadExtension(test_data_dir_.AppendASCII("incognito").AppendASCII("apis"),
                    {.allow_in_incognito = true}));

  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}

// Tests that the APIs in an incognito-enabled split-mode extension work
// properly.
// TODO(https://crbug.com/390226690): Enable on Android when chrome.windows
// and chrome.tabs are supported.
IN_PROC_BROWSER_TEST_F(IncognitoApiTest, IncognitoSplitMode) {
  // We need 2 ResultCatchers because we'll be running the same test in both
  // regular and incognito mode.
  ResultCatcher catcher;
  catcher.RestrictToBrowserContext(browser()->profile());
  ResultCatcher catcher_incognito;
  catcher_incognito.RestrictToBrowserContext(
      browser()->profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true));

  ExtensionTestMessageListener listener("waiting", ReplyBehavior::kWillReply);
  ExtensionTestMessageListener listener_incognito("waiting_incognito",
                                                  ReplyBehavior::kWillReply);

  // Open incognito window and navigate to test page.
  OpenURLOffTheRecord(browser()->profile(), embedded_test_server()->GetURL(
                                                "/extensions/test_file.html"));

  ASSERT_TRUE(LoadExtension(
      test_data_dir_.AppendASCII("incognito").AppendASCII("split"),
      {.allow_in_incognito = true}));

  // Wait for both extensions to be ready before telling them to proceed.
  EXPECT_TRUE(listener.WaitUntilSatisfied());
  EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
  listener.Reply("go");
  listener_incognito.Reply("go");

  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
  EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
}

// Tests that the APIs in an incognito-disabled extension don't see incognito
// events or callbacks.
// TODO(https://crbug.com/390226690): Enable on Android when chrome.windows
// is supported.
IN_PROC_BROWSER_TEST_F(IncognitoApiTest, IncognitoDisabled) {
  ResultCatcher catcher;
  ExtensionTestMessageListener listener("createIncognitoTab",
                                        ReplyBehavior::kWillReply);

  // Open incognito window and navigate to test page.
  OpenURLOffTheRecord(browser()->profile(), embedded_test_server()->GetURL(
                                                "/extensions/test_file.html"));

  ASSERT_TRUE(LoadExtension(test_data_dir_
      .AppendASCII("incognito").AppendASCII("apis_disabled")));

  EXPECT_TRUE(listener.WaitUntilSatisfied());
  OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
  listener.Reply("created");

  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}

// Test that opening a popup from an incognito browser window works properly.
// http://crbug.com/180759.
IN_PROC_BROWSER_TEST_F(IncognitoApiTest, DISABLED_IncognitoPopup) {
  ResultCatcher catcher;

  const extensions::Extension* const extension = LoadExtension(
      test_data_dir_.AppendASCII("incognito").AppendASCII("popup"),
      {.allow_in_incognito = true});
  ASSERT_TRUE(extension);

  // Open incognito window and navigate to test page.
  Browser* incognito_browser = OpenURLOffTheRecord(
      browser()->profile(),
      embedded_test_server()->GetURL("/extensions/test_file.html"));

  // Simulate the incognito's browser action being clicked.
  ExtensionActionTestHelper::Create(incognito_browser)->Press(extension->id());

  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}
#endif

}  // namespace extensions