File: extension_frame_host_browsertest.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 (205 lines) | stat: -rw-r--r-- 7,784 bytes parent folder | download | duplicates (9)
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
// Copyright 2021 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/extension_frame_host.h"

#include <string>

#include "base/memory/ref_counted.h"
#include "content/public/browser/web_contents_user_data.h"
#include "extensions/browser/browsertest_util.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/shell/browser/shell_extension_host_delegate.h"
#include "extensions/shell/browser/shell_extension_web_contents_observer.h"
#include "extensions/shell/browser/shell_extensions_browser_client.h"
#include "extensions/shell/test/shell_apitest.h"
#include "extensions/test/result_catcher.h"
#include "testing/gtest/include/gtest/gtest-spi.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

namespace {

class TestExtensionFrameHost : public ExtensionFrameHost {
 public:
  explicit TestExtensionFrameHost(content::WebContents* web_contents)
      : ExtensionFrameHost(web_contents) {}
  TestExtensionFrameHost(const TestExtensionFrameHost&) = delete;
  TestExtensionFrameHost& operator=(const TestExtensionFrameHost&) = delete;
  ~TestExtensionFrameHost() override = default;

  void SetInvalidRequest(const std::string& name) { invalid_request_ = name; }

 private:
  // mojom::LocalFrameHost:
  void Request(mojom::RequestParamsPtr params,
               RequestCallback callback) override {
    // If the name of |params| is set to an invalid request, it sets it to
    // an empty string so that the request causes an error.
    if (invalid_request_ == params->name) {
      params->name = std::string();
    }
    ExtensionFrameHost::Request(std::move(params), std::move(callback));
  }

  std::string invalid_request_;
};

class TestShellExtensionWebContentsObserver
    : public ExtensionWebContentsObserver,
      public content::WebContentsUserData<
          TestShellExtensionWebContentsObserver> {
 public:
  TestShellExtensionWebContentsObserver(
      const TestShellExtensionWebContentsObserver&) = delete;
  TestShellExtensionWebContentsObserver& operator=(
      const TestShellExtensionWebContentsObserver&) = delete;
  ~TestShellExtensionWebContentsObserver() override = default;

  // Creates and initializes an instance of this class for the given
  // |web_contents|, if it doesn't already exist.
  static void CreateForWebContents(content::WebContents* web_contents) {
    content::WebContentsUserData<TestShellExtensionWebContentsObserver>::
        CreateForWebContents(web_contents);
    // Initialize this instance if necessary.
    FromWebContents(web_contents)->Initialize();
  }

  // Overrides to create TestExtensionFrameHost.
  std::unique_ptr<ExtensionFrameHost> CreateExtensionFrameHost(
      content::WebContents* web_contents) override {
    return std::make_unique<TestExtensionFrameHost>(web_contents);
  }

 private:
  friend class content::WebContentsUserData<
      TestShellExtensionWebContentsObserver>;

  explicit TestShellExtensionWebContentsObserver(
      content::WebContents* web_contents)
      : ExtensionWebContentsObserver(web_contents),
        content::WebContentsUserData<TestShellExtensionWebContentsObserver>(
            *web_contents) {}

  WEB_CONTENTS_USER_DATA_KEY_DECL();
};

WEB_CONTENTS_USER_DATA_KEY_IMPL(TestShellExtensionWebContentsObserver);

class TestShellExtensionHostDelegate : public ShellExtensionHostDelegate {
 public:
  TestShellExtensionHostDelegate() = default;
  TestShellExtensionHostDelegate(const TestShellExtensionHostDelegate&) =
      delete;
  TestShellExtensionHostDelegate& operator=(
      const TestShellExtensionHostDelegate&) = delete;
  ~TestShellExtensionHostDelegate() override = default;

  // Overrides to create TestShellExtensionWebContentsObserver.
  void OnExtensionHostCreated(content::WebContents* web_contents) override {
    TestShellExtensionWebContentsObserver::CreateForWebContents(web_contents);
  }
};

class ExtensionFrameHostTestExtensionsBrowserClient
    : public ShellExtensionsBrowserClient {
 public:
  ExtensionFrameHostTestExtensionsBrowserClient() = default;
  ExtensionFrameHostTestExtensionsBrowserClient(
      const ExtensionFrameHostTestExtensionsBrowserClient&) = delete;
  ExtensionFrameHostTestExtensionsBrowserClient& operator=(
      const ExtensionFrameHostTestExtensionsBrowserClient&) = delete;
  ~ExtensionFrameHostTestExtensionsBrowserClient() override = default;

  // Overrides to create TestShellExtensionHostDelegate.
  std::unique_ptr<ExtensionHostDelegate> CreateExtensionHostDelegate()
      override {
    return std::make_unique<TestShellExtensionHostDelegate>();
  }

  // Overrides to return TestShellExtensionWebContentsObserver.
  ExtensionWebContentsObserver* GetExtensionWebContentsObserver(
      content::WebContents* web_contents) override {
    return TestShellExtensionWebContentsObserver::FromWebContents(web_contents);
  }
};

}  // namespace

class ExtensionFrameHostBrowserTest : public ShellApiTest {
 public:
  ExtensionFrameHostBrowserTest() = default;
  ExtensionFrameHostBrowserTest(const ExtensionFrameHostBrowserTest&) = delete;
  ExtensionFrameHostBrowserTest& operator=(
      const ExtensionFrameHostBrowserTest&) = delete;
  ~ExtensionFrameHostBrowserTest() override = default;

  void SetUpOnMainThread() override {
    ShellApiTest::SetUpOnMainThread();

    extensions_browser_client_ =
        std::make_unique<ExtensionFrameHostTestExtensionsBrowserClient>();
    extensions_browser_client_->InitWithBrowserContext(
        browser_context(),
        ExtensionPrefs::Get(browser_context())->pref_service());
    ExtensionsBrowserClient::Set(extensions_browser_client_.get());

    extension_ = LoadExtension("extension");
    ASSERT_TRUE(extension_.get());
    ResultCatcher catcher;
    ASSERT_TRUE(catcher.GetNextResult());
  }

 protected:
  const Extension* extension() const { return extension_.get(); }

  void SetInvalidNameOnRequest(const std::string& method_name) {
    ExtensionHost* host =
        ProcessManager::Get(browser_context())
            ->GetBackgroundHostForExtension(extension()->id());
    ASSERT_TRUE(host);
    ASSERT_TRUE(host->host_contents());
    ExtensionWebContentsObserver* observer =
        extensions_browser_client_->GetExtensionWebContentsObserver(
            host->host_contents());
    static_cast<TestExtensionFrameHost*>(
        observer->extension_frame_host_for_testing())
        ->SetInvalidRequest(method_name);
  }

 private:
  scoped_refptr<const Extension> extension_;
  std::unique_ptr<ExtensionFrameHostTestExtensionsBrowserClient>
      extensions_browser_client_;
};

// Test that when ExtensionFrameHost dispatches an invalid request it gets
// an error associated with it. This is a regression test for
// https://crbug.com/1196377.
IN_PROC_BROWSER_TEST_F(ExtensionFrameHostBrowserTest, InValidNameRequest) {
  // Set 'test.getConfig' is invalid request.
  SetInvalidNameOnRequest("test.getConfig");
  // Run a script asynchronously that passes the test.
  ResultCatcher catcher;
  ASSERT_TRUE(browsertest_util::ExecuteScriptInBackgroundPageNoWait(
      browser_context(), extension()->id(), R"(
        chrome.test.getConfig(() => {
          const expectedError = 'Access to extension API denied.';
          if (chrome.runtime.lastError &&
            expectedError == chrome.runtime.lastError.message) {
            chrome.test.notifyPass();
          } else {
            chrome.test.notifyFail('TestFailed');
          }
        });)"));

  ASSERT_TRUE(catcher.GetNextResult());
}

}  // namespace extensions