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

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: Merge Created 4 years, 4 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 class BlimpGeolocationDelegate : public content::GeolocationDelegate {
24 public:
25 explicit BlimpGeolocationDelegate(
26 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate) {
27 feature_delegate_ = feature_delegate;
28 }
29
30 bool UseNetworkLocationProviders() final { return false; }
31
32 std::unique_ptr<content::LocationProvider> OverrideSystemLocationProvider()
33 final {
34 return base::WrapUnique(new BlimpLocationProvider(feature_delegate_));
35 }
36
37 private:
38 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate_;
39
40 DISALLOW_COPY_AND_ASSIGN(BlimpGeolocationDelegate);
41 };
42
43 content::Geoposition::ErrorCode ConvertErrorCode(
44 const GeolocationErrorMessage::ErrorCode& error_code) {
45 switch (error_code) {
46 case GeolocationErrorMessage::PERMISSION_DENIED:
47 return content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED;
48 case GeolocationErrorMessage::POSITION_UNAVAILABLE:
49 return content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE;
50 case GeolocationErrorMessage::TIMEOUT:
51 return content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT;
52 }
53 }
54
55 content::Geoposition ConvertLocationMessage(
56 const GeolocationCoordinatesMessage& coordinates) {
57 content::Geoposition output;
58 output.latitude = coordinates.latitude();
59 output.longitude = coordinates.longitude();
60 output.altitude = coordinates.altitude();
61 output.accuracy = coordinates.accuracy();
62 output.altitude_accuracy = coordinates.altitude_accuracy();
63 output.heading = coordinates.heading();
64 output.speed = coordinates.speed();
65 output.timestamp = base::Time::Now();
66 output.error_code = content::Geoposition::ErrorCode::ERROR_CODE_NONE;
67 return output;
68 }
69
70 } // namespace
71
72 EngineGeolocationFeature::EngineGeolocationFeature() : weak_factory_(this) {}
73
74 EngineGeolocationFeature::~EngineGeolocationFeature() {}
75
76 void EngineGeolocationFeature::set_outgoing_message_processor(
77 std::unique_ptr<BlimpMessageProcessor> message_processor) {
78 DCHECK(message_processor);
79 outgoing_message_processor_ = std::move(message_processor);
80 }
81
82 content::GeolocationDelegate*
83 EngineGeolocationFeature::CreateGeolocationDelegate() {
84 return new BlimpGeolocationDelegate(weak_factory_.GetWeakPtr());
85 }
86
87 void EngineGeolocationFeature::ProcessMessage(
88 std::unique_ptr<BlimpMessage> message,
89 const net::CompletionCallback& callback) {
90 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case());
91
92 int result = net::OK;
93 const GeolocationMessage& geolocation_message = message->geolocation();
94 switch (geolocation_message.type_case()) {
95 case GeolocationMessage::kCoordinates: {
96 const GeolocationCoordinatesMessage& location =
97 geolocation_message.coordinates();
98 content::Geoposition output = ConvertLocationMessage(location);
99 NotifyCallback(output);
100 break;
101 }
102 case GeolocationMessage::kError: {
103 const GeolocationErrorMessage& error_message =
104 geolocation_message.error();
105 content::Geoposition output;
106 output.error_message = error_message.error_message();
107 output.error_code = ConvertErrorCode(error_message.error_code());
108 NotifyCallback(output);
109 break;
110 }
111 case GeolocationMessage::kSetInterestLevel:
112 case GeolocationMessage::kRequestRefresh:
113 case GeolocationMessage::TYPE_NOT_SET:
114 result = net::ERR_UNEXPECTED;
115 }
116 if (!callback.is_null()) {
117 callback.Run(result);
118 }
119 }
120
121 void EngineGeolocationFeature::NotifyCallback(
122 const content::Geoposition& position) {
123 geoposition_received_callback_.Run(position);
124 }
125
126 void EngineGeolocationFeature::RequestAccuracy(
127 GeolocationSetInterestLevelMessage::Level level) {
128 GeolocationMessage* geolocation_message = nullptr;
129 std::unique_ptr<BlimpMessage> blimp_message =
130 CreateBlimpMessage(&geolocation_message);
131
132 GeolocationSetInterestLevelMessage* geolocation_interest =
133 geolocation_message->mutable_set_interest_level();
134 geolocation_interest->set_level(level);
135
136 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
137 net::CompletionCallback());
138 }
139
140 void EngineGeolocationFeature::RequestRefresh() {
141 GeolocationMessage* geolocation_message = nullptr;
142 std::unique_ptr<BlimpMessage> blimp_message =
143 CreateBlimpMessage(&geolocation_message);
144
145 geolocation_message->mutable_request_refresh();
146
147 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
148 net::CompletionCallback());
149 }
150
151 void EngineGeolocationFeature::SetUpdateCallback(
152 const GeopositionReceivedCallback& callback) {
153 geoposition_received_callback_ = callback;
154 }
155
156 } // namespace engine
157 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698