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