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 "blimp/common/proto/geolocation.pb.h" | |
Kevin M
2016/08/01 20:51:04
Already included in .h
CJ
2016/08/02 00:36:01
Done.
| |
14 #include "device/geolocation/geoposition.h" | |
15 #include "device/geolocation/location_provider.h" | |
16 #include "net/base/net_errors.h" | |
17 | |
18 namespace blimp { | |
19 namespace client { | |
20 | |
21 GeolocationFeature::GeolocationFeature( | |
22 std::unique_ptr<device::LocationProvider> location_provider) { | |
23 location_provider_ = std::move(location_provider); | |
24 location_provider_->SetUpdateCallback(base::Bind( | |
25 &GeolocationFeature::OnLocationUpdate, | |
26 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 } | |
35 | |
36 void GeolocationFeature::ProcessMessage( | |
37 std::unique_ptr<BlimpMessage> message, | |
38 const net::CompletionCallback& callback) { | |
39 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); | |
40 DCHECK(location_provider_); | |
Kevin M
2016/08/01 20:51:04
You can DCHECK location_provider_ in the ctor, but
CJ
2016/08/02 00:36:00
Done.
| |
41 | |
42 const GeolocationMessage& geolocation_message = message->geolocation(); | |
43 switch (geolocation_message.type_case()) { | |
44 case GeolocationMessage::kSetInterestLevel: { | |
45 const GeolocationSetInterestLevelMessage& set_level_message = | |
Kevin M
2016/08/01 20:51:04
You can inline this and remove the braces
CJ
2016/08/02 00:36:01
What does "inline" mean in this context?
| |
46 geolocation_message.set_interest_level(); | |
47 OnGeolocationInterestUpdate(set_level_message.level()); | |
48 break; | |
49 } | |
50 case GeolocationMessage::kRequestRefresh: | |
51 location_provider_->RequestRefresh(); | |
52 break; | |
53 case GeolocationMessage::kCoordinates: | |
54 case GeolocationMessage::kError: | |
55 case GeolocationMessage::TYPE_NOT_SET: | |
56 DLOG(FATAL) << "Unsupported message type."; | |
57 callback.Run(net::ERR_UNEXPECTED); | |
58 return; | |
59 } | |
60 callback.Run(net::OK); | |
61 } | |
62 | |
63 void GeolocationFeature::OnLocationUpdate( | |
64 const device::LocationProvider* location_provider, | |
65 const device::Geoposition& position) { | |
66 if (position.error_code == device::Geoposition::ERROR_CODE_NONE) { | |
67 SendGeolocationPositionMessage(position); | |
68 } else { | |
69 SendGeolocationErrorMessage(position.error_code, position.error_message); | |
70 } | |
71 } | |
72 | |
73 void GeolocationFeature::OnGeolocationInterestUpdate( | |
74 GeolocationSetInterestLevelMessage::Level level) { | |
75 switch (level) { | |
76 case GeolocationSetInterestLevelMessage::HIGH_ACCURACY: | |
77 location_provider_->StartProvider(true); | |
78 break; | |
79 case GeolocationSetInterestLevelMessage::LOW_ACCURACY: | |
80 location_provider_->StartProvider(false); | |
81 break; | |
82 case GeolocationSetInterestLevelMessage::NO_INTEREST: | |
83 location_provider_->StopProvider(); | |
84 break; | |
85 } | |
86 } | |
87 | |
88 void GeolocationFeature::SendGeolocationPositionMessage( | |
89 const device::Geoposition& position) { | |
90 GeolocationMessage* geolocation_message = nullptr; | |
91 std::unique_ptr<BlimpMessage> blimp_message = | |
92 CreateBlimpMessage(&geolocation_message); | |
93 GeolocationCoordinatesMessage* coordinates = | |
94 geolocation_message->mutable_coordinates(); | |
95 | |
96 coordinates->set_latitude(position.latitude); | |
97 coordinates->set_longitude(position.longitude); | |
98 coordinates->set_altitude(position.altitude); | |
99 coordinates->set_accuracy(position.accuracy); | |
100 coordinates->set_altitude_accuracy(position.altitude_accuracy); | |
101 coordinates->set_heading(position.heading); | |
102 coordinates->set_speed(position.speed); | |
103 | |
104 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
105 net::CompletionCallback()); | |
106 } | |
107 | |
108 void GeolocationFeature::SendGeolocationErrorMessage( | |
109 const device::Geoposition::ErrorCode& error_code, | |
110 const std::string& error_message) { | |
111 GeolocationMessage* geolocation_message = nullptr; | |
112 std::unique_ptr<BlimpMessage> blimp_message = | |
113 CreateBlimpMessage(&geolocation_message); | |
114 | |
115 GeolocationErrorMessage* error = geolocation_message->mutable_error(); | |
116 switch (error_code) { | |
117 case device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED: | |
118 error->set_error_code(GeolocationErrorMessage::PERMISSION_DENIED); | |
119 break; | |
120 case device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE: | |
121 error->set_error_code(GeolocationErrorMessage::POSITION_UNAVAILABLE); | |
122 break; | |
123 case device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT: | |
124 error->set_error_code(GeolocationErrorMessage::TIMEOUT); | |
125 break; | |
126 case device::Geoposition::ErrorCode::ERROR_CODE_NONE: | |
127 NOTREACHED(); | |
128 break; | |
129 } | |
130 | |
131 error->set_error_message(error_message); | |
132 | |
133 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
134 net::CompletionCallback()); | |
135 } | |
136 | |
137 } // namespace client | |
138 } // namespace blimp | |
OLD | NEW |