Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(751)

Side by Side Diff: chrome/browser/notifications/notification_permission_context.cc

Issue 1575623002: Disable Web Notifications in Incognito (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@permfix
Patch Set: No Profile* in WebsiteSettingsPopupView Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/notifications/notification_permission_context.h" 5 #include "chrome/browser/notifications/notification_permission_context.h"
6 6
7 #include <queue>
8
9 #include "base/callback.h"
10 #include "base/location.h"
11 #include "base/rand_util.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/timer/timer.h"
7 #include "chrome/browser/notifications/desktop_notification_profile_util.h" 15 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
16 #include "chrome/browser/permissions/permission_request_id.h"
17 #include "chrome/browser/profiles/profile.h"
8 #include "components/content_settings/core/common/content_settings_pattern.h" 18 #include "components/content_settings/core/common/content_settings_pattern.h"
19 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/permission_type.h" 20 #include "content/public/browser/permission_type.h"
21 #include "content/public/browser/render_frame_host.h"
22 #include "content/public/browser/web_contents_observer.h"
23 #include "content/public/browser/web_contents_user_data.h"
10 #include "url/gurl.h" 24 #include "url/gurl.h"
11 25
26 namespace {
27
28 // At most one of these is attached to each WebContents. It allows posting
29 // delayed tasks whose timer only counts down whilst the WebContents is visible
30 // (and whose timer is reset whenever the WebContents stops being visible).
31 class VisibilityTimerTabHelper
32 : public content::WebContentsObserver,
33 public content::WebContentsUserData<VisibilityTimerTabHelper> {
34 public:
35 ~VisibilityTimerTabHelper() override {}
36
37 // Runs |task| after the WebContents has been visible for a consecutive
38 // duration of at least |visible_delay|.
39 void PostTaskAfterVisibleDelay(const tracked_objects::Location& from_here,
40 const base::Closure& task,
41 base::TimeDelta visible_delay);
42
43 // WebContentsObserver:
44 void WasShown() override;
45 void WasHidden() override;
46 void WebContentsDestroyed() override;
47
48 private:
49 friend class content::WebContentsUserData<VisibilityTimerTabHelper>;
50 explicit VisibilityTimerTabHelper(content::WebContents* contents);
51
52 void RunTask(const base::Closure& task);
53
54 bool is_visible_;
55 std::queue<scoped_ptr<base::Timer>> task_queue_;
56
57 DISALLOW_COPY_AND_ASSIGN(VisibilityTimerTabHelper);
58 };
59
60 VisibilityTimerTabHelper::VisibilityTimerTabHelper(
61 content::WebContents* contents)
62 : content::WebContentsObserver(contents) {
63 if (!contents->GetMainFrame()) {
64 is_visible_ = false;
65 } else {
66 switch (contents->GetMainFrame()->GetVisibilityState()) {
67 case blink::WebPageVisibilityStateHidden:
68 case blink::WebPageVisibilityStatePrerender:
69 is_visible_ = false;
70 break;
71 case blink::WebPageVisibilityStateVisible:
72 is_visible_ = true;
73 break;
74 }
75 }
76 }
77
78 void VisibilityTimerTabHelper::PostTaskAfterVisibleDelay(
79 const tracked_objects::Location& from_here,
80 const base::Closure& task,
81 base::TimeDelta visible_delay) {
82 // Safe to use Unretained, as destroying this will destroy task_queue_, hence
83 // cancelling all timers.
84 task_queue_.push(make_scoped_ptr(new base::Timer(
85 from_here, visible_delay, base::Bind(&VisibilityTimerTabHelper::RunTask,
86 base::Unretained(this), task),
87 false /* is_repeating */)));
88 DCHECK(!task_queue_.back()->IsRunning());
89 if (is_visible_ && task_queue_.size() == 1)
90 task_queue_.front()->Reset();
91 }
92
93 void VisibilityTimerTabHelper::WasShown() {
94 if (!is_visible_ && !task_queue_.empty())
95 task_queue_.front()->Reset();
96 is_visible_ = true;
97 }
98
99 void VisibilityTimerTabHelper::WasHidden() {
100 if (is_visible_ && !task_queue_.empty())
101 task_queue_.front()->Stop();
102 is_visible_ = false;
103 }
104
105 void VisibilityTimerTabHelper::WebContentsDestroyed() {
106 // Delete ourselves, to avoid running tasks after WebContents is destroyed.
107 web_contents()->RemoveUserData(UserDataKey());
108 // |this| has been deleted now.
109 }
110
111 void VisibilityTimerTabHelper::RunTask(const base::Closure& task) {
112 DCHECK(is_visible_);
113 task.Run();
114 task_queue_.pop();
115 if (!task_queue_.empty()) {
116 task_queue_.front()->Reset();
117 return;
118 }
119 web_contents()->RemoveUserData(UserDataKey());
120 // |this| has been deleted now.
121 }
122
123 } // namespace
124
125 DEFINE_WEB_CONTENTS_USER_DATA_KEY(VisibilityTimerTabHelper);
126
12 NotificationPermissionContext::NotificationPermissionContext(Profile* profile) 127 NotificationPermissionContext::NotificationPermissionContext(Profile* profile)
13 : PermissionContextBase(profile, 128 : PermissionContextBase(profile,
14 content::PermissionType::NOTIFICATIONS, 129 content::PermissionType::NOTIFICATIONS,
15 CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {} 130 CONTENT_SETTINGS_TYPE_NOTIFICATIONS),
131 weak_factory_ui_thread_(this) {}
16 132
17 NotificationPermissionContext::~NotificationPermissionContext() {} 133 NotificationPermissionContext::~NotificationPermissionContext() {}
18 134
19 void NotificationPermissionContext::ResetPermission( 135 void NotificationPermissionContext::ResetPermission(
20 const GURL& requesting_origin, 136 const GURL& requesting_origin,
21 const GURL& embedder_origin) { 137 const GURL& embedder_origin) {
22 DesktopNotificationProfileUtil::ClearSetting( 138 DesktopNotificationProfileUtil::ClearSetting(
23 profile(), ContentSettingsPattern::FromURLNoWildcard(requesting_origin)); 139 profile(), ContentSettingsPattern::FromURLNoWildcard(requesting_origin));
24 } 140 }
25 141
142 void NotificationPermissionContext::DecidePermission(
143 content::WebContents* web_contents,
144 const PermissionRequestID& id,
145 const GURL& requesting_origin,
146 const GURL& embedding_origin,
147 bool user_gesture,
148 const BrowserPermissionCallback& callback) {
149 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
150
151 // Notifications permission is always denied in incognito. To prevent sites
152 // from using that to detect whether incognito mode is active, we deny after a
153 // random time delay, to simulate a user clicking a bubble/infobar. See also
154 // ContentSettingsRegistry::Init, which marks notifications as
155 // INHERIT_IN_INCOGNITO_EXCEPT_ALLOW, and
156 // PermissionMenuModel::PermissionMenuModel which prevents users from manually
157 // allowing the permission.
158 if (profile()->IsOffTheRecord()) {
159 // Random number of seconds in the range [1.0, 2.0).
160 double delay_seconds = 1.0 + 1.0 * base::RandDouble();
161 VisibilityTimerTabHelper::CreateForWebContents(web_contents);
162 VisibilityTimerTabHelper::FromWebContents(web_contents)
163 ->PostTaskAfterVisibleDelay(
164 FROM_HERE,
165 base::Bind(&NotificationPermissionContext::NotifyPermissionSet,
166 weak_factory_ui_thread_.GetWeakPtr(), id,
167 requesting_origin, embedding_origin, callback,
168 true /* persist */, CONTENT_SETTING_BLOCK),
169 base::TimeDelta::FromSecondsD(delay_seconds));
170 return;
171 }
172
173 PermissionContextBase::DecidePermission(web_contents, id, requesting_origin,
174 embedding_origin, user_gesture,
175 callback);
176 }
177
26 // Unlike other permission types, granting a notification for a given origin 178 // Unlike other permission types, granting a notification for a given origin
27 // will not take into account the |embedder_origin|, it will only be based 179 // will not take into account the |embedder_origin|, it will only be based
28 // on the requesting iframe origin. 180 // on the requesting iframe origin.
29 // TODO(mukai) Consider why notifications behave differently than 181 // TODO(mukai) Consider why notifications behave differently than
30 // other permissions. https://crbug.com/416894 182 // other permissions. https://crbug.com/416894
31 void NotificationPermissionContext::UpdateContentSetting( 183 void NotificationPermissionContext::UpdateContentSetting(
32 const GURL& requesting_origin, 184 const GURL& requesting_origin,
33 const GURL& embedder_origin, 185 const GURL& embedder_origin,
34 ContentSetting content_setting) { 186 ContentSetting content_setting) {
35 DCHECK(content_setting == CONTENT_SETTING_ALLOW || 187 DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
36 content_setting == CONTENT_SETTING_BLOCK); 188 content_setting == CONTENT_SETTING_BLOCK);
37 189
38 if (content_setting == CONTENT_SETTING_ALLOW) { 190 if (content_setting == CONTENT_SETTING_ALLOW) {
39 DesktopNotificationProfileUtil::GrantPermission(profile(), 191 DesktopNotificationProfileUtil::GrantPermission(profile(),
40 requesting_origin); 192 requesting_origin);
41 } else { 193 } else {
42 DesktopNotificationProfileUtil::DenyPermission(profile(), 194 DesktopNotificationProfileUtil::DenyPermission(profile(),
43 requesting_origin); 195 requesting_origin);
44 } 196 }
45 } 197 }
46 198
47 bool NotificationPermissionContext::IsRestrictedToSecureOrigins() const { 199 bool NotificationPermissionContext::IsRestrictedToSecureOrigins() const {
48 return false; 200 return false;
49 } 201 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698