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/utf_string_conversions.h"
13 #include "chrome/browser/content_settings/host_content_settings_map.h"
14 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/extension_system.h"
17 #include "chrome/browser/extensions/suggest_permission_util.h"
18 #include "chrome/browser/geolocation/geolocation_infobar_queue_controller.h"
19 #include "chrome/browser/geolocation/geolocation_permission_request_id.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/tab_contents/tab_util.h"
22 #include "chrome/common/extensions/extension.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/view_type_utils.h"
27 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
30 using extensions::APIPermission
;
32 ChromeGeolocationPermissionContext::ChromeGeolocationPermissionContext(
35 shutting_down_(false) {
38 ChromeGeolocationPermissionContext::~ChromeGeolocationPermissionContext() {
39 // ChromeGeolocationPermissionContext may be destroyed on either the UI thread
40 // or the IO thread, but the GeolocationInfobarQueueController must have been
41 // destroyed on the UI thread.
42 DCHECK(!geolocation_infobar_queue_controller_
.get());
45 void ChromeGeolocationPermissionContext::RequestGeolocationPermission(
46 int render_process_id
,
49 const GURL
& requesting_frame
,
50 base::Callback
<void(bool)> callback
) {
51 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
)) {
52 content::BrowserThread::PostTask(
53 content::BrowserThread::UI
, FROM_HERE
,
55 &ChromeGeolocationPermissionContext::RequestGeolocationPermission
,
56 this, render_process_id
, render_view_id
, bridge_id
,
57 requesting_frame
, callback
));
61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
65 content::WebContents
* web_contents
=
66 tab_util::GetWebContentsByID(render_process_id
, render_view_id
);
67 const GeolocationPermissionRequestID
id(render_process_id
, render_view_id
,
69 ExtensionService
* extension_service
=
70 extensions::ExtensionSystem::Get(profile_
)->extension_service();
71 if (extension_service
) {
72 const extensions::Extension
* extension
=
73 extension_service
->extensions()->GetExtensionOrAppByURL(
74 ExtensionURLInfo(WebKit::WebSecurityOrigin::createFromString(
75 UTF8ToUTF16(requesting_frame
.spec())),
77 if (IsExtensionWithPermissionOrSuggestInConsole(APIPermission::kGeolocation
,
80 // Make sure the extension is in the calling process.
81 if (extension_service
->process_map()->Contains(extension
->id(),
82 id
.render_process_id())) {
83 NotifyPermissionSet(id
, requesting_frame
, callback
, true);
89 if (extensions::GetViewType(web_contents
) !=
90 extensions::VIEW_TYPE_TAB_CONTENTS
) {
91 // The tab may have gone away, or the request may not be from a tab at all.
92 // TODO(mpcomplete): the request could be from a background page or
93 // extension popup (web_contents will have a different ViewType). But why do
94 // we care? Shouldn't we still put an infobar up in the current tab?
95 LOG(WARNING
) << "Attempt to use geolocation tabless renderer: "
97 << " (can't prompt user without a visible tab)";
98 NotifyPermissionSet(id
, requesting_frame
, callback
, false);
102 GURL embedder
= web_contents
->GetURL();
103 if (!requesting_frame
.is_valid() || !embedder
.is_valid()) {
104 LOG(WARNING
) << "Attempt to use geolocation from an invalid URL: "
105 << requesting_frame
<< "," << embedder
106 << " (geolocation is not supported in popups)";
107 NotifyPermissionSet(id
, requesting_frame
, callback
, false);
111 DecidePermission(id
, requesting_frame
, embedder
, callback
);
114 void ChromeGeolocationPermissionContext::CancelGeolocationPermissionRequest(
115 int render_process_id
,
118 const GURL
& requesting_frame
) {
119 CancelPendingInfoBarRequest(GeolocationPermissionRequestID(
120 render_process_id
, render_view_id
, bridge_id
));
123 void ChromeGeolocationPermissionContext::DecidePermission(
124 const GeolocationPermissionRequestID
& id
,
125 const GURL
& requesting_frame
,
126 const GURL
& embedder
,
127 base::Callback
<void(bool)> callback
) {
128 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
130 ContentSetting content_setting
=
131 profile_
->GetHostContentSettingsMap()->GetContentSetting(
132 requesting_frame
, embedder
, CONTENT_SETTINGS_TYPE_GEOLOCATION
,
134 switch (content_setting
) {
135 case CONTENT_SETTING_BLOCK
:
136 PermissionDecided(id
, requesting_frame
, embedder
, callback
, false);
138 case CONTENT_SETTING_ALLOW
:
139 PermissionDecided(id
, requesting_frame
, embedder
, callback
, true);
142 // setting == ask. Prompt the user.
143 QueueController()->CreateInfoBarRequest(
144 id
, requesting_frame
, embedder
, base::Bind(
145 &ChromeGeolocationPermissionContext::NotifyPermissionSet
,
146 base::Unretained(this), id
, requesting_frame
, callback
));
150 void ChromeGeolocationPermissionContext::ShutdownOnUIThread() {
151 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
152 geolocation_infobar_queue_controller_
.reset();
153 shutting_down_
= true;
156 void ChromeGeolocationPermissionContext::PermissionDecided(
157 const GeolocationPermissionRequestID
& id
,
158 const GURL
& requesting_frame
,
159 const GURL
& embedder
,
160 base::Callback
<void(bool)> callback
,
162 NotifyPermissionSet(id
, requesting_frame
, callback
, allowed
);
165 void ChromeGeolocationPermissionContext::NotifyPermissionSet(
166 const GeolocationPermissionRequestID
& id
,
167 const GURL
& requesting_frame
,
168 base::Callback
<void(bool)> callback
,
170 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
172 // WebContents may have gone away (or not exists for extension).
173 TabSpecificContentSettings
* content_settings
=
174 TabSpecificContentSettings::Get(id
.render_process_id(),
175 id
.render_view_id());
176 if (content_settings
) {
177 content_settings
->OnGeolocationPermissionSet(requesting_frame
.GetOrigin(),
181 callback
.Run(allowed
);
184 GeolocationInfoBarQueueController
*
185 ChromeGeolocationPermissionContext::QueueController() {
186 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
187 DCHECK(!shutting_down_
);
188 if (!geolocation_infobar_queue_controller_
)
189 geolocation_infobar_queue_controller_
.reset(CreateQueueController());
190 return geolocation_infobar_queue_controller_
.get();
193 GeolocationInfoBarQueueController
*
194 ChromeGeolocationPermissionContext::CreateQueueController() {
195 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
196 return new GeolocationInfoBarQueueController(profile());
199 void ChromeGeolocationPermissionContext::CancelPendingInfoBarRequest(
200 const GeolocationPermissionRequestID
& id
) {
201 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
)) {
202 content::BrowserThread::PostTask(
203 content::BrowserThread::UI
, FROM_HERE
,
205 &ChromeGeolocationPermissionContext::CancelPendingInfoBarRequest
,
209 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
212 QueueController()->CancelInfoBarRequest(id
);