Skip to content

Commit

Permalink
Merge pull request #596 from brave/pdf-js-take-2
Browse files Browse the repository at this point in the history
Use component updater for installing PDF.js
  • Loading branch information
bbondy authored Oct 10, 2018
2 parents cda0e77 + 4cadca1 commit a76b249
Show file tree
Hide file tree
Showing 26 changed files with 110 additions and 38 deletions.
17 changes: 16 additions & 1 deletion browser/component_updater/brave_component_installer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ bool RewriteManifestFile(
return true;
}

std::string GetManifestString(const base::DictionaryValue& manifest,
const std::string &public_key) {
std::unique_ptr<base::DictionaryValue> final_manifest(manifest.DeepCopy());
final_manifest->SetString(extensions::manifest_keys::kPublicKey, public_key);

std::string manifest_json;
JSONStringValueSerializer serializer(&manifest_json);
serializer.set_pretty_print(true);
if (!serializer.Serialize(*final_manifest)) {
return "";
}
return manifest_json;
}


} // namespace

namespace brave {
Expand Down Expand Up @@ -107,7 +122,7 @@ void BraveComponentInstallerPolicy::ComponentReady(
const base::Version& version,
const base::FilePath& install_dir,
std::unique_ptr<base::DictionaryValue> manifest) {
ready_callback_.Run(install_dir);
ready_callback_.Run(install_dir, GetManifestString(*manifest, base64_public_key_));
}

base::FilePath BraveComponentInstallerPolicy::GetRelativeInstallDir() const {
Expand Down
3 changes: 2 additions & 1 deletion browser/component_updater/brave_component_installer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ namespace base {
class FilePath;
} // namespace base

using ReadyCallback = base::Callback<void(const base::FilePath&)>;
using ReadyCallback = base::Callback<void(const base::FilePath&,
const std::string& manifest)>;

namespace brave {

Expand Down
3 changes: 2 additions & 1 deletion browser/extensions/brave_component_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ void BraveComponentExtension::OnComponentRegistered(const std::string& component

void BraveComponentExtension::OnComponentReady(
const std::string& component_id,
const base::FilePath& install_dir) {
const base::FilePath& install_dir,
const std::string& manifest) {
}
3 changes: 2 additions & 1 deletion browser/extensions/brave_component_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class BraveComponentExtension : public ComponentsUI {
protected:
virtual void OnComponentRegistered(const std::string& component_id);
virtual void OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir);
const base::FilePath& install_dir,
const std::string& manifest);

private:
std::string component_name_;
Expand Down
42 changes: 41 additions & 1 deletion browser/extensions/brave_component_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
#include "brave/browser/extensions/brave_component_loader.h"

#include "base/command_line.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/browser/component_updater/brave_component_installer.h"
#include "brave/browser/extensions/brave_component_extension.h"
#include "brave/common/brave_switches.h"
#include "brave/common/extensions/extension_constants.h"
#include "brave/components/brave_rewards/browser/buildflags/buildflags.h"
#include "brave/components/brave_rewards/extension/grit/brave_rewards_resources.h"
#include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h"
#include "components/grit/brave_components_resources.h"
#include "extensions/browser/extension_prefs.h"

namespace extensions {

Expand All @@ -18,12 +23,43 @@ BraveComponentLoader::BraveComponentLoader(
PrefService* profile_prefs,
PrefService* local_state,
Profile* profile)
: ComponentLoader(extension_service, profile_prefs, local_state, profile) {
: ComponentLoader(extension_service, profile_prefs, local_state, profile),
profile_(profile) {
}

BraveComponentLoader::~BraveComponentLoader() {
}

void BraveComponentLoader::OnComponentRegistered(std::string extension_id) {
ComponentsUI demand_updater;
// This weird looking call is ok, it is just like this to not need
// to patch for friend access.
demand_updater.OnDemandUpdate(g_browser_process->component_updater(),
extension_id);
}

void BraveComponentLoader::OnComponentReady(std::string extension_id,
bool allow_file_access,
const base::FilePath& install_dir,
const std::string& manifest) {
Add(manifest, install_dir);
if (allow_file_access) {
ExtensionPrefs::Get((content::BrowserContext *)profile_)->
SetAllowFileAccess(extension_id, true);
}
}

void BraveComponentLoader::AddExtension(const std::string& extension_id,
const std::string& name, const std::string& public_key) {
brave::RegisterComponent(g_browser_process->component_updater(),
name,
public_key,
base::Bind(&BraveComponentLoader::OnComponentRegistered,
base::Unretained(this), extension_id),
base::Bind(&BraveComponentLoader::OnComponentReady,
base::Unretained(this), extension_id, true));
}

void BraveComponentLoader::AddDefaultComponentExtensions(
bool skip_session_components) {
ComponentLoader::AddDefaultComponentExtensions(skip_session_components);
Expand All @@ -37,6 +73,10 @@ void BraveComponentLoader::AddDefaultComponentExtensions(
Add(IDR_BRAVE_EXTENSON, brave_extension_path);
}

if (!command_line.HasSwitch(switches::kDisablePDFJSExtension)) {
AddExtension(pdfjs_extension_id, pdfjs_extension_name, pdfjs_extension_public_key);
}

#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
if (!command_line.HasSwitch(switches::kDisableBraveRewardsExtension)) {
base::FilePath brave_rewards_path(FILE_PATH_LITERAL(""));
Expand Down
12 changes: 11 additions & 1 deletion browser/extensions/brave_component_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef BRAVE_BROWSER_EXTENSIONS_BRAVE_COMPONENT_LOADER_H_
#define BRAVE_BROWSER_EXTENSIONS_BRAVE_COMPONENT_LOADER_H_

#include "base/files/file_path.h"
#include "chrome/browser/extensions/component_loader.h"

namespace extensions {
Expand All @@ -23,7 +24,16 @@ class BraveComponentLoader : public ComponentLoader {
// be loaded unless we are in signed user session (ChromeOS). For all other
// platforms this |skip_session_components| is expected to be unset.
void AddDefaultComponentExtensions(bool skip_session_components) override;

void OnComponentRegistered(std::string extension_id);
void OnComponentReady(std::string extension_id,
bool allow_file_access,
const base::FilePath& install_dir,
const std::string& manifest);
void AddExtension(const std::string& id,
const std::string& name, const std::string& public_key);

private:
Profile* profile_;
DISALLOW_COPY_AND_ASSIGN(BraveComponentLoader);
};

Expand Down
13 changes: 0 additions & 13 deletions browser/extensions/brave_extension_management.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,12 @@ BraveExtensionManagement::BraveExtensionManagement(
: ExtensionManagement(pref_service, is_signin_profile) {
providers_.push_back(
std::make_unique<BraveExtensionProvider>());
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (!command_line.HasSwitch(switches::kDisablePDFJSExtension)) {
RegisterForceInstalledExtensions();
}
RegisterBraveExtensions();
}

BraveExtensionManagement::~BraveExtensionManagement() {
}

void BraveExtensionManagement::RegisterForceInstalledExtensions() {
base::DictionaryValue forced_list_pref;
extensions::ExternalPolicyLoader::AddExtension(
&forced_list_pref, pdfjs_extension_id,
extension_urls::kChromeWebstoreUpdateURL);
UpdateForcedExtensions(&forced_list_pref);
}

void BraveExtensionManagement::RegisterBraveExtensions() {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
Expand Down
1 change: 0 additions & 1 deletion browser/extensions/brave_extension_management.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class BraveExtensionManagement : public ExtensionManagement {
~BraveExtensionManagement() override;

private:
void RegisterForceInstalledExtensions();
void RegisterBraveExtensions();
DISALLOW_COPY_AND_ASSIGN(BraveExtensionManagement);
};
Expand Down
3 changes: 2 additions & 1 deletion browser/extensions/brave_tor_client_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ void BraveTorClientUpdater::InitExecutablePath(

void BraveTorClientUpdater::OnComponentReady(
const std::string& component_id,
const base::FilePath& install_dir) {
const base::FilePath& install_dir,
const std::string& manifest) {
GetTaskRunner()->PostTask(
FROM_HERE, base::Bind(&BraveTorClientUpdater::InitExecutablePath,
base::Unretained(this), install_dir));
Expand Down
3 changes: 2 additions & 1 deletion browser/extensions/brave_tor_client_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class BraveTorClientUpdater : public BraveComponentExtension {

protected:
void OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir) override;
const base::FilePath& install_dir,
const std::string& manifest) override;

private:
friend class ::BraveTorClientUpdaterTest;
Expand Down
2 changes: 1 addition & 1 deletion browser/extensions/brave_tor_client_updater_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class BraveTorClientUpdaterTest : public ExtensionBrowserTest {
return false;

g_brave_browser_process->tor_client_updater()->OnComponentReady(
tor_client_updater->id(), tor_client_updater->path());
tor_client_updater->id(), tor_client_updater->path(), "");
WaitForTorClientUpdaterThread();

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace extensions {
bool IsComponentExtensionWhitelisted(const std::string& extension_id) {
const char* const kAllowed[] = {
brave_extension_id,
pdfjs_extension_id,
brave_rewards_extension_id,
brave_webtorrent_extension_id
};
Expand Down
5 changes: 4 additions & 1 deletion common/extensions/extension_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
const char brave_extension_id[] = "mnojpmjdmbbfmejpflffifhffcmidifd";
const char brave_rewards_extension_id[] = "jidkidbbcafjabdphckchenhfomhnfma";
const char brave_webtorrent_extension_id[] = "lgjmpdmojkpocjcopdikifhejkkjglho";
const char pdfjs_extension_id[] = "oemmndcbldboiebfnladdacbdfmadadm";
const char widevine_extension_id[] = "oimompecagnajdejgnnjijobebaeigek";

const char pdfjs_extension_id[] = "oemmndcbldboiebfnladdacbdfmadadm";
const char pdfjs_extension_name[] = "PDF Viewer (PDF.js)";
const char pdfjs_extension_public_key[] = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDb5PIb8ayK6vHvEIY1nJKRSCDE8iJ1T43qFN+5dvCVQrmyEkgqB9ZuZNT24Lwot96HV51VoITHKRNIVKI2Nrbfn0M49t7qtaP34g/GXJ7mAIbSzsY4+i+Wsz8EL2SNEIw6uH8RmXG7nZ29NJ7sk7jn17QmMsO2UJ01UT8hfOOOEQIDAQAB";
5 changes: 4 additions & 1 deletion common/extensions/extension_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
extern const char brave_extension_id[];
extern const char brave_rewards_extension_id[];
extern const char brave_webtorrent_extension_id[];
extern const char pdfjs_extension_id[];
extern const char widevine_extension_id[];

extern const char pdfjs_extension_id[];
extern const char pdfjs_extension_name[];
extern const char pdfjs_extension_public_key[];
3 changes: 2 additions & 1 deletion common/shield_exceptions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ bool IsUAWhitelisted(const GURL& gurl) {
bool IsBlockedResource(const GURL& gurl) {
static std::vector<URLPattern> blocked_patterns({
URLPattern(URLPattern::SCHEME_ALL, "https://www.lesechos.fr/xtcore.js"),
URLPattern(URLPattern::SCHEME_ALL, "https://*.y8.com/js/sdkloader/outstream.js")
URLPattern(URLPattern::SCHEME_ALL, "https://*.y8.com/js/sdkloader/outstream.js"),
URLPattern(URLPattern::SCHEME_ALL, "https://pdfjs.robwu.nl/*")
});
return std::any_of(blocked_patterns.begin(), blocked_patterns.end(),
[&gurl](URLPattern pattern){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ void AdBlockRegionalService::OnComponentRegistered(

void AdBlockRegionalService::OnComponentReady(
const std::string& component_id,
const base::FilePath& install_dir) {
const base::FilePath& install_dir,
const std::string& manifest) {
base::FilePath dat_file_path =
install_dir.AppendASCII(g_ad_block_regional_dat_file_version_)
.AppendASCII(uuid_)
Expand Down
3 changes: 2 additions & 1 deletion components/brave_shields/browser/ad_block_regional_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class AdBlockRegionalService : public AdBlockBaseService {
bool Init() override;
void OnComponentRegistered(const std::string& component_id) override;
void OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir) override;
const base::FilePath& install_dir,
const std::string& manifest) override;

private:
friend class ::AdBlockServiceTest;
Expand Down
3 changes: 2 additions & 1 deletion components/brave_shields/browser/ad_block_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ bool AdBlockService::Init() {
}

void AdBlockService::OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir) {
const base::FilePath& install_dir,
const std::string& manifest) {
base::FilePath dat_file_path =
install_dir.AppendASCII(g_ad_block_dat_file_version_)
.AppendASCII(DAT_FILE);
Expand Down
3 changes: 2 additions & 1 deletion components/brave_shields/browser/ad_block_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class AdBlockService : public AdBlockBaseService {
protected:
bool Init() override;
void OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir) override;
const base::FilePath& install_dir,
const std::string& manifest) override;

private:
friend class ::AdBlockServiceTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class AdBlockServiceTest : public ExtensionBrowserTest {
return false;

g_brave_browser_process->ad_block_service()->OnComponentReady(
ad_block_extension->id(), ad_block_extension->path());
ad_block_extension->id(), ad_block_extension->path(), "");
WaitForDefaultAdBlockServiceThread();

return true;
Expand All @@ -116,7 +116,7 @@ class AdBlockServiceTest : public ExtensionBrowserTest {
return false;

g_brave_browser_process->ad_block_regional_service()->OnComponentReady(
ad_block_extension->id(), ad_block_extension->path());
ad_block_extension->id(), ad_block_extension->path(), "");
WaitForRegionalAdBlockServiceThread();

return true;
Expand Down
3 changes: 2 additions & 1 deletion components/brave_shields/browser/https_everywhere_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ void HTTPSEverywhereService::InitDB(const base::FilePath& install_dir) {

void HTTPSEverywhereService::OnComponentReady(
const std::string& component_id,
const base::FilePath& install_dir) {
const base::FilePath& install_dir,
const std::string& manifest) {
GetTaskRunner()->PostTask(
FROM_HERE,
base::Bind(&HTTPSEverywhereService::InitDB,
Expand Down
3 changes: 2 additions & 1 deletion components/brave_shields/browser/https_everywhere_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class HTTPSEverywhereService : public BaseBraveShieldsService {
bool Init() override;
void Cleanup() override;
void OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir) override;
const base::FilePath& install_dir,
const std::string& manifest) override;

void AddHTTPSEUrlToRedirectList(const uint64_t& request_id);
bool ShouldHTTPSERedirect(const uint64_t& request_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class HTTPSEverywhereServiceTest : public ExtensionBrowserTest {
return false;

g_brave_browser_process->https_everywhere_service()->OnComponentReady(
httpse_extension->id(), httpse_extension->path());
httpse_extension->id(), httpse_extension->path(), "");
WaitForHTTPSEverywhereServiceThread();

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ void TrackingProtectionService::OnDATFileDataReady() {

void TrackingProtectionService::OnComponentReady(
const std::string& component_id,
const base::FilePath& install_dir) {
const base::FilePath& install_dir,
const std::string& manifest) {
base::FilePath dat_file_path =
install_dir.AppendASCII(DAT_FILE_VERSION).AppendASCII(DAT_FILE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class TrackingProtectionService : public BaseBraveShieldsService {
bool Init() override;
void Cleanup() override;
void OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir) override;
const base::FilePath& install_dir,
const std::string& manifest) override;

private:
friend class ::TrackingProtectionServiceTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class TrackingProtectionServiceTest : public ExtensionBrowserTest {
return false;

g_brave_browser_process->tracking_protection_service()->OnComponentReady(
tracking_protection_extension->id(), tracking_protection_extension->path());
tracking_protection_extension->id(), tracking_protection_extension->path(), "");
WaitForTrackingProtectionServiceThread();

return true;
Expand Down

0 comments on commit a76b249

Please sign in to comment.