OLD | NEW |
---|---|
(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 const GeolocationSetInterestLevelMessage& set_level_message = | |
47 geolocation_message.set_interest_level(); | |
48 SetInterestLevel(set_level_message.level()); | |
Kevin M
2016/08/09 22:08:55
Can just inline line 47 here
CJ
2016/08/10 00:13:49
Done.
| |
49 break; | |
50 } | |
51 case GeolocationMessage::kRequestRefresh: | |
52 location_provider_->RequestRefresh(); | |
53 break; | |
54 case GeolocationMessage::kCoordinates: | |
55 case GeolocationMessage::kError: | |
56 case GeolocationMessage::TYPE_NOT_SET: | |
57 DLOG(FATAL) << "Invalid message type."; | |
Kevin M
2016/08/09 22:08:55
output the type_case in the log
CJ
2016/08/10 00:13:49
Done.
| |
58 result = net::ERR_UNEXPECTED; | |
59 break; | |
60 } | |
61 | |
62 if (!callback.is_null()) { | |
63 callback.Run(result); | |
64 } | |
65 } | |
66 | |
67 void GeolocationFeature::OnLocationUpdate( | |
68 const device::LocationProvider* location_provider, | |
69 const device::Geoposition& position) { | |
70 switch (position.error_code) { | |
71 case device::Geoposition::ERROR_CODE_NONE: | |
72 SendGeolocationPositionMessage(position); | |
73 break; | |
74 case device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED: | |
75 SendGeolocationErrorMessage(GeolocationErrorMessage::PERMISSION_DENIED, | |
76 position.error_message); | |
77 break; | |
78 case device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE: | |
79 SendGeolocationErrorMessage(GeolocationErrorMessage::POSITION_UNAVAILABLE, | |
80 position.error_message); | |
81 break; | |
82 case device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT: | |
83 SendGeolocationErrorMessage(GeolocationErrorMessage::TIMEOUT, | |
84 position.error_message); | |
85 break; | |
86 } | |
87 } | |
88 | |
89 void GeolocationFeature::SetInterestLevel( | |
90 GeolocationSetInterestLevelMessage::Level level) { | |
91 switch (level) { | |
92 case GeolocationSetInterestLevelMessage::HIGH_ACCURACY: | |
93 location_provider_->StartProvider(true); | |
94 break; | |
95 case GeolocationSetInterestLevelMessage::LOW_ACCURACY: | |
96 location_provider_->StartProvider(false); | |
97 break; | |
98 case GeolocationSetInterestLevelMessage::NO_INTEREST: | |
99 location_provider_->StopProvider(); | |
100 break; | |
101 } | |
102 } | |
103 | |
104 void GeolocationFeature::SendGeolocationPositionMessage( | |
105 const device::Geoposition& position) { | |
106 GeolocationMessage* geolocation_message = nullptr; | |
107 std::unique_ptr<BlimpMessage> blimp_message = | |
108 CreateBlimpMessage(&geolocation_message); | |
109 GeolocationCoordinatesMessage* coordinates = | |
110 geolocation_message->mutable_coordinates(); | |
111 | |
112 coordinates->set_latitude(position.latitude); | |
113 coordinates->set_longitude(position.longitude); | |
114 coordinates->set_altitude(position.altitude); | |
115 coordinates->set_accuracy(position.accuracy); | |
116 coordinates->set_altitude_accuracy(position.altitude_accuracy); | |
117 coordinates->set_heading(position.heading); | |
118 coordinates->set_speed(position.speed); | |
119 | |
120 if (can_send_message_) { | |
121 can_send_message_ = false; | |
122 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
123 completion_callback_); | |
124 } | |
125 } | |
126 | |
127 void GeolocationFeature::SendGeolocationErrorMessage( | |
128 const GeolocationErrorMessage::ErrorCode& error_code, | |
129 const std::string& error_message) { | |
130 GeolocationMessage* geolocation_message = nullptr; | |
131 std::unique_ptr<BlimpMessage> blimp_message = | |
132 CreateBlimpMessage(&geolocation_message); | |
133 | |
134 GeolocationErrorMessage* error = geolocation_message->mutable_error(); | |
135 error->set_error_code(error_code); | |
136 error->set_error_message(error_message); | |
137 | |
138 if (can_send_message_) { | |
139 can_send_message_ = false; | |
140 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
141 completion_callback_); | |
142 } | |
143 } | |
144 | |
145 void GeolocationFeature::OnSendComplete(int result) { | |
146 can_send_message_ = true; | |
Kevin M
2016/08/09 22:08:55
Should we send a new location if a Send was previo
CJ
2016/08/10 00:13:49
Done.
| |
147 } | |
148 | |
149 } // namespace client | |
150 } // namespace blimp | |
OLD | NEW |