Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / permissions / permission_context_base.cc
blob7d2d173735b3b958bccf80622f54fe8cf523a56e
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/permissions/permission_context_base.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/permissions/permission_bubble_request_impl.h"
10 #include "chrome/browser/permissions/permission_context_uma_util.h"
11 #include "chrome/browser/permissions/permission_queue_controller.h"
12 #include "chrome/browser/permissions/permission_request_id.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/content_settings/core/browser/content_settings_utils.h"
17 #include "components/content_settings/core/browser/host_content_settings_map.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/origin_util.h"
22 PermissionContextBase::PermissionContextBase(
23 Profile* profile,
24 const ContentSettingsType permission_type)
25 : profile_(profile),
26 permission_type_(permission_type),
27 weak_factory_(this) {
28 permission_queue_controller_.reset(
29 new PermissionQueueController(profile_, permission_type_));
32 PermissionContextBase::~PermissionContextBase() {
33 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
36 void PermissionContextBase::RequestPermission(
37 content::WebContents* web_contents,
38 const PermissionRequestID& id,
39 const GURL& requesting_frame,
40 bool user_gesture,
41 const BrowserPermissionCallback& callback) {
42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44 DecidePermission(web_contents,
45 id,
46 requesting_frame.GetOrigin(),
47 web_contents->GetLastCommittedURL().GetOrigin(),
48 user_gesture,
49 callback);
52 ContentSetting PermissionContextBase::GetPermissionStatus(
53 const GURL& requesting_origin,
54 const GURL& embedding_origin) const {
55 if (IsRestrictedToSecureOrigins() &&
56 !content::IsOriginSecure(requesting_origin)) {
57 return CONTENT_SETTING_BLOCK;
60 return profile_->GetHostContentSettingsMap()->GetContentSetting(
61 requesting_origin, embedding_origin, permission_type_, std::string());
64 void PermissionContextBase::ResetPermission(
65 const GURL& requesting_origin,
66 const GURL& embedding_origin) {
67 profile_->GetHostContentSettingsMap()->SetContentSetting(
68 ContentSettingsPattern::FromURLNoWildcard(requesting_origin),
69 ContentSettingsPattern::FromURLNoWildcard(embedding_origin),
70 permission_type_, std::string(), CONTENT_SETTING_DEFAULT);
73 void PermissionContextBase::CancelPermissionRequest(
74 content::WebContents* web_contents,
75 const PermissionRequestID& id) {
76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
78 if (PermissionBubbleManager::Enabled()) {
79 PermissionBubbleRequest* cancelling =
80 pending_bubbles_.get(id.ToString());
81 if (cancelling != NULL && web_contents != NULL &&
82 PermissionBubbleManager::FromWebContents(web_contents) != NULL) {
83 PermissionBubbleManager::FromWebContents(web_contents)->
84 CancelRequest(cancelling);
86 return;
89 GetQueueController()->CancelInfoBarRequest(id);
92 void PermissionContextBase::DecidePermission(
93 content::WebContents* web_contents,
94 const PermissionRequestID& id,
95 const GURL& requesting_origin,
96 const GURL& embedding_origin,
97 bool user_gesture,
98 const BrowserPermissionCallback& callback) {
99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
101 if (!requesting_origin.is_valid() || !embedding_origin.is_valid()) {
102 DVLOG(1)
103 << "Attempt to use " << content_settings::GetTypeName(permission_type_)
104 << " from an invalid URL: " << requesting_origin
105 << "," << embedding_origin
106 << " (" << content_settings::GetTypeName(permission_type_)
107 << " is not supported in popups)";
108 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
109 false /* persist */, CONTENT_SETTING_BLOCK);
110 return;
113 if (IsRestrictedToSecureOrigins() &&
114 !content::IsOriginSecure(requesting_origin)) {
115 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
116 false /* persist */, CONTENT_SETTING_BLOCK);
117 return;
120 ContentSetting content_setting =
121 profile_->GetHostContentSettingsMap()
122 ->GetContentSettingAndMaybeUpdateLastUsage(
123 requesting_origin, embedding_origin, permission_type_,
124 std::string());
126 if (content_setting == CONTENT_SETTING_ALLOW ||
127 content_setting == CONTENT_SETTING_BLOCK) {
128 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
129 false /* persist */, content_setting);
130 return;
133 PermissionContextUmaUtil::PermissionRequested(
134 permission_type_, requesting_origin, embedding_origin, profile_);
136 if (PermissionBubbleManager::Enabled()) {
137 PermissionBubbleManager* bubble_manager =
138 PermissionBubbleManager::FromWebContents(web_contents);
139 DCHECK(bubble_manager);
140 scoped_ptr<PermissionBubbleRequest> request_ptr(
141 new PermissionBubbleRequestImpl(
142 requesting_origin, user_gesture, permission_type_,
143 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages),
144 base::Bind(&PermissionContextBase::PermissionDecided,
145 weak_factory_.GetWeakPtr(), id, requesting_origin,
146 embedding_origin, callback),
147 base::Bind(&PermissionContextBase::CleanUpBubble,
148 weak_factory_.GetWeakPtr(), id)));
149 PermissionBubbleRequest* request = request_ptr.get();
151 bool inserted = pending_bubbles_.add(
152 id.ToString(), request_ptr.Pass()).second;
153 DCHECK(inserted) << "Duplicate id " << id.ToString();
154 bubble_manager->AddRequest(request);
155 return;
158 // TODO(gbillock): Delete this and the infobar delegate when
159 // we're using only bubbles. crbug.com/337458
160 GetQueueController()->CreateInfoBarRequest(
161 id, requesting_origin, embedding_origin,
162 base::Bind(&PermissionContextBase::PermissionDecided,
163 weak_factory_.GetWeakPtr(), id, requesting_origin,
164 embedding_origin, callback,
165 // the queue controller takes care of persisting the
166 // permission
167 false));
170 void PermissionContextBase::PermissionDecided(
171 const PermissionRequestID& id,
172 const GURL& requesting_origin,
173 const GURL& embedding_origin,
174 const BrowserPermissionCallback& callback,
175 bool persist,
176 ContentSetting content_setting) {
177 // Infobar persistance and its related UMA is tracked on the infobar
178 // controller directly.
179 if (PermissionBubbleManager::Enabled()) {
180 if (persist) {
181 DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
182 content_setting == CONTENT_SETTING_BLOCK);
183 if (content_setting == CONTENT_SETTING_ALLOW)
184 PermissionContextUmaUtil::PermissionGranted(permission_type_,
185 requesting_origin);
186 else
187 PermissionContextUmaUtil::PermissionDenied(permission_type_,
188 requesting_origin);
189 } else {
190 DCHECK_EQ(content_setting, CONTENT_SETTING_DEFAULT);
191 PermissionContextUmaUtil::PermissionDismissed(permission_type_,
192 requesting_origin);
196 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
197 persist, content_setting);
200 PermissionQueueController* PermissionContextBase::GetQueueController() {
201 return permission_queue_controller_.get();
204 Profile* PermissionContextBase::profile() const {
205 return profile_;
208 void PermissionContextBase::NotifyPermissionSet(
209 const PermissionRequestID& id,
210 const GURL& requesting_origin,
211 const GURL& embedding_origin,
212 const BrowserPermissionCallback& callback,
213 bool persist,
214 ContentSetting content_setting) {
215 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
217 if (persist)
218 UpdateContentSetting(requesting_origin, embedding_origin, content_setting);
220 UpdateTabContext(id, requesting_origin,
221 content_setting == CONTENT_SETTING_ALLOW);
223 if (content_setting == CONTENT_SETTING_DEFAULT) {
224 content_setting =
225 profile_->GetHostContentSettingsMap()->GetDefaultContentSetting(
226 permission_type_, nullptr);
229 DCHECK_NE(content_setting, CONTENT_SETTING_DEFAULT);
230 callback.Run(content_setting);
233 void PermissionContextBase::CleanUpBubble(const PermissionRequestID& id) {
234 size_t success = pending_bubbles_.erase(id.ToString());
235 DCHECK(success == 1) << "Missing request " << id.ToString();
238 void PermissionContextBase::UpdateContentSetting(
239 const GURL& requesting_origin,
240 const GURL& embedding_origin,
241 ContentSetting content_setting) {
242 DCHECK_EQ(requesting_origin, requesting_origin.GetOrigin());
243 DCHECK_EQ(embedding_origin, embedding_origin.GetOrigin());
244 DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
245 content_setting == CONTENT_SETTING_BLOCK);
247 profile_->GetHostContentSettingsMap()->SetContentSetting(
248 ContentSettingsPattern::FromURLNoWildcard(requesting_origin),
249 ContentSettingsPattern::FromURLNoWildcard(embedding_origin),
250 permission_type_, std::string(), content_setting);