Chromium Code Reviews| Index: chrome/browser/permissions/permission_context_base.cc |
| diff --git a/chrome/browser/permissions/permission_context_base.cc b/chrome/browser/permissions/permission_context_base.cc |
| index fccd23005ca9d88386be7974a8d2d6555e1cfbe1..41b38b06d516e504c25fc9d37ed881198d9e64a7 100644 |
| --- a/chrome/browser/permissions/permission_context_base.cc |
| +++ b/chrome/browser/permissions/permission_context_base.cc |
| @@ -5,11 +5,18 @@ |
| #include "chrome/browser/permissions/permission_context_base.h" |
| #include <stddef.h> |
| + |
| +#include <queue> |
| #include <utility> |
| +#include "base/location.h" |
| #include "base/logging.h" |
| #include "base/prefs/pref_service.h" |
| +#include "base/rand_util.h" |
| +#include "base/single_thread_task_runner.h" |
| #include "base/strings/stringprintf.h" |
| +#include "base/thread_task_runner_handle.h" |
| +#include "base/timer/timer.h" |
| #include "build/build_config.h" |
| #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| #include "chrome/browser/permissions/permission_request_id.h" |
| @@ -17,12 +24,16 @@ |
| #include "chrome/browser/permissions/permission_util.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/common/pref_names.h" |
| +#include "components/content_settings/core/browser/content_settings_info.h" |
| +#include "components/content_settings/core/browser/content_settings_registry.h" |
| #include "components/content_settings/core/browser/host_content_settings_map.h" |
| #include "components/content_settings/core/browser/website_settings_registry.h" |
| #include "components/variations/variations_associated_data.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/render_frame_host.h" |
| #include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_contents_observer.h" |
| +#include "content/public/browser/web_contents_user_data.h" |
| #include "content/public/common/origin_util.h" |
| #if defined(OS_ANDROID) |
| @@ -32,6 +43,105 @@ |
| #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
| #endif |
| +namespace { |
| + |
| +// At most one of these is attached to each WebContents. |
| +class VisibilityTimerTabHelper |
| + : public content::WebContentsObserver, |
| + public content::WebContentsUserData<VisibilityTimerTabHelper> { |
| + public: |
| + ~VisibilityTimerTabHelper() override {}; |
| + |
| + // Runs |task| after the WebContents has been visible for a consecutive |
| + // duration of at least |visible_delay|. |
| + void PostTaskAfterVisibleDelay(const tracked_objects::Location& from_here, |
| + const base::Closure& task, |
| + base::TimeDelta visible_delay); |
| + |
| + // WebContentsObserver: |
| + void WasShown() override; |
| + void WasHidden() override; |
| + void WebContentsDestroyed() override; |
| + |
| + private: |
| + friend class content::WebContentsUserData<VisibilityTimerTabHelper>; |
| + explicit VisibilityTimerTabHelper(content::WebContents* contents); |
| + |
| + void RunTask(const base::Closure& task); |
| + |
| + bool is_visible_; |
| + std::queue<scoped_ptr<base::Timer>> task_queue_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(VisibilityTimerTabHelper); |
| +}; |
| + |
| +VisibilityTimerTabHelper::VisibilityTimerTabHelper( |
| + content::WebContents* contents) |
| + : content::WebContentsObserver(contents) { |
| + if (!contents->GetMainFrame()) { |
| + is_visible_ = false; |
| + } else { |
| + switch (contents->GetMainFrame()->GetVisibilityState()) { |
| + case blink::WebPageVisibilityStateHidden: |
| + case blink::WebPageVisibilityStatePrerender: |
| + is_visible_ = false; |
| + break; |
| + case blink::WebPageVisibilityStateVisible: |
| + is_visible_ = true; |
| + break; |
| + } |
| + } |
| +} |
| + |
| +void VisibilityTimerTabHelper::PostTaskAfterVisibleDelay( |
| + const tracked_objects::Location& from_here, |
| + const base::Closure& task, |
| + base::TimeDelta visible_delay) { |
| + // Safe to use Unretained, as destroying this will destroy task_queue_, hence |
| + // cancelling all timers. |
| + task_queue_.push(make_scoped_ptr(new base::Timer( |
| + from_here, visible_delay, base::Bind(&VisibilityTimerTabHelper::RunTask, |
| + base::Unretained(this), task), |
| + false /* is_repeating */))); |
| + DCHECK(!task_queue_.back()->IsRunning()); |
| + if (is_visible_ && task_queue_.size() == 1) |
| + task_queue_.front()->Reset(); |
| +} |
| + |
| +void VisibilityTimerTabHelper::WasShown() { |
| + if (!is_visible_ && !task_queue_.empty()) |
| + task_queue_.front()->Reset(); |
| + is_visible_ = true; |
| +} |
| + |
| +void VisibilityTimerTabHelper::WasHidden() { |
| + if (is_visible_ && !task_queue_.empty()) |
| + task_queue_.front()->Stop(); |
| + is_visible_ = false; |
| +} |
| + |
| +void VisibilityTimerTabHelper::WebContentsDestroyed() { |
| + // Delete ourselves, to avoid running tasks after WebContents is destroyed. |
| + web_contents()->RemoveUserData(UserDataKey()); |
| + // |this| has been deleted now. |
| +} |
| + |
| +void VisibilityTimerTabHelper::RunTask(const base::Closure& task) { |
| + DCHECK(is_visible_); |
| + task.Run(); |
| + task_queue_.pop(); |
| + if (!task_queue_.empty()) { |
| + task_queue_.front()->Reset(); |
| + return; |
| + } |
| + web_contents()->RemoveUserData(UserDataKey()); |
| + // |this| has been deleted now. |
| +} |
| + |
| +} // namespace |
| + |
| +DEFINE_WEB_CONTENTS_USER_DATA_KEY(VisibilityTimerTabHelper); |
| + |
| // static |
| const char PermissionContextBase::kPermissionsKillSwitchFieldStudy[] = |
| "PermissionsKillSwitch"; |
| @@ -169,6 +279,31 @@ void PermissionContextBase::DecidePermission( |
| const BrowserPermissionCallback& callback) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + // Some permissions are always denied in incognito. To prevent sites from |
|
mlamouri (slow - plz ping)
2016/01/11 16:03:57
Today, that's only Push, right? Do we have plans t
johnme
2016/01/11 16:29:20
Push and notifications. We probably won't add any
|
| + // using these to detect whether incognito mode is active, we deny after a |
| + // random time delay, to simulate a user clicking a bubble/infobar. |
| + if (profile()->IsOffTheRecord()) { |
| + const content_settings::ContentSettingsInfo* info = |
| + content_settings::ContentSettingsRegistry::GetInstance()->Get( |
| + content_settings_type_); |
|
mlamouri (slow - plz ping)
2016/01/11 16:03:57
Why are we getting the information from content se
johnme
2016/01/11 16:29:20
I could move this block of code to a NotificationP
johnme
2016/01/12 18:04:52
Ok, I've moved all this code from PermissionContex
|
| + if (info && |
| + info->incognito_behavior() == content_settings::ContentSettingsInfo:: |
| + DENY_IN_INCOGNITO_AFTER_DELAY) { |
| + // Random number of seconds in the range [1.0, 2.0). |
| + double delay_seconds = 1.0 + 1.0 * base::RandDouble(); |
| + VisibilityTimerTabHelper::CreateForWebContents(web_contents); |
| + VisibilityTimerTabHelper::FromWebContents(web_contents) |
| + ->PostTaskAfterVisibleDelay( |
| + FROM_HERE, |
| + base::Bind(&PermissionContextBase::NotifyPermissionSet, |
| + weak_factory_.GetWeakPtr(), id, requesting_origin, |
| + embedding_origin, callback, true /* persist */, |
| + CONTENT_SETTING_BLOCK), |
| + base::TimeDelta::FromSecondsD(delay_seconds)); |
| + return; |
| + } |
| + } |
| + |
| #if !defined(OS_ANDROID) |
| PermissionBubbleManager* bubble_manager = |
| PermissionBubbleManager::FromWebContents(web_contents); |