| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h" | |
| 6 | |
| 7 #include <functional> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 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/profiles/profile.h" | |
| 18 #include "chrome/browser/tab_contents/tab_util.h" | |
| 19 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | |
| 20 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" | |
| 21 #include "chrome/common/pref_names.h" | |
| 22 #include "content/public/browser/browser_thread.h" | |
| 23 #include "content/public/browser/render_process_host.h" | |
| 24 #include "content/public/browser/render_view_host.h" | |
| 25 #include "content/public/browser/web_contents.h" | |
| 26 #include "grit/generated_resources.h" | |
| 27 #include "grit/theme_resources.h" | |
| 28 #include "net/base/net_util.h" | |
| 29 #include "ui/base/l10n/l10n_util.h" | |
| 30 | |
| 31 class GeolocationPermissionRequest : public PermissionBubbleRequest { | |
| 32 public: | |
| 33 GeolocationPermissionRequest( | |
| 34 ChromeGeolocationPermissionContext* context, | |
| 35 const PermissionRequestID& id, | |
| 36 const GURL& requesting_frame, | |
| 37 bool user_gesture, | |
| 38 base::Callback<void(bool)> callback, | |
| 39 const std::string& display_languages); | |
| 40 virtual ~GeolocationPermissionRequest(); | |
| 41 | |
| 42 // PermissionBubbleDelegate: | |
| 43 virtual int GetIconID() const OVERRIDE; | |
| 44 virtual base::string16 GetMessageText() const OVERRIDE; | |
| 45 virtual base::string16 GetMessageTextFragment() const OVERRIDE; | |
| 46 virtual bool HasUserGesture() const OVERRIDE; | |
| 47 virtual GURL GetRequestingHostname() const OVERRIDE; | |
| 48 virtual void PermissionGranted() OVERRIDE; | |
| 49 virtual void PermissionDenied() OVERRIDE; | |
| 50 virtual void Cancelled() OVERRIDE; | |
| 51 virtual void RequestFinished() OVERRIDE; | |
| 52 | |
| 53 private: | |
| 54 ChromeGeolocationPermissionContext* context_; | |
| 55 PermissionRequestID id_; | |
| 56 GURL requesting_frame_; | |
| 57 bool user_gesture_; | |
| 58 base::Callback<void(bool)> callback_; | |
| 59 std::string display_languages_; | |
| 60 }; | |
| 61 | |
| 62 GeolocationPermissionRequest::GeolocationPermissionRequest( | |
| 63 ChromeGeolocationPermissionContext* context, | |
| 64 const PermissionRequestID& id, | |
| 65 const GURL& requesting_frame, | |
| 66 bool user_gesture, | |
| 67 base::Callback<void(bool)> callback, | |
| 68 const std::string& display_languages) | |
| 69 : context_(context), | |
| 70 id_(id), | |
| 71 requesting_frame_(requesting_frame), | |
| 72 user_gesture_(user_gesture), | |
| 73 callback_(callback), | |
| 74 display_languages_(display_languages) {} | |
| 75 | |
| 76 GeolocationPermissionRequest::~GeolocationPermissionRequest() {} | |
| 77 | |
| 78 int GeolocationPermissionRequest::GetIconID() const { | |
| 79 return IDR_INFOBAR_GEOLOCATION; | |
| 80 } | |
| 81 | |
| 82 base::string16 GeolocationPermissionRequest::GetMessageText() const { | |
| 83 return l10n_util::GetStringFUTF16(IDS_GEOLOCATION_INFOBAR_QUESTION, | |
| 84 net::FormatUrl(requesting_frame_, display_languages_)); | |
| 85 } | |
| 86 | |
| 87 base::string16 GeolocationPermissionRequest::GetMessageTextFragment() const { | |
| 88 return l10n_util::GetStringUTF16(IDS_GEOLOCATION_INFOBAR_PERMISSION_FRAGMENT); | |
| 89 } | |
| 90 | |
| 91 bool GeolocationPermissionRequest::HasUserGesture() const { | |
| 92 return user_gesture_; | |
| 93 } | |
| 94 | |
| 95 GURL GeolocationPermissionRequest::GetRequestingHostname() const { | |
| 96 return requesting_frame_; | |
| 97 } | |
| 98 | |
| 99 void GeolocationPermissionRequest::PermissionGranted() { | |
| 100 context_->NotifyPermissionSet(id_, requesting_frame_, callback_, true); | |
| 101 } | |
| 102 | |
| 103 void GeolocationPermissionRequest::PermissionDenied() { | |
| 104 context_->NotifyPermissionSet(id_, requesting_frame_, callback_, false); | |
| 105 } | |
| 106 | |
| 107 void GeolocationPermissionRequest::Cancelled() { | |
| 108 } | |
| 109 | |
| 110 void GeolocationPermissionRequest::RequestFinished() { | |
| 111 // Deletes 'this'. | |
| 112 context_->RequestFinished(this); | |
| 113 } | |
| 114 | |
| 115 | |
| 116 ChromeGeolocationPermissionContext::ChromeGeolocationPermissionContext( | |
| 117 Profile* profile) | |
| 118 : profile_(profile), | |
| 119 shutting_down_(false), | |
| 120 extensions_context_(profile) { | |
| 121 } | |
| 122 | |
| 123 ChromeGeolocationPermissionContext::~ChromeGeolocationPermissionContext() { | |
| 124 // ChromeGeolocationPermissionContext may be destroyed on either the UI thread | |
| 125 // or the IO thread, but the PermissionQueueController must have been | |
| 126 // destroyed on the UI thread. | |
| 127 DCHECK(!permission_queue_controller_.get()); | |
| 128 } | |
| 129 | |
| 130 void ChromeGeolocationPermissionContext::RequestGeolocationPermission( | |
| 131 content::WebContents* web_contents, | |
| 132 int bridge_id, | |
| 133 const GURL& requesting_frame, | |
| 134 bool user_gesture, | |
| 135 base::Callback<void(bool)> callback) { | |
| 136 GURL requesting_frame_origin = requesting_frame.GetOrigin(); | |
| 137 if (shutting_down_) | |
| 138 return; | |
| 139 | |
| 140 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); | |
| 141 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); | |
| 142 const PermissionRequestID id( | |
| 143 render_process_id, render_view_id, bridge_id, GURL()); | |
| 144 | |
| 145 bool permission_set; | |
| 146 bool new_permission; | |
| 147 if (extensions_context_.RequestPermission( | |
| 148 web_contents, id, bridge_id, requesting_frame, user_gesture, | |
| 149 callback, &permission_set, &new_permission)) { | |
| 150 if (permission_set) { | |
| 151 NotifyPermissionSet(id, requesting_frame_origin, callback, | |
| 152 new_permission); | |
| 153 } | |
| 154 return; | |
| 155 } | |
| 156 | |
| 157 GURL embedder = web_contents->GetLastCommittedURL().GetOrigin(); | |
| 158 if (!requesting_frame_origin.is_valid() || !embedder.is_valid()) { | |
| 159 LOG(WARNING) << "Attempt to use geolocation from an invalid URL: " | |
| 160 << requesting_frame_origin << "," << embedder | |
| 161 << " (geolocation is not supported in popups)"; | |
| 162 NotifyPermissionSet(id, requesting_frame_origin, callback, false); | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 DecidePermission(web_contents, id, requesting_frame_origin, user_gesture, | |
| 167 embedder, "", callback); | |
| 168 } | |
| 169 | |
| 170 void ChromeGeolocationPermissionContext::CancelGeolocationPermissionRequest( | |
| 171 content::WebContents* web_contents, | |
| 172 int bridge_id, | |
| 173 const GURL& requesting_frame) { | |
| 174 if (extensions_context_.CancelPermissionRequest(web_contents, bridge_id)) | |
| 175 return; | |
| 176 | |
| 177 int render_process_id = web_contents->GetRenderProcessHost()->GetID(); | |
| 178 int render_view_id = web_contents->GetRenderViewHost()->GetRoutingID(); | |
| 179 CancelPendingInfobarRequest(PermissionRequestID( | |
| 180 render_process_id, render_view_id, bridge_id, GURL())); | |
| 181 } | |
| 182 | |
| 183 void ChromeGeolocationPermissionContext::DecidePermission( | |
| 184 content::WebContents* web_contents, | |
| 185 const PermissionRequestID& id, | |
| 186 const GURL& requesting_frame, | |
| 187 bool user_gesture, | |
| 188 const GURL& embedder, | |
| 189 const std::string& accept_button_label, | |
| 190 base::Callback<void(bool)> callback) { | |
| 191 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 192 | |
| 193 ContentSetting content_setting = | |
| 194 profile_->GetHostContentSettingsMap()->GetContentSetting( | |
| 195 requesting_frame, embedder, CONTENT_SETTINGS_TYPE_GEOLOCATION, | |
| 196 std::string()); | |
| 197 switch (content_setting) { | |
| 198 case CONTENT_SETTING_BLOCK: | |
| 199 PermissionDecided(id, requesting_frame, embedder, callback, false); | |
| 200 break; | |
| 201 case CONTENT_SETTING_ALLOW: | |
| 202 PermissionDecided(id, requesting_frame, embedder, callback, true); | |
| 203 break; | |
| 204 default: | |
| 205 if (PermissionBubbleManager::Enabled()) { | |
| 206 PermissionBubbleManager* mgr = | |
| 207 PermissionBubbleManager::FromWebContents(web_contents); | |
| 208 if (mgr) { | |
| 209 scoped_ptr<GeolocationPermissionRequest> request_ptr( | |
| 210 new GeolocationPermissionRequest( | |
| 211 this, id, requesting_frame, user_gesture, callback, | |
| 212 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages))); | |
| 213 GeolocationPermissionRequest* request = request_ptr.get(); | |
| 214 pending_requests_.add(id.ToString(), request_ptr.Pass()); | |
| 215 mgr->AddRequest(request); | |
| 216 } | |
| 217 } else { | |
| 218 // setting == ask. Prompt the user. | |
| 219 QueueController()->CreateInfoBarRequest( | |
| 220 id, requesting_frame, embedder, accept_button_label, | |
| 221 base::Bind( | |
| 222 &ChromeGeolocationPermissionContext::NotifyPermissionSet, | |
| 223 base::Unretained(this), id, requesting_frame, callback)); | |
| 224 } | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 void ChromeGeolocationPermissionContext::CreateInfoBarRequest( | |
| 229 const PermissionRequestID& id, | |
| 230 const GURL& requesting_frame, | |
| 231 const GURL& embedder, | |
| 232 const std::string accept_button_label, | |
| 233 base::Callback<void(bool)> callback) { | |
| 234 QueueController()->CreateInfoBarRequest( | |
| 235 id, requesting_frame, embedder, accept_button_label, base::Bind( | |
| 236 &ChromeGeolocationPermissionContext::NotifyPermissionSet, | |
| 237 base::Unretained(this), id, requesting_frame, callback)); | |
| 238 } | |
| 239 | |
| 240 void ChromeGeolocationPermissionContext::RequestFinished( | |
| 241 GeolocationPermissionRequest* request) { | |
| 242 base::ScopedPtrHashMap<std::string, | |
| 243 GeolocationPermissionRequest>::iterator it; | |
| 244 for (it = pending_requests_.begin(); it != pending_requests_.end(); ++it) { | |
| 245 if (it->second == request) { | |
| 246 pending_requests_.take_and_erase(it); | |
| 247 return; | |
| 248 } | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 | |
| 253 void ChromeGeolocationPermissionContext::ShutdownOnUIThread() { | |
| 254 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 255 permission_queue_controller_.reset(); | |
| 256 shutting_down_ = true; | |
| 257 } | |
| 258 | |
| 259 void ChromeGeolocationPermissionContext::PermissionDecided( | |
| 260 const PermissionRequestID& id, | |
| 261 const GURL& requesting_frame, | |
| 262 const GURL& embedder, | |
| 263 base::Callback<void(bool)> callback, | |
| 264 bool allowed) { | |
| 265 NotifyPermissionSet(id, requesting_frame, callback, allowed); | |
| 266 } | |
| 267 | |
| 268 void ChromeGeolocationPermissionContext::NotifyPermissionSet( | |
| 269 const PermissionRequestID& id, | |
| 270 const GURL& requesting_frame, | |
| 271 base::Callback<void(bool)> callback, | |
| 272 bool allowed) { | |
| 273 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 274 | |
| 275 // WebContents may have gone away (or not exists for extension). | |
| 276 TabSpecificContentSettings* content_settings = | |
| 277 TabSpecificContentSettings::Get(id.render_process_id(), | |
| 278 id.render_view_id()); | |
| 279 if (content_settings) { | |
| 280 content_settings->OnGeolocationPermissionSet(requesting_frame.GetOrigin(), | |
| 281 allowed); | |
| 282 } | |
| 283 | |
| 284 callback.Run(allowed); | |
| 285 } | |
| 286 | |
| 287 PermissionQueueController* | |
| 288 ChromeGeolocationPermissionContext::QueueController() { | |
| 289 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 290 DCHECK(!shutting_down_); | |
| 291 if (!permission_queue_controller_) | |
| 292 permission_queue_controller_.reset(CreateQueueController()); | |
| 293 return permission_queue_controller_.get(); | |
| 294 } | |
| 295 | |
| 296 PermissionQueueController* | |
| 297 ChromeGeolocationPermissionContext::CreateQueueController() { | |
| 298 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 299 return new PermissionQueueController(profile(), | |
| 300 CONTENT_SETTINGS_TYPE_GEOLOCATION); | |
| 301 } | |
| 302 | |
| 303 void ChromeGeolocationPermissionContext::CancelPendingInfobarRequest( | |
| 304 const PermissionRequestID& id) { | |
| 305 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 306 if (shutting_down_) | |
| 307 return; | |
| 308 | |
| 309 if (PermissionBubbleManager::Enabled()) { | |
| 310 GeolocationPermissionRequest* cancelling = | |
| 311 pending_requests_.get(id.ToString()); | |
| 312 content::WebContents* web_contents = tab_util::GetWebContentsByID( | |
| 313 id.render_process_id(), id.render_view_id()); | |
| 314 if (cancelling != NULL && web_contents != NULL && | |
| 315 PermissionBubbleManager::FromWebContents(web_contents) != NULL) { | |
| 316 PermissionBubbleManager::FromWebContents(web_contents)-> | |
| 317 CancelRequest(cancelling); | |
| 318 } | |
| 319 return; | |
| 320 } | |
| 321 | |
| 322 QueueController()->CancelInfoBarRequest(id); | |
| 323 } | |
| OLD | NEW |