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