Refactor ProtectedMediaIdentifierPermissionContext to derive from PermissionContextBase.
[chromium-blink-merge.git] / chrome / browser / content_settings / permission_context_base.cc
blob921170389d4488a9799c7d735f698ab18f85f0ad
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/content_settings/permission_context_base.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/content_settings/permission_bubble_request_impl.h"
10 #include "chrome/browser/content_settings/permission_context_uma_util.h"
11 #include "chrome/browser/content_settings/permission_queue_controller.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
14 #include "chrome/common/pref_names.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h"
16 #include "components/content_settings/core/common/permission_request_id.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/web_contents.h"
20 PermissionContextBase::PermissionContextBase(
21 Profile* profile,
22 const ContentSettingsType permission_type)
23 : profile_(profile),
24 permission_type_(permission_type),
25 weak_factory_(this) {
26 permission_queue_controller_.reset(
27 new PermissionQueueController(profile_, permission_type_));
30 PermissionContextBase::~PermissionContextBase() {
31 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
34 void PermissionContextBase::RequestPermission(
35 content::WebContents* web_contents,
36 const PermissionRequestID& id,
37 const GURL& requesting_frame,
38 bool user_gesture,
39 const BrowserPermissionCallback& callback) {
40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
42 DecidePermission(web_contents,
43 id,
44 requesting_frame.GetOrigin(),
45 web_contents->GetLastCommittedURL().GetOrigin(),
46 user_gesture,
47 callback);
50 ContentSetting PermissionContextBase::GetPermissionStatus(
51 const GURL& requesting_origin,
52 const GURL& embedding_origin) const {
53 return profile_->GetHostContentSettingsMap()->GetContentSetting(
54 requesting_origin, embedding_origin, permission_type_, std::string());
57 void PermissionContextBase::CancelPermissionRequest(
58 content::WebContents* web_contents,
59 const PermissionRequestID& id) {
60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
62 if (PermissionBubbleManager::Enabled()) {
63 PermissionBubbleRequest* cancelling =
64 pending_bubbles_.get(id.ToString());
65 if (cancelling != NULL && web_contents != NULL &&
66 PermissionBubbleManager::FromWebContents(web_contents) != NULL) {
67 PermissionBubbleManager::FromWebContents(web_contents)->
68 CancelRequest(cancelling);
70 return;
73 GetQueueController()->CancelInfoBarRequest(id);
76 void PermissionContextBase::DecidePermission(
77 content::WebContents* web_contents,
78 const PermissionRequestID& id,
79 const GURL& requesting_origin,
80 const GURL& embedding_origin,
81 bool user_gesture,
82 const BrowserPermissionCallback& callback) {
83 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
85 ContentSetting content_setting =
86 profile_->GetHostContentSettingsMap()
87 ->GetContentSettingAndMaybeUpdateLastUsage(
88 requesting_origin, embedding_origin, permission_type_,
89 std::string());
90 switch (content_setting) {
91 case CONTENT_SETTING_BLOCK:
92 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
93 false /* persist */, false /* granted */);
94 return;
95 case CONTENT_SETTING_ALLOW:
96 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
97 false /* persist */, true /* granted */);
98 return;
99 default:
100 break;
103 PermissionContextUmaUtil::PermissionRequested(
104 permission_type_, requesting_origin);
106 if (PermissionBubbleManager::Enabled()) {
107 PermissionBubbleManager* bubble_manager =
108 PermissionBubbleManager::FromWebContents(web_contents);
109 DCHECK(bubble_manager);
110 scoped_ptr<PermissionBubbleRequest> request_ptr(
111 new PermissionBubbleRequestImpl(
112 requesting_origin, user_gesture, permission_type_,
113 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages),
114 base::Bind(&PermissionContextBase::PermissionDecided,
115 weak_factory_.GetWeakPtr(), id, requesting_origin,
116 embedding_origin, callback),
117 base::Bind(&PermissionContextBase::CleanUpBubble,
118 weak_factory_.GetWeakPtr(), id)));
119 PermissionBubbleRequest* request = request_ptr.get();
121 bool inserted = pending_bubbles_.add(
122 id.ToString(), request_ptr.Pass()).second;
123 DCHECK(inserted) << "Duplicate id " << id.ToString();
124 bubble_manager->AddRequest(request);
125 return;
128 // TODO(gbillock): Delete this and the infobar delegate when
129 // we're using only bubbles. crbug.com/337458
130 GetQueueController()->CreateInfoBarRequest(
131 id, requesting_origin, embedding_origin,
132 base::Bind(&PermissionContextBase::PermissionDecided,
133 weak_factory_.GetWeakPtr(), id, requesting_origin,
134 embedding_origin, callback,
135 // the queue controller takes care of persisting the
136 // permission
137 false));
140 void PermissionContextBase::PermissionDecided(
141 const PermissionRequestID& id,
142 const GURL& requesting_origin,
143 const GURL& embedding_origin,
144 const BrowserPermissionCallback& callback,
145 bool persist,
146 bool allowed) {
147 // Infobar persistance and its related UMA is tracked on the infobar
148 // controller directly.
149 if (PermissionBubbleManager::Enabled()) {
150 if (persist) {
151 if (allowed)
152 PermissionContextUmaUtil::PermissionGranted(permission_type_,
153 requesting_origin);
154 else
155 PermissionContextUmaUtil::PermissionDenied(permission_type_,
156 requesting_origin);
157 } else {
158 PermissionContextUmaUtil::PermissionDismissed(permission_type_,
159 requesting_origin);
163 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
164 persist, allowed);
167 PermissionQueueController* PermissionContextBase::GetQueueController() {
168 return permission_queue_controller_.get();
171 Profile* PermissionContextBase::profile() const {
172 return profile_;
175 void PermissionContextBase::NotifyPermissionSet(
176 const PermissionRequestID& id,
177 const GURL& requesting_origin,
178 const GURL& embedding_origin,
179 const BrowserPermissionCallback& callback,
180 bool persist,
181 bool allowed) {
182 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
183 if (persist)
184 UpdateContentSetting(requesting_origin, embedding_origin, allowed);
186 UpdateTabContext(id, requesting_origin, allowed);
187 callback.Run(allowed);
190 void PermissionContextBase::CleanUpBubble(const PermissionRequestID& id) {
191 size_t success = pending_bubbles_.erase(id.ToString());
192 DCHECK(success == 1) << "Missing request " << id.ToString();
195 void PermissionContextBase::UpdateContentSetting(const GURL& requesting_origin,
196 const GURL& embedding_origin,
197 bool allowed) {
198 DCHECK_EQ(requesting_origin, requesting_origin.GetOrigin());
199 DCHECK_EQ(embedding_origin, embedding_origin.GetOrigin());
200 ContentSetting content_setting =
201 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
202 profile_->GetHostContentSettingsMap()->SetContentSetting(
203 ContentSettingsPattern::FromURLNoWildcard(requesting_origin),
204 ContentSettingsPattern::FromURLNoWildcard(embedding_origin),
205 permission_type_, std::string(), content_setting);