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/common/create_blimp_message.h" | |
6 #include "blimp/common/proto/blimp_message.pb.h" | |
7 #include "blimp/common/proto/geolocation.pb.h" | |
8 #include "blimp/engine/feature/geolocation/engine_geolocation_feature.h" | |
9 #include "content/public/common/geoposition.h" | |
10 | |
11 namespace blimp { | |
12 namespace engine { | |
13 namespace { | |
14 | |
15 content::Geoposition::ErrorCode GetErrorCode( | |
16 const ErrorMessage::ErrorCode& error_code) { | |
17 switch (error_code) { | |
18 case ErrorMessage::ERROR_CODE_NONE: | |
19 return content::Geoposition::ErrorCode::ERROR_CODE_NONE; | |
20 case ErrorMessage::ERROR_CODE_PERMISSION_DENIED: | |
21 return content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; | |
22 case ErrorMessage::ERROR_CODE_POSITION_UNAVAILABLE: | |
23 return content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; | |
24 case ErrorMessage::ERROR_CODE_TIMEOUT: | |
25 return content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; | |
26 } | |
27 } | |
28 | |
29 void ConvertLocationMessage(content::Geoposition* output, | |
30 const LocationMessage& location) { | |
31 output->latitude = location.latitude(); | |
32 output->longitude = location.longitude(); | |
33 output->altitude = location.altitude(); | |
34 output->accuracy = location.accuracy(); | |
35 output->altitude_accuracy = location.altitude_accuracy(); | |
36 output->heading = location.heading(); | |
37 output->speed = location.speed(); | |
38 output->timestamp = base::Time::FromJsTime(location.timestamp_millis()); | |
39 } | |
40 | |
41 } // namespace | |
42 | |
43 EngineGeolocationFeature::EngineGeolocationFeature() {} | |
44 EngineGeolocationFeature::~EngineGeolocationFeature() {} | |
45 | |
46 void EngineGeolocationFeature::set_outgoing_message_processor( | |
47 std::unique_ptr<BlimpMessageProcessor> message_processor) { | |
48 DCHECK(message_processor); | |
49 outgoing_message_processor_ = std::move(message_processor); | |
50 } | |
51 | |
52 void EngineGeolocationFeature::SendUpdateListenStateMessage( | |
53 bool enable_high_accuracy) { | |
54 GeolocationMessage* geolocation_message; | |
55 std::unique_ptr<BlimpMessage> blimp_message = | |
56 CreateBlimpMessage(&geolocation_message); | |
57 | |
58 UpdateListenStateMessage* details = | |
59 geolocation_message->mutable_update_listen_state(); | |
60 if (enable_high_accuracy) { | |
61 details->set_listen_state(UpdateListenStateMessage::ACCURACY_HIGH); | |
62 } else { | |
63 details->set_listen_state(UpdateListenStateMessage::ACCURACY_LOW); | |
64 } | |
65 | |
66 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
67 net::CompletionCallback()); | |
68 } | |
69 | |
70 void EngineGeolocationFeature::SendStopListenStateMessage() { | |
71 GeolocationMessage* geolocation_message; | |
72 std::unique_ptr<BlimpMessage> blimp_message = | |
73 CreateBlimpMessage(&geolocation_message); | |
74 | |
75 UpdateListenStateMessage* details = | |
76 geolocation_message->mutable_update_listen_state(); | |
77 details->set_listen_state(UpdateListenStateMessage::STOPPED); | |
78 | |
79 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
80 net::CompletionCallback()); | |
81 } | |
82 | |
83 void EngineGeolocationFeature::SendRequestRefreshMessage() { | |
84 GeolocationMessage* geolocation_message; | |
85 std::unique_ptr<BlimpMessage> blimp_message = | |
86 CreateBlimpMessage(&geolocation_message); | |
87 | |
88 geolocation_message->mutable_request_refresh(); | |
89 | |
90 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
91 net::CompletionCallback()); | |
92 } | |
93 | |
94 void EngineGeolocationFeature::ProcessMessage( | |
95 std::unique_ptr<BlimpMessage> message, | |
96 const net::CompletionCallback& callback) { | |
97 DCHECK(!callback.is_null()); | |
98 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); | |
99 DCHECK(location_provider_); | |
100 | |
101 const GeolocationMessage& geolocation_message = message->geolocation(); | |
102 content::Geoposition output; | |
103 | |
104 switch (geolocation_message.type_case()) { | |
105 case GeolocationMessage::kLocation: { | |
106 const LocationMessage location = geolocation_message.location(); | |
107 | |
Kevin M
2016/06/29 17:52:56
remove these blank lines - they're part of the sam
CJ
2016/07/11 23:21:07
Done.
| |
108 ConvertLocationMessage(&output, location); | |
109 | |
110 location_provider_->OnLocationResponse(output); | |
111 break; | |
112 } | |
113 case GeolocationMessage::kError: { | |
114 const ErrorMessage error_message = geolocation_message.error(); | |
115 output.error_message = error_message.error_message(); | |
116 | |
Kevin M
2016/06/29 17:52:56
ditto
CJ
2016/07/11 23:21:07
Done.
| |
117 output.error_code = GetErrorCode(error_message.error_code()); | |
118 | |
119 location_provider_->OnLocationResponse(output); | |
120 break; | |
121 } | |
122 case GeolocationMessage::kUpdateListenState: | |
123 case GeolocationMessage::kRequestRefresh: | |
124 NOTREACHED() << "Engine received unexpected geolocation type."; | |
Kevin M
2016/06/29 17:52:56
break;
CJ
2016/07/11 23:21:07
Done.
| |
125 case GeolocationMessage::TYPE_NOT_SET: | |
126 NOTREACHED(); | |
127 break; | |
128 } | |
129 } | |
130 | |
131 } // namespace engine | |
132 } // namespace blimp | |
OLD | NEW |