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

Side by Side Diff: blimp/client/feature/geolocation_feature.cc

Issue 2161223003: Adds GeolocationFeature for Blimp Geolocation project. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@engine_feature_prep
Patch Set: Adding more deps to BUILD for device/geolocation. 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/client/feature/geolocation_feature.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10
11 #include "blimp/common/create_blimp_message.h"
12 #include "blimp/common/proto/blimp_message.pb.h"
13 #include "device/geolocation/geoposition.h"
14 #include "device/geolocation/location_provider.h"
15 #include "net/base/net_errors.h"
16
17 namespace blimp {
18 namespace client {
19
20 GeolocationFeature::GeolocationFeature(
21 std::unique_ptr<device::LocationProvider> location_provider)
22 : location_provider_(std::move(location_provider)),
23 completion_callback_(base::Bind(&GeolocationFeature::OnSendComplete,
24 base::Unretained(this))) {
25 location_provider_->SetUpdateCallback(base::Bind(
26 &GeolocationFeature::OnLocationUpdate, base::Unretained(this)));
27 }
28
29 GeolocationFeature::~GeolocationFeature() {}
30
31 void GeolocationFeature::set_outgoing_message_processor(
32 std::unique_ptr<BlimpMessageProcessor> processor) {
33 outgoing_message_processor_ = std::move(processor);
34 can_send_message_ = true;
35 }
36
37 void GeolocationFeature::ProcessMessage(
38 std::unique_ptr<BlimpMessage> message,
39 const net::CompletionCallback& callback) {
40 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case());
41
42 int result = net::OK;
43 const GeolocationMessage& geolocation_message = message->geolocation();
44 switch (geolocation_message.type_case()) {
45 case GeolocationMessage::kSetInterestLevel: {
46 SetInterestLevel(geolocation_message.set_interest_level().level());
47 break;
48 }
49 case GeolocationMessage::kRequestRefresh:
50 location_provider_->RequestRefresh();
51 break;
52 case GeolocationMessage::kCoordinates:
53 case GeolocationMessage::kError:
54 case GeolocationMessage::TYPE_NOT_SET:
55 DLOG(FATAL) << "Invalid message type: " << geolocation_message.type_case()
56 << ".";
57 result = net::ERR_UNEXPECTED;
58 break;
59 }
60
61 if (!callback.is_null()) {
62 callback.Run(result);
63 }
64 }
65
66 void GeolocationFeature::OnLocationUpdate(
67 const device::LocationProvider* location_provider,
68 const device::Geoposition& position) {
69 switch (position.error_code) {
70 case device::Geoposition::ERROR_CODE_NONE:
71 SendGeolocationPositionMessage(position);
72 break;
73 case device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED:
74 SendGeolocationErrorMessage(GeolocationErrorMessage::PERMISSION_DENIED,
75 position.error_message);
76 break;
77 case device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE:
78 SendGeolocationErrorMessage(GeolocationErrorMessage::POSITION_UNAVAILABLE,
79 position.error_message);
80 break;
81 case device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT:
82 SendGeolocationErrorMessage(GeolocationErrorMessage::TIMEOUT,
83 position.error_message);
84 break;
85 }
86 }
87
88 void GeolocationFeature::SetInterestLevel(
89 GeolocationSetInterestLevelMessage::Level level) {
90 switch (level) {
91 case GeolocationSetInterestLevelMessage::HIGH_ACCURACY:
92 location_provider_->StartProvider(true);
93 break;
94 case GeolocationSetInterestLevelMessage::LOW_ACCURACY:
95 location_provider_->StartProvider(false);
96 break;
97 case GeolocationSetInterestLevelMessage::NO_INTEREST:
98 location_provider_->StopProvider();
99 break;
100 }
101 }
102
103 void GeolocationFeature::SendGeolocationPositionMessage(
104 const device::Geoposition& position) {
105 GeolocationMessage* geolocation_message = nullptr;
106 std::unique_ptr<BlimpMessage> blimp_message =
107 CreateBlimpMessage(&geolocation_message);
108 GeolocationCoordinatesMessage* coordinates =
109 geolocation_message->mutable_coordinates();
110
111 coordinates->set_latitude(position.latitude);
112 coordinates->set_longitude(position.longitude);
113 coordinates->set_altitude(position.altitude);
114 coordinates->set_accuracy(position.accuracy);
115 coordinates->set_altitude_accuracy(position.altitude_accuracy);
116 coordinates->set_heading(position.heading);
117 coordinates->set_speed(position.speed);
118
119 if (can_send_message_) {
120 can_send_message_ = false;
121 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
122 completion_callback_);
123 }
124 }
125
126 void GeolocationFeature::SendGeolocationErrorMessage(
127 const GeolocationErrorMessage::ErrorCode& error_code,
128 const std::string& error_message) {
129 GeolocationMessage* geolocation_message = nullptr;
130 std::unique_ptr<BlimpMessage> blimp_message =
131 CreateBlimpMessage(&geolocation_message);
132
133 GeolocationErrorMessage* error = geolocation_message->mutable_error();
134 error->set_error_code(error_code);
135 error->set_error_message(error_message);
136
137 if (can_send_message_) {
138 can_send_message_ = false;
139 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
140 completion_callback_);
141 }
142 }
143
144 void GeolocationFeature::OnSendComplete(int result) {
145 can_send_message_ = true;
146 if (result != net::OK) {
147 device::Geoposition position;
148 location_provider_->GetPosition(&position);
149 OnLocationUpdate(location_provider_.get(), position);
Kevin M 2016/08/10 23:23:34 Won't this just update endlessly?
Wez 2016/08/12 00:39:42 Right; we only want to send a new update following
CJ 2016/08/12 21:57:46 Yeah, I wasn't sure what we are doing in the error
150 }
151 }
152
153 } // namespace client
154 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698