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 // A provider of services needed by Geolocation. | |
Wez
2016/07/15 01:46:17
nit: Suggest something more like "content::Geoloca
CJ
2016/07/18 21:11:56
Done.
| |
22 class BlimpGeolocationDelegate : public content::GeolocationDelegate { | |
23 public: | |
24 explicit BlimpGeolocationDelegate( | |
25 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate) { | |
26 feature_delegate_ = feature_delegate; | |
27 } | |
28 | |
29 bool UseNetworkLocationProviders() final { return false; } | |
Wez
2016/07/15 01:46:17
BTW I had no idea we were using final in C++ now.
CJ
2016/07/18 21:11:56
Acknowledged.
| |
30 | |
31 std::unique_ptr<content::LocationProvider> OverrideSystemLocationProvider() | |
32 final { | |
33 BlimpLocationProvider* location_provider = | |
34 new BlimpLocationProvider(feature_delegate_); | |
35 return base::WrapUnique(location_provider); | |
Wez
2016/07/15 01:46:17
nit: Just put the new inside WrapUnique()
CJ
2016/07/18 21:11:56
Done.
| |
36 } | |
37 | |
38 private: | |
39 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate_; | |
40 | |
41 DISALLOW_COPY_AND_ASSIGN(BlimpGeolocationDelegate); | |
42 }; | |
43 | |
44 content::Geoposition::ErrorCode ConvertErrorCode( | |
45 const GeolocationErrorMessage::ErrorCode& error_code) { | |
46 switch (error_code) { | |
47 case GeolocationErrorMessage::PERMISSION_DENIED: | |
48 return content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; | |
49 case GeolocationErrorMessage::POSITION_UNAVAILABLE: | |
50 return content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; | |
51 case GeolocationErrorMessage::TIMEOUT: | |
52 return content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; | |
53 } | |
54 } | |
55 | |
56 content::Geoposition ConvertLocationMessage( | |
57 const GeolocationCoordinatesMessage& coordinates) { | |
58 content::Geoposition output; | |
59 output.latitude = coordinates.latitude(); | |
60 output.longitude = coordinates.longitude(); | |
61 output.altitude = coordinates.altitude(); | |
62 output.accuracy = coordinates.accuracy(); | |
63 output.altitude_accuracy = coordinates.altitude_accuracy(); | |
64 output.heading = coordinates.heading(); | |
65 output.speed = coordinates.speed(); | |
66 output.timestamp = base::Time::Now(); | |
67 output.error_code = content::Geoposition::ErrorCode::ERROR_CODE_NONE; | |
68 return output; | |
69 } | |
70 | |
71 } // namespace | |
72 | |
73 EngineGeolocationFeature::EngineGeolocationFeature() : weak_factory_(this) {} | |
74 | |
75 EngineGeolocationFeature::~EngineGeolocationFeature() {} | |
76 | |
77 void EngineGeolocationFeature::set_outgoing_message_processor( | |
78 std::unique_ptr<BlimpMessageProcessor> message_processor) { | |
79 DCHECK(message_processor); | |
80 outgoing_message_processor_ = std::move(message_processor); | |
81 } | |
82 | |
83 content::GeolocationDelegate* | |
84 EngineGeolocationFeature::CreateGeolocationDelegate() { | |
85 return new BlimpGeolocationDelegate(GetWeakPtr()); | |
Wez
2016/07/15 01:46:17
Just call weak_factory_.GetweakPtr() directly here
CJ
2016/07/18 21:11:56
Done.
| |
86 } | |
87 | |
88 base::WeakPtr<EngineGeolocationFeature> EngineGeolocationFeature::GetWeakPtr() { | |
89 return weak_factory_.GetWeakPtr(); | |
90 } | |
91 | |
92 void EngineGeolocationFeature::RequestAccuracy( | |
93 GeolocationSetInterestLevelMessage::Level level) { | |
94 GeolocationMessage* geolocation_message = nullptr; | |
95 std::unique_ptr<BlimpMessage> blimp_message = | |
96 CreateBlimpMessage(&geolocation_message); | |
97 | |
98 GeolocationSetInterestLevelMessage* geolocation_interest = | |
99 geolocation_message->mutable_set_interest_level(); | |
100 geolocation_interest->set_level(level); | |
101 | |
102 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
103 net::CompletionCallback()); | |
104 } | |
105 | |
106 void EngineGeolocationFeature::RequestRefresh() { | |
107 GeolocationMessage* geolocation_message = nullptr; | |
108 std::unique_ptr<BlimpMessage> blimp_message = | |
109 CreateBlimpMessage(&geolocation_message); | |
110 | |
111 geolocation_message->mutable_request_refresh(); | |
112 | |
113 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
114 net::CompletionCallback()); | |
115 } | |
116 | |
117 void EngineGeolocationFeature::SetUpdateCallback( | |
118 const base::Callback<void(const content::Geoposition&)>& callback) { | |
119 geoposition_received_callback_ = callback; | |
120 } | |
121 | |
122 void EngineGeolocationFeature::ProcessMessage( | |
123 std::unique_ptr<BlimpMessage> message, | |
124 const net::CompletionCallback& callback) { | |
125 DCHECK(!callback.is_null()); | |
126 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); | |
127 | |
128 const GeolocationMessage& geolocation_message = message->geolocation(); | |
129 | |
130 switch (geolocation_message.type_case()) { | |
131 case GeolocationMessage::kCoordinates: { | |
132 const GeolocationCoordinatesMessage& location = | |
133 geolocation_message.coordinates(); | |
134 content::Geoposition output = ConvertLocationMessage(location); | |
135 NotifyCallback(output); | |
136 break; | |
137 } | |
138 case GeolocationMessage::kError: { | |
139 const GeolocationErrorMessage& error_message = | |
140 geolocation_message.error(); | |
141 content::Geoposition output; | |
142 output.error_message = error_message.error_message(); | |
143 output.error_code = ConvertErrorCode(error_message.error_code()); | |
144 NotifyCallback(output); | |
145 break; | |
146 } | |
147 case GeolocationMessage::kSetInterestLevel: | |
148 case GeolocationMessage::kRequestRefresh: | |
149 case GeolocationMessage::TYPE_NOT_SET: | |
150 DLOG(FATAL) << "Client sent unexpected message type." | |
151 << geolocation_message.type_case(); | |
152 break; | |
153 } | |
154 callback.Run(net::OK); | |
155 } | |
156 | |
157 void EngineGeolocationFeature::NotifyCallback( | |
158 const content::Geoposition& position) { | |
159 geoposition_received_callback_.Run(position); | |
160 } | |
161 | |
162 } // namespace engine | |
163 } // namespace blimp | |
OLD | NEW |