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/engine/feature/geolocation/engine_geolocation_feature.h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/threading/thread_task_runner_handle.h" | |
13 #include "blimp/common/create_blimp_message.h" | |
14 #include "blimp/common/proto/blimp_message.pb.h" | |
15 #include "blimp/common/proto/geolocation.pb.h" | |
16 #include "device/geolocation/geolocation_delegate.h" | |
17 #include "device/geolocation/location_provider.h" | |
18 #include "device/geolocation/geoposition.h" | |
19 #include "net/base/net_errors.h" | |
20 | |
21 namespace blimp { | |
22 namespace engine { | |
23 namespace { | |
24 | |
25 class BlimpGeolocationDelegate : public device::GeolocationDelegate { | |
26 public: | |
27 explicit BlimpGeolocationDelegate( | |
28 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate) { | |
29 feature_delegate_ = feature_delegate; | |
30 feature_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
31 } | |
32 | |
33 bool UseNetworkLocationProviders() final { return false; } | |
34 | |
35 std::unique_ptr<device::LocationProvider> OverrideSystemLocationProvider() | |
36 final { | |
37 return base::MakeUnique<BlimpLocationProvider>(feature_delegate_, | |
38 feature_task_runner_); | |
39 } | |
40 | |
41 private: | |
42 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate_; | |
43 scoped_refptr<base::SingleThreadTaskRunner> feature_task_runner_; | |
44 | |
45 DISALLOW_COPY_AND_ASSIGN(BlimpGeolocationDelegate); | |
46 }; | |
47 | |
48 device::Geoposition::ErrorCode ConvertErrorCode( | |
49 const GeolocationErrorMessage::ErrorCode& error_code) { | |
50 switch (error_code) { | |
51 case GeolocationErrorMessage::PERMISSION_DENIED: | |
52 return device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; | |
53 case GeolocationErrorMessage::POSITION_UNAVAILABLE: | |
54 return device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; | |
55 case GeolocationErrorMessage::TIMEOUT: | |
56 return device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; | |
57 } | |
58 } | |
59 | |
60 device::Geoposition ConvertLocationMessage( | |
61 const GeolocationCoordinatesMessage& coordinates) { | |
62 device::Geoposition output; | |
63 output.latitude = coordinates.latitude(); | |
64 output.longitude = coordinates.longitude(); | |
65 output.altitude = coordinates.altitude(); | |
66 output.accuracy = coordinates.accuracy(); | |
67 output.altitude_accuracy = coordinates.altitude_accuracy(); | |
68 output.heading = coordinates.heading(); | |
69 output.speed = coordinates.speed(); | |
70 output.timestamp = base::Time::Now(); | |
71 output.error_code = device::Geoposition::ErrorCode::ERROR_CODE_NONE; | |
72 return output; | |
73 } | |
74 | |
75 } // namespace | |
76 | |
77 EngineGeolocationFeature::EngineGeolocationFeature() : weak_factory_(this) {} | |
78 | |
79 EngineGeolocationFeature::~EngineGeolocationFeature() {} | |
80 | |
81 void EngineGeolocationFeature::set_outgoing_message_processor( | |
82 std::unique_ptr<BlimpMessageProcessor> message_processor) { | |
83 DCHECK(message_processor); | |
84 outgoing_message_processor_ = std::move(message_processor); | |
85 } | |
86 | |
87 device::GeolocationDelegate* | |
88 EngineGeolocationFeature::CreateGeolocationDelegate() { | |
89 return new BlimpGeolocationDelegate(weak_factory_.GetWeakPtr()); | |
90 } | |
91 | |
92 void EngineGeolocationFeature::ProcessMessage( | |
93 std::unique_ptr<BlimpMessage> message, | |
94 const net::CompletionCallback& callback) { | |
95 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); | |
96 | |
97 int result = net::OK; | |
98 const GeolocationMessage& geolocation_message = message->geolocation(); | |
99 switch (geolocation_message.type_case()) { | |
100 case GeolocationMessage::kCoordinates: { | |
101 const GeolocationCoordinatesMessage& location = | |
102 geolocation_message.coordinates(); | |
103 device::Geoposition output = ConvertLocationMessage(location); | |
104 NotifyCallback(output); | |
105 break; | |
106 } | |
107 case GeolocationMessage::kError: { | |
108 const GeolocationErrorMessage& error_message = | |
109 geolocation_message.error(); | |
110 device::Geoposition output; | |
111 output.error_message = error_message.error_message(); | |
112 output.error_code = ConvertErrorCode(error_message.error_code()); | |
113 NotifyCallback(output); | |
114 break; | |
115 } | |
116 case GeolocationMessage::kSetInterestLevel: | |
117 case GeolocationMessage::kRequestRefresh: | |
118 case GeolocationMessage::TYPE_NOT_SET: | |
119 result = net::ERR_UNEXPECTED; | |
120 } | |
121 if (!callback.is_null()) { | |
122 callback.Run(result); | |
123 } | |
124 } | |
125 | |
126 void EngineGeolocationFeature::NotifyCallback( | |
127 const device::Geoposition& position) { | |
128 geoposition_received_callback_.Run(position); | |
129 } | |
130 | |
131 void EngineGeolocationFeature::RequestAccuracy( | |
132 GeolocationSetInterestLevelMessage::Level level) { | |
133 GeolocationMessage* geolocation_message = nullptr; | |
134 std::unique_ptr<BlimpMessage> blimp_message = | |
135 CreateBlimpMessage(&geolocation_message); | |
136 | |
137 GeolocationSetInterestLevelMessage* geolocation_interest = | |
138 geolocation_message->mutable_set_interest_level(); | |
139 geolocation_interest->set_level(level); | |
140 | |
141 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
142 net::CompletionCallback()); | |
143 } | |
144 | |
145 void EngineGeolocationFeature::OnPermissionGranted() { | |
146 GeolocationMessage* geolocation_message = nullptr; | |
147 std::unique_ptr<BlimpMessage> blimp_message = | |
148 CreateBlimpMessage(&geolocation_message); | |
149 | |
150 geolocation_message->mutable_request_refresh(); | |
151 | |
152 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
153 net::CompletionCallback()); | |
154 } | |
155 | |
156 void EngineGeolocationFeature::SetUpdateCallback( | |
157 const GeopositionReceivedCallback& callback) { | |
158 geoposition_received_callback_ = callback; | |
159 } | |
160 | |
161 } // namespace engine | |
162 } // namespace blimp | |
OLD | NEW |