Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / geolocation / chrome_geolocation_permission_context.cc
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/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"
34
35 using extensions::APIPermission;
36 using extensions::ExtensionRegistry;
37
38 class GeolocationPermissionRequest : public PermissionBubbleRequest {
39  public:
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();
47
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;
58
59  private:
60   ChromeGeolocationPermissionContext* context_;
61   PermissionRequestID id_;
62   GURL requesting_frame_;
63   base::Callback<void(bool)> callback_;
64   std::string display_languages_;
65 };
66
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)
73     : context_(context),
74       id_(id),
75       requesting_frame_(requesting_frame),
76       callback_(callback),
77       display_languages_(display_languages) {}
78
79 GeolocationPermissionRequest::~GeolocationPermissionRequest() {}
80
81 int GeolocationPermissionRequest::GetIconID() const {
82   return IDR_INFOBAR_GEOLOCATION;
83 }
84
85 base::string16 GeolocationPermissionRequest::GetMessageText() const {
86   return l10n_util::GetStringFUTF16(IDS_GEOLOCATION_INFOBAR_QUESTION,
87       net::FormatUrl(requesting_frame_, display_languages_));
88 }
89
90 base::string16 GeolocationPermissionRequest::GetMessageTextFragment() const {
91   return l10n_util::GetStringUTF16(IDS_GEOLOCATION_INFOBAR_PERMISSION_FRAGMENT);
92 }
93
94 bool GeolocationPermissionRequest::HasUserGesture() const {
95   // TODO(gbillock): plumb this through from GeolocationDispatcher.
96   return false;
97 }
98
99 GURL GeolocationPermissionRequest::GetRequestingHostname() const {
100   return requesting_frame_;
101 }
102
103 void GeolocationPermissionRequest::PermissionGranted() {
104   context_->NotifyPermissionSet(id_, requesting_frame_, callback_, true);
105 }
106
107 void GeolocationPermissionRequest::PermissionDenied() {
108   context_->NotifyPermissionSet(id_, requesting_frame_, callback_, false);
109 }
110
111 void GeolocationPermissionRequest::Cancelled() {
112   context_->NotifyPermissionSet(id_, requesting_frame_, callback_, false);
113 }
114
115 void GeolocationPermissionRequest::RequestFinished() {
116   delete this;
117 }
118
119
120 ChromeGeolocationPermissionContext::ChromeGeolocationPermissionContext(
121     Profile* profile)
122     : profile_(profile),
123       shutting_down_(false) {
124 }
125
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());
131 }
132
133 void ChromeGeolocationPermissionContext::RequestGeolocationPermission(
134     int render_process_id,
135     int render_view_id,
136     int bridge_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,
143         base::Bind(
144             &ChromeGeolocationPermissionContext::RequestGeolocationPermission,
145             this, render_process_id, render_view_id, bridge_id,
146             requesting_frame_origin, callback));
147     return;
148   }
149
150   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
151   if (shutting_down_)
152     return;
153
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,
163                                                     extension,
164                                                     profile_)) {
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);
169         return;
170       }
171     }
172   }
173
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: "
181                  << id.ToString()
182                  << " (can't prompt user without a visible tab)";
183     NotifyPermissionSet(id, requesting_frame_origin, callback, false);
184     return;
185   }
186
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);
193     return;
194   }
195
196   DecidePermission(web_contents, id, requesting_frame_origin,
197                    embedder, "", callback);
198 }
199
200 void ChromeGeolocationPermissionContext::CancelGeolocationPermissionRequest(
201     int render_process_id,
202     int render_view_id,
203     int bridge_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));
208 }
209
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));
218
219   ContentSetting content_setting =
220      profile_->GetHostContentSettingsMap()->GetContentSetting(
221           requesting_frame, embedder, CONTENT_SETTINGS_TYPE_GEOLOCATION,
222           std::string());
223   switch (content_setting) {
224     case CONTENT_SETTING_BLOCK:
225       PermissionDecided(id, requesting_frame, embedder, callback, false);
226       break;
227     case CONTENT_SETTING_ALLOW:
228       PermissionDecided(id, requesting_frame, embedder, callback, true);
229       break;
230     default:
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)));
237       } else {
238         // setting == ask. Prompt the user.
239         QueueController()->CreateInfoBarRequest(
240             id, requesting_frame, embedder, accept_button_label,
241                 base::Bind(
242                     &ChromeGeolocationPermissionContext::NotifyPermissionSet,
243                 base::Unretained(this), id, requesting_frame, callback));
244       }
245   }
246 }
247
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));
258 }
259
260 void ChromeGeolocationPermissionContext::ShutdownOnUIThread() {
261   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
262   permission_queue_controller_.reset();
263   shutting_down_ = true;
264 }
265
266 void ChromeGeolocationPermissionContext::PermissionDecided(
267     const PermissionRequestID& id,
268     const GURL& requesting_frame,
269     const GURL& embedder,
270     base::Callback<void(bool)> callback,
271     bool allowed) {
272   NotifyPermissionSet(id, requesting_frame, callback, allowed);
273 }
274
275 void ChromeGeolocationPermissionContext::NotifyPermissionSet(
276     const PermissionRequestID& id,
277     const GURL& requesting_frame,
278     base::Callback<void(bool)> callback,
279     bool allowed) {
280   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
281
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(),
288                                                  allowed);
289   }
290
291   callback.Run(allowed);
292 }
293
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();
301 }
302
303 PermissionQueueController*
304     ChromeGeolocationPermissionContext::CreateQueueController() {
305   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
306   return new PermissionQueueController(profile(),
307                                        CONTENT_SETTINGS_TYPE_GEOLOCATION);
308 }
309
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,
315         base::Bind(
316             &ChromeGeolocationPermissionContext::CancelPendingInfobarRequest,
317             this, id));
318      return;
319   }
320   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
321   if (shutting_down_)
322     return;
323
324   // TODO(gbillock): handle permission bubble cancellation.
325   QueueController()->CancelInfoBarRequest(id);
326 }