Skip to content

Commit fc54d67

Browse files
committed
Merge pull request #941 from brave/maxk-toggle-pdf-download
Allow downloading PDFs instead of opening them in Brave.
1 parent d9098a3 commit fc54d67

10 files changed

+434
-4
lines changed

browser/extensions/brave_component_loader.cc

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,30 @@
1313
#include "brave/components/brave_rewards/browser/buildflags/buildflags.h"
1414
#include "brave/components/brave_rewards/resources/extension/grit/brave_rewards_extension_resources.h"
1515
#include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h"
16+
#include "chrome/browser/profiles/profile.h"
17+
#include "chrome/common/pref_names.h"
1618
#include "components/grit/brave_components_resources.h"
1719
#include "extensions/browser/extension_prefs.h"
1820

1921
namespace extensions {
2022

23+
//static
24+
bool BraveComponentLoader::IsPdfjsDisabled() {
25+
const base::CommandLine& command_line =
26+
*base::CommandLine::ForCurrentProcess();
27+
return command_line.HasSwitch(switches::kDisablePDFJSExtension);
28+
}
29+
2130
BraveComponentLoader::BraveComponentLoader(
2231
ExtensionServiceInterface* extension_service,
2332
PrefService* profile_prefs,
2433
PrefService* local_state,
2534
Profile* profile)
2635
: ComponentLoader(extension_service, profile_prefs, local_state, profile),
27-
profile_(profile) {
36+
profile_(profile),
37+
profile_prefs_(profile_prefs),
38+
testing_callbacks_(nullptr) {
39+
ObserveOpenPdfExternallySetting();
2840
}
2941

3042
BraveComponentLoader::~BraveComponentLoader() {
@@ -73,8 +85,10 @@ void BraveComponentLoader::AddDefaultComponentExtensions(
7385
Add(IDR_BRAVE_EXTENSON, brave_extension_path);
7486
}
7587

76-
if (!command_line.HasSwitch(switches::kDisablePDFJSExtension)) {
77-
AddExtension(pdfjs_extension_id, pdfjs_extension_name, pdfjs_extension_public_key);
88+
if (!profile_prefs_->GetBoolean(prefs::kPluginsAlwaysOpenPdfExternally) &&
89+
!command_line.HasSwitch(switches::kDisablePDFJSExtension)) {
90+
AddExtension(pdfjs_extension_id, pdfjs_extension_name,
91+
pdfjs_extension_public_key);
7892
}
7993

8094
#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
@@ -94,4 +108,37 @@ void BraveComponentLoader::AddDefaultComponentExtensions(
94108
}
95109
}
96110

111+
void BraveComponentLoader::ObserveOpenPdfExternallySetting() {
112+
// Observe the setting change only in regular profiles since the PDF settings
113+
// page is not available in Guest/Tor profiles.
114+
DCHECK(profile_ && profile_prefs_);
115+
if (!profile_->IsGuestSession()) {
116+
registrar_.Init(profile_prefs_);
117+
registrar_.Add(prefs::kPluginsAlwaysOpenPdfExternally,
118+
base::Bind(&BraveComponentLoader::UpdatePdfExtension,
119+
base::Unretained(this)));
120+
}
121+
}
122+
123+
void BraveComponentLoader::UpdatePdfExtension(const std::string& pref_name) {
124+
DCHECK(pref_name == prefs::kPluginsAlwaysOpenPdfExternally);
125+
DCHECK(profile_prefs_);
126+
if (profile_prefs_->GetBoolean(prefs::kPluginsAlwaysOpenPdfExternally) ||
127+
IsPdfjsDisabled()) {
128+
if (testing_callbacks_)
129+
testing_callbacks_->OnPdfExtensionAction(TestingCallbacks::WILL_REMOVE);
130+
Remove(pdfjs_extension_id);
131+
} else if (!Exists(pdfjs_extension_id)) {
132+
if (testing_callbacks_)
133+
testing_callbacks_->OnPdfExtensionAction(TestingCallbacks::WILL_ADD);
134+
AddExtension(pdfjs_extension_id, pdfjs_extension_name,
135+
pdfjs_extension_public_key);
136+
}
137+
}
138+
139+
void BraveComponentLoader::set_testing_callbacks(
140+
TestingCallbacks* testing_callbacks) {
141+
testing_callbacks_ = testing_callbacks;
142+
}
143+
97144
} // namespace extensions

browser/extensions/brave_component_loader.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
#include "base/files/file_path.h"
99
#include "chrome/browser/extensions/component_loader.h"
10+
#include "components/prefs/pref_change_registrar.h"
11+
12+
class BravePDFExtensionTest;
1013

1114
namespace extensions {
1215

@@ -32,8 +35,29 @@ class BraveComponentLoader : public ComponentLoader {
3235
void AddExtension(const std::string& id,
3336
const std::string& name, const std::string& public_key);
3437

38+
static bool IsPdfjsDisabled();
39+
3540
private:
41+
friend class ::BravePDFExtensionTest;
42+
void ObserveOpenPdfExternallySetting();
43+
// Callback for changes to the AlwaysOpenPdfExternally setting.
44+
void UpdatePdfExtension(const std::string& pref_name);
45+
46+
struct TestingCallbacks {
47+
enum PdfExtensionAction {
48+
NONE,
49+
WILL_ADD,
50+
WILL_REMOVE,
51+
};
52+
virtual void OnPdfExtensionAction(PdfExtensionAction action) = 0;
53+
};
54+
55+
void set_testing_callbacks(TestingCallbacks* testing_callbacks);
56+
3657
Profile* profile_;
58+
PrefService* profile_prefs_;
59+
PrefChangeRegistrar registrar_;
60+
TestingCallbacks* testing_callbacks_;
3761
DISALLOW_COPY_AND_ASSIGN(BraveComponentLoader);
3862
};
3963

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#include "brave/browser/extensions/brave_component_loader.h"
6+
#include "brave/browser/extensions/brave_extension_functional_test.h"
7+
#include "brave/common/brave_switches.h"
8+
#include "chrome/browser/extensions/extension_service.h"
9+
#include "chrome/browser/profiles/profile.h"
10+
#include "chrome/browser/ui/browser.h"
11+
#include "chrome/common/pref_names.h"
12+
#include "components/prefs/pref_service.h"
13+
#include "content/public/test/browser_test_utils.h"
14+
#include "testing/gtest/include/gtest/gtest.h"
15+
16+
using extensions::BraveComponentLoader;
17+
18+
class BravePDFExtensionTest : public extensions::ExtensionFunctionalTest,
19+
public BraveComponentLoader::TestingCallbacks {
20+
public:
21+
BravePDFExtensionTest() : pdf_extension_action_(TestingCallbacks::NONE) {}
22+
~BravePDFExtensionTest() override = default;
23+
24+
protected:
25+
void SetUpOnMainThread() override {
26+
extensions::ExtensionService* service =
27+
extensions::ExtensionSystem::Get(profile())->extension_service();
28+
DCHECK(service);
29+
(static_cast<BraveComponentLoader*>(service->component_loader()))
30+
->set_testing_callbacks(this);
31+
}
32+
33+
// BraveComponentLoader::TestingCallbacks
34+
void OnPdfExtensionAction(
35+
TestingCallbacks::PdfExtensionAction action) override {
36+
pdf_extension_action_ = action;
37+
}
38+
39+
void SetDownloadPDFs(bool value) {
40+
DCHECK(browser());
41+
profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysOpenPdfExternally,
42+
value);
43+
}
44+
45+
TestingCallbacks::PdfExtensionAction pdf_extension_action() {
46+
return pdf_extension_action_;
47+
}
48+
49+
private:
50+
TestingCallbacks::PdfExtensionAction pdf_extension_action_;
51+
};
52+
53+
IN_PROC_BROWSER_TEST_F(BravePDFExtensionTest, ToggleDownloadPDFs) {
54+
// Set preference to always download PDFs.
55+
SetDownloadPDFs(true);
56+
EXPECT_EQ(TestingCallbacks::WILL_REMOVE, pdf_extension_action());
57+
58+
// Toggle the preference to view PDFs in the browser.
59+
SetDownloadPDFs(false);
60+
EXPECT_EQ(TestingCallbacks::WILL_ADD, pdf_extension_action());
61+
}
62+
63+
class BravePDFExtensionDisabledTest : public BravePDFExtensionTest {
64+
public:
65+
BravePDFExtensionDisabledTest() = default;
66+
~BravePDFExtensionDisabledTest() override = default;
67+
68+
protected:
69+
void SetUpCommandLine(base::CommandLine* command_line) override {
70+
ExtensionFunctionalTest::SetUpCommandLine(command_line);
71+
// Disable loading of our PDF extension.
72+
command_line->AppendSwitch(switches::kDisablePDFJSExtension);
73+
}
74+
};
75+
76+
IN_PROC_BROWSER_TEST_F(BravePDFExtensionDisabledTest, ToggleDownloadPDFs) {
77+
// Set preference to always download PDFs.
78+
SetDownloadPDFs(true);
79+
EXPECT_EQ(TestingCallbacks::WILL_REMOVE, pdf_extension_action());
80+
81+
// Toggle the preference to view PDFs in the browser.
82+
SetDownloadPDFs(false);
83+
EXPECT_EQ(TestingCallbacks::WILL_REMOVE, pdf_extension_action());
84+
}

browser/ui/webui/brave_md_settings_ui.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "brave/browser/ui/webui/brave_md_settings_ui.h"
66

7+
#include "brave/browser/extensions/brave_component_loader.h"
78
#include "brave/browser/resources/grit/brave_settings_resources.h"
89
#include "brave/browser/resources/grit/brave_settings_resources_map.h"
910
#include "brave/browser/ui/webui/settings/brave_privacy_handler.h"
@@ -35,8 +36,11 @@ BraveMdSettingsUI::~BraveMdSettingsUI() {
3536
// static
3637
void BraveMdSettingsUI::AddResources(content::WebUIDataSource* html_source,
3738
Profile* profile) {
38-
for (size_t i = 0; i < kBraveSettingsResourcesSize; ++i) {
39+
for (size_t i = 0; i < kBraveSettingsResourcesSize; ++i) {
3940
html_source->AddResourcePath(kBraveSettingsResources[i].name,
4041
kBraveSettingsResources[i].value);
4142
}
43+
44+
html_source->AddBoolean("isPdfjsDisabled",
45+
extensions::BraveComponentLoader::IsPdfjsDisabled());
4246
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#include "../../../../extensions/common/manifest_handlers/mime_types_handler.cc"
6+
7+
// static
8+
std::vector<std::string> MimeTypesHandler::BraveGetMIMETypeWhitelist() {
9+
std::vector<std::string> whitelist = MimeTypesHandler::GetMIMETypeWhitelist();
10+
auto pos = std::find(whitelist.begin(), whitelist.end(),
11+
extension_misc::kPdfExtensionId);
12+
if (pos != whitelist.end())
13+
whitelist.erase(pos);
14+
return whitelist;
15+
}
16+

0 commit comments

Comments
 (0)