1 // Copyright (c) 2012 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/geolocation/chrome_geolocation_permission_context.h"
11 #include "base/bind.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/content_settings/host_content_settings_map.h"
15 #include "chrome/browser/content_settings/permission_request_id.h"
16 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
17 #include "chrome/browser/extensions/suggest_permission_util.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/tab_contents/tab_util.h"
20 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
21 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
22 #include "chrome/common/pref_names.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/render_view_host.h"
25 #include "content/public/browser/web_contents.h"
26 #include "extensions/browser/extension_registry.h"
27 #include "extensions/browser/process_map.h"
28 #include "extensions/browser/view_type_utils.h"
29 #include "extensions/common/extension.h"
30 #include "grit/generated_resources.h"
31 #include "grit/theme_resources.h"
32 #include "net/base/net_util.h"
33 #include "ui/base/l10n/l10n_util.h"
35 using extensions::APIPermission;
36 using extensions::ExtensionRegistry;
38 class GeolocationPermissionRequest : public PermissionBubbleRequest {
40 GeolocationPermissionRequest(
41 ChromeGeolocationPermissionContext* context,
42 const PermissionRequestID& id,
43 const GURL& requesting_frame,
44 base::Callback<void(bool)> callback,
45 const std::string& display_languages);
46 virtual ~GeolocationPermissionRequest();
48 // PermissionBubbleDelegate:
49 virtual int GetIconID() const OVERRIDE;
50 virtual base::string16 GetMessageText() const OVERRIDE;
51 virtual base::string16 GetMessageTextFragment() const OVERRIDE;
52 virtual bool HasUserGesture() const OVERRIDE;
53 virtual GURL GetRequestingHostname() const OVERRIDE;
54 virtual void PermissionGranted() OVERRIDE;
55 virtual void PermissionDenied() OVERRIDE;
56 virtual void Cancelled() OVERRIDE;
57 virtual void RequestFinished() OVERRIDE;
60 ChromeGeolocationPermissionContext* context_;
61 PermissionRequestID id_;
62 GURL requesting_frame_;
63 base::Callback<void(bool)> callback_;
64 std::string display_languages_;
67 GeolocationPermissionRequest::GeolocationPermissionRequest(
68 ChromeGeolocationPermissionContext* context,
69 const PermissionRequestID& id,
70 const GURL& requesting_frame,
71 base::Callback<void(bool)> callback,
72 const std::string& display_languages)
75 requesting_frame_(requesting_frame),
77 display_languages_(display_languages) {}
79 GeolocationPermissionRequest::~GeolocationPermissionRequest() {}
81 int GeolocationPermissionRequest::GetIconID() const {
82 return IDR_INFOBAR_GEOLOCATION;
85 base::string16 GeolocationPermissionRequest::GetMessageText() const {
86 return l10n_util::GetStringFUTF16(IDS_GEOLOCATION_INFOBAR_QUESTION,
87 net::FormatUrl(requesting_frame_, display_languages_));
90 base::string16 GeolocationPermissionRequest::GetMessageTextFragment() const {
91 return l10n_util::GetStringUTF16(IDS_GEOLOCATION_INFOBAR_PERMISSION_FRAGMENT);
94 bool GeolocationPermissionRequest::HasUserGesture() const {
95 // TODO(gbillock): plumb this through from GeolocationDispatcher.
99 GURL GeolocationPermissionRequest::GetRequestingHostname() const {
100 return requesting_frame_;
103 void GeolocationPermissionRequest::PermissionGranted() {
104 context_->NotifyPermissionSet(id_, requesting_frame_, callback_, true);
107 void GeolocationPermissionRequest::PermissionDenied() {
108 context_->NotifyPermissionSet(id_, requesting_frame_, callback_, false);
111 void GeolocationPermissionRequest::Cancelled() {
112 context_->NotifyPermissionSet(id_, requesting_frame_, callback_, false);
115 void GeolocationPermissionRequest::RequestFinished() {
120 ChromeGeolocationPermissionContext::ChromeGeolocationPermissionContext(
123 shutting_down_(false) {
126 ChromeGeolocationPermissionContext::~ChromeGeolocationPermissionContext() {
127 // ChromeGeolocationPermissionContext may be destroyed on either the UI thread
128 // or the IO thread, but the PermissionQueueController must have been
129 // destroyed on the UI thread.
130 DCHECK(!permission_queue_controller_.get());
133 void ChromeGeolocationPermissionContext::RequestGeolocationPermission(
134 int render_process_id,
137 const GURL& requesting_frame,
138 base::Callback<void(bool)> callback) {
139 GURL requesting_frame_origin = requesting_frame.GetOrigin();
140 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
141 content::BrowserThread::PostTask(
142 content::BrowserThread::UI, FROM_HERE,
144 &ChromeGeolocationPermissionContext::RequestGeolocationPermission,
145 this, render_process_id, render_view_id, bridge_id,
146 requesting_frame_origin, callback));
150 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
154 content::WebContents* web_contents =
155 tab_util::GetWebContentsByID(render_process_id, render_view_id);
156 const PermissionRequestID id(render_process_id, render_view_id, bridge_id, 0);
157 ExtensionRegistry* extension_registry = ExtensionRegistry::Get(profile_);
158 if (extension_registry) {
159 const extensions::Extension* extension =
160 extension_registry->enabled_extensions().GetExtensionOrAppByURL(
161 requesting_frame_origin);
162 if (IsExtensionWithPermissionOrSuggestInConsole(APIPermission::kGeolocation,
165 // Make sure the extension is in the calling process.
166 if (extensions::ProcessMap::Get(profile_)
167 ->Contains(extension->id(), id.render_process_id())) {
168 NotifyPermissionSet(id, requesting_frame_origin, callback, true);
174 if (extensions::GetViewType(web_contents) !=
175 extensions::VIEW_TYPE_TAB_CONTENTS) {
176 // The tab may have gone away, or the request may not be from a tab at all.
177 // TODO(mpcomplete): the request could be from a background page or
178 // extension popup (web_contents will have a different ViewType). But why do
179 // we care? Shouldn't we still put an infobar up in the current tab?
180 LOG(WARNING) << "Attempt to use geolocation tabless renderer: "
182 << " (can't prompt user without a visible tab)";
183 NotifyPermissionSet(id, requesting_frame_origin, callback, false);
187 GURL embedder = web_contents->GetLastCommittedURL().GetOrigin();
188 if (!requesting_frame_origin.is_valid() || !embedder.is_valid()) {
189 LOG(WARNING) << "Attempt to use geolocation from an invalid URL: "
190 << requesting_frame_origin << "," << embedder
191 << " (geolocation is not supported in popups)";
192 NotifyPermissionSet(id, requesting_frame_origin, callback, false);
196 DecidePermission(web_contents, id, requesting_frame_origin,
197 embedder, "", callback);
200 void ChromeGeolocationPermissionContext::CancelGeolocationPermissionRequest(
201 int render_process_id,
204 const GURL& requesting_frame) {
205 // TODO(gbillock): cancel permission bubble request.
206 CancelPendingInfobarRequest(PermissionRequestID(
207 render_process_id, render_view_id, bridge_id, 0));
210 void ChromeGeolocationPermissionContext::DecidePermission(
211 content::WebContents* web_contents,
212 const PermissionRequestID& id,
213 const GURL& requesting_frame,
214 const GURL& embedder,
215 const std::string& accept_button_label,
216 base::Callback<void(bool)> callback) {
217 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
219 ContentSetting content_setting =
220 profile_->GetHostContentSettingsMap()->GetContentSetting(
221 requesting_frame, embedder, CONTENT_SETTINGS_TYPE_GEOLOCATION,
223 switch (content_setting) {
224 case CONTENT_SETTING_BLOCK:
225 PermissionDecided(id, requesting_frame, embedder, callback, false);
227 case CONTENT_SETTING_ALLOW:
228 PermissionDecided(id, requesting_frame, embedder, callback, true);
231 if (PermissionBubbleManager::Enabled()) {
232 PermissionBubbleManager* mgr =
233 PermissionBubbleManager::FromWebContents(web_contents);
234 mgr->AddRequest(new GeolocationPermissionRequest(
235 this, id, requesting_frame, callback,
236 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)));
238 // setting == ask. Prompt the user.
239 QueueController()->CreateInfoBarRequest(
240 id, requesting_frame, embedder, accept_button_label,
242 &ChromeGeolocationPermissionContext::NotifyPermissionSet,
243 base::Unretained(this), id, requesting_frame, callback));
248 void ChromeGeolocationPermissionContext::CreateInfoBarRequest(
249 const PermissionRequestID& id,
250 const GURL& requesting_frame,
251 const GURL& embedder,
252 const std::string accept_button_label,
253 base::Callback<void(bool)> callback) {
254 QueueController()->CreateInfoBarRequest(
255 id, requesting_frame, embedder, accept_button_label, base::Bind(
256 &ChromeGeolocationPermissionContext::NotifyPermissionSet,
257 base::Unretained(this), id, requesting_frame, callback));
260 void ChromeGeolocationPermissionContext::ShutdownOnUIThread() {
261 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
262 permission_queue_controller_.reset();
263 shutting_down_ = true;
266 void ChromeGeolocationPermissionContext::PermissionDecided(
267 const PermissionRequestID& id,
268 const GURL& requesting_frame,
269 const GURL& embedder,
270 base::Callback<void(bool)> callback,
272 NotifyPermissionSet(id, requesting_frame, callback, allowed);
275 void ChromeGeolocationPermissionContext::NotifyPermissionSet(
276 const PermissionRequestID& id,
277 const GURL& requesting_frame,
278 base::Callback<void(bool)> callback,
280 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
282 // WebContents may have gone away (or not exists for extension).
283 TabSpecificContentSettings* content_settings =
284 TabSpecificContentSettings::Get(id.render_process_id(),
285 id.render_view_id());
286 if (content_settings) {
287 content_settings->OnGeolocationPermissionSet(requesting_frame.GetOrigin(),
291 callback.Run(allowed);
294 PermissionQueueController*
295 ChromeGeolocationPermissionContext::QueueController() {
296 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
297 DCHECK(!shutting_down_);
298 if (!permission_queue_controller_)
299 permission_queue_controller_.reset(CreateQueueController());
300 return permission_queue_controller_.get();
303 PermissionQueueController*
304 ChromeGeolocationPermissionContext::CreateQueueController() {
305 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
306 return new PermissionQueueController(profile(),
307 CONTENT_SETTINGS_TYPE_GEOLOCATION);
310 void ChromeGeolocationPermissionContext::CancelPendingInfobarRequest(
311 const PermissionRequestID& id) {
312 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
313 content::BrowserThread::PostTask(
314 content::BrowserThread::UI, FROM_HERE,
316 &ChromeGeolocationPermissionContext::CancelPendingInfobarRequest,
320 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
324 // TODO(gbillock): handle permission bubble cancellation.
325 QueueController()->CancelInfoBarRequest(id);