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 "content/browser/geolocation/geolocation_dispatcher_host.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"
19 GeolocationDispatcherHost::PendingPermission::PendingPermission(
21 int render_process_id,
24 : render_frame_id(render_frame_id),
25 render_process_id(render_process_id),
30 GeolocationDispatcherHost::PendingPermission::~PendingPermission() {
33 GeolocationDispatcherHost::GeolocationDispatcherHost(
34 WebContents* web_contents)
35 : WebContentsObserver(web_contents),
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.
42 GeolocationDispatcherHost::~GeolocationDispatcherHost() {
45 void GeolocationDispatcherHost::RenderFrameDeleted(
46 RenderFrameHost* render_frame_host) {
47 CancelPermissionRequestsForFrame(render_frame_host);
50 void GeolocationDispatcherHost::DidNavigateAnyFrame(
51 RenderFrameHost* render_frame_host,
52 const LoadCommittedDetails& details,
53 const FrameNavigateParams& params) {
54 if (details.is_in_page)
57 CancelPermissionRequestsForFrame(render_frame_host);
60 bool GeolocationDispatcherHost::OnMessageReceived(
61 const IPC::Message& msg, RenderFrameHost* render_frame_host) {
63 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost, msg,
65 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission,
67 IPC_MESSAGE_UNHANDLED(handled = false)
72 void GeolocationDispatcherHost::OnRequestPermission(
73 RenderFrameHost* render_frame_host,
75 const GURL& requesting_origin,
77 int render_process_id = render_frame_host->GetProcess()->GetID();
78 int render_frame_id = render_frame_host->GetRoutingID();
80 PendingPermission pending_permission(
81 render_frame_id, render_process_id, bridge_id, requesting_origin);
82 pending_permissions_.push_back(pending_permission);
84 GetContentClient()->browser()->RequestPermission(
85 content::PERMISSION_GEOLOCATION,
90 base::Bind(&GeolocationDispatcherHost::SendGeolocationPermissionResponse,
91 weak_factory_.GetWeakPtr(),
97 void GeolocationDispatcherHost::SendGeolocationPermissionResponse(
98 int render_process_id,
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));
114 GeolocationProviderImpl::GetInstance()->
115 UserDidOptIntoLocationServices();
118 pending_permissions_.erase(pending_permissions_.begin() + i);
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();
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,
137 pending_permissions_[i].bridge_id,
138 pending_permissions_[i].origin);
139 pending_permissions_.erase(pending_permissions_.begin() + i);
144 } // namespace content