summaryrefslogtreecommitdiffstats
path: root/content/browser/geolocation/geolocation_dispatcher_host.cc
blob: aec217091e3563a77d24a534957a2f6e07f98cc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/geolocation/geolocation_dispatcher_host.h"

#include <utility>

#include "base/bind.h"
#include "base/metrics/histogram.h"
#include "content/browser/frame_host/render_frame_host_impl.h"
#include "content/browser/renderer_host/render_message_filter.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/geolocation_permission_context.h"
#include "content/public/common/geoposition.h"
#include "content/common/geolocation_messages.h"

namespace content {
namespace {

// Geoposition error codes for reporting in UMA.
enum GeopositionErrorCode {
  // NOTE: Do not renumber these as that would confuse interpretation of
  // previously logged data. When making changes, also update the enum list
  // in tools/metrics/histograms/histograms.xml to keep it in sync.

  // There was no error.
  GEOPOSITION_ERROR_CODE_NONE = 0,

  // User denied use of geolocation.
  GEOPOSITION_ERROR_CODE_PERMISSION_DENIED = 1,

  // Geoposition could not be determined.
  GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE = 2,

  // Timeout.
  GEOPOSITION_ERROR_CODE_TIMEOUT = 3,

  // NOTE: Add entries only immediately above this line.
  GEOPOSITION_ERROR_CODE_COUNT = 4
};

void RecordGeopositionErrorCode(Geoposition::ErrorCode error_code) {
  GeopositionErrorCode code = GEOPOSITION_ERROR_CODE_NONE;
  switch (error_code) {
    case Geoposition::ERROR_CODE_NONE:
      code = GEOPOSITION_ERROR_CODE_NONE;
      break;
    case Geoposition::ERROR_CODE_PERMISSION_DENIED:
      code = GEOPOSITION_ERROR_CODE_PERMISSION_DENIED;
      break;
    case Geoposition::ERROR_CODE_POSITION_UNAVAILABLE:
      code = GEOPOSITION_ERROR_CODE_POSITION_UNAVAILABLE;
      break;
    case Geoposition::ERROR_CODE_TIMEOUT:
      code = GEOPOSITION_ERROR_CODE_TIMEOUT;
      break;
  }
  UMA_HISTOGRAM_ENUMERATION("Geolocation.LocationUpdate.ErrorCode",
                            code,
                            GEOPOSITION_ERROR_CODE_COUNT);
}

void SendGeolocationPermissionResponse(int render_process_id,
                                       int render_frame_id,
                                       int bridge_id,
                                       bool allowed) {
  RenderFrameHost* render_frame_host =
      RenderFrameHost::FromID(render_process_id, render_frame_id);
  if (!render_frame_host)
    return;
  render_frame_host->Send(
      new GeolocationMsg_PermissionSet(render_frame_id, bridge_id, allowed));

  if (allowed)
    GeolocationProviderImpl::GetInstance()->UserDidOptIntoLocationServices();
}

}  // namespace

GeolocationDispatcherHost::GeolocationDispatcherHost(
    WebContents* web_contents)
    : WebContentsObserver(web_contents),
      paused_(false) {
  // This is initialized by WebContentsImpl. Do not add any non-trivial
  // initialization here, defer to OnStartUpdating which is triggered whenever
  // a javascript geolocation object is actually initialized.
}

GeolocationDispatcherHost::~GeolocationDispatcherHost() {
}

void GeolocationDispatcherHost::RenderFrameDeleted(
    RenderFrameHost* render_frame_host) {
  OnStopUpdating(render_frame_host);
}

void GeolocationDispatcherHost::RenderViewHostChanged(
    RenderViewHost* old_host,
    RenderViewHost* new_host) {
  updating_frames_.clear();
  paused_ = false;
  geolocation_subscription_.reset();
}

bool GeolocationDispatcherHost::OnMessageReceived(
    const IPC::Message& msg, RenderFrameHost* render_frame_host) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(GeolocationDispatcherHost, msg,
                                   render_frame_host)
    IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission,
                        OnRequestPermission)
    IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest,
                        OnCancelPermissionRequest)
    IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating)
    IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void GeolocationDispatcherHost::OnLocationUpdate(
    const Geoposition& geoposition) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  RecordGeopositionErrorCode(geoposition.error_code);
  if (paused_)
    return;

  for (std::map<RenderFrameHost*, bool>::iterator i = updating_frames_.begin();
       i != updating_frames_.end(); ++i) {
    i->first->Send(new GeolocationMsg_PositionUpdated(
        i->first->GetRoutingID(), geoposition));
  }
}

void GeolocationDispatcherHost::OnRequestPermission(
    RenderFrameHost* render_frame_host,
    int bridge_id,
    const GURL& requesting_frame,
    bool user_gesture) {
  GeolocationPermissionContext* context =
      web_contents()->GetBrowserContext()->GetGeolocationPermissionContext();
  int render_process_id = render_frame_host->GetProcess()->GetID();
  int render_frame_id = render_frame_host->GetRoutingID();
  if (context) {
    context->RequestGeolocationPermission(
        web_contents(),
        bridge_id,
        requesting_frame,
        user_gesture,
        base::Bind(&SendGeolocationPermissionResponse,
                   render_process_id,
                   render_frame_id,
                   bridge_id));
  } else {
    SendGeolocationPermissionResponse(
        render_process_id, render_frame_id, bridge_id, true);
  }
}

void GeolocationDispatcherHost::OnCancelPermissionRequest(
    RenderFrameHost* render_frame_host,
    int bridge_id,
    const GURL& requesting_frame) {
  GeolocationPermissionContext* context =
      web_contents()->GetBrowserContext()->GetGeolocationPermissionContext();
  if (context) {
    context->CancelGeolocationPermissionRequest(
        web_contents(), bridge_id, requesting_frame);
  }
}

void GeolocationDispatcherHost::OnStartUpdating(
    RenderFrameHost* render_frame_host,
    const GURL& requesting_frame,
    bool enable_high_accuracy) {
  // StartUpdating() can be invoked as a result of high-accuracy mode
  // being enabled / disabled. No need to record the dispatcher again.
  UMA_HISTOGRAM_BOOLEAN(
      "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy",
      enable_high_accuracy);

  updating_frames_[render_frame_host] = enable_high_accuracy;
  RefreshGeolocationOptions();
}

void GeolocationDispatcherHost::OnStopUpdating(
    RenderFrameHost* render_frame_host) {
  updating_frames_.erase(render_frame_host);
  RefreshGeolocationOptions();
}

void GeolocationDispatcherHost::PauseOrResume(bool should_pause) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  paused_ = should_pause;
  RefreshGeolocationOptions();
}

void GeolocationDispatcherHost::RefreshGeolocationOptions() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (updating_frames_.empty() || paused_) {
    geolocation_subscription_.reset();
    return;
  }

  bool high_accuracy = false;
  for (std::map<RenderFrameHost*, bool>::iterator i =
            updating_frames_.begin(); i != updating_frames_.end(); ++i) {
    if (i->second) {
      high_accuracy = true;
      break;
    }
  }
  geolocation_subscription_ = GeolocationProvider::GetInstance()->
      AddLocationUpdateCallback(
          base::Bind(&GeolocationDispatcherHost::OnLocationUpdate,
                      base::Unretained(this)),
          high_accuracy);
}

}  // namespace content