OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "blimp/engine/feature/geolocation/engine_geolocation_feature.h" | 5 #include "blimp/engine/feature/geolocation/engine_geolocation_feature.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "blimp/common/create_blimp_message.h" | 12 #include "blimp/common/create_blimp_message.h" |
13 #include "blimp/common/proto/blimp_message.pb.h" | 13 #include "blimp/common/proto/blimp_message.pb.h" |
14 #include "blimp/common/proto/geolocation.pb.h" | 14 #include "blimp/common/proto/geolocation.pb.h" |
15 #include "content/public/browser/geolocation_delegate.h" | 15 #include "device/geolocation/geolocation_delegate.h" |
16 #include "content/public/browser/location_provider.h" | 16 #include "device/geolocation/location_provider.h" |
17 #include "content/public/common/geoposition.h" | 17 #include "device/geolocation/geoposition.h" |
18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
19 | 19 |
20 namespace blimp { | 20 namespace blimp { |
21 namespace engine { | 21 namespace engine { |
22 namespace { | 22 namespace { |
23 class BlimpGeolocationDelegate : public content::GeolocationDelegate { | 23 class BlimpGeolocationDelegate : public device::GeolocationDelegate { |
24 public: | 24 public: |
25 explicit BlimpGeolocationDelegate( | 25 explicit BlimpGeolocationDelegate( |
26 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate) { | 26 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate) { |
27 feature_delegate_ = feature_delegate; | 27 feature_delegate_ = feature_delegate; |
28 } | 28 } |
29 | 29 |
30 bool UseNetworkLocationProviders() final { return false; } | 30 bool UseNetworkLocationProviders() final { return false; } |
31 | 31 |
32 std::unique_ptr<content::LocationProvider> OverrideSystemLocationProvider() | 32 std::unique_ptr<device::LocationProvider> OverrideSystemLocationProvider() |
33 final { | 33 final { |
34 return base::WrapUnique(new BlimpLocationProvider(feature_delegate_)); | 34 return base::WrapUnique(new BlimpLocationProvider(feature_delegate_)); |
35 } | 35 } |
36 | 36 |
37 private: | 37 private: |
38 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate_; | 38 base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate_; |
39 | 39 |
40 DISALLOW_COPY_AND_ASSIGN(BlimpGeolocationDelegate); | 40 DISALLOW_COPY_AND_ASSIGN(BlimpGeolocationDelegate); |
41 }; | 41 }; |
42 | 42 |
43 content::Geoposition::ErrorCode ConvertErrorCode( | 43 device::Geoposition::ErrorCode ConvertErrorCode( |
44 const GeolocationErrorMessage::ErrorCode& error_code) { | 44 const GeolocationErrorMessage::ErrorCode& error_code) { |
45 switch (error_code) { | 45 switch (error_code) { |
46 case GeolocationErrorMessage::PERMISSION_DENIED: | 46 case GeolocationErrorMessage::PERMISSION_DENIED: |
47 return content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; | 47 return device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; |
48 case GeolocationErrorMessage::POSITION_UNAVAILABLE: | 48 case GeolocationErrorMessage::POSITION_UNAVAILABLE: |
49 return content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; | 49 return device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; |
50 case GeolocationErrorMessage::TIMEOUT: | 50 case GeolocationErrorMessage::TIMEOUT: |
51 return content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; | 51 return device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 content::Geoposition ConvertLocationMessage( | 55 device::Geoposition ConvertLocationMessage( |
56 const GeolocationCoordinatesMessage& coordinates) { | 56 const GeolocationCoordinatesMessage& coordinates) { |
57 content::Geoposition output; | 57 device::Geoposition output; |
58 output.latitude = coordinates.latitude(); | 58 output.latitude = coordinates.latitude(); |
59 output.longitude = coordinates.longitude(); | 59 output.longitude = coordinates.longitude(); |
60 output.altitude = coordinates.altitude(); | 60 output.altitude = coordinates.altitude(); |
61 output.accuracy = coordinates.accuracy(); | 61 output.accuracy = coordinates.accuracy(); |
62 output.altitude_accuracy = coordinates.altitude_accuracy(); | 62 output.altitude_accuracy = coordinates.altitude_accuracy(); |
63 output.heading = coordinates.heading(); | 63 output.heading = coordinates.heading(); |
64 output.speed = coordinates.speed(); | 64 output.speed = coordinates.speed(); |
65 output.timestamp = base::Time::Now(); | 65 output.timestamp = base::Time::Now(); |
66 output.error_code = content::Geoposition::ErrorCode::ERROR_CODE_NONE; | 66 output.error_code = device::Geoposition::ErrorCode::ERROR_CODE_NONE; |
67 return output; | 67 return output; |
68 } | 68 } |
69 | 69 |
70 } // namespace | 70 } // namespace |
71 | 71 |
72 EngineGeolocationFeature::EngineGeolocationFeature() : weak_factory_(this) {} | 72 EngineGeolocationFeature::EngineGeolocationFeature() : weak_factory_(this) {} |
73 | 73 |
74 EngineGeolocationFeature::~EngineGeolocationFeature() {} | 74 EngineGeolocationFeature::~EngineGeolocationFeature() {} |
75 | 75 |
76 void EngineGeolocationFeature::set_outgoing_message_processor( | 76 void EngineGeolocationFeature::set_outgoing_message_processor( |
77 std::unique_ptr<BlimpMessageProcessor> message_processor) { | 77 std::unique_ptr<BlimpMessageProcessor> message_processor) { |
78 DCHECK(message_processor); | 78 DCHECK(message_processor); |
79 outgoing_message_processor_ = std::move(message_processor); | 79 outgoing_message_processor_ = std::move(message_processor); |
80 } | 80 } |
81 | 81 |
82 content::GeolocationDelegate* | 82 device::GeolocationDelegate* |
83 EngineGeolocationFeature::CreateGeolocationDelegate() { | 83 EngineGeolocationFeature::CreateGeolocationDelegate() { |
84 return new BlimpGeolocationDelegate(weak_factory_.GetWeakPtr()); | 84 return new BlimpGeolocationDelegate(weak_factory_.GetWeakPtr()); |
85 } | 85 } |
86 | 86 |
87 void EngineGeolocationFeature::ProcessMessage( | 87 void EngineGeolocationFeature::ProcessMessage( |
88 std::unique_ptr<BlimpMessage> message, | 88 std::unique_ptr<BlimpMessage> message, |
89 const net::CompletionCallback& callback) { | 89 const net::CompletionCallback& callback) { |
90 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); | 90 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); |
91 | 91 |
92 int result = net::OK; | 92 int result = net::OK; |
93 const GeolocationMessage& geolocation_message = message->geolocation(); | 93 const GeolocationMessage& geolocation_message = message->geolocation(); |
94 switch (geolocation_message.type_case()) { | 94 switch (geolocation_message.type_case()) { |
95 case GeolocationMessage::kCoordinates: { | 95 case GeolocationMessage::kCoordinates: { |
96 const GeolocationCoordinatesMessage& location = | 96 const GeolocationCoordinatesMessage& location = |
97 geolocation_message.coordinates(); | 97 geolocation_message.coordinates(); |
98 content::Geoposition output = ConvertLocationMessage(location); | 98 device::Geoposition output = ConvertLocationMessage(location); |
99 NotifyCallback(output); | 99 NotifyCallback(output); |
100 break; | 100 break; |
101 } | 101 } |
102 case GeolocationMessage::kError: { | 102 case GeolocationMessage::kError: { |
103 const GeolocationErrorMessage& error_message = | 103 const GeolocationErrorMessage& error_message = |
104 geolocation_message.error(); | 104 geolocation_message.error(); |
105 content::Geoposition output; | 105 device::Geoposition output; |
106 output.error_message = error_message.error_message(); | 106 output.error_message = error_message.error_message(); |
107 output.error_code = ConvertErrorCode(error_message.error_code()); | 107 output.error_code = ConvertErrorCode(error_message.error_code()); |
108 NotifyCallback(output); | 108 NotifyCallback(output); |
109 break; | 109 break; |
110 } | 110 } |
111 case GeolocationMessage::kSetInterestLevel: | 111 case GeolocationMessage::kSetInterestLevel: |
112 case GeolocationMessage::kRequestRefresh: | 112 case GeolocationMessage::kRequestRefresh: |
113 case GeolocationMessage::TYPE_NOT_SET: | 113 case GeolocationMessage::TYPE_NOT_SET: |
114 result = net::ERR_UNEXPECTED; | 114 result = net::ERR_UNEXPECTED; |
115 } | 115 } |
116 if (!callback.is_null()) { | 116 if (!callback.is_null()) { |
117 callback.Run(result); | 117 callback.Run(result); |
118 } | 118 } |
119 } | 119 } |
120 | 120 |
121 void EngineGeolocationFeature::NotifyCallback( | 121 void EngineGeolocationFeature::NotifyCallback( |
122 const content::Geoposition& position) { | 122 const device::Geoposition& position) { |
123 geoposition_received_callback_.Run(position); | 123 geoposition_received_callback_.Run(position); |
124 } | 124 } |
125 | 125 |
126 void EngineGeolocationFeature::RequestAccuracy( | 126 void EngineGeolocationFeature::RequestAccuracy( |
127 GeolocationSetInterestLevelMessage::Level level) { | 127 GeolocationSetInterestLevelMessage::Level level) { |
128 GeolocationMessage* geolocation_message = nullptr; | 128 GeolocationMessage* geolocation_message = nullptr; |
129 std::unique_ptr<BlimpMessage> blimp_message = | 129 std::unique_ptr<BlimpMessage> blimp_message = |
130 CreateBlimpMessage(&geolocation_message); | 130 CreateBlimpMessage(&geolocation_message); |
131 | 131 |
132 GeolocationSetInterestLevelMessage* geolocation_interest = | 132 GeolocationSetInterestLevelMessage* geolocation_interest = |
(...skipping 15 matching lines...) Expand all Loading... |
148 net::CompletionCallback()); | 148 net::CompletionCallback()); |
149 } | 149 } |
150 | 150 |
151 void EngineGeolocationFeature::SetUpdateCallback( | 151 void EngineGeolocationFeature::SetUpdateCallback( |
152 const GeopositionReceivedCallback& callback) { | 152 const GeopositionReceivedCallback& callback) { |
153 geoposition_received_callback_ = callback; | 153 geoposition_received_callback_ = callback; |
154 } | 154 } |
155 | 155 |
156 } // namespace engine | 156 } // namespace engine |
157 } // namespace blimp | 157 } // namespace blimp |
OLD | NEW |