GoogleURLTrackerInfoBarDelegate: Initialize uninitialized member in constructor.
[chromium-blink-merge.git] / chrome / browser / geolocation / chrome_geolocation_permission_context.cc
blobf65d36463ae051dcab5cf4d957cae60d8a91004b
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"
7 #include <functional>
8 #include <string>
9 #include <vector>
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/guest_view/web_view/web_view_guest.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/tab_contents/tab_util.h"
21 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
22 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
23 #include "chrome/common/pref_names.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/render_process_host.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/browser/web_contents.h"
28 #include "extensions/browser/extension_registry.h"
29 #include "extensions/browser/process_map.h"
30 #include "extensions/browser/view_type_utils.h"
31 #include "extensions/common/extension.h"
32 #include "grit/generated_resources.h"
33 #include "grit/theme_resources.h"
34 #include "net/base/net_util.h"
35 #include "ui/base/l10n/l10n_util.h"
37 using extensions::APIPermission;
38 using extensions::ExtensionRegistry;
40 class GeolocationPermissionRequest : public PermissionBubbleRequest {
41 public:
42 GeolocationPermissionRequest(
43 ChromeGeolocationPermissionContext* context,
44 const PermissionRequestID& id,
45 const GURL& requesting_frame,
46 bool user_gesture,
47 base::Callback<void(bool)> callback,
48 const std::string& display_languages);
49 virtual ~GeolocationPermissionRequest();
51 // PermissionBubbleDelegate:
52 virtual int GetIconID() const OVERRIDE;
53 virtual base::string16 GetMessageText() const OVERRIDE;
54 virtual base::string16 GetMessageTextFragment() const OVERRIDE;
55 virtual bool HasUserGesture() const OVERRIDE;
56 virtual GURL GetRequestingHostname() const OVERRIDE;
57 virtual void PermissionGranted() OVERRIDE;
58 virtual void PermissionDenied() OVERRIDE;
59 virtual void Cancelled() OVERRIDE;
60 virtual void RequestFinished() OVERRIDE;
62 private:
63 ChromeGeolocationPermissionContext* context_;
64 PermissionRequestID id_;
65 GURL requesting_frame_;
66 bool user_gesture_;
67 base::Callback<void(bool)> callback_;
68 std::string display_languages_;
71 GeolocationPermissionRequest::GeolocationPermissionRequest(
72 ChromeGeolocationPermissionContext* context,
73 const PermissionRequestID& id,
74 const GURL& requesting_frame,
75 bool user_gesture,
76 base::Callback<void(bool)> callback,
77 const std::string& display_languages)
78 : context_(context),
79 id_(id),
80 requesting_frame_(requesting_frame),
81 user_gesture_(user_gesture),
82 callback_(callback),
83 display_languages_(display_languages) {}
85 GeolocationPermissionRequest::~GeolocationPermissionRequest() {}
87 int GeolocationPermissionRequest::GetIconID() const {
88 return IDR_INFOBAR_GEOLOCATION;
91 base::string16 GeolocationPermissionRequest::GetMessageText() const {
92 return l10n_util::GetStringFUTF16(IDS_GEOLOCATION_INFOBAR_QUESTION,
93 net::FormatUrl(requesting_frame_, display_languages_));
96 base::string16 GeolocationPermissionRequest::GetMessageTextFragment() const {
97 return l10n_util::GetStringUTF16(IDS_GEOLOCATION_INFOBAR_PERMISSION_FRAGMENT);
100 bool GeolocationPermissionRequest::HasUserGesture() const {
101 return user_gesture_;
104 GURL GeolocationPermissionRequest::GetRequestingHostname() const {
105 return requesting_frame_;
108 void GeolocationPermissionRequest::PermissionGranted() {
109 context_->NotifyPermissionSet(id_, requesting_frame_, callback_, true);
112 void GeolocationPermissionRequest::PermissionDenied() {
113 context_->NotifyPermissionSet(id_, requesting_frame_, callback_, false);
116 void GeolocationPermissionRequest::Cancelled() {
119 void GeolocationPermissionRequest::RequestFinished() {
120 // Deletes 'this'.
121 context_->RequestFinished(this);
125 ChromeGeolocationPermissionContext::ChromeGeolocationPermissionContext(
126 Profile* profile)
127 : profile_(profile),
128 shutting_down_(false) {
131 ChromeGeolocationPermissionContext::~ChromeGeolocationPermissionContext() {
132 // ChromeGeolocationPermissionContext may be destroyed on either the UI thread
133 // or the IO thread, but the PermissionQueueController must have been
134 // destroyed on the UI thread.
135 DCHECK(!permission_queue_controller_.get());
138 void ChromeGeolocationPermissionContext::RequestGeolocationPermission(
139 content::WebContents* web_contents,
140 int bridge_id,
141 const GURL& requesting_frame,
142 bool user_gesture,
143 base::Callback<void(bool)> callback) {
144 GURL requesting_frame_origin = requesting_frame.GetOrigin();
145 if (shutting_down_)
146 return;
148 WebViewGuest* guest = WebViewGuest::FromWebContents(web_contents);
149 if (guest) {
150 guest->RequestGeolocationPermission(bridge_id,
151 requesting_frame,
152 user_gesture,
153 callback);
154 return;
157 int render_process_id = web_contents->GetRenderProcessHost()->GetID();
158 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID();
159 const PermissionRequestID id(
160 render_process_id, render_view_id, bridge_id, GURL());
161 ExtensionRegistry* extension_registry = ExtensionRegistry::Get(profile_);
162 if (extension_registry) {
163 const extensions::Extension* extension =
164 extension_registry->enabled_extensions().GetExtensionOrAppByURL(
165 requesting_frame_origin);
166 if (IsExtensionWithPermissionOrSuggestInConsole(
167 APIPermission::kGeolocation, extension,
168 web_contents->GetRenderViewHost())) {
169 // Make sure the extension is in the calling process.
170 if (extensions::ProcessMap::Get(profile_)
171 ->Contains(extension->id(), id.render_process_id())) {
172 NotifyPermissionSet(id, requesting_frame_origin, callback, true);
173 return;
178 if (extensions::GetViewType(web_contents) !=
179 extensions::VIEW_TYPE_TAB_CONTENTS) {
180 // The tab may have gone away, or the request may not be from a tab at all.
181 // TODO(mpcomplete): the request could be from a background page or
182 // extension popup (web_contents will have a different ViewType). But why do
183 // we care? Shouldn't we still put an infobar up in the current tab?
184 LOG(WARNING) << "Attempt to use geolocation tabless renderer: "
185 << id.ToString()
186 << " (can't prompt user without a visible tab)";
187 NotifyPermissionSet(id, requesting_frame_origin, callback, false);
188 return;
191 GURL embedder = web_contents->GetLastCommittedURL().GetOrigin();
192 if (!requesting_frame_origin.is_valid() || !embedder.is_valid()) {
193 LOG(WARNING) << "Attempt to use geolocation from an invalid URL: "
194 << requesting_frame_origin << "," << embedder
195 << " (geolocation is not supported in popups)";
196 NotifyPermissionSet(id, requesting_frame_origin, callback, false);
197 return;
200 DecidePermission(web_contents, id, requesting_frame_origin, user_gesture,
201 embedder, "", callback);
204 void ChromeGeolocationPermissionContext::CancelGeolocationPermissionRequest(
205 content::WebContents* web_contents,
206 int bridge_id,
207 const GURL& requesting_frame) {
208 WebViewGuest* guest =
209 web_contents ? WebViewGuest::FromWebContents(web_contents) : NULL;
210 if (guest) {
211 guest->CancelGeolocationPermissionRequest(bridge_id);
212 return;
214 int render_process_id = web_contents->GetRenderProcessHost()->GetID();
215 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID();
216 CancelPendingInfobarRequest(PermissionRequestID(
217 render_process_id, render_view_id, bridge_id, GURL()));
220 void ChromeGeolocationPermissionContext::DecidePermission(
221 content::WebContents* web_contents,
222 const PermissionRequestID& id,
223 const GURL& requesting_frame,
224 bool user_gesture,
225 const GURL& embedder,
226 const std::string& accept_button_label,
227 base::Callback<void(bool)> callback) {
228 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
230 ContentSetting content_setting =
231 profile_->GetHostContentSettingsMap()->GetContentSetting(
232 requesting_frame, embedder, CONTENT_SETTINGS_TYPE_GEOLOCATION,
233 std::string());
234 switch (content_setting) {
235 case CONTENT_SETTING_BLOCK:
236 PermissionDecided(id, requesting_frame, embedder, callback, false);
237 break;
238 case CONTENT_SETTING_ALLOW:
239 PermissionDecided(id, requesting_frame, embedder, callback, true);
240 break;
241 default:
242 if (PermissionBubbleManager::Enabled()) {
243 PermissionBubbleManager* mgr =
244 PermissionBubbleManager::FromWebContents(web_contents);
245 if (mgr) {
246 scoped_ptr<GeolocationPermissionRequest> request_ptr(
247 new GeolocationPermissionRequest(
248 this, id, requesting_frame, user_gesture, callback,
249 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)));
250 GeolocationPermissionRequest* request = request_ptr.get();
251 pending_requests_.add(id.ToString(), request_ptr.Pass());
252 mgr->AddRequest(request);
254 } else {
255 // setting == ask. Prompt the user.
256 QueueController()->CreateInfoBarRequest(
257 id, requesting_frame, embedder, accept_button_label,
258 base::Bind(
259 &ChromeGeolocationPermissionContext::NotifyPermissionSet,
260 base::Unretained(this), id, requesting_frame, callback));
265 void ChromeGeolocationPermissionContext::CreateInfoBarRequest(
266 const PermissionRequestID& id,
267 const GURL& requesting_frame,
268 const GURL& embedder,
269 const std::string accept_button_label,
270 base::Callback<void(bool)> callback) {
271 QueueController()->CreateInfoBarRequest(
272 id, requesting_frame, embedder, accept_button_label, base::Bind(
273 &ChromeGeolocationPermissionContext::NotifyPermissionSet,
274 base::Unretained(this), id, requesting_frame, callback));
277 void ChromeGeolocationPermissionContext::RequestFinished(
278 GeolocationPermissionRequest* request) {
279 base::ScopedPtrHashMap<std::string,
280 GeolocationPermissionRequest>::iterator it;
281 for (it = pending_requests_.begin(); it != pending_requests_.end(); it++) {
282 if (it->second == request) {
283 pending_requests_.take_and_erase(it);
284 return;
290 void ChromeGeolocationPermissionContext::ShutdownOnUIThread() {
291 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
292 permission_queue_controller_.reset();
293 shutting_down_ = true;
296 void ChromeGeolocationPermissionContext::PermissionDecided(
297 const PermissionRequestID& id,
298 const GURL& requesting_frame,
299 const GURL& embedder,
300 base::Callback<void(bool)> callback,
301 bool allowed) {
302 NotifyPermissionSet(id, requesting_frame, callback, allowed);
305 void ChromeGeolocationPermissionContext::NotifyPermissionSet(
306 const PermissionRequestID& id,
307 const GURL& requesting_frame,
308 base::Callback<void(bool)> callback,
309 bool allowed) {
310 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
312 // WebContents may have gone away (or not exists for extension).
313 TabSpecificContentSettings* content_settings =
314 TabSpecificContentSettings::Get(id.render_process_id(),
315 id.render_view_id());
316 if (content_settings) {
317 content_settings->OnGeolocationPermissionSet(requesting_frame.GetOrigin(),
318 allowed);
321 callback.Run(allowed);
324 PermissionQueueController*
325 ChromeGeolocationPermissionContext::QueueController() {
326 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
327 DCHECK(!shutting_down_);
328 if (!permission_queue_controller_)
329 permission_queue_controller_.reset(CreateQueueController());
330 return permission_queue_controller_.get();
333 PermissionQueueController*
334 ChromeGeolocationPermissionContext::CreateQueueController() {
335 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
336 return new PermissionQueueController(profile(),
337 CONTENT_SETTINGS_TYPE_GEOLOCATION);
340 void ChromeGeolocationPermissionContext::CancelPendingInfobarRequest(
341 const PermissionRequestID& id) {
342 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
343 if (shutting_down_)
344 return;
346 if (PermissionBubbleManager::Enabled()) {
347 GeolocationPermissionRequest* cancelling =
348 pending_requests_.get(id.ToString());
349 content::WebContents* web_contents = tab_util::GetWebContentsByID(
350 id.render_process_id(), id.render_view_id());
351 if (cancelling != NULL && web_contents != NULL &&
352 PermissionBubbleManager::FromWebContents(web_contents) != NULL) {
353 PermissionBubbleManager::FromWebContents(web_contents)->
354 CancelRequest(cancelling);
356 return;
359 QueueController()->CancelInfoBarRequest(id);