Index: blimp/engine/feature/geolocation/engine_geolocation_feature.cc |
diff --git a/blimp/engine/feature/geolocation/engine_geolocation_feature.cc b/blimp/engine/feature/geolocation/engine_geolocation_feature.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2a5ab4689499c251d3670656a76ca6af92c6fa00 |
--- /dev/null |
+++ b/blimp/engine/feature/geolocation/engine_geolocation_feature.cc |
@@ -0,0 +1,158 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "blimp/engine/feature/geolocation/engine_geolocation_feature.h" |
+ |
+#include "base/memory/ptr_util.h" |
+#include "base/memory/weak_ptr.h" |
+#include "blimp/common/create_blimp_message.h" |
+#include "blimp/common/proto/blimp_message.pb.h" |
+#include "blimp/common/proto/geolocation.pb.h" |
+#include "content/public/browser/geolocation_delegate.h" |
+#include "content/public/browser/location_provider.h" |
+#include "content/public/common/geoposition.h" |
+#include "net/base/net_errors.h" |
+ |
+namespace blimp { |
+namespace engine { |
+namespace { |
+ |
+// content::GeolocationDelegate implementation integrated with |
+// the EngineGeolocationFeature. |
+class BlimpGeolocationDelegate : public content::GeolocationDelegate { |
+ public: |
+ explicit BlimpGeolocationDelegate( |
+ base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate) { |
+ feature_delegate_ = feature_delegate; |
+ } |
+ |
+ bool UseNetworkLocationProviders() final { return false; } |
+ |
+ std::unique_ptr<content::LocationProvider> OverrideSystemLocationProvider() |
+ final { |
+ return base::WrapUnique(new BlimpLocationProvider(feature_delegate_)); |
+ } |
+ |
+ private: |
+ base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BlimpGeolocationDelegate); |
+}; |
+ |
+content::Geoposition::ErrorCode ConvertErrorCode( |
+ const GeolocationErrorMessage::ErrorCode& error_code) { |
+ switch (error_code) { |
+ case GeolocationErrorMessage::PERMISSION_DENIED: |
+ return content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; |
+ case GeolocationErrorMessage::POSITION_UNAVAILABLE: |
+ return content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; |
+ case GeolocationErrorMessage::TIMEOUT: |
+ return content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; |
+ } |
+} |
+ |
+content::Geoposition ConvertLocationMessage( |
+ const GeolocationCoordinatesMessage& coordinates) { |
+ content::Geoposition output; |
+ output.latitude = coordinates.latitude(); |
+ output.longitude = coordinates.longitude(); |
+ output.altitude = coordinates.altitude(); |
+ output.accuracy = coordinates.accuracy(); |
+ output.altitude_accuracy = coordinates.altitude_accuracy(); |
+ output.heading = coordinates.heading(); |
+ output.speed = coordinates.speed(); |
+ output.timestamp = base::Time::Now(); |
+ output.error_code = content::Geoposition::ErrorCode::ERROR_CODE_NONE; |
+ return output; |
+} |
+ |
+} // namespace |
+ |
+EngineGeolocationFeature::EngineGeolocationFeature() : weak_factory_(this) {} |
+ |
+EngineGeolocationFeature::~EngineGeolocationFeature() {} |
+ |
+void EngineGeolocationFeature::set_outgoing_message_processor( |
+ std::unique_ptr<BlimpMessageProcessor> message_processor) { |
+ DCHECK(message_processor); |
+ outgoing_message_processor_ = std::move(message_processor); |
+} |
+ |
+std::unique_ptr<content::GeolocationDelegate> |
+EngineGeolocationFeature::CreateGeolocationDelegate() { |
+ return base::WrapUnique( |
+ new BlimpGeolocationDelegate(weak_factory_.GetWeakPtr())); |
+} |
+ |
+void EngineGeolocationFeature::ProcessMessage( |
+ std::unique_ptr<BlimpMessage> message, |
+ const net::CompletionCallback& callback) { |
+ DCHECK(!callback.is_null()); |
+ DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); |
+ |
+ const GeolocationMessage& geolocation_message = message->geolocation(); |
+ |
+ switch (geolocation_message.type_case()) { |
+ case GeolocationMessage::kCoordinates: { |
+ const GeolocationCoordinatesMessage& location = |
+ geolocation_message.coordinates(); |
+ content::Geoposition output = ConvertLocationMessage(location); |
+ NotifyCallback(output); |
+ break; |
+ } |
+ case GeolocationMessage::kError: { |
+ const GeolocationErrorMessage& error_message = |
+ geolocation_message.error(); |
+ content::Geoposition output; |
+ output.error_message = error_message.error_message(); |
+ output.error_code = ConvertErrorCode(error_message.error_code()); |
+ NotifyCallback(output); |
+ break; |
+ } |
+ case GeolocationMessage::kSetInterestLevel: |
+ case GeolocationMessage::kRequestRefresh: |
+ case GeolocationMessage::TYPE_NOT_SET: |
+ 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?
|
+ break; |
+ } |
+ callback.Run(net::OK); |
+} |
+ |
+void EngineGeolocationFeature::NotifyCallback( |
+ const content::Geoposition& position) { |
+ geoposition_received_callback_.Run(position); |
+} |
+ |
+void EngineGeolocationFeature::RequestAccuracy( |
+ GeolocationSetInterestLevelMessage::Level level) { |
+ GeolocationMessage* geolocation_message = nullptr; |
+ std::unique_ptr<BlimpMessage> blimp_message = |
+ CreateBlimpMessage(&geolocation_message); |
+ |
+ GeolocationSetInterestLevelMessage* geolocation_interest = |
+ geolocation_message->mutable_set_interest_level(); |
+ geolocation_interest->set_level(level); |
+ |
+ outgoing_message_processor_->ProcessMessage(std::move(blimp_message), |
+ net::CompletionCallback()); |
+} |
+ |
+void EngineGeolocationFeature::RequestRefresh() { |
+ GeolocationMessage* geolocation_message = nullptr; |
+ std::unique_ptr<BlimpMessage> blimp_message = |
+ CreateBlimpMessage(&geolocation_message); |
+ |
+ geolocation_message->mutable_request_refresh(); |
+ |
+ outgoing_message_processor_->ProcessMessage(std::move(blimp_message), |
+ net::CompletionCallback()); |
+} |
+ |
+void EngineGeolocationFeature::SetUpdateCallback( |
+ const base::Callback<void(const content::Geoposition&)>& callback) { |
+ geoposition_received_callback_ = callback; |
+} |
+ |
+} // namespace engine |
+} // namespace blimp |