Skip to content

Commit

Permalink
Issue 1549: Return constant results for navigator.plugins and navigat…
Browse files Browse the repository at this point in the history
…or.mimeTypes

auditors: @diracdeltas, @bridiver
  • Loading branch information
jumde committed Nov 19, 2018
1 parent a60b817 commit f813bb8
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/content/browser/renderer_host/brave_plugin_registry_impl.h"

#define PluginRegistryImpl BravePluginRegistryImpl
#include "../../../../../content/browser/renderer_host/render_process_host_impl.cc"
#undef PluginRegistryImpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "base/path_service.h"
#include "brave/browser/brave_content_browser_client.h"
#include "brave/common/brave_paths.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_content_client.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "content/public/test/browser_test_utils.h"

const char kNavigatorPluginsTest[] = "/navigatorplugins.html";
const char kNavigatorMimeTypesTest[] = "/navigatormimetypes.html";
const char kNavigatorPluginsTestWithFlash[] = "/navigatorpluginswithflash.html";
const char kNavigatorMimeTypesTestWithFlash[] = "/navigatormimetypeswithflash.html";

class NavigatorPluginsTest : public InProcessBrowserTest {
public:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();

content_client_.reset(new ChromeContentClient);
content::SetContentClient(content_client_.get());
browser_content_client_.reset(new BraveContentBrowserClient());
content::SetBrowserClientForTesting(browser_content_client_.get());
content::SetupCrossSiteRedirector(embedded_test_server());

brave::RegisterPathProvider();
base::FilePath test_data_dir;
base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir);
embedded_test_server()->ServeFilesFromDirectory(test_data_dir);

ASSERT_TRUE(embedded_test_server()->Start());
}

HostContentSettingsMap * content_settings() {
return HostContentSettingsMapFactory::GetForProfile(browser()->profile());
}

void AllowFlash(GURL url) {
content_settings()->SetContentSettingCustomScope(
ContentSettingsPattern::FromString(url.spec()),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_PLUGINS,
std::string(),
CONTENT_SETTING_ALLOW);
}

void TearDown() override {
browser_content_client_.reset();
content_client_.reset();
}

private:
std::unique_ptr<ChromeContentClient> content_client_;
std::unique_ptr<BraveContentBrowserClient> browser_content_client_;
base::ScopedTempDir temp_user_data_dir_;
};

IN_PROC_BROWSER_TEST_F(NavigatorPluginsTest, ConstPluginsWithoutFlash) {
GURL url = embedded_test_server()->GetURL(kNavigatorPluginsTest);
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(content::WaitForLoadStop(contents));
EXPECT_EQ(url, contents->GetURL());

bool constantPlugins;
ASSERT_TRUE(ExecuteScriptAndExtractBool(
contents,
"window.domAutomationController.send(constantPlugins())",
&constantPlugins));
EXPECT_TRUE(constantPlugins);
}

IN_PROC_BROWSER_TEST_F(NavigatorPluginsTest, ConstMimeTypesWithoutFlash) {
GURL url = embedded_test_server()->GetURL(kNavigatorMimeTypesTest);
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(content::WaitForLoadStop(contents));
EXPECT_EQ(url, contents->GetURL());

bool constantMimeTypes;
ASSERT_TRUE(ExecuteScriptAndExtractBool(
contents,
"window.domAutomationController.send(constantMimeTypes())",
&constantMimeTypes));
EXPECT_TRUE(constantMimeTypes);
}

IN_PROC_BROWSER_TEST_F(NavigatorPluginsTest, ConstPluginsWithFlash) {
GURL url = embedded_test_server()->GetURL(kNavigatorPluginsTestWithFlash);
AllowFlash(url);
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(content::WaitForLoadStop(contents));
EXPECT_EQ(url, contents->GetURL());

bool constantPlugins;
ASSERT_TRUE(ExecuteScriptAndExtractBool(
contents,
"window.domAutomationController.send(constantPlugins())",
&constantPlugins));
EXPECT_TRUE(constantPlugins);
}

IN_PROC_BROWSER_TEST_F(NavigatorPluginsTest, ConstMimeTypesWithFlash) {
GURL url = embedded_test_server()->GetURL(kNavigatorMimeTypesTestWithFlash);
AllowFlash(url);
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(content::WaitForLoadStop(contents));
EXPECT_EQ(url, contents->GetURL());

bool constantMimeTypes;
ASSERT_TRUE(ExecuteScriptAndExtractBool(
contents,
"window.domAutomationController.send(constantMimeTypes())",
&constantMimeTypes));
EXPECT_TRUE(constantMimeTypes);
}
8 changes: 8 additions & 0 deletions content/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source_set("browser") {
visibility = [ "//content/browser" ]

sources = [
"renderer_host/brave_plugin_registry_impl.cc",
"renderer_host/brave_plugin_registry_impl.h",
]
}
38 changes: 38 additions & 0 deletions content/browser/renderer_host/brave_plugin_registry_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/content/browser/renderer_host/brave_plugin_registry_impl.h"

#include "base/strings/utf_string_conversions.h"

namespace content {

BravePluginRegistryImpl::BravePluginRegistryImpl(ResourceContext* resource_context)
: PluginRegistryImpl(resource_context) {}

BravePluginRegistryImpl::~BravePluginRegistryImpl() {}

void BravePluginRegistryImpl::GetPluginsComplete(GetPluginsCallback callback,
std::vector<blink::mojom::PluginInfoPtr> plugins) {
std::vector<blink::mojom::PluginInfoPtr> filtered_plugins;

for(std::vector<blink::mojom::PluginInfoPtr>::iterator it = plugins.begin();
it != plugins.end(); ++it) {
if ((*it) && (*it)->name == base::ASCIIToUTF16("Shockwave Flash")) {
filtered_plugins.push_back(std::move(*it));
}
}
std::move(callback).Run(std::move(filtered_plugins));
}

void BravePluginRegistryImpl::GetPlugins(bool refresh,
const url::Origin& main_frame_origin,
GetPluginsCallback callback) {
PluginRegistryImpl::GetPlugins(refresh, main_frame_origin,
base::BindOnce(
&BravePluginRegistryImpl::GetPluginsComplete, base::Unretained(this),
std::move(callback)));
}

} // namespace content
29 changes: 29 additions & 0 deletions content/browser/renderer_host/brave_plugin_registry_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CONTENT_BROWSER_RENDERER_HOST_PLUGIN_REGISTRY_IMPL_H_
#define BRAVE_CONTENT_BROWSER_RENDERER_HOST_PLUGIN_REGISTRY_IMPL_H_

#include "content/browser/renderer_host/plugin_registry_impl.h"

namespace content {

class ResourceContext;

class BravePluginRegistryImpl : public PluginRegistryImpl {
public:
BravePluginRegistryImpl(ResourceContext* resource_context);
~BravePluginRegistryImpl() override;

void GetPlugins(bool refresh,
const url::Origin& main_frame_origin,
GetPluginsCallback callback) override;
private:
void GetPluginsComplete(GetPluginsCallback callback,
std::vector<blink::mojom::PluginInfoPtr> plugins);
};

} // namespace content

#endif // BRAVE_CONTENT_BROWSER_RENDERER_HOST_PLUGIN_REGISTRY_IMPL_H_
15 changes: 15 additions & 0 deletions patches/content-browser-BUILD.gn.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/content/browser/BUILD.gn b/content/browser/BUILD.gn
index b9925142f676f2a66fb50c7679e28ab91f81031b..ef53b8d144ee6a80ae0c2d56de60c49d07802100 100644
--- a/content/browser/BUILD.gn
+++ b/content/browser/BUILD.gn
@@ -1864,6 +1864,10 @@ jumbo_source_set("browser") {
"webui/web_ui_url_loader_factory_internal.h",
]

+ deps += [
+ "//brave/content/browser",
+ ]
+
if (toolkit_views) {
deps += [ "//ui/events" ]
}
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ test("brave_browser_tests") {
"//brave/chromium_src/third_party/blink/renderer/modules/battery/navigator_batterytest.cc",
"//brave/chromium_src/third_party/blink/renderer/modules/bluetooth/navigator_bluetoothtest.cc",
"//brave/chromium_src/third_party/blink/renderer/modules/credentialmanager/credentials_containertest.cc",
"//brave/chromium_src/third_party/blink/renderer/modules/plugins/navigator_pluginstest.cc",
"//brave/browser/autoplay/autoplay_permission_context_browsertest.cc",
"//brave/browser/brave_content_browser_client_browsertest.cc",
"//brave/browser/brave_features_browsertest.cc",
Expand Down
5 changes: 5 additions & 0 deletions test/data/navigatormimetypes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
function constantMimeTypes() {
return navigator.mimeTypes.length == 0;
}
</script>
5 changes: 5 additions & 0 deletions test/data/navigatormimetypeswithflash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
function constantMimeTypes() {
return navigator.mimeTypes.length == 2;
}
</script>
5 changes: 5 additions & 0 deletions test/data/navigatorplugins.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
function constantPlugins() {
return navigator.plugins.length == 0;
}
</script>
5 changes: 5 additions & 0 deletions test/data/navigatorpluginswithflash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
function constantPlugins() {
return navigator.plugins.length == 1;
}
</script>

0 comments on commit f813bb8

Please sign in to comment.