File: webstore_installer_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (301 lines) | stat: -rw-r--r-- 11,672 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
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/webstore_installer.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "chrome/browser/extensions/install_approval.h"
#include "chrome/browser/extensions/permissions/scripting_permissions_modifier.h"
#include "chrome/browser/extensions/webstore_installer_test.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/test/browser_test.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/permissions_manager.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/extension_features.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/extension_service.h"
#include "extensions/browser/extension_system.h"
#endif

namespace extensions {

namespace {

const char kWebstoreDomain[] = "cws.com";
const char kAppDomain[] = "app.com";
const char kNonAppDomain[] = "nonapp.com";
const char kTestExtensionId[] = "ecglahbcnmdpdciemllbhojghbkagdje";
const char kTestExtensionWithPermissionsId[] =
    "lpbboafeefjeccjhdhcfdibnjcecpmhd";
const char kTestDataPath[] = "extensions/api_test/webstore_inline_install";
const char kCrxFilename[] = "extension.crx";
const char kCrxWithPermissionsFilename[] =
    "extension_with_host_permissions.crx";

}  // namespace

// Test version of WebstoreInstaller that intercepts the destructor.
class TestWebstoreInstaller : public WebstoreInstaller {
 public:
  TestWebstoreInstaller(Profile* profile,
                        SuccessCallback success_callback,
                        FailureCallback failure_callback,
                        content::WebContents* web_contents,
                        const std::string& id,
                        std::unique_ptr<InstallApproval> approval,
                        InstallSource source)
      : WebstoreInstaller(profile,
                          std::move(success_callback),
                          std::move(failure_callback),
                          web_contents,
                          id,
                          std::move(approval),
                          source) {}

  void SetDeletedClosure(base::OnceClosure cb) {
    deleted_closure_ = std::move(cb);
  }

 private:
  ~TestWebstoreInstaller() override {
    if (!deleted_closure_.is_null())
      std::move(deleted_closure_).Run();
  }

  base::OnceClosure deleted_closure_;
};

class WebstoreInstallerBrowserTest : public WebstoreInstallerTest {
 public:
  WebstoreInstallerBrowserTest(const std::string& webstore_domain,
                               const std::string& test_data_path,
                               const std::string& crx_filename,
                               const std::string& verified_domain,
                               const std::string& unverified_domain)
      : WebstoreInstallerTest(webstore_domain,
                              test_data_path,
                              crx_filename,
                              verified_domain,
                              unverified_domain) {}
  ~WebstoreInstallerBrowserTest() override = default;

  void SetDoneClosure(base::OnceClosure done_closure) {
    done_closure_ = std::move(done_closure);
  }

  bool success() const { return success_; }

  void OnExtensionInstallSuccess(const std::string& id) {
    success_ = true;
    std::move(done_closure_).Run();
  }

  void OnExtensionInstallFailure(const std::string& id,
                                 const std::string& error,
                                 WebstoreInstaller::FailureReason reason) {
    success_ = false;
    std::move(done_closure_).Run();
  }

 private:
  base::OnceClosure done_closure_;
  bool success_;
};

class WebstoreInstallerMV2BrowserTest : public WebstoreInstallerBrowserTest {
 public:
  WebstoreInstallerMV2BrowserTest()
      : WebstoreInstallerBrowserTest(kWebstoreDomain,
                                     kTestDataPath,
                                     kCrxFilename,
                                     kAppDomain,
                                     kNonAppDomain) {}
  ~WebstoreInstallerMV2BrowserTest() override = default;

  // The manifest used by the test installer must match `kCrxFilename` manifest
  // in the test directory.
  base::Value::Dict GetManifest() {
    return base::Value::Dict()
        .Set("name", "Installer Extension")
        .Set("manifest_version", 2)
        .Set("version", "1.0")
        .Set("permissions", base::Value::List().Append("tabs"));
  }
};

IN_PROC_BROWSER_TEST_F(WebstoreInstallerMV2BrowserTest, WebstoreInstall) {
  content::WebContents* active_web_contents = GetActiveWebContents();
  ASSERT_TRUE(active_web_contents);

  // Create an approval.
  std::unique_ptr<InstallApproval> approval =
      InstallApproval::CreateWithNoInstallPrompt(profile(), kTestExtensionId,
                                                 GetManifest(), false);

  // Create and run a WebstoreInstaller.
  base::RunLoop run_loop;
  SetDoneClosure(run_loop.QuitClosure());
  TestWebstoreInstaller* installer = new TestWebstoreInstaller(
      profile(),
      base::BindOnce(&WebstoreInstallerBrowserTest::OnExtensionInstallSuccess,
                     base::Unretained(this)),
      base::BindOnce(&WebstoreInstallerBrowserTest::OnExtensionInstallFailure,
                     base::Unretained(this)),
      active_web_contents, kTestExtensionId, std::move(approval),
      WebstoreInstaller::INSTALL_SOURCE_OTHER);
  installer->Start();
  run_loop.Run();

  EXPECT_TRUE(success());
  ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
  ASSERT_TRUE(registry->enabled_extensions().GetByID(kTestExtensionId));
}

// TODO(crbug.com/398299722): Port to desktop Android when
// ExtensionService::OnExtensionInstalled() moves out of ExtensionService.
#if BUILDFLAG(ENABLE_EXTENSIONS)
IN_PROC_BROWSER_TEST_F(WebstoreInstallerMV2BrowserTest, SimultaneousInstall) {
  base::Value::Dict manifest = GetManifest();

  content::WebContents* active_web_contents = GetActiveWebContents();
  ASSERT_TRUE(active_web_contents);

  // Create an approval.
  std::unique_ptr<InstallApproval> approval =
      InstallApproval::CreateWithNoInstallPrompt(profile(), kTestExtensionId,
                                                 manifest.Clone(), false);

  // Create and run a WebstoreInstaller.
  base::RunLoop run_loop;
  SetDoneClosure(run_loop.QuitClosure());
  scoped_refptr<TestWebstoreInstaller> installer = new TestWebstoreInstaller(
      profile(),
      base::BindOnce(&WebstoreInstallerBrowserTest::OnExtensionInstallSuccess,
                     base::Unretained(this)),
      base::BindOnce(&WebstoreInstallerBrowserTest::OnExtensionInstallFailure,
                     base::Unretained(this)),
      active_web_contents, kTestExtensionId, std::move(approval),
      WebstoreInstaller::INSTALL_SOURCE_OTHER);
  installer->Start();

  // Simulate another mechanism installing the same extension.
  scoped_refptr<const Extension> extension =
      ExtensionBuilder()
          .SetLocation(mojom::ManifestLocation::kInternal)
          .SetID(kTestExtensionId)
          .SetManifest(std::move(manifest))
          .Build();
  ExtensionRegistrar::Get(profile())->OnExtensionInstalled(
      extension.get(), syncer::StringOrdinal(), 0);

  run_loop.Run();

  // Wait for the WebstoreInstaller to be destroyed. Bad things happen if we
  // don't wait for this.
  base::RunLoop run_loop2;
  installer->SetDeletedClosure(run_loop2.QuitClosure());
  installer = nullptr;
  run_loop2.Run();

  EXPECT_TRUE(success());
  ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
  // Extension ends up as disabled because of permissions.
  ASSERT_TRUE(registry->disabled_extensions().GetByID(kTestExtensionId));
}
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

class WebstoreInstallerWithWithholdingUIBrowserTest
    : public WebstoreInstallerBrowserTest,
      public testing::WithParamInterface<bool> {
 public:
  WebstoreInstallerWithWithholdingUIBrowserTest()
      : WebstoreInstallerBrowserTest(kWebstoreDomain,
                                     kTestDataPath,
                                     kCrxWithPermissionsFilename,
                                     kAppDomain,
                                     kNonAppDomain) {
    feature_list_.InitAndEnableFeature(
        extensions_features::kAllowWithholdingExtensionPermissionsOnInstall);
  }
  ~WebstoreInstallerWithWithholdingUIBrowserTest() override = default;

  // Th manifest used by the test installer must match
  // `kCrxWithPermissionsFilename` manifest in the test directory.
  base::Value::Dict GetManifest() {
    return base::Value::Dict()
        .Set("name", "Installer Extension")
        .Set("manifest_version", 3)
        .Set("version", "1.0")
        .Set("host_permissions", base::Value::List().Append("<all_urls>"));
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

// Tests host permissions are withheld at installation only when the checkbox is
// selected.
IN_PROC_BROWSER_TEST_P(WebstoreInstallerWithWithholdingUIBrowserTest,
                       WithholdingHostsOnInstall) {
  bool shoud_withhold_permissions = GetParam();

  content::WebContents* active_web_contents = GetActiveWebContents();
  ASSERT_TRUE(active_web_contents);

  // Create an approval that withholds permissions when the checkbox is not
  // selected.
  std::unique_ptr<InstallApproval> approval =
      InstallApproval::CreateWithNoInstallPrompt(
          profile(), kTestExtensionWithPermissionsId, GetManifest(), false);
  approval->withhold_permissions = shoud_withhold_permissions;

  // Create and run a WebstoreInstaller.
  base::RunLoop run_loop;
  SetDoneClosure(run_loop.QuitClosure());
  TestWebstoreInstaller* installer = new TestWebstoreInstaller(
      profile(),
      base::BindOnce(&WebstoreInstallerBrowserTest::OnExtensionInstallSuccess,
                     base::Unretained(this)),
      base::BindOnce(&WebstoreInstallerBrowserTest::OnExtensionInstallFailure,
                     base::Unretained(this)),
      active_web_contents, kTestExtensionWithPermissionsId, std::move(approval),
      WebstoreInstaller::INSTALL_SOURCE_OTHER);
  installer->Start();
  run_loop.Run();

  // Verify extension was installed.
  EXPECT_TRUE(success());
  ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
  const Extension* extension =
      registry->enabled_extensions().GetByID(kTestExtensionWithPermissionsId);
  ASSERT_TRUE(extension);

  // Host permissions should be withheld only when the params indicate so.
  PermissionsManager* permissions_manager = PermissionsManager::Get(profile());
  EXPECT_EQ(permissions_manager->HasWithheldHostPermissions(*extension),
            shoud_withhold_permissions);

  // Access to google.com should be withheld only when the params indicate so.
  const PermissionsManager::ExtensionSiteAccess site_access =
      permissions_manager->GetSiteAccess(*extension,
                                         GURL("https://www.google.com"));
  EXPECT_EQ(site_access.withheld_site_access, shoud_withhold_permissions);
  EXPECT_EQ(site_access.has_site_access, !shoud_withhold_permissions);
}

INSTANTIATE_TEST_SUITE_P(All,
                         WebstoreInstallerWithWithholdingUIBrowserTest,
                         testing::Bool());

}  // namespace extensions