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