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

Side by Side Diff: blimp/engine/feature/geolocation/engine_geolocation_feature.cc

Issue 2091023006: Adds EngineGeolocationFeature for Blimp Geolocation project. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addresses Wez's #61 comments Created 4 years, 5 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 2016 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 "blimp/engine/feature/geolocation/engine_geolocation_feature.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/memory/ptr_util.h"
11 #include "base/memory/weak_ptr.h"
12 #include "blimp/common/create_blimp_message.h"
13 #include "blimp/common/proto/blimp_message.pb.h"
14 #include "blimp/common/proto/geolocation.pb.h"
15 #include "content/public/browser/geolocation_delegate.h"
16 #include "content/public/browser/location_provider.h"
17 #include "content/public/common/geoposition.h"
18 #include "net/base/net_errors.h"
19
20 namespace blimp {
21 namespace engine {
22 namespace {
23
24 // content::GeolocationDelegate implementation integrated with
25 // the EngineGeolocationFeature.
26 class BlimpGeolocationDelegate : public content::GeolocationDelegate {
27 public:
28 explicit BlimpGeolocationDelegate(
29 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate) {
30 feature_delegate_ = feature_delegate;
31 }
32
33 bool UseNetworkLocationProviders() final { return false; }
34
35 std::unique_ptr<content::LocationProvider> OverrideSystemLocationProvider()
36 final {
37 return base::WrapUnique(new BlimpLocationProvider(feature_delegate_));
38 }
39
40 private:
41 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate_;
42
43 DISALLOW_COPY_AND_ASSIGN(BlimpGeolocationDelegate);
44 };
45
46 content::Geoposition::ErrorCode ConvertErrorCode(
47 const GeolocationErrorMessage::ErrorCode& error_code) {
48 switch (error_code) {
49 case GeolocationErrorMessage::PERMISSION_DENIED:
50 return content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED;
51 case GeolocationErrorMessage::POSITION_UNAVAILABLE:
52 return content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE;
53 case GeolocationErrorMessage::TIMEOUT:
54 return content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT;
55 }
56 }
57
58 content::Geoposition ConvertLocationMessage(
59 const GeolocationCoordinatesMessage& coordinates) {
60 content::Geoposition output;
61 output.latitude = coordinates.latitude();
62 output.longitude = coordinates.longitude();
63 output.altitude = coordinates.altitude();
64 output.accuracy = coordinates.accuracy();
65 output.altitude_accuracy = coordinates.altitude_accuracy();
66 output.heading = coordinates.heading();
67 output.speed = coordinates.speed();
68 output.timestamp = base::Time::Now();
69 output.error_code = content::Geoposition::ErrorCode::ERROR_CODE_NONE;
70 return output;
71 }
72
73 } // namespace
74
75 EngineGeolocationFeature::EngineGeolocationFeature() : weak_factory_(this) {}
76
77 EngineGeolocationFeature::~EngineGeolocationFeature() {}
78
79 void EngineGeolocationFeature::set_outgoing_message_processor(
80 std::unique_ptr<BlimpMessageProcessor> message_processor) {
81 DCHECK(message_processor);
82 outgoing_message_processor_ = std::move(message_processor);
83 }
84
85 std::unique_ptr<content::GeolocationDelegate>
86 EngineGeolocationFeature::CreateGeolocationDelegate() {
87 return base::WrapUnique(
88 new BlimpGeolocationDelegate(weak_factory_.GetWeakPtr()));
89 }
90
91 void EngineGeolocationFeature::ProcessMessage(
92 std::unique_ptr<BlimpMessage> message,
93 const net::CompletionCallback& callback) {
94 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case());
95
96 const GeolocationMessage& geolocation_message = message->geolocation();
97
98 switch (geolocation_message.type_case()) {
99 case GeolocationMessage::kCoordinates: {
100 const GeolocationCoordinatesMessage& location =
101 geolocation_message.coordinates();
102 content::Geoposition output = ConvertLocationMessage(location);
103 NotifyCallback(output);
104 break;
105 }
106 case GeolocationMessage::kError: {
107 const GeolocationErrorMessage& error_message =
108 geolocation_message.error();
109 content::Geoposition output;
110 output.error_message = error_message.error_message();
111 output.error_code = ConvertErrorCode(error_message.error_code());
112 NotifyCallback(output);
113 break;
114 }
115 case GeolocationMessage::kSetInterestLevel:
116 case GeolocationMessage::kRequestRefresh:
117 case GeolocationMessage::TYPE_NOT_SET:
118 callback.Run(net::ERR_UNEXPECTED);
Wez 2016/07/22 22:09:44 You need the if (!callback_is_null()) here as well
CJ 2016/07/22 22:31:19 Done.
119 return;
120 }
121 if (!callback.is_null()) {
122 callback.Run(net::OK);
123 }
124 }
125
126 void EngineGeolocationFeature::NotifyCallback(
127 const content::Geoposition& position) {
128 geoposition_received_callback_.Run(position);
129 }
130
131 void EngineGeolocationFeature::RequestAccuracy(
132 GeolocationSetInterestLevelMessage::Level level) {
133 GeolocationMessage* geolocation_message = nullptr;
134 std::unique_ptr<BlimpMessage> blimp_message =
135 CreateBlimpMessage(&geolocation_message);
136
137 GeolocationSetInterestLevelMessage* geolocation_interest =
138 geolocation_message->mutable_set_interest_level();
139 geolocation_interest->set_level(level);
140
141 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
142 net::CompletionCallback());
143 }
144
145 void EngineGeolocationFeature::RequestRefresh() {
146 GeolocationMessage* geolocation_message = nullptr;
147 std::unique_ptr<BlimpMessage> blimp_message =
148 CreateBlimpMessage(&geolocation_message);
149
150 geolocation_message->mutable_request_refresh();
151
152 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
153 net::CompletionCallback());
154 }
155
156 void EngineGeolocationFeature::SetUpdateCallback(
157 const GeopositionReceivedCallback& callback) {
158 geoposition_received_callback_ = callback;
159 }
160
161 } // namespace engine
162 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698