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 DCHECK(location_provider); | |
Wez
2016/08/02 01:18:10
nit: Strictly you don't need this, since you de-re
CJ
2016/08/03 20:06:03
Done.
| |
23 location_provider_ = std::move(location_provider); | |
Wez
2016/08/02 01:18:10
nit: Move this std::move() to be a member-initiali
CJ
2016/08/03 20:06:03
Done.
| |
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 | |
41 const GeolocationMessage& geolocation_message = message->geolocation(); | |
42 switch (geolocation_message.type_case()) { | |
43 case GeolocationMessage::kSetInterestLevel: { | |
44 const GeolocationSetInterestLevelMessage& set_level_message = | |
45 geolocation_message.set_interest_level(); | |
46 OnGeolocationInterestUpdate(set_level_message.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) << "Unsupported message type."; | |
Wez
2016/08/02 01:18:10
nit: This should be "Invalid message type." - "Uns
CJ
2016/08/03 20:06:03
Done.
| |
56 callback.Run(net::ERR_UNEXPECTED); | |
57 return; | |
58 } | |
59 callback.Run(net::OK); | |
Wez
2016/08/02 01:18:11
nit: Consider using ScopedTaskRunner for this.
As
CJ
2016/08/03 20:06:03
Done.
| |
60 } | |
61 | |
62 void GeolocationFeature::OnLocationUpdate( | |
63 const device::LocationProvider* location_provider, | |
64 const device::Geoposition& position) { | |
65 if (position.error_code == device::Geoposition::ERROR_CODE_NONE) { | |
66 SendGeolocationPositionMessage(position); | |
67 } else { | |
68 SendGeolocationErrorMessage(position.error_code, position.error_message); | |
Wez
2016/08/02 01:18:10
If you change the signature of SendGeolocationErro
CJ
2016/08/03 20:06:03
Done.
| |
69 } | |
70 } | |
71 | |
72 void GeolocationFeature::OnGeolocationInterestUpdate( | |
73 GeolocationSetInterestLevelMessage::Level level) { | |
74 switch (level) { | |
75 case GeolocationSetInterestLevelMessage::HIGH_ACCURACY: | |
76 location_provider_->StartProvider(true); | |
77 break; | |
78 case GeolocationSetInterestLevelMessage::LOW_ACCURACY: | |
79 location_provider_->StartProvider(false); | |
80 break; | |
81 case GeolocationSetInterestLevelMessage::NO_INTEREST: | |
82 location_provider_->StopProvider(); | |
83 break; | |
84 } | |
85 } | |
86 | |
87 void GeolocationFeature::SendGeolocationPositionMessage( | |
88 const device::Geoposition& position) { | |
89 GeolocationMessage* geolocation_message = nullptr; | |
90 std::unique_ptr<BlimpMessage> blimp_message = | |
91 CreateBlimpMessage(&geolocation_message); | |
92 GeolocationCoordinatesMessage* coordinates = | |
93 geolocation_message->mutable_coordinates(); | |
94 | |
95 coordinates->set_latitude(position.latitude); | |
96 coordinates->set_longitude(position.longitude); | |
97 coordinates->set_altitude(position.altitude); | |
98 coordinates->set_accuracy(position.accuracy); | |
99 coordinates->set_altitude_accuracy(position.altitude_accuracy); | |
100 coordinates->set_heading(position.heading); | |
101 coordinates->set_speed(position.speed); | |
102 | |
103 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
104 net::CompletionCallback()); | |
Wez
2016/08/02 01:18:10
nit: Perhaps we should register a completion callb
CJ
2016/08/03 20:06:03
How does one know if the network is ready?
Wez
2016/08/09 01:40:33
By passing a non-null CompletionCallback and setti
CJ
2016/08/09 21:40:37
Done.
| |
105 } | |
106 | |
107 void GeolocationFeature::SendGeolocationErrorMessage( | |
108 const device::Geoposition::ErrorCode& error_code, | |
109 const std::string& error_message) { | |
110 GeolocationMessage* geolocation_message = nullptr; | |
111 std::unique_ptr<BlimpMessage> blimp_message = | |
112 CreateBlimpMessage(&geolocation_message); | |
113 | |
114 GeolocationErrorMessage* error = geolocation_message->mutable_error(); | |
115 switch (error_code) { | |
116 case device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED: | |
117 error->set_error_code(GeolocationErrorMessage::PERMISSION_DENIED); | |
118 break; | |
119 case device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE: | |
120 error->set_error_code(GeolocationErrorMessage::POSITION_UNAVAILABLE); | |
121 break; | |
122 case device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT: | |
123 error->set_error_code(GeolocationErrorMessage::TIMEOUT); | |
124 break; | |
125 case device::Geoposition::ErrorCode::ERROR_CODE_NONE: | |
126 NOTREACHED(); | |
127 break; | |
128 } | |
129 | |
130 error->set_error_message(error_message); | |
131 | |
132 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
133 net::CompletionCallback()); | |
134 } | |
135 | |
136 } // namespace client | |
137 } // namespace blimp | |
OLD | NEW |