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

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

Issue 1702633002: Fix crash due to VisibilityTimerTabHelper not being cancelled (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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> 7 #include <algorithm>
8 #include <deque>
8 9
9 #include "base/callback.h" 10 #include "base/callback.h"
10 #include "base/location.h" 11 #include "base/location.h"
11 #include "base/rand_util.h" 12 #include "base/rand_util.h"
12 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h" 14 #include "base/thread_task_runner_handle.h"
14 #include "base/timer/timer.h" 15 #include "base/timer/timer.h"
15 #include "chrome/browser/notifications/desktop_notification_profile_util.h" 16 #include "chrome/browser/notifications/desktop_notification_profile_util.h"
16 #include "chrome/browser/permissions/permission_request_id.h" 17 #include "chrome/browser/permissions/permission_request_id.h"
17 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
(...skipping 13 matching lines...) Expand all
31 class VisibilityTimerTabHelper 32 class VisibilityTimerTabHelper
32 : public content::WebContentsObserver, 33 : public content::WebContentsObserver,
33 public content::WebContentsUserData<VisibilityTimerTabHelper> { 34 public content::WebContentsUserData<VisibilityTimerTabHelper> {
34 public: 35 public:
35 ~VisibilityTimerTabHelper() override {} 36 ~VisibilityTimerTabHelper() override {}
36 37
37 // Runs |task| after the WebContents has been visible for a consecutive 38 // Runs |task| after the WebContents has been visible for a consecutive
38 // duration of at least |visible_delay|. 39 // duration of at least |visible_delay|.
39 void PostTaskAfterVisibleDelay(const tracked_objects::Location& from_here, 40 void PostTaskAfterVisibleDelay(const tracked_objects::Location& from_here,
40 const base::Closure& task, 41 const base::Closure& task,
41 base::TimeDelta visible_delay); 42 base::TimeDelta visible_delay,
43 const PermissionRequestID& id);
44
45 void CancelTask(const PermissionRequestID& id);
Peter Beverloo 2016/02/16 15:28:28 +docs
johnme 2016/02/16 18:14:34 Done.
42 46
43 // WebContentsObserver: 47 // WebContentsObserver:
44 void WasShown() override; 48 void WasShown() override;
45 void WasHidden() override; 49 void WasHidden() override;
46 void WebContentsDestroyed() override; 50 void WebContentsDestroyed() override;
47 51
48 private: 52 private:
49 friend class content::WebContentsUserData<VisibilityTimerTabHelper>; 53 friend class content::WebContentsUserData<VisibilityTimerTabHelper>;
50 explicit VisibilityTimerTabHelper(content::WebContents* contents); 54 explicit VisibilityTimerTabHelper(content::WebContents* contents);
51 55
52 void RunTask(const base::Closure& task); 56 void RunTask(const base::Closure& task);
53 57
54 bool is_visible_; 58 bool is_visible_;
55 std::queue<scoped_ptr<base::Timer>> task_queue_; 59
60 typedef std::pair<PermissionRequestID, scoped_ptr<base::Timer>> TaskEntry;
Peter Beverloo 2016/02/16 15:28:28 Could you define a small structure instead? |task_
johnme 2016/02/16 18:14:34 Done.
61 std::deque<TaskEntry> task_queue_;
56 62
57 DISALLOW_COPY_AND_ASSIGN(VisibilityTimerTabHelper); 63 DISALLOW_COPY_AND_ASSIGN(VisibilityTimerTabHelper);
58 }; 64 };
59 65
60 VisibilityTimerTabHelper::VisibilityTimerTabHelper( 66 VisibilityTimerTabHelper::VisibilityTimerTabHelper(
61 content::WebContents* contents) 67 content::WebContents* contents)
62 : content::WebContentsObserver(contents) { 68 : content::WebContentsObserver(contents) {
63 if (!contents->GetMainFrame()) { 69 if (!contents->GetMainFrame()) {
64 is_visible_ = false; 70 is_visible_ = false;
65 } else { 71 } else {
66 switch (contents->GetMainFrame()->GetVisibilityState()) { 72 switch (contents->GetMainFrame()->GetVisibilityState()) {
67 case blink::WebPageVisibilityStateHidden: 73 case blink::WebPageVisibilityStateHidden:
68 case blink::WebPageVisibilityStatePrerender: 74 case blink::WebPageVisibilityStatePrerender:
69 is_visible_ = false; 75 is_visible_ = false;
70 break; 76 break;
71 case blink::WebPageVisibilityStateVisible: 77 case blink::WebPageVisibilityStateVisible:
72 is_visible_ = true; 78 is_visible_ = true;
73 break; 79 break;
74 } 80 }
75 } 81 }
76 } 82 }
77 83
78 void VisibilityTimerTabHelper::PostTaskAfterVisibleDelay( 84 void VisibilityTimerTabHelper::PostTaskAfterVisibleDelay(
79 const tracked_objects::Location& from_here, 85 const tracked_objects::Location& from_here,
80 const base::Closure& task, 86 const base::Closure& task,
81 base::TimeDelta visible_delay) { 87 base::TimeDelta visible_delay,
88 const PermissionRequestID& id) {
82 // Safe to use Unretained, as destroying this will destroy task_queue_, hence 89 // Safe to use Unretained, as destroying this will destroy task_queue_, hence
83 // cancelling all timers. 90 // cancelling all timers.
84 task_queue_.push(make_scoped_ptr(new base::Timer( 91 task_queue_.push_back(
85 from_here, visible_delay, base::Bind(&VisibilityTimerTabHelper::RunTask, 92 std::make_pair(id, make_scoped_ptr(new base::Timer(
86 base::Unretained(this), task), 93 from_here, visible_delay,
87 false /* is_repeating */))); 94 base::Bind(&VisibilityTimerTabHelper::RunTask,
88 DCHECK(!task_queue_.back()->IsRunning()); 95 base::Unretained(this), task),
96 false /* is_repeating */))));
Peter Beverloo 2016/02/16 15:28:28 nit: readability ===== scoped_ptr<base::Timer>
johnme 2016/02/16 18:14:34 Done.
97 DCHECK(!task_queue_.back().second->IsRunning());
89 if (is_visible_ && task_queue_.size() == 1) 98 if (is_visible_ && task_queue_.size() == 1)
90 task_queue_.front()->Reset(); 99 task_queue_.front().second->Reset();
100 }
101
102 void VisibilityTimerTabHelper::CancelTask(const PermissionRequestID& id) {
103 bool deleting_front = task_queue_.front().first == id;
104 task_queue_.erase(std::remove_if(task_queue_.begin(), task_queue_.end(),
105 [id](const TaskEntry& entry) {
106 return entry.first == id;
107 }),
108 task_queue_.end());
109 if (!task_queue_.empty()) {
110 if (is_visible_ && deleting_front)
111 task_queue_.front().second->Reset();
112 return;
113 }
114 web_contents()->RemoveUserData(UserDataKey());
Peter Beverloo 2016/02/16 15:28:28 What is the significance of eagerly deleting |this
johnme 2016/02/16 18:14:34 It seemed neater at the time, but it adds a bunch
115 // |this| has been deleted now.
91 } 116 }
92 117
93 void VisibilityTimerTabHelper::WasShown() { 118 void VisibilityTimerTabHelper::WasShown() {
94 if (!is_visible_ && !task_queue_.empty()) 119 if (!is_visible_ && !task_queue_.empty())
95 task_queue_.front()->Reset(); 120 task_queue_.front().second->Reset();
96 is_visible_ = true; 121 is_visible_ = true;
97 } 122 }
98 123
99 void VisibilityTimerTabHelper::WasHidden() { 124 void VisibilityTimerTabHelper::WasHidden() {
100 if (is_visible_ && !task_queue_.empty()) 125 if (is_visible_ && !task_queue_.empty())
101 task_queue_.front()->Stop(); 126 task_queue_.front().second->Stop();
102 is_visible_ = false; 127 is_visible_ = false;
103 } 128 }
104 129
105 void VisibilityTimerTabHelper::WebContentsDestroyed() { 130 void VisibilityTimerTabHelper::WebContentsDestroyed() {
106 // Delete ourselves, to avoid running tasks after WebContents is destroyed. 131 // Delete ourselves, to avoid running tasks after WebContents is destroyed.
107 web_contents()->RemoveUserData(UserDataKey()); 132 web_contents()->RemoveUserData(UserDataKey());
108 // |this| has been deleted now. 133 // |this| has been deleted now.
109 } 134 }
110 135
111 void VisibilityTimerTabHelper::RunTask(const base::Closure& task) { 136 void VisibilityTimerTabHelper::RunTask(const base::Closure& task) {
112 DCHECK(is_visible_); 137 DCHECK(is_visible_);
113 task.Run(); 138 task.Run();
114 task_queue_.pop(); 139 task_queue_.pop_front();
115 if (!task_queue_.empty()) { 140 if (!task_queue_.empty()) {
116 task_queue_.front()->Reset(); 141 task_queue_.front().second->Reset();
117 return; 142 return;
118 } 143 }
119 web_contents()->RemoveUserData(UserDataKey()); 144 web_contents()->RemoveUserData(UserDataKey());
120 // |this| has been deleted now. 145 // |this| has been deleted now.
121 } 146 }
122 147
123 } // namespace 148 } // namespace
124 149
125 DEFINE_WEB_CONTENTS_USER_DATA_KEY(VisibilityTimerTabHelper); 150 DEFINE_WEB_CONTENTS_USER_DATA_KEY(VisibilityTimerTabHelper);
126 151
127 NotificationPermissionContext::NotificationPermissionContext(Profile* profile) 152 NotificationPermissionContext::NotificationPermissionContext(Profile* profile)
128 : PermissionContextBase(profile, 153 : PermissionContextBase(profile,
129 content::PermissionType::NOTIFICATIONS, 154 content::PermissionType::NOTIFICATIONS,
130 CONTENT_SETTINGS_TYPE_NOTIFICATIONS), 155 CONTENT_SETTINGS_TYPE_NOTIFICATIONS),
131 weak_factory_ui_thread_(this) {} 156 weak_factory_ui_thread_(this) {}
132 157
133 NotificationPermissionContext::~NotificationPermissionContext() {} 158 NotificationPermissionContext::~NotificationPermissionContext() {}
134 159
135 void NotificationPermissionContext::ResetPermission( 160 void NotificationPermissionContext::ResetPermission(
136 const GURL& requesting_origin, 161 const GURL& requesting_origin,
137 const GURL& embedder_origin) { 162 const GURL& embedder_origin) {
138 DesktopNotificationProfileUtil::ClearSetting( 163 DesktopNotificationProfileUtil::ClearSetting(
139 profile(), ContentSettingsPattern::FromURLNoWildcard(requesting_origin)); 164 profile(), ContentSettingsPattern::FromURLNoWildcard(requesting_origin));
140 } 165 }
141 166
167 void NotificationPermissionContext::CancelPermissionRequest(
168 content::WebContents* web_contents,
169 const PermissionRequestID& id) {
170 if (profile()->IsOffTheRecord()) {
171 VisibilityTimerTabHelper::FromWebContents(web_contents)->CancelTask(id);
172 } else {
173 PermissionContextBase::CancelPermissionRequest(web_contents, id);
174 }
175 }
176
142 void NotificationPermissionContext::DecidePermission( 177 void NotificationPermissionContext::DecidePermission(
143 content::WebContents* web_contents, 178 content::WebContents* web_contents,
144 const PermissionRequestID& id, 179 const PermissionRequestID& id,
145 const GURL& requesting_origin, 180 const GURL& requesting_origin,
146 const GURL& embedding_origin, 181 const GURL& embedding_origin,
147 bool user_gesture, 182 bool user_gesture,
148 const BrowserPermissionCallback& callback) { 183 const BrowserPermissionCallback& callback) {
149 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 184 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
150 185
151 // Notifications permission is always denied in incognito. To prevent sites 186 // 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 187 // 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 188 // random time delay, to simulate a user clicking a bubble/infobar. See also
154 // ContentSettingsRegistry::Init, which marks notifications as 189 // ContentSettingsRegistry::Init, which marks notifications as
155 // INHERIT_IN_INCOGNITO_EXCEPT_ALLOW, and 190 // INHERIT_IN_INCOGNITO_EXCEPT_ALLOW, and
156 // PermissionMenuModel::PermissionMenuModel which prevents users from manually 191 // PermissionMenuModel::PermissionMenuModel which prevents users from manually
157 // allowing the permission. 192 // allowing the permission.
158 if (profile()->IsOffTheRecord()) { 193 if (profile()->IsOffTheRecord()) {
159 // Random number of seconds in the range [1.0, 2.0). 194 // Random number of seconds in the range [1.0, 2.0).
160 double delay_seconds = 1.0 + 1.0 * base::RandDouble(); 195 double delay_seconds = 1.0 + 1.0 * base::RandDouble();
161 VisibilityTimerTabHelper::CreateForWebContents(web_contents); 196 VisibilityTimerTabHelper::CreateForWebContents(web_contents);
162 VisibilityTimerTabHelper::FromWebContents(web_contents) 197 VisibilityTimerTabHelper::FromWebContents(web_contents)
163 ->PostTaskAfterVisibleDelay( 198 ->PostTaskAfterVisibleDelay(
164 FROM_HERE, 199 FROM_HERE,
165 base::Bind(&NotificationPermissionContext::NotifyPermissionSet, 200 base::Bind(&NotificationPermissionContext::NotifyPermissionSet,
166 weak_factory_ui_thread_.GetWeakPtr(), id, 201 weak_factory_ui_thread_.GetWeakPtr(), id,
167 requesting_origin, embedding_origin, callback, 202 requesting_origin, embedding_origin, callback,
168 true /* persist */, CONTENT_SETTING_BLOCK), 203 true /* persist */, CONTENT_SETTING_BLOCK),
169 base::TimeDelta::FromSecondsD(delay_seconds)); 204 base::TimeDelta::FromSecondsD(delay_seconds), id);
170 return; 205 return;
171 } 206 }
172 207
173 PermissionContextBase::DecidePermission(web_contents, id, requesting_origin, 208 PermissionContextBase::DecidePermission(web_contents, id, requesting_origin,
174 embedding_origin, user_gesture, 209 embedding_origin, user_gesture,
175 callback); 210 callback);
176 } 211 }
177 212
178 // Unlike other permission types, granting a notification for a given origin 213 // Unlike other permission types, granting a notification for a given origin
179 // will not take into account the |embedder_origin|, it will only be based 214 // will not take into account the |embedder_origin|, it will only be based
(...skipping 12 matching lines...) Expand all
192 requesting_origin); 227 requesting_origin);
193 } else { 228 } else {
194 DesktopNotificationProfileUtil::DenyPermission(profile(), 229 DesktopNotificationProfileUtil::DenyPermission(profile(),
195 requesting_origin); 230 requesting_origin);
196 } 231 }
197 } 232 }
198 233
199 bool NotificationPermissionContext::IsRestrictedToSecureOrigins() const { 234 bool NotificationPermissionContext::IsRestrictedToSecureOrigins() const {
200 return false; 235 return false;
201 } 236 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698