From 0f7eb5064dae06de26b8be750ad83679ec3a90d2 Mon Sep 17 00:00:00 2001 From: "Brian R. Bondy" Date: Tue, 9 Oct 2018 21:38:39 -0400 Subject: [PATCH] Install PDF.js from component updater This also fixes a problem where PDF.js was not always installed. This also fixes PDF.js disabling command line. This also fixes intermittent PDF.js failures in tests. Fix https://github.com/brave/brave-browser/issues/1375 Fix https://github.com/brave/brave-browser/issues/1253 Fix https://github.com/brave/brave-browser/issues/1072 --- .../brave_component_installer.cc | 17 +++++++++- .../brave_component_installer.h | 3 +- .../extensions/brave_component_extension.cc | 3 +- .../extensions/brave_component_extension.h | 3 +- browser/extensions/brave_component_loader.cc | 33 +++++++++++++++++++ browser/extensions/brave_component_loader.h | 8 +++++ .../extensions/brave_tor_client_updater.cc | 3 +- browser/extensions/brave_tor_client_updater.h | 3 +- .../brave_tor_client_updater_browsertest.cc | 2 +- .../whitelist.cc | 1 + common/extensions/extension_constants.cc | 5 ++- common/extensions/extension_constants.h | 5 ++- .../browser/ad_block_regional_service.cc | 3 +- .../browser/ad_block_regional_service.h | 3 +- .../brave_shields/browser/ad_block_service.cc | 3 +- .../brave_shields/browser/ad_block_service.h | 3 +- .../browser/ad_block_service_browsertest.cc | 4 +-- .../browser/https_everywhere_service.cc | 3 +- .../browser/https_everywhere_service.h | 3 +- .../https_everywhere_service_browsertest.cc | 2 +- .../browser/tracking_protection_service.cc | 3 +- .../browser/tracking_protection_service.h | 3 +- ...tracking_protection_service_browsertest.cc | 2 +- 23 files changed, 97 insertions(+), 21 deletions(-) diff --git a/browser/component_updater/brave_component_installer.cc b/browser/component_updater/brave_component_installer.cc index 07916687f88a..b2b8907c546d 100644 --- a/browser/component_updater/brave_component_installer.cc +++ b/browser/component_updater/brave_component_installer.cc @@ -57,6 +57,21 @@ bool RewriteManifestFile( return true; } +std::string GetManifestString(const base::DictionaryValue& manifest, + const std::string &public_key) { + std::unique_ptr 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 { @@ -107,7 +122,7 @@ void BraveComponentInstallerPolicy::ComponentReady( const base::Version& version, const base::FilePath& install_dir, std::unique_ptr manifest) { - ready_callback_.Run(install_dir); + ready_callback_.Run(install_dir, GetManifestString(*manifest, base64_public_key_)); } base::FilePath BraveComponentInstallerPolicy::GetRelativeInstallDir() const { diff --git a/browser/component_updater/brave_component_installer.h b/browser/component_updater/brave_component_installer.h index 88f5c5b1803b..520b885e16c7 100644 --- a/browser/component_updater/brave_component_installer.h +++ b/browser/component_updater/brave_component_installer.h @@ -22,7 +22,8 @@ namespace base { class FilePath; } // namespace base -using ReadyCallback = base::Callback; +using ReadyCallback = base::Callback; namespace brave { diff --git a/browser/extensions/brave_component_extension.cc b/browser/extensions/brave_component_extension.cc index 4a1413b8c1b2..a11f8c0b3eba 100644 --- a/browser/extensions/brave_component_extension.cc +++ b/browser/extensions/brave_component_extension.cc @@ -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) { } diff --git a/browser/extensions/brave_component_extension.h b/browser/extensions/brave_component_extension.h index 183527f64565..cc239489b5f4 100644 --- a/browser/extensions/brave_component_extension.h +++ b/browser/extensions/brave_component_extension.h @@ -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_; diff --git a/browser/extensions/brave_component_loader.cc b/browser/extensions/brave_component_loader.cc index bb13a5fb5871..bb37c3dde42f 100644 --- a/browser/extensions/brave_component_loader.cc +++ b/browser/extensions/brave_component_loader.cc @@ -5,7 +5,11 @@ #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" @@ -24,6 +28,31 @@ BraveComponentLoader::BraveComponentLoader( 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, + const base::FilePath& install_dir, + const std::string& manifest) { + Add(manifest, install_dir); +} + +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)); +} + void BraveComponentLoader::AddDefaultComponentExtensions( bool skip_session_components) { ComponentLoader::AddDefaultComponentExtensions(skip_session_components); @@ -37,6 +66,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("")); diff --git a/browser/extensions/brave_component_loader.h b/browser/extensions/brave_component_loader.h index 46e4ac24ad80..a9ade3e4da68 100644 --- a/browser/extensions/brave_component_loader.h +++ b/browser/extensions/brave_component_loader.h @@ -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 { @@ -23,6 +24,13 @@ 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, + const base::FilePath& install_dir, + const std::string& manifest); + void AddExtension(const std::string& id, + const std::string& name, const std::string& public_key); + DISALLOW_COPY_AND_ASSIGN(BraveComponentLoader); }; diff --git a/browser/extensions/brave_tor_client_updater.cc b/browser/extensions/brave_tor_client_updater.cc index 2986d083b9a3..8550b7f47516 100644 --- a/browser/extensions/brave_tor_client_updater.cc +++ b/browser/extensions/brave_tor_client_updater.cc @@ -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)); diff --git a/browser/extensions/brave_tor_client_updater.h b/browser/extensions/brave_tor_client_updater.h index f0110431eb49..0ddfabc3757f 100644 --- a/browser/extensions/brave_tor_client_updater.h +++ b/browser/extensions/brave_tor_client_updater.h @@ -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; diff --git a/browser/extensions/brave_tor_client_updater_browsertest.cc b/browser/extensions/brave_tor_client_updater_browsertest.cc index 970d119d5a46..ce8c544f84a4 100644 --- a/browser/extensions/brave_tor_client_updater_browsertest.cc +++ b/browser/extensions/brave_tor_client_updater_browsertest.cc @@ -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; diff --git a/chromium_src/chrome/browser/extensions/component_extensions_whitelist/whitelist.cc b/chromium_src/chrome/browser/extensions/component_extensions_whitelist/whitelist.cc index a1740d2298ea..068024c6751a 100644 --- a/chromium_src/chrome/browser/extensions/component_extensions_whitelist/whitelist.cc +++ b/chromium_src/chrome/browser/extensions/component_extensions_whitelist/whitelist.cc @@ -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 }; diff --git a/common/extensions/extension_constants.cc b/common/extensions/extension_constants.cc index a028e79673d5..819eeb92a768 100644 --- a/common/extensions/extension_constants.cc +++ b/common/extensions/extension_constants.cc @@ -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"; diff --git a/common/extensions/extension_constants.h b/common/extensions/extension_constants.h index 305d84e73e9b..83bc28a08032 100644 --- a/common/extensions/extension_constants.h +++ b/common/extensions/extension_constants.h @@ -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[]; diff --git a/components/brave_shields/browser/ad_block_regional_service.cc b/components/brave_shields/browser/ad_block_regional_service.cc index 0a49f4c5bac6..27a70b66b8c7 100644 --- a/components/brave_shields/browser/ad_block_regional_service.cc +++ b/components/brave_shields/browser/ad_block_regional_service.cc @@ -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_) diff --git a/components/brave_shields/browser/ad_block_regional_service.h b/components/brave_shields/browser/ad_block_regional_service.h index 40956a358cdb..77fe3b687d04 100644 --- a/components/brave_shields/browser/ad_block_regional_service.h +++ b/components/brave_shields/browser/ad_block_regional_service.h @@ -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; diff --git a/components/brave_shields/browser/ad_block_service.cc b/components/brave_shields/browser/ad_block_service.cc index 67243f1f50a8..9583483211f6 100644 --- a/components/brave_shields/browser/ad_block_service.cc +++ b/components/brave_shields/browser/ad_block_service.cc @@ -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); diff --git a/components/brave_shields/browser/ad_block_service.h b/components/brave_shields/browser/ad_block_service.h index d6257b985e00..bdbbdaf7c03f 100644 --- a/components/brave_shields/browser/ad_block_service.h +++ b/components/brave_shields/browser/ad_block_service.h @@ -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; diff --git a/components/brave_shields/browser/ad_block_service_browsertest.cc b/components/brave_shields/browser/ad_block_service_browsertest.cc index c67f0c361615..c01df4661253 100644 --- a/components/brave_shields/browser/ad_block_service_browsertest.cc +++ b/components/brave_shields/browser/ad_block_service_browsertest.cc @@ -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; @@ -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; diff --git a/components/brave_shields/browser/https_everywhere_service.cc b/components/brave_shields/browser/https_everywhere_service.cc index 8434f347070d..961116132b4b 100644 --- a/components/brave_shields/browser/https_everywhere_service.cc +++ b/components/brave_shields/browser/https_everywhere_service.cc @@ -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, diff --git a/components/brave_shields/browser/https_everywhere_service.h b/components/brave_shields/browser/https_everywhere_service.h index 48418f4bc2a5..9b90f10aa9f5 100644 --- a/components/brave_shields/browser/https_everywhere_service.h +++ b/components/brave_shields/browser/https_everywhere_service.h @@ -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); diff --git a/components/brave_shields/browser/https_everywhere_service_browsertest.cc b/components/brave_shields/browser/https_everywhere_service_browsertest.cc index acc3ef8204aa..888b4dbd8442 100644 --- a/components/brave_shields/browser/https_everywhere_service_browsertest.cc +++ b/components/brave_shields/browser/https_everywhere_service_browsertest.cc @@ -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; diff --git a/components/brave_shields/browser/tracking_protection_service.cc b/components/brave_shields/browser/tracking_protection_service.cc index 15542a76279b..eb39de28b151 100644 --- a/components/brave_shields/browser/tracking_protection_service.cc +++ b/components/brave_shields/browser/tracking_protection_service.cc @@ -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); diff --git a/components/brave_shields/browser/tracking_protection_service.h b/components/brave_shields/browser/tracking_protection_service.h index 1a68d45c6f3f..aa3332f0a3ff 100644 --- a/components/brave_shields/browser/tracking_protection_service.h +++ b/components/brave_shields/browser/tracking_protection_service.h @@ -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; diff --git a/components/brave_shields/browser/tracking_protection_service_browsertest.cc b/components/brave_shields/browser/tracking_protection_service_browsertest.cc index c76c40e4e7a6..aa0c26090880 100644 --- a/components/brave_shields/browser/tracking_protection_service_browsertest.cc +++ b/components/brave_shields/browser/tracking_protection_service_browsertest.cc @@ -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;