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

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: Remove unclear std::move 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 // Deletes any earlier task(s) that match |id|.
46 void CancelTask(const PermissionRequestID& id);
42 47
43 // WebContentsObserver: 48 // WebContentsObserver:
44 void WasShown() override; 49 void WasShown() override;
45 void WasHidden() override; 50 void WasHidden() override;
46 void WebContentsDestroyed() override; 51 void WebContentsDestroyed() override;
47 52
48 private: 53 private:
49 friend class content::WebContentsUserData<VisibilityTimerTabHelper>; 54 friend class content::WebContentsUserData<VisibilityTimerTabHelper>;
50 explicit VisibilityTimerTabHelper(content::WebContents* contents); 55 explicit VisibilityTimerTabHelper(content::WebContents* contents);
51 56
52 void RunTask(const base::Closure& task); 57 void RunTask(const base::Closure& task);
53 58
54 bool is_visible_; 59 bool is_visible_;
55 std::queue<scoped_ptr<base::Timer>> task_queue_; 60
61 struct Task {
62 Task(const PermissionRequestID& id, scoped_ptr<base::Timer> timer)
63 : id(id), timer(std::move(timer)) {}
64
65 Task& operator=(Task&& other) {
66 id = other.id;
67 timer = std::move(other.timer);
68 return *this;
69 }
70
71 PermissionRequestID id;
72 scoped_ptr<base::Timer> timer;
73
74 private:
75 DISALLOW_COPY_AND_ASSIGN(Task);
76 };
77 std::deque<Task> task_queue_;
56 78
57 DISALLOW_COPY_AND_ASSIGN(VisibilityTimerTabHelper); 79 DISALLOW_COPY_AND_ASSIGN(VisibilityTimerTabHelper);
58 }; 80 };
59 81
60 VisibilityTimerTabHelper::VisibilityTimerTabHelper( 82 VisibilityTimerTabHelper::VisibilityTimerTabHelper(
61 content::WebContents* contents) 83 content::WebContents* contents)
62 : content::WebContentsObserver(contents) { 84 : content::WebContentsObserver(contents) {
63 if (!contents->GetMainFrame()) { 85 if (!contents->GetMainFrame()) {
64 is_visible_ = false; 86 is_visible_ = false;
65 } else { 87 } else {
66 switch (contents->GetMainFrame()->GetVisibilityState()) { 88 switch (contents->GetMainFrame()->GetVisibilityState()) {
67 case blink::WebPageVisibilityStateHidden: 89 case blink::WebPageVisibilityStateHidden:
68 case blink::WebPageVisibilityStatePrerender: 90 case blink::WebPageVisibilityStatePrerender:
69 is_visible_ = false; 91 is_visible_ = false;
70 break; 92 break;
71 case blink::WebPageVisibilityStateVisible: 93 case blink::WebPageVisibilityStateVisible:
72 is_visible_ = true; 94 is_visible_ = true;
73 break; 95 break;
74 } 96 }
75 } 97 }
76 } 98 }
77 99
78 void VisibilityTimerTabHelper::PostTaskAfterVisibleDelay( 100 void VisibilityTimerTabHelper::PostTaskAfterVisibleDelay(
79 const tracked_objects::Location& from_here, 101 const tracked_objects::Location& from_here,
80 const base::Closure& task, 102 const base::Closure& task,
81 base::TimeDelta visible_delay) { 103 base::TimeDelta visible_delay,
104 const PermissionRequestID& id) {
105 if (web_contents()->IsBeingDestroyed())
106 return;
107
82 // Safe to use Unretained, as destroying this will destroy task_queue_, hence 108 // Safe to use Unretained, as destroying this will destroy task_queue_, hence
83 // cancelling all timers. 109 // cancelling all timers.
84 task_queue_.push(make_scoped_ptr(new base::Timer( 110 scoped_ptr<base::Timer> timer(new base::Timer(
85 from_here, visible_delay, base::Bind(&VisibilityTimerTabHelper::RunTask, 111 from_here, visible_delay, base::Bind(&VisibilityTimerTabHelper::RunTask,
86 base::Unretained(this), task), 112 base::Unretained(this), task),
87 false /* is_repeating */))); 113 false /* is_repeating */));
88 DCHECK(!task_queue_.back()->IsRunning()); 114 DCHECK(!timer->IsRunning());
115
116 task_queue_.emplace_back(id, std::move(timer));
117
89 if (is_visible_ && task_queue_.size() == 1) 118 if (is_visible_ && task_queue_.size() == 1)
90 task_queue_.front()->Reset(); 119 task_queue_.front().timer->Reset();
120 }
121
122 void VisibilityTimerTabHelper::CancelTask(const PermissionRequestID& id) {
123 bool deleting_front = task_queue_.front().id == id;
124
125 task_queue_.erase(
126 std::remove_if(task_queue_.begin(), task_queue_.end(),
127 [id](const Task& task) { return task.id == id; }),
128 task_queue_.end());
129
130 if (!task_queue_.empty() && is_visible_ && deleting_front)
131 task_queue_.front().timer->Reset();
91 } 132 }
92 133
93 void VisibilityTimerTabHelper::WasShown() { 134 void VisibilityTimerTabHelper::WasShown() {
94 if (!is_visible_ && !task_queue_.empty()) 135 if (!is_visible_ && !task_queue_.empty())
95 task_queue_.front()->Reset(); 136 task_queue_.front().timer->Reset();
96 is_visible_ = true; 137 is_visible_ = true;
97 } 138 }
98 139
99 void VisibilityTimerTabHelper::WasHidden() { 140 void VisibilityTimerTabHelper::WasHidden() {
100 if (is_visible_ && !task_queue_.empty()) 141 if (is_visible_ && !task_queue_.empty())
101 task_queue_.front()->Stop(); 142 task_queue_.front().timer->Stop();
102 is_visible_ = false; 143 is_visible_ = false;
103 } 144 }
104 145
105 void VisibilityTimerTabHelper::WebContentsDestroyed() { 146 void VisibilityTimerTabHelper::WebContentsDestroyed() {
106 // Delete ourselves, to avoid running tasks after WebContents is destroyed. 147 task_queue_.clear();
107 web_contents()->RemoveUserData(UserDataKey());
108 // |this| has been deleted now.
109 } 148 }
110 149
111 void VisibilityTimerTabHelper::RunTask(const base::Closure& task) { 150 void VisibilityTimerTabHelper::RunTask(const base::Closure& task) {
112 DCHECK(is_visible_); 151 DCHECK(is_visible_);
113 task.Run(); 152 task.Run();
114 task_queue_.pop(); 153 task_queue_.pop_front();
115 if (!task_queue_.empty()) { 154 if (!task_queue_.empty())
116 task_queue_.front()->Reset(); 155 task_queue_.front().timer->Reset();
117 return;
118 }
119 web_contents()->RemoveUserData(UserDataKey());
120 // |this| has been deleted now.
121 } 156 }
122 157
123 } // namespace 158 } // namespace
124 159
125 DEFINE_WEB_CONTENTS_USER_DATA_KEY(VisibilityTimerTabHelper); 160 DEFINE_WEB_CONTENTS_USER_DATA_KEY(VisibilityTimerTabHelper);
126 161
127 NotificationPermissionContext::NotificationPermissionContext(Profile* profile) 162 NotificationPermissionContext::NotificationPermissionContext(Profile* profile)
128 : PermissionContextBase(profile, 163 : PermissionContextBase(profile,
129 content::PermissionType::NOTIFICATIONS, 164 content::PermissionType::NOTIFICATIONS,
130 CONTENT_SETTINGS_TYPE_NOTIFICATIONS), 165 CONTENT_SETTINGS_TYPE_NOTIFICATIONS),
131 weak_factory_ui_thread_(this) {} 166 weak_factory_ui_thread_(this) {}
132 167
133 NotificationPermissionContext::~NotificationPermissionContext() {} 168 NotificationPermissionContext::~NotificationPermissionContext() {}
134 169
135 void NotificationPermissionContext::ResetPermission( 170 void NotificationPermissionContext::ResetPermission(
136 const GURL& requesting_origin, 171 const GURL& requesting_origin,
137 const GURL& embedder_origin) { 172 const GURL& embedder_origin) {
138 DesktopNotificationProfileUtil::ClearSetting( 173 DesktopNotificationProfileUtil::ClearSetting(
139 profile(), ContentSettingsPattern::FromURLNoWildcard(requesting_origin)); 174 profile(), ContentSettingsPattern::FromURLNoWildcard(requesting_origin));
140 } 175 }
141 176
177 void NotificationPermissionContext::CancelPermissionRequest(
178 content::WebContents* web_contents,
179 const PermissionRequestID& id) {
180 if (profile()->IsOffTheRecord()) {
181 VisibilityTimerTabHelper::FromWebContents(web_contents)->CancelTask(id);
182 } else {
183 PermissionContextBase::CancelPermissionRequest(web_contents, id);
184 }
185 }
186
142 void NotificationPermissionContext::DecidePermission( 187 void NotificationPermissionContext::DecidePermission(
143 content::WebContents* web_contents, 188 content::WebContents* web_contents,
144 const PermissionRequestID& id, 189 const PermissionRequestID& id,
145 const GURL& requesting_origin, 190 const GURL& requesting_origin,
146 const GURL& embedding_origin, 191 const GURL& embedding_origin,
147 bool user_gesture, 192 bool user_gesture,
148 const BrowserPermissionCallback& callback) { 193 const BrowserPermissionCallback& callback) {
149 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 194 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
150 195
151 // Notifications permission is always denied in incognito. To prevent sites 196 // 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 197 // 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 198 // random time delay, to simulate a user clicking a bubble/infobar. See also
154 // ContentSettingsRegistry::Init, which marks notifications as 199 // ContentSettingsRegistry::Init, which marks notifications as
155 // INHERIT_IN_INCOGNITO_EXCEPT_ALLOW, and 200 // INHERIT_IN_INCOGNITO_EXCEPT_ALLOW, and
156 // PermissionMenuModel::PermissionMenuModel which prevents users from manually 201 // PermissionMenuModel::PermissionMenuModel which prevents users from manually
157 // allowing the permission. 202 // allowing the permission.
158 if (profile()->IsOffTheRecord()) { 203 if (profile()->IsOffTheRecord()) {
159 // Random number of seconds in the range [1.0, 2.0). 204 // Random number of seconds in the range [1.0, 2.0).
160 double delay_seconds = 1.0 + 1.0 * base::RandDouble(); 205 double delay_seconds = 1.0 + 1.0 * base::RandDouble();
161 VisibilityTimerTabHelper::CreateForWebContents(web_contents); 206 VisibilityTimerTabHelper::CreateForWebContents(web_contents);
162 VisibilityTimerTabHelper::FromWebContents(web_contents) 207 VisibilityTimerTabHelper::FromWebContents(web_contents)
163 ->PostTaskAfterVisibleDelay( 208 ->PostTaskAfterVisibleDelay(
164 FROM_HERE, 209 FROM_HERE,
165 base::Bind(&NotificationPermissionContext::NotifyPermissionSet, 210 base::Bind(&NotificationPermissionContext::NotifyPermissionSet,
166 weak_factory_ui_thread_.GetWeakPtr(), id, 211 weak_factory_ui_thread_.GetWeakPtr(), id,
167 requesting_origin, embedding_origin, callback, 212 requesting_origin, embedding_origin, callback,
168 true /* persist */, CONTENT_SETTING_BLOCK), 213 true /* persist */, CONTENT_SETTING_BLOCK),
169 base::TimeDelta::FromSecondsD(delay_seconds)); 214 base::TimeDelta::FromSecondsD(delay_seconds), id);
170 return; 215 return;
171 } 216 }
172 217
173 PermissionContextBase::DecidePermission(web_contents, id, requesting_origin, 218 PermissionContextBase::DecidePermission(web_contents, id, requesting_origin,
174 embedding_origin, user_gesture, 219 embedding_origin, user_gesture,
175 callback); 220 callback);
176 } 221 }
177 222
178 // Unlike other permission types, granting a notification for a given origin 223 // 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 224 // will not take into account the |embedder_origin|, it will only be based
(...skipping 12 matching lines...) Expand all
192 requesting_origin); 237 requesting_origin);
193 } else { 238 } else {
194 DesktopNotificationProfileUtil::DenyPermission(profile(), 239 DesktopNotificationProfileUtil::DenyPermission(profile(),
195 requesting_origin); 240 requesting_origin);
196 } 241 }
197 } 242 }
198 243
199 bool NotificationPermissionContext::IsRestrictedToSecureOrigins() const { 244 bool NotificationPermissionContext::IsRestrictedToSecureOrigins() const {
200 return false; 245 return false;
201 } 246 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698