Chromium Code Reviews| 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..8af873bef4be9ffb13939970717ada974593d5eb |
| --- /dev/null |
| +++ b/blimp/engine/feature/geolocation/engine_geolocation_feature.cc |
| @@ -0,0 +1,151 @@ |
| +// 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/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/location_provider.h" |
| +#include "content/public/common/geoposition.h" |
| +#include "net/base/net_errors.h" |
| + |
| +namespace blimp { |
| +namespace engine { |
| +namespace { |
| + |
| +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 GeolocationPositionMessage& location) { |
| + content::Geoposition output; |
| + output.latitude = location.coordinates().latitude(); |
| + output.longitude = location.coordinates().longitude(); |
| + output.altitude = location.coordinates().altitude(); |
| + output.accuracy = location.coordinates().accuracy(); |
| + output.altitude_accuracy = location.coordinates().altitude_accuracy(); |
| + output.heading = location.coordinates().heading(); |
| + output.speed = location.coordinates().speed(); |
| + output.timestamp = base::Time::FromJsTime(location.timestamp_millis()); |
| + 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); |
| +} |
| + |
| +base::WeakPtr<EngineGeolocationFeature> EngineGeolocationFeature::GetWeakPtr() { |
| + return weak_factory_.GetWeakPtr(); |
| +} |
| + |
| +void EngineGeolocationFeature::StartProvider(bool enable_high_accuracy) { |
| + GeolocationMessage* geolocation_message = nullptr; |
| + std::unique_ptr<BlimpMessage> blimp_message = |
| + CreateBlimpMessage(&geolocation_message); |
| + |
| + GeolocationInterestMessage* geolocation_interest = |
| + geolocation_message->mutable_update_listen_state(); |
| + if (enable_high_accuracy) { |
| + geolocation_interest->set_listen_state( |
| + GeolocationInterestMessage::ACCURACY_HIGH); |
| + } else { |
| + geolocation_interest->set_listen_state( |
| + GeolocationInterestMessage::ACCURACY_LOW); |
| + } |
| + |
| + outgoing_message_processor_->ProcessMessage(std::move(blimp_message), |
| + net::CompletionCallback()); |
| +} |
| + |
| +void EngineGeolocationFeature::StopProvider() { |
| + GeolocationMessage* geolocation_message = nullptr; |
| + std::unique_ptr<BlimpMessage> blimp_message = |
| + CreateBlimpMessage(&geolocation_message); |
| + |
| + GeolocationInterestMessage* geolocation_interest = |
| + geolocation_message->mutable_update_listen_state(); |
| + geolocation_interest->set_listen_state( |
| + GeolocationInterestMessage::REQUEST_NONE); |
| + |
| + 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) { |
| + geolocation_received_callback_ = callback; |
| +} |
| + |
| +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::kLocation: { |
| + const GeolocationPositionMessage location = |
|
Wez
2016/07/14 01:19:22
nit: const& here
CJ
2016/07/14 23:55:09
Done.
|
| + geolocation_message.location(); |
| + content::Geoposition output = ConvertLocationMessage(location); |
| + NotifyCallback(output); |
| + break; |
| + } |
| + case GeolocationMessage::kError: { |
| + content::Geoposition output; |
|
Wez
2016/07/14 01:19:22
nit: As per style-guide, only declare |output| imm
CJ
2016/07/14 23:55:09
Done.
|
| + const GeolocationErrorMessage error_message = geolocation_message.error(); |
|
Wez
2016/07/14 01:19:22
nit: const &
CJ
2016/07/14 23:55:09
Done.
|
| + output.error_message = error_message.error_message(); |
| + output.error_code = ConvertErrorCode(error_message.error_code()); |
| + NotifyCallback(output); |
| + break; |
| + } |
| + case GeolocationMessage::kUpdateListenState: |
| + case GeolocationMessage::kRequestRefresh: |
| + case GeolocationMessage::TYPE_NOT_SET: |
| + DLOG(FATAL) << "Client sent unexpected message type." |
| + << geolocation_message.type_case(); |
| + break; |
| + } |
| + callback.Run(net::OK); |
| +} |
| + |
| +void EngineGeolocationFeature::NotifyCallback( |
| + const content::Geoposition& position) { |
| + geolocation_received_callback_.Run(position); |
| +} |
| + |
| +} // namespace engine |
| +} // namespace blimp |