Skip to content

Commit

Permalink
Reload active tab when widevine library is ready to use
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhong committed Mar 19, 2020
1 parent ee5f58a commit cc517d5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
57 changes: 55 additions & 2 deletions browser/brave_drm_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,57 @@

#include "brave/browser/brave_drm_tab_helper.h"

#include <algorithm>
#include <vector>

#include "brave/browser/widevine/widevine_utils.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/browser_process_impl.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_handle.h"

using component_updater::ComponentUpdateService;

namespace {

// Copied from widevine_cdm_component_installer.cc.
// There is no shared constant value.
constexpr char kWidevineComponentId[] = "oimompecagnajdejgnnjijobebaeigek";

bool IsAlreadyRegistered(ComponentUpdateService* cus) {
std::vector<std::string> component_ids;
component_ids = cus->GetComponentIDs();
return std::find(component_ids.begin(),
component_ids.end(),
kWidevineComponentId) != component_ids.end();
}

content::WebContents* GetActiveWebContents() {
if (Browser* browser = chrome::FindLastActive())
return browser->tab_strip_model()->GetActiveWebContents();
return nullptr;
}

void ReloadIfActive(content::WebContents* web_contents) {
if (GetActiveWebContents() == web_contents)
web_contents->GetController().Reload(content::ReloadType::NORMAL, false);
}

} // namespace

BraveDrmTabHelper::BraveDrmTabHelper(content::WebContents* contents)
: WebContentsObserver(contents), receivers_(contents, this) {}
: WebContentsObserver(contents),
receivers_(contents, this),
observer_(this) {
auto* updater = g_browser_process->component_updater();
// We don't need to observe if widevine is already registered.
if (!IsAlreadyRegistered(updater))
observer_.Add(updater);
}

BraveDrmTabHelper::~BraveDrmTabHelper() {}

Expand Down Expand Up @@ -47,4 +89,15 @@ void BraveDrmTabHelper::OnWidevineKeySystemAccessRequest() {
}
}

void BraveDrmTabHelper::OnEvent(Events event, const std::string& id) {
if (event == ComponentUpdateService::Observer::Events::COMPONENT_UPDATED &&
id == kWidevineComponentId) {
// When widevine is ready to use, only active tab that requests widevine is
// reloaded automatically. Then, stop observing component update.
if (is_widevine_requested_)
ReloadIfActive(web_contents());
observer_.RemoveAll();
}
}

WEB_CONTENTS_USER_DATA_KEY_IMPL(BraveDrmTabHelper)
13 changes: 12 additions & 1 deletion browser/brave_drm_tab_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
#ifndef BRAVE_BROWSER_BRAVE_DRM_TAB_HELPER_H_
#define BRAVE_BROWSER_BRAVE_DRM_TAB_HELPER_H_

#include <string>

#include "base/scoped_observer.h"
#include "brave/components/brave_drm/brave_drm.mojom.h"
#include "components/component_updater/component_updater_service.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_receiver_set.h"
#include "content/public/browser/web_contents_user_data.h"
Expand All @@ -15,7 +19,8 @@
class BraveDrmTabHelper final
: public content::WebContentsObserver,
public content::WebContentsUserData<BraveDrmTabHelper>,
public brave_drm::mojom::BraveDRM {
public brave_drm::mojom::BraveDRM,
public component_updater::ComponentUpdateService::Observer {
public:
explicit BraveDrmTabHelper(content::WebContents* contents);
~BraveDrmTabHelper() override;
Expand All @@ -29,6 +34,9 @@ class BraveDrmTabHelper final
// blink::mojom::BraveDRM
void OnWidevineKeySystemAccessRequest() override;

// component_updater::ComponentUpdateService::Observer
void OnEvent(Events event, const std::string& id) override;

WEB_CONTENTS_USER_DATA_KEY_DECL();

private:
Expand All @@ -41,6 +49,9 @@ class BraveDrmTabHelper final

// True if we are notified that a page requested widevine availability.
bool is_widevine_requested_ = false;

ScopedObserver<component_updater::ComponentUpdateService,
component_updater::ComponentUpdateService::Observer> observer_;
};

#endif // BRAVE_BROWSER_BRAVE_DRM_TAB_HELPER_H_
3 changes: 0 additions & 3 deletions browser/widevine/widevine_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "brave/grit/brave_generated_resources.h"
#include "chrome/browser/permissions/permission_request_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/pref_registry/pref_registry_syncable.h"
Expand Down Expand Up @@ -87,8 +86,6 @@ void EnableWidevineCdmComponent(content::WebContents* web_contents) {

SetWidevineOptedIn(true);
RegisterWidevineCdmComponent(g_brave_browser_process->component_updater());
ChromeSubresourceFilterClient::FromWebContents(web_contents)
->OnReloadRequested();
}
#endif

Expand Down

0 comments on commit cc517d5

Please sign in to comment.