Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / content / browser / geolocation / geolocation_dispatcher_host.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 "content/browser/geolocation/geolocation_dispatcher_host.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "content/browser/frame_host/render_frame_host_impl.h"
11 #include "content/browser/geolocation/geolocation_provider_impl.h"
12 #include "content/browser/renderer_host/render_message_filter.h"
13 #include "content/browser/web_contents/web_contents_impl.h"
14 #include "content/public/browser/content_browser_client.h"
15 #include "content/common/geolocation_messages.h"
16
17 namespace content {
18
19 GeolocationDispatcherHost::PendingPermission::PendingPermission(
20     int render_frame_id,
21     int render_process_id,
22     int bridge_id,
23     const GURL& origin)
24     : render_frame_id(render_frame_id),
25       render_process_id(render_process_id),
26       bridge_id(bridge_id),
27       origin(origin) {
28 }
29
30 GeolocationDispatcherHost::PendingPermission::~PendingPermission() {
31 }
32
33 GeolocationDispatcherHost::GeolocationDispatcherHost(
34     WebContents* web_contents)
35     : WebContentsObserver(web_contents),
36       weak_factory_(this) {
37   // This is initialized by WebContentsImpl. Do not add any non-trivial
38   // initialization here, defer to OnStartUpdating which is triggered whenever
39   // a javascript geolocation object is actually initialized.
40 }
41
42 GeolocationDispatcherHost::~GeolocationDispatcherHost() {
43 }
44
45 void GeolocationDispatcherHost::RenderFrameDeleted(
46     RenderFrameHost* render_frame_host) {
47   CancelPermissionRequestsForFrame(render_frame_host);
48 }
49
50 void GeolocationDispatcherHost::DidNavigateAnyFrame(
51     RenderFrameHost* render_frame_host,
52     const LoadCommittedDetails& details,
53     const FrameNavigateParams& params) {
54   if (details.is_in_page)
55     return;
56
57   CancelPermissionRequestsForFrame(render_frame_host);
58 }
59
60 bool GeolocationDispatcherHost::OnMessageReceived(
61     const IPC::Message& msg, RenderFrameHost* render_frame_host) {
62   bool handled = true;
63   IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost, msg,
64                                    render_frame_host)
65     IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission,
66                         OnRequestPermission)
67     IPC_MESSAGE_UNHANDLED(handled = false)
68   IPC_END_MESSAGE_MAP()
69   return handled;
70 }
71
72 void GeolocationDispatcherHost::OnRequestPermission(
73     RenderFrameHost* render_frame_host,
74     int bridge_id,
75     const GURL& requesting_origin,
76     bool user_gesture) {
77   int render_process_id = render_frame_host->GetProcess()->GetID();
78   int render_frame_id = render_frame_host->GetRoutingID();
79
80   PendingPermission pending_permission(
81       render_frame_id, render_process_id, bridge_id, requesting_origin);
82   pending_permissions_.push_back(pending_permission);
83
84   GetContentClient()->browser()->RequestPermission(
85       content::PERMISSION_GEOLOCATION,
86       web_contents(),
87       bridge_id,
88       requesting_origin,
89       user_gesture,
90       base::Bind(&GeolocationDispatcherHost::SendGeolocationPermissionResponse,
91                  weak_factory_.GetWeakPtr(),
92                  render_process_id,
93                  render_frame_id,
94                  bridge_id));
95 }
96
97 void GeolocationDispatcherHost::SendGeolocationPermissionResponse(
98     int render_process_id,
99     int render_frame_id,
100     int bridge_id,
101     bool allowed) {
102   for (size_t i = 0; i < pending_permissions_.size(); ++i) {
103     if (pending_permissions_[i].render_process_id == render_process_id &&
104         pending_permissions_[i].render_frame_id == render_frame_id &&
105         pending_permissions_[i].bridge_id == bridge_id) {
106       RenderFrameHost* render_frame_host =
107           RenderFrameHost::FromID(render_process_id, render_frame_id);
108       if (render_frame_host) {
109         render_frame_host->Send(new GeolocationMsg_PermissionSet(
110             render_frame_id, bridge_id, allowed));
111       }
112
113       if (allowed) {
114         GeolocationProviderImpl::GetInstance()->
115             UserDidOptIntoLocationServices();
116       }
117
118       pending_permissions_.erase(pending_permissions_.begin() + i);
119       return;
120     }
121   }
122
123   NOTREACHED();
124 }
125
126 void GeolocationDispatcherHost::CancelPermissionRequestsForFrame(
127     RenderFrameHost* render_frame_host) {
128   int render_process_id = render_frame_host->GetProcess()->GetID();
129   int render_frame_id = render_frame_host->GetRoutingID();
130
131   for (size_t i = 0; i < pending_permissions_.size(); ++i) {
132     if (pending_permissions_[i].render_process_id == render_process_id &&
133         pending_permissions_[i].render_frame_id == render_frame_id) {
134       GetContentClient()->browser()->CancelPermissionRequest(
135           content::PERMISSION_GEOLOCATION,
136           web_contents(),
137           pending_permissions_[i].bridge_id,
138           pending_permissions_[i].origin);
139       pending_permissions_.erase(pending_permissions_.begin() + i);
140     }
141   }
142 }
143
144 }  // namespace content