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/core/geolocation/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 sending_message_ = false; | |
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 case GeolocationMessage::kRequestRefresh: | |
49 location_provider_->OnPermissionGranted(); | |
50 break; | |
51 case GeolocationMessage::kCoordinates: | |
52 case GeolocationMessage::kError: | |
53 case GeolocationMessage::TYPE_NOT_SET: | |
54 result = net::ERR_UNEXPECTED; | |
55 break; | |
56 } | |
57 | |
58 if (!callback.is_null()) { | |
59 callback.Run(result); | |
60 } | |
61 } | |
62 | |
63 void GeolocationFeature::OnLocationUpdate( | |
64 const device::LocationProvider* location_provider, | |
65 const device::Geoposition& position) { | |
Wez
2016/08/19 18:50:25
nit: DCHECK_EQ(location_provider_, location_provid
CJ
2016/08/19 22:22:33
Done.
| |
66 if (!sending_message_) { | |
67 switch (position.error_code) { | |
68 case device::Geoposition::ERROR_CODE_NONE: | |
69 SendGeolocationPositionMessage(position); | |
70 break; | |
71 case device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED: | |
72 SendGeolocationErrorMessage(GeolocationErrorMessage::PERMISSION_DENIED, | |
73 position.error_message); | |
74 break; | |
75 case device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE: | |
76 SendGeolocationErrorMessage( | |
77 GeolocationErrorMessage::POSITION_UNAVAILABLE, | |
78 position.error_message); | |
79 break; | |
80 case device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT: | |
81 SendGeolocationErrorMessage(GeolocationErrorMessage::TIMEOUT, | |
82 position.error_message); | |
83 break; | |
84 } | |
85 } else { | |
86 need_to_send_message_ = true; | |
87 } | |
88 } | |
89 | |
90 void GeolocationFeature::SetInterestLevel( | |
91 GeolocationSetInterestLevelMessage::Level level) { | |
92 switch (level) { | |
93 case GeolocationSetInterestLevelMessage::HIGH_ACCURACY: | |
94 location_provider_->StartProvider(true); | |
95 break; | |
96 case GeolocationSetInterestLevelMessage::LOW_ACCURACY: | |
97 location_provider_->StartProvider(false); | |
98 break; | |
99 case GeolocationSetInterestLevelMessage::NO_INTEREST: | |
100 location_provider_->StopProvider(); | |
101 break; | |
102 } | |
103 } | |
104 | |
105 void GeolocationFeature::SendGeolocationPositionMessage( | |
106 const device::Geoposition& position) { | |
107 GeolocationMessage* geolocation_message = nullptr; | |
108 std::unique_ptr<BlimpMessage> blimp_message = | |
109 CreateBlimpMessage(&geolocation_message); | |
110 | |
111 GeolocationCoordinatesMessage* coordinates = | |
112 geolocation_message->mutable_coordinates(); | |
113 coordinates->set_latitude(position.latitude); | |
114 coordinates->set_longitude(position.longitude); | |
115 coordinates->set_altitude(position.altitude); | |
116 coordinates->set_accuracy(position.accuracy); | |
117 coordinates->set_altitude_accuracy(position.altitude_accuracy); | |
118 coordinates->set_heading(position.heading); | |
119 coordinates->set_speed(position.speed); | |
120 | |
121 sending_message_ = true; | |
122 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
123 completion_callback_); | |
124 } | |
125 | |
126 void GeolocationFeature::SendGeolocationErrorMessage( | |
127 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 sending_message_ = true; | |
138 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
139 completion_callback_); | |
140 } | |
141 | |
142 void GeolocationFeature::OnSendComplete(int result) { | |
143 sending_message_ = false; | |
144 if (need_to_send_message_) { | |
145 need_to_send_message_ = false; | |
146 OnLocationUpdate(location_provider_.get(), | |
147 location_provider_->GetPosition()); | |
148 } | |
149 } | |
150 | |
151 } // namespace client | |
152 } // namespace blimp | |
OLD | NEW |