Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / content_settings / permission_context_base.cc
blobc1883776af64f114038e73a0b0f19acf94540c15
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/content_settings_utils.h"
16 #include "components/content_settings/core/browser/host_content_settings_map.h"
17 #include "components/content_settings/core/common/permission_request_id.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/web_contents.h"
21 PermissionContextBase::PermissionContextBase(
22 Profile* profile,
23 const ContentSettingsType permission_type)
24 : profile_(profile),
25 permission_type_(permission_type),
26 weak_factory_(this) {
27 permission_queue_controller_.reset(
28 new PermissionQueueController(profile_, permission_type_));
31 PermissionContextBase::~PermissionContextBase() {
32 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
35 void PermissionContextBase::RequestPermission(
36 content::WebContents* web_contents,
37 const PermissionRequestID& id,
38 const GURL& requesting_frame,
39 bool user_gesture,
40 const BrowserPermissionCallback& callback) {
41 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
43 DecidePermission(web_contents,
44 id,
45 requesting_frame.GetOrigin(),
46 web_contents->GetLastCommittedURL().GetOrigin(),
47 user_gesture,
48 callback);
51 ContentSetting PermissionContextBase::GetPermissionStatus(
52 const GURL& requesting_origin,
53 const GURL& embedding_origin) const {
54 return profile_->GetHostContentSettingsMap()->GetContentSetting(
55 requesting_origin, embedding_origin, permission_type_, std::string());
58 void PermissionContextBase::ResetPermission(
59 const GURL& requesting_origin,
60 const GURL& embedding_origin) {
61 profile_->GetHostContentSettingsMap()->SetContentSetting(
62 ContentSettingsPattern::FromURLNoWildcard(requesting_origin),
63 ContentSettingsPattern::FromURLNoWildcard(embedding_origin),
64 permission_type_, std::string(), CONTENT_SETTING_DEFAULT);
67 void PermissionContextBase::CancelPermissionRequest(
68 content::WebContents* web_contents,
69 const PermissionRequestID& id) {
70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
72 if (PermissionBubbleManager::Enabled()) {
73 PermissionBubbleRequest* cancelling =
74 pending_bubbles_.get(id.ToString());
75 if (cancelling != NULL && web_contents != NULL &&
76 PermissionBubbleManager::FromWebContents(web_contents) != NULL) {
77 PermissionBubbleManager::FromWebContents(web_contents)->
78 CancelRequest(cancelling);
80 return;
83 GetQueueController()->CancelInfoBarRequest(id);
86 void PermissionContextBase::DecidePermission(
87 content::WebContents* web_contents,
88 const PermissionRequestID& id,
89 const GURL& requesting_origin,
90 const GURL& embedding_origin,
91 bool user_gesture,
92 const BrowserPermissionCallback& callback) {
93 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
95 if (!requesting_origin.is_valid() || !embedding_origin.is_valid()) {
96 DVLOG(1)
97 << "Attempt to use " << content_settings::GetTypeName(permission_type_)
98 << " from an invalid URL: " << requesting_origin
99 << "," << embedding_origin
100 << " (" << content_settings::GetTypeName(permission_type_)
101 << " is not supported in popups)";
102 NotifyPermissionSet(id, requesting_origin, embedding_origin,
103 callback, false /* persist */, false /* granted */);
104 return;
107 ContentSetting content_setting =
108 profile_->GetHostContentSettingsMap()
109 ->GetContentSettingAndMaybeUpdateLastUsage(
110 requesting_origin, embedding_origin, permission_type_,
111 std::string());
113 switch (content_setting) {
114 case CONTENT_SETTING_BLOCK:
115 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
116 false /* persist */, false /* granted */);
117 return;
118 case CONTENT_SETTING_ALLOW:
119 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
120 false /* persist */, true /* granted */);
121 return;
122 default:
123 break;
126 PermissionContextUmaUtil::PermissionRequested(
127 permission_type_, requesting_origin);
129 if (PermissionBubbleManager::Enabled()) {
130 if (pending_bubbles_.get(id.ToString()) != NULL)
131 return;
132 PermissionBubbleManager* bubble_manager =
133 PermissionBubbleManager::FromWebContents(web_contents);
134 DCHECK(bubble_manager);
135 scoped_ptr<PermissionBubbleRequest> request_ptr(
136 new PermissionBubbleRequestImpl(
137 requesting_origin, user_gesture, permission_type_,
138 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages),
139 base::Bind(&PermissionContextBase::PermissionDecided,
140 weak_factory_.GetWeakPtr(), id, requesting_origin,
141 embedding_origin, callback),
142 base::Bind(&PermissionContextBase::CleanUpBubble,
143 weak_factory_.GetWeakPtr(), id)));
144 PermissionBubbleRequest* request = request_ptr.get();
146 bool inserted = pending_bubbles_.add(
147 id.ToString(), request_ptr.Pass()).second;
148 DCHECK(inserted) << "Duplicate id " << id.ToString();
149 bubble_manager->AddRequest(request);
150 return;
153 // TODO(gbillock): Delete this and the infobar delegate when
154 // we're using only bubbles. crbug.com/337458
155 GetQueueController()->CreateInfoBarRequest(
156 id, requesting_origin, embedding_origin,
157 base::Bind(&PermissionContextBase::PermissionDecided,
158 weak_factory_.GetWeakPtr(), id, requesting_origin,
159 embedding_origin, callback,
160 // the queue controller takes care of persisting the
161 // permission
162 false));
165 void PermissionContextBase::PermissionDecided(
166 const PermissionRequestID& id,
167 const GURL& requesting_origin,
168 const GURL& embedding_origin,
169 const BrowserPermissionCallback& callback,
170 bool persist,
171 bool allowed) {
172 // Infobar persistance and its related UMA is tracked on the infobar
173 // controller directly.
174 if (PermissionBubbleManager::Enabled()) {
175 if (persist) {
176 if (allowed)
177 PermissionContextUmaUtil::PermissionGranted(permission_type_,
178 requesting_origin);
179 else
180 PermissionContextUmaUtil::PermissionDenied(permission_type_,
181 requesting_origin);
182 } else {
183 PermissionContextUmaUtil::PermissionDismissed(permission_type_,
184 requesting_origin);
188 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
189 persist, allowed);
192 PermissionQueueController* PermissionContextBase::GetQueueController() {
193 return permission_queue_controller_.get();
196 Profile* PermissionContextBase::profile() const {
197 return profile_;
200 void PermissionContextBase::NotifyPermissionSet(
201 const PermissionRequestID& id,
202 const GURL& requesting_origin,
203 const GURL& embedding_origin,
204 const BrowserPermissionCallback& callback,
205 bool persist,
206 bool allowed) {
207 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
208 if (persist)
209 UpdateContentSetting(requesting_origin, embedding_origin, allowed);
211 UpdateTabContext(id, requesting_origin, allowed);
212 callback.Run(allowed);
215 void PermissionContextBase::CleanUpBubble(const PermissionRequestID& id) {
216 size_t success = pending_bubbles_.erase(id.ToString());
217 DCHECK(success == 1) << "Missing request " << id.ToString();
220 void PermissionContextBase::UpdateContentSetting(const GURL& requesting_origin,
221 const GURL& embedding_origin,
222 bool allowed) {
223 DCHECK_EQ(requesting_origin, requesting_origin.GetOrigin());
224 DCHECK_EQ(embedding_origin, embedding_origin.GetOrigin());
225 ContentSetting content_setting =
226 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
227 profile_->GetHostContentSettingsMap()->SetContentSetting(
228 ContentSettingsPattern::FromURLNoWildcard(requesting_origin),
229 ContentSettingsPattern::FromURLNoWildcard(embedding_origin),
230 permission_type_, std::string(), content_setting);