Skip to content

Commit

Permalink
Merge pull request #222 from brave/autoplay-rebase
Browse files Browse the repository at this point in the history
Enable granular control of autoplay
  • Loading branch information
darkdh authored Aug 7, 2018
2 parents 7754a95 + 09feca1 commit 409515d
Show file tree
Hide file tree
Showing 65 changed files with 1,908 additions and 11 deletions.
22 changes: 22 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_BOOKMARK_GROUP_FROM_BRAVE" desc="The group name of bookmarks from Brave">
Imported From Brave
</message>
<!-- Autoplay -->
<message name="IDS_AUTOPLAY_PERMISSION_FRAGMENT" desc="Permission request shown if the user is visiting a site that wants to autoplay media. Follows a prompt: 'This site would like to:'">
Autoplay media
</message>
<message name="IDS_SETTINGS_SITE_SETTINGS_AUTOPLAY" desc="Label for autoplay site settings.">
Autoplay
</message>
<message name="IDS_SETTINGS_SITE_SETTINGS_AUTOPLAY_ASK" desc="The ask label for autoplay in site settings.">
Ask when a site wants to autoplay media
</message>
<message name="IDS_SETTINGS_SITE_SETTINGS_AUTOPLAY_ASK_RECOMMENDED" desc="The ask label for autoplay in site settings (with the 'recommended' suffix).">
Ask when a site wants to autoplay media (recommended)
</message>
<message name="IDS_BLOCKED_AUTOPLAY_TITLE" desc="Tooltip and bubble info header text when a page is not allowed to autoplay media.">
Autoplay was blocked on this page
</message>
<message name="IDS_BLOCKED_AUTOPLAY_UNBLOCK" desc="Radio button choice to unblock a site from autoplay media, displayed in bubble when a page tries to autoplay media.">
Always allow autoplay on <ph name="HOST">$1<ex>mail.google.com</ex></ph>
</message>
<message name="IDS_BLOCKED_AUTOPLAY_NO_ACTION" desc="Radio button choice to continue blocking a site from autoplay media, displayed in bubble when a page tries to autoplay media.">
Continue blocking autoplay
</message>
</messages>
<includes>
<include name="IDR_BRAVE_TAG_SERVICES_POLYFILL" file="resources/js/tag_services_polyfill.js" type="BINDATA" />
Expand Down
2 changes: 2 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ source_set("browser") {
]

deps = [
"autoplay",
"//brave/components/brave_shields/browser:brave_shields",
"extensions",
"net",
"permissions",
]

if (is_mac) {
Expand Down
12 changes: 12 additions & 0 deletions browser/autoplay/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import("//build/config/features.gni")

source_set("autoplay") {
sources = [
"autoplay_permission_context.cc",
"autoplay_permission_context.h",
]

deps = [
"//chrome/browser",
]
}
58 changes: 58 additions & 0 deletions browser/autoplay/autoplay_permission_context.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* 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/browser/autoplay/autoplay_permission_context.h"

#include "chrome/browser/content_settings/tab_specific_content_settings.h"
#include "chrome/browser/permissions/permission_request_id.h"
#include "chrome/common/chrome_features.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "third_party/blink/public/mojom/feature_policy/feature_policy.mojom.h"

AutoplayPermissionContext::AutoplayPermissionContext(Profile* profile)
: PermissionContextBase(
profile,
CONTENT_SETTINGS_TYPE_AUTOPLAY,
blink::mojom::FeaturePolicyFeature::kAutoplay) {}

AutoplayPermissionContext::~AutoplayPermissionContext() = default;

void AutoplayPermissionContext::UpdateTabContext(
const PermissionRequestID& id,
const GURL& requesting_frame,
bool allowed) {
TabSpecificContentSettings* content_settings =
TabSpecificContentSettings::GetForFrame(id.render_process_id(),
id.render_frame_id());
if (!content_settings)
return;

if (!allowed) {
content_settings->OnContentBlocked(CONTENT_SETTINGS_TYPE_AUTOPLAY);
}
}

void AutoplayPermissionContext::NotifyPermissionSet(
const PermissionRequestID& id,
const GURL& requesting_origin,
const GURL& embedding_origin,
const BrowserPermissionCallback& callback,
bool persist,
ContentSetting content_setting) {
PermissionContextBase::NotifyPermissionSet(id, requesting_origin,
embedding_origin, callback,
persist, content_setting);
// Ask -> Allow
if (persist && content_setting == CONTENT_SETTING_ALLOW) {
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(
content::RenderFrameHost::FromID(id.render_process_id(),
id.render_frame_id()));
web_contents->GetController().Reload(content::ReloadType::NORMAL, false);
}
}

bool AutoplayPermissionContext::IsRestrictedToSecureOrigins() const {
return false;
}
32 changes: 32 additions & 0 deletions browser/autoplay/autoplay_permission_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* 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_BROWSER_AUTOPLAY_AUTOPLAY_PERMISSION_CONTEXT_H_
#define BRAVE_BROWSER_AUTOPLAY_AUTOPLAY_PERMISSION_CONTEXT_H_

#include "base/macros.h"
#include "chrome/browser/permissions/permission_context_base.h"

class AutoplayPermissionContext : public PermissionContextBase {
public:
explicit AutoplayPermissionContext(Profile* profile);
~AutoplayPermissionContext() override;

private:
// PermissionContextBase:
void UpdateTabContext(const PermissionRequestID& id,
const GURL& requesting_frame,
bool allowed) override;
void NotifyPermissionSet(const PermissionRequestID& id,
const GURL& requesting_origin,
const GURL& embedding_origin,
const BrowserPermissionCallback& callback,
bool persist,
ContentSetting content_setting) override;
bool IsRestrictedToSecureOrigins() const override;

DISALLOW_COPY_AND_ASSIGN(AutoplayPermissionContext);
};

#endif // BRAVE_BROWSER_AUTOPLAY_AUTOPLAY_PERMISSION_CONTEXT_H_
Loading

0 comments on commit 409515d

Please sign in to comment.