File: plugin_info_host_impl_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 (419 lines) | stat: -rw-r--r-- 16,481 bytes parent folder | download | duplicates (5)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright 2022 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/plugins/plugin_info_host_impl.h"

#include <stddef.h>

#include <memory>
#include <string>

#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/i18n/base_i18n_switches.h"
#include "base/i18n/rtl.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gmock_move_support.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "build/branding_buildflags.h"
#include "chrome/browser/chrome_browser_main_extra_parts_nacl_deprecation.h"
#include "chrome/browser/plugins/plugin_prefs.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_content_client.h"
#include "chrome/common/plugin.mojom.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/nacl/common/buildflags.h"
#include "content/public/browser/plugin_service.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/webplugininfo.h"
#include "content/public/test/browser_test.h"
#include "pdf/buildflags.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"
#include "url/gurl.h"
#include "url/origin.h"

#if BUILDFLAG(ENABLE_NACL)
#include "components/nacl/common/nacl_constants.h"
#include "ppapi/shared_impl/ppapi_permissions.h"
#endif  // BUILDFLAG(ENABLE_NACL)

#if BUILDFLAG(ENABLE_PDF)
#include "components/pdf/common/constants.h"
#endif  // BUILDFLAG(ENABLE_PDF)

namespace {

using ::chrome::mojom::PluginInfo;
using ::chrome::mojom::PluginInfoHost;
using ::chrome::mojom::PluginInfoPtr;
using ::chrome::mojom::PluginStatus;
using ::content::WebPluginInfo;
using ::content::WebPluginMimeType;
using ::testing::Contains;
using ::testing::ElementsAre;
using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::SizeIs;

}  // namespace

class PluginInfoHostImplTest : public InProcessBrowserTest {
 public:
  PluginInfoHostImplTest() { feature_list_.InitAndEnableFeature(kNaclAllow); }

  void SetUpOnMainThread() override {
    int active_render_process_id = browser()
                                       ->tab_strip_model()
                                       ->GetActiveWebContents()
                                       ->GetPrimaryMainFrame()
                                       ->GetProcess()
                                       ->GetDeprecatedID();

    plugin_info_host_impl_ = std::make_unique<PluginInfoHostImpl>(
        active_render_process_id, browser()->profile());
  }

  void TearDownOnMainThread() override { plugin_info_host_impl_.reset(); }

 protected:
  PluginInfoPtr GetPluginInfo(const GURL& url,
                              const url::Origin& origin,
                              const std::string& mime_type) {
    PluginInfoPtr plugin_info;

    base::MockCallback<PluginInfoHost::GetPluginInfoCallback> mock_callback;
    EXPECT_CALL(mock_callback, Run).WillOnce(MoveArg(&plugin_info));

    base::RunLoop run_loop;
    plugin_info_host_impl_->GetPluginInfo(
        url, origin, mime_type,
        mock_callback.Get().Then(run_loop.QuitClosure()));
    run_loop.Run();

    return plugin_info;
  }

  void SetAlwaysOpenPdfExternally() {
    PluginPrefs::GetForProfile(browser()->profile())
        ->SetAlwaysOpenPdfExternallyForTests(true);
  }

  std::unique_ptr<PluginInfoHostImpl> plugin_info_host_impl_;
  base::test::ScopedFeatureList feature_list_;
};

#if BUILDFLAG(ENABLE_PDF)
class PluginInfoHostImplBidiTestBase : public PluginInfoHostImplTest {
 public:
  virtual bool rtl() const { return false; }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    if (rtl()) {
      // Pass "--force-ui-direction=rtl" instead of setting an RTL locale, as
      // setting a locale requires extra code on GLib platforms. Setting the UI
      // direction has the same effect on the extension name, which is all
      // that's needed for these tests.
      command_line->AppendSwitchASCII(switches::kForceUIDirection,
                                      switches::kForceDirectionRTL);
    }
  }
};

// Variation that tests under left-to-right and right-to-left directions. The
// direction affects the PDF viewer extension, as the plugin name is derived
// from the extension name, and the extension name may be adjusted to include
// Unicode bidirectional control characters in RTL mode. These extra control
// characters can break string comparisons (see crbug.com/1404260).
class PluginInfoHostImplBidiTest : public PluginInfoHostImplBidiTestBase,
                                   public testing::WithParamInterface<bool> {
 public:
  bool rtl() const override { return GetParam(); }
};
#endif  // BUILDFLAG(ENABLE_PDF)

IN_PROC_BROWSER_TEST_F(PluginInfoHostImplTest, CoverAllPlugins) {
  // Note that "internal" plugins are the only type that can be registered with
  // `content::PluginService` now.
  std::vector<WebPluginInfo> plugins;
  content::PluginService::GetInstance()->GetInternalPlugins(&plugins);

  size_t expected_plugin_count = 0;

#if BUILDFLAG(ENABLE_NACL)
  EXPECT_THAT(plugins, Contains(Field(
                           "path", &WebPluginInfo::path,
                           base::FilePath(nacl::kInternalNaClPluginFileName))));
  expected_plugin_count += 1;
#endif  // BUILDFLAG_ENABLE_NACL)

#if BUILDFLAG(ENABLE_PDF)
  EXPECT_THAT(
      plugins,
      Contains(
          Field("path", &WebPluginInfo::path,
                base::FilePath(ChromeContentClient::kPDFExtensionPluginPath))));
  EXPECT_THAT(
      plugins,
      Contains(
          Field("path", &WebPluginInfo::path,
                base::FilePath(ChromeContentClient ::kPDFInternalPluginPath))));
  expected_plugin_count += 2;
#endif  // BUILDFLAG(ENABLE_PDF)

  EXPECT_THAT(plugins, SizeIs(expected_plugin_count));
}

IN_PROC_BROWSER_TEST_F(PluginInfoHostImplTest, GetPluginInfoForFlash) {
  PluginInfoPtr plugin_info = GetPluginInfo(GURL("fake.swf"), url::Origin(),
                                            "application/x-shockwave-flash");
  ASSERT_TRUE(plugin_info);

  EXPECT_EQ(PluginStatus::kNotFound, plugin_info->status);
}

IN_PROC_BROWSER_TEST_F(PluginInfoHostImplTest, GetPluginInfoForFutureSplash) {
  PluginInfoPtr plugin_info = GetPluginInfo(GURL("fake.spl"), url::Origin(),
                                            "application/futuresplash");
  ASSERT_TRUE(plugin_info);

  EXPECT_EQ(PluginStatus::kNotFound, plugin_info->status);
}

#if BUILDFLAG(ENABLE_NACL)
IN_PROC_BROWSER_TEST_F(PluginInfoHostImplTest, GetPluginInfoForNaCl) {
  const std::u16string kPluginName = base::UTF8ToUTF16(nacl::kNaClPluginName);
  const base::FilePath kPluginPath =
      base::FilePath(nacl::kInternalNaClPluginFileName);

  PluginInfoPtr plugin_info = GetPluginInfo(
      GURL("fake-resource"), url::Origin(), nacl::kNaClPluginMimeType);
  ASSERT_TRUE(plugin_info);

  EXPECT_EQ(PluginStatus::kPlayImportantContent, plugin_info->status);
  EXPECT_EQ(nacl::kNaClPluginMimeType, plugin_info->actual_mime_type);

  // Group ID and name synthesized by `PluginInfoHostImpl`.
  EXPECT_EQ(kPluginPath.BaseName().AsUTF8Unsafe(),
            plugin_info->group_identifier);
  EXPECT_EQ(kPluginName, plugin_info->group_name);

  // `WebPluginInfo` fields.
  EXPECT_EQ(kPluginName, plugin_info->plugin.name);
  EXPECT_EQ(kPluginPath, plugin_info->plugin.path);
  EXPECT_EQ(u"", plugin_info->plugin.version);
  EXPECT_EQ(u"", plugin_info->plugin.desc);
  EXPECT_EQ(WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS,
            plugin_info->plugin.type);
  EXPECT_EQ(ppapi::PERMISSION_PRIVATE | ppapi::PERMISSION_DEV,
            plugin_info->plugin.pepper_permissions);
  EXPECT_EQ(WebPluginInfo::kDefaultBackgroundColor,
            plugin_info->plugin.background_color);

  // Has both NaCl and Pnacl MIME types.
  ASSERT_THAT(plugin_info->plugin.mime_types, SizeIs(2));

  WebPluginMimeType nacl_mime_type = plugin_info->plugin.mime_types[0];
  EXPECT_EQ(nacl::kNaClPluginMimeType, nacl_mime_type.mime_type);
  EXPECT_THAT(nacl_mime_type.file_extensions,
              ElementsAre(nacl::kNaClPluginExtension));
  EXPECT_EQ(base::UTF8ToUTF16(nacl::kNaClPluginDescription),
            nacl_mime_type.description);

  // Depends on modules registered by `extensions::NaClModulesHandler`.
  EXPECT_THAT(nacl_mime_type.additional_params, IsEmpty());

  WebPluginMimeType pnacl_mime_type = plugin_info->plugin.mime_types[1];
  EXPECT_EQ(nacl::kPnaclPluginMimeType, pnacl_mime_type.mime_type);
  EXPECT_THAT(pnacl_mime_type.file_extensions,
              ElementsAre(nacl::kPnaclPluginExtension));
  EXPECT_EQ(base::UTF8ToUTF16(nacl::kPnaclPluginDescription),
            pnacl_mime_type.description);
  EXPECT_THAT(pnacl_mime_type.additional_params, IsEmpty());
}

IN_PROC_BROWSER_TEST_F(PluginInfoHostImplTest, GetPluginInfoForPnacl) {
  const std::u16string kPluginName = base::UTF8ToUTF16(nacl::kNaClPluginName);
  const base::FilePath kPluginPath =
      base::FilePath(nacl::kInternalNaClPluginFileName);

  PluginInfoPtr plugin_info = GetPluginInfo(
      GURL("fake-resource"), url::Origin(), nacl::kPnaclPluginMimeType);
  ASSERT_TRUE(plugin_info);

  EXPECT_EQ(PluginStatus::kPlayImportantContent, plugin_info->status);
  EXPECT_EQ(nacl::kPnaclPluginMimeType, plugin_info->actual_mime_type);

  // Group ID and name synthesized by `PluginInfoHostImpl`.
  EXPECT_EQ(kPluginPath.BaseName().AsUTF8Unsafe(),
            plugin_info->group_identifier);
  EXPECT_EQ(kPluginName, plugin_info->group_name);

  // `WebPluginInfo` fields.
  EXPECT_EQ(kPluginName, plugin_info->plugin.name);
  EXPECT_EQ(kPluginPath, plugin_info->plugin.path);
  EXPECT_EQ(u"", plugin_info->plugin.version);
  EXPECT_EQ(u"", plugin_info->plugin.desc);
  EXPECT_EQ(WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS,
            plugin_info->plugin.type);
  EXPECT_EQ(ppapi::PERMISSION_PRIVATE | ppapi::PERMISSION_DEV,
            plugin_info->plugin.pepper_permissions);
  EXPECT_EQ(WebPluginInfo::kDefaultBackgroundColor,
            plugin_info->plugin.background_color);

  // Has both NaCl and Pnacl MIME types.
  ASSERT_THAT(plugin_info->plugin.mime_types, SizeIs(2));

  WebPluginMimeType nacl_mime_type = plugin_info->plugin.mime_types[0];
  EXPECT_EQ(nacl::kNaClPluginMimeType, nacl_mime_type.mime_type);
  EXPECT_THAT(nacl_mime_type.file_extensions,
              ElementsAre(nacl::kNaClPluginExtension));
  EXPECT_EQ(base::UTF8ToUTF16(nacl::kNaClPluginDescription),
            nacl_mime_type.description);

  // Depends on modules registered by `extensions::NaClModulesHandler`.
  EXPECT_THAT(nacl_mime_type.additional_params, IsEmpty());

  WebPluginMimeType pnacl_mime_type = plugin_info->plugin.mime_types[1];
  EXPECT_EQ(nacl::kPnaclPluginMimeType, pnacl_mime_type.mime_type);
  EXPECT_THAT(pnacl_mime_type.file_extensions,
              ElementsAre(nacl::kPnaclPluginExtension));
  EXPECT_EQ(base::UTF8ToUTF16(nacl::kPnaclPluginDescription),
            pnacl_mime_type.description);

  EXPECT_THAT(pnacl_mime_type.additional_params, IsEmpty());
}
#endif  // BUILDFLAG(ENABLE_NACL)

#if BUILDFLAG(ENABLE_PDF)
IN_PROC_BROWSER_TEST_P(PluginInfoHostImplBidiTest,
                       GetPluginInfoForPdfViewerExtension) {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  const std::u16string kPluginName = u"Chrome PDF Viewer";
  const std::string kGroupId = "google-chrome-pdf";
#else
  const std::u16string kPluginName = u"Chromium PDF Viewer";
  const std::string kGroupId = "chromium-pdf";
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

  PluginInfoPtr plugin_info =
      GetPluginInfo(GURL("fake.pdf"), url::Origin(), pdf::kPDFMimeType);
  ASSERT_TRUE(plugin_info);

  EXPECT_EQ(PluginStatus::kAllowed, plugin_info->status);
  EXPECT_EQ(pdf::kPDFMimeType, plugin_info->actual_mime_type);

  // Group ID and name defined by `PluginInfoHostImpl`.
  EXPECT_EQ(kGroupId, plugin_info->group_identifier);
  EXPECT_EQ(kPluginName, plugin_info->group_name);

  // `WebPluginInfo` fields.
  std::u16string expected_plugin_name = kPluginName;
  if (rtl()) {
    // Extra characters are added by `extensions::Extension::LoadName()`.
    ASSERT_TRUE(
        base::i18n::AdjustStringForLocaleDirection(&expected_plugin_name));
  }
  EXPECT_EQ(expected_plugin_name, plugin_info->plugin.name);

  EXPECT_EQ(base::FilePath(ChromeContentClient::kPDFExtensionPluginPath),
            plugin_info->plugin.path);
  EXPECT_EQ(u"", plugin_info->plugin.version);
  EXPECT_EQ(u"", plugin_info->plugin.desc);
  EXPECT_EQ(WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN,
            plugin_info->plugin.type);
  EXPECT_EQ(0, plugin_info->plugin.pepper_permissions);

  // Background color hard-coded in `GetPdfBackgroundColor()`.
  EXPECT_EQ(SkColorSetRGB(40, 40, 40), plugin_info->plugin.background_color);

  // Has PDF MIME type.
  ASSERT_THAT(plugin_info->plugin.mime_types, SizeIs(1));

  WebPluginMimeType mime_type = plugin_info->plugin.mime_types[0];
  EXPECT_EQ(pdf::kPDFMimeType, mime_type.mime_type);
  EXPECT_THAT(mime_type.file_extensions, ElementsAre("pdf"));
  EXPECT_EQ(u"", mime_type.description);
  EXPECT_THAT(mime_type.additional_params, IsEmpty());
}

IN_PROC_BROWSER_TEST_P(PluginInfoHostImplBidiTest,
                       GetPluginInfoForPdfViewerExtensionWhenDisabled) {
  SetAlwaysOpenPdfExternally();

  PluginInfoPtr plugin_info =
      GetPluginInfo(GURL("fake.pdf"), url::Origin(), pdf::kPDFMimeType);
  ASSERT_TRUE(plugin_info);

  // PDF viewer extension is disabled by PDF content setting.
  EXPECT_EQ(PluginStatus::kDisabled, plugin_info->status);
  EXPECT_EQ(pdf::kPDFMimeType, plugin_info->actual_mime_type);
}

IN_PROC_BROWSER_TEST_F(PluginInfoHostImplTest,
                       GetPluginInfoForPdfInternalPlugin) {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  const std::u16string kPluginName = u"Chrome PDF Plugin";
  const std::string kGroupId = "google-chrome-pdf-plugin";
#else
  const std::u16string kPluginName = u"Chromium PDF Plugin";
  const std::string kGroupId = "chromium-pdf-plugin";
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

  PluginInfoPtr plugin_info = GetPluginInfo(GURL("fake.pdf"), url::Origin(),
                                            pdf::kInternalPluginMimeType);
  ASSERT_TRUE(plugin_info);

  EXPECT_EQ(PluginStatus::kAllowed, plugin_info->status);
  EXPECT_EQ(pdf::kInternalPluginMimeType, plugin_info->actual_mime_type);

  // Group ID and name defined by `PluginInfoHostImpl`.
  EXPECT_EQ(kGroupId, plugin_info->group_identifier);
  EXPECT_EQ(kPluginName, plugin_info->group_name);

  // `WebPluginInfo` fields.
  EXPECT_EQ(kPluginName, plugin_info->plugin.name);
  EXPECT_EQ(base::FilePath(ChromeContentClient::kPDFInternalPluginPath),
            plugin_info->plugin.path);
  EXPECT_EQ(u"", plugin_info->plugin.version);
  EXPECT_EQ(u"Portable Document Format", plugin_info->plugin.desc);
  EXPECT_EQ(WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS,
            plugin_info->plugin.type);
  EXPECT_EQ(0, plugin_info->plugin.pepper_permissions);
  EXPECT_EQ(WebPluginInfo::kDefaultBackgroundColor,
            plugin_info->plugin.background_color);

  // Has PDF MIME type.
  ASSERT_THAT(plugin_info->plugin.mime_types, SizeIs(1));

  WebPluginMimeType mime_type = plugin_info->plugin.mime_types[0];
  EXPECT_EQ(pdf::kInternalPluginMimeType, mime_type.mime_type);
  EXPECT_THAT(mime_type.file_extensions, ElementsAre("pdf"));
  EXPECT_EQ(u"Portable Document Format", mime_type.description);
  EXPECT_THAT(mime_type.additional_params, IsEmpty());
}

IN_PROC_BROWSER_TEST_F(PluginInfoHostImplTest,
                       GetPluginInfoForPdfInternalPluginWhenDisabled) {
  SetAlwaysOpenPdfExternally();

  PluginInfoPtr plugin_info = GetPluginInfo(GURL("fake.pdf"), url::Origin(),
                                            pdf::kInternalPluginMimeType);
  ASSERT_TRUE(plugin_info);

  // Internal PDF plugin is not affected by PDF content setting.
  EXPECT_EQ(PluginStatus::kAllowed, plugin_info->status);
  EXPECT_EQ(pdf::kInternalPluginMimeType, plugin_info->actual_mime_type);
}

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

#endif  // BUILDFLAG(ENABLE_PDF)