Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Side by Side Diff: content/renderer/geolocation_dispatcher.cc

Issue 1367853002: Move GeolocationDispatcher into blink. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/renderer/geolocation_dispatcher.h"
6
7 #include "content/public/common/geoposition.h"
8 #include "content/renderer/render_view_impl.h"
9 #include "third_party/WebKit/public/platform/WebString.h"
10 #include "third_party/WebKit/public/web/WebGeolocationPermissionRequest.h"
11 #include "third_party/WebKit/public/web/WebGeolocationPermissionRequestManager.h "
12 #include "third_party/WebKit/public/web/WebGeolocationClient.h"
13 #include "third_party/WebKit/public/web/WebGeolocationPosition.h"
14 #include "third_party/WebKit/public/web/WebGeolocationError.h"
15 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
16
17 using blink::WebGeolocationController;
18 using blink::WebGeolocationError;
19 using blink::WebGeolocationPermissionRequest;
20 using blink::WebGeolocationPermissionRequestManager;
21 using blink::WebGeolocationPosition;
22
23 namespace content {
24
25 GeolocationDispatcher::GeolocationDispatcher(RenderFrame* render_frame)
26 : RenderFrameObserver(render_frame),
27 pending_permissions_(new WebGeolocationPermissionRequestManager()),
28 enable_high_accuracy_(false) {
29 }
30
31 GeolocationDispatcher::~GeolocationDispatcher() {}
32
33 void GeolocationDispatcher::startUpdating() {
34 if (!geolocation_service_) {
35 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
36 mojo::GetProxy(&geolocation_service_));
37 }
38 if (enable_high_accuracy_)
39 geolocation_service_->SetHighAccuracy(true);
40 QueryNextPosition();
41 }
42
43 void GeolocationDispatcher::stopUpdating() {
44 geolocation_service_.reset();
45 }
46
47 void GeolocationDispatcher::setEnableHighAccuracy(bool enable_high_accuracy) {
48 // GeolocationController calls setEnableHighAccuracy(true) before
49 // startUpdating in response to the first high-accuracy Geolocation
50 // subscription. When the last high-accuracy Geolocation unsubscribes
51 // it calls setEnableHighAccuracy(false) after stopUpdating.
52 bool has_changed = enable_high_accuracy_ != enable_high_accuracy;
53 enable_high_accuracy_ = enable_high_accuracy;
54 // We have a different accuracy requirement. Request browser to update.
55 if (geolocation_service_ && has_changed)
56 geolocation_service_->SetHighAccuracy(enable_high_accuracy_);
57 }
58
59 void GeolocationDispatcher::setController(
60 WebGeolocationController* controller) {
61 controller_.reset(controller);
62 }
63
64 bool GeolocationDispatcher::lastPosition(WebGeolocationPosition&) {
65 // The latest position is stored in the browser, not the renderer, so we
66 // would have to fetch it synchronously to give a good value here. The
67 // WebCore::GeolocationController already caches the last position it
68 // receives, so there is not much benefit to more position caching here.
69 return false;
70 }
71
72 // TODO(jknotten): Change the messages to use a security origin, so no
73 // conversion is necessary.
74 void GeolocationDispatcher::requestPermission(
75 const WebGeolocationPermissionRequest& permissionRequest) {
76 if (!permission_service_.get()) {
77 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
78 mojo::GetProxy(&permission_service_));
79 }
80
81 int permission_request_id = pending_permissions_->add(permissionRequest);
82
83 permission_service_->RequestPermission(
84 permission::NAME_GEOLOCATION,
85 permissionRequest.securityOrigin().toString().utf8(),
86 blink::WebUserGestureIndicator::isProcessingUserGesture(),
87 base::Bind(&GeolocationDispatcher::OnPermissionSet,
88 base::Unretained(this), permission_request_id));
89 }
90
91 void GeolocationDispatcher::cancelPermissionRequest(
92 const blink::WebGeolocationPermissionRequest& permissionRequest) {
93 int permission_request_id;
94 pending_permissions_->remove(permissionRequest, permission_request_id);
95 }
96
97 // Permission for using geolocation has been set.
98 void GeolocationDispatcher::OnPermissionSet(int permission_request_id,
99 permission::Status status) {
100 WebGeolocationPermissionRequest permissionRequest;
101 if (!pending_permissions_->remove(permission_request_id, permissionRequest))
102 return;
103
104 permissionRequest.setIsAllowed(status == permission::STATUS_GRANTED);
105 }
106
107 void GeolocationDispatcher::QueryNextPosition() {
108 DCHECK(geolocation_service_);
109 geolocation_service_->QueryNextPosition(
110 base::Bind(&GeolocationDispatcher::OnPositionUpdate,
111 base::Unretained(this)));
112 }
113
114 void GeolocationDispatcher::OnPositionUpdate(
115 geolocation::GeopositionPtr geoposition) {
116 QueryNextPosition();
117
118 if (geoposition->valid) {
119 controller_->positionChanged(WebGeolocationPosition(
120 geoposition->timestamp,
121 geoposition->latitude,
122 geoposition->longitude,
123 geoposition->accuracy,
124 // Lowest point on land is at approximately -400 meters.
125 geoposition->altitude > -10000.,
126 geoposition->altitude,
127 geoposition->altitude_accuracy >= 0.,
128 geoposition->altitude_accuracy,
129 geoposition->heading >= 0. && geoposition->heading <= 360.,
130 geoposition->heading,
131 geoposition->speed >= 0.,
132 geoposition->speed));
133 } else {
134 WebGeolocationError::Error code;
135 switch (geoposition->error_code) {
136 case geolocation::Geoposition::ERROR_CODE_PERMISSION_DENIED:
137 code = WebGeolocationError::ErrorPermissionDenied;
138 break;
139 case geolocation::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE:
140 code = WebGeolocationError::ErrorPositionUnavailable;
141 break;
142 default:
143 NOTREACHED() << geoposition->error_code;
144 return;
145 }
146 controller_->errorOccurred(WebGeolocationError(
147 code, blink::WebString::fromUTF8(geoposition->error_message)));
148 }
149 }
150
151 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698