1// Copyright 2013 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 "content/browser/browser_plugin/browser_plugin_geolocation_permission_context.h"
6
7#include "base/bind.h"
8#include "content/browser/browser_plugin/browser_plugin_guest.h"
9#include "content/browser/web_contents/web_contents_impl.h"
10#include "content/public/browser/browser_thread.h"
11#include "content/public/browser/render_process_host.h"
12#include "content/public/browser/render_view_host.h"
13
14namespace content {
15
16BrowserPluginGeolocationPermissionContext::
17    BrowserPluginGeolocationPermissionContext() {
18}
19
20BrowserPluginGeolocationPermissionContext::
21    ~BrowserPluginGeolocationPermissionContext() {
22}
23
24void BrowserPluginGeolocationPermissionContext::RequestGeolocationPermission(
25    int render_process_id,
26    int render_view_id,
27    int bridge_id,
28    const GURL& requesting_frame,
29    base::Callback<void(bool)> callback) {
30  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
31    BrowserThread::PostTask(
32        BrowserThread::UI, FROM_HERE,
33        base::Bind(
34            &BrowserPluginGeolocationPermissionContext::
35                RequestGeolocationPermission,
36            this,
37            render_process_id,
38            render_view_id,
39            bridge_id,
40            requesting_frame,
41            callback));
42    return;
43  }
44  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
45
46  // Note that callback.Run(true) allows geolocation access, callback.Run(false)
47  // denies geolocation access.
48  // We need to go to the renderer to ask embedder's js if we are allowed to
49  // have geolocation access.
50  RenderViewHost* rvh = RenderViewHost::FromID(render_process_id,
51                                               render_view_id);
52  if (rvh) {
53    DCHECK(rvh->GetProcess()->IsGuest());
54    WebContentsImpl* guest_web_contents =
55        static_cast<WebContentsImpl*>(rvh->GetDelegate()->GetAsWebContents());
56    BrowserPluginGuest* guest = guest_web_contents->GetBrowserPluginGuest();
57    guest->AskEmbedderForGeolocationPermission(bridge_id,
58                                               requesting_frame,
59                                               callback);
60  }
61}
62
63void BrowserPluginGeolocationPermissionContext::
64    CancelGeolocationPermissionRequest(int render_process_id,
65                                       int render_view_id,
66                                       int bridge_id,
67                                       const GURL& requesting_frame) {
68  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
69    BrowserThread::PostTask(
70        BrowserThread::UI, FROM_HERE,
71        base::Bind(
72            &BrowserPluginGeolocationPermissionContext::
73                CancelGeolocationPermissionRequest,
74            this,
75            render_process_id,
76            render_view_id,
77            bridge_id,
78            requesting_frame));
79    return;
80  }
81  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
82  RenderViewHost* rvh = RenderViewHost::FromID(render_process_id,
83                                               render_view_id);
84  if (rvh) {
85    DCHECK(rvh->GetProcess()->IsGuest());
86    WebContentsImpl* guest_web_contents =
87        static_cast<WebContentsImpl*>(rvh->GetDelegate()->GetAsWebContents());
88    BrowserPluginGuest* guest = guest_web_contents->GetBrowserPluginGuest();
89    if (guest)
90      guest->CancelGeolocationRequest(bridge_id);
91  }
92}
93
94}  // namespace content
95