Chromium Code Reviews| Index: blimp/64 |
| diff --git a/blimp/64 b/blimp/64 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dce0e65434535416105bbc1b486f90695bf51d11 |
| --- /dev/null |
| +++ b/blimp/64 |
| @@ -0,0 +1,153 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
|
Wez
2016/07/12 21:35:12
Looks like you copied a file into your working tre
CJ
2016/07/13 21:49:19
Yeah not even sure where this came from. Sorry abo
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#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 "blimp/engine/feature/geolocation/engine_geolocation_feature.h" |
| +#include "content/public/browser/location_provider.h" |
| +#include "content/public/common/geoposition.h" |
| +#include "net/base/net_errors.h" |
| + |
| +#include "blimp/common/logging.h" |
| + |
| +namespace blimp { |
| +namespace engine { |
| +namespace { |
| + |
| +content::Geoposition::ErrorCode GetErrorCode( |
| + const ErrorMessage::ErrorCode& error_code) { |
| + switch (error_code) { |
| + case ErrorMessage::ERROR_CODE_NONE: |
| + return content::Geoposition::ErrorCode::ERROR_CODE_NONE; |
| + case ErrorMessage::ERROR_CODE_PERMISSION_DENIED: |
| + return content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; |
| + case ErrorMessage::ERROR_CODE_POSITION_UNAVAILABLE: |
| + return content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; |
| + case ErrorMessage::ERROR_CODE_TIMEOUT: |
| + return content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; |
| + } |
| +} |
| + |
| +void ConvertLocationMessage(content::Geoposition* output, |
| + const LocationMessage& location) { |
| + output->latitude = location.latitude(); |
| + output->longitude = location.longitude(); |
| + output->altitude = location.altitude(); |
| + output->accuracy = location.accuracy(); |
| + output->altitude_accuracy = location.altitude_accuracy(); |
| + output->heading = location.heading(); |
| + output->speed = location.speed(); |
| + output->timestamp = base::Time::FromJsTime(location.timestamp_millis()); |
| +} |
| + |
| +} // 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::UpdateListenState( |
| + bool enable_high_accuracy) { |
| + GeolocationMessage* geolocation_message; |
| + std::unique_ptr<BlimpMessage> blimp_message = |
| + CreateBlimpMessage(&geolocation_message); |
| + |
| + UpdateListenStateMessage* details = |
| + geolocation_message->mutable_update_listen_state(); |
| + if (enable_high_accuracy) { |
| + details->set_listen_state(UpdateListenStateMessage::ACCURACY_HIGH); |
| + } else { |
| + details->set_listen_state(UpdateListenStateMessage::ACCURACY_LOW); |
| + } |
| + |
| + outgoing_message_processor_->ProcessMessage(std::move(blimp_message), |
| + net::CompletionCallback()); |
| +} |
| + |
| +void EngineGeolocationFeature::StopListenState() { |
| + CHECK(false); |
| + GeolocationMessage* geolocation_message; |
| + std::unique_ptr<BlimpMessage> blimp_message = |
| + CreateBlimpMessage(&geolocation_message); |
| + |
| + UpdateListenStateMessage* details = |
| + geolocation_message->mutable_update_listen_state(); |
| + details->set_listen_state(UpdateListenStateMessage::STOPPED); |
| + |
| + outgoing_message_processor_->ProcessMessage(std::move(blimp_message), |
| + net::CompletionCallback()); |
| +} |
| + |
| +void EngineGeolocationFeature::RequestRefresh() { |
| + GeolocationMessage* geolocation_message; |
| + 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 SetUpdateCallback(const LocationProviderUpdateCallback& callback) { |
| + DCHECK(!callback.is_null()); |
| + |
| + callback_ = callback; |
| +} |
| + |
| + |
| +void EngineGeolocationFeature::NotifyCallback( |
| + const content::Geoposition& position) { |
| + callback_.Run(this, position); |
| +} |
| + |
| +void EngineGeolocationFeature::ProcessMessage( |
| + std::unique_ptr<BlimpMessage> message, |
| + const net::CompletionCallback& callback) { |
| + DCHECK(!callback.is_null()); |
| + DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); |
| + DCHECK(location_provider_); |
| + |
| + const GeolocationMessage& geolocation_message = message->geolocation(); |
| + content::Geoposition output; |
| + |
| + switch (geolocation_message.type_case()) { |
| + case GeolocationMessage::kLocation: { |
| + const LocationMessage location = geolocation_message.location(); |
| + ConvertLocationMessage(&output, location); |
| + location_provider_->NotifyCallback(output); |
| + break; |
| + } |
| + case GeolocationMessage::kError: { |
| + const ErrorMessage error_message = geolocation_message.error(); |
| + output.error_message = error_message.error_message(); |
| + output.error_code = GetErrorCode(error_message.error_code()); |
| + location_provider_->NotifyCallback(output); |
| + break; |
| + } |
| + case GeolocationMessage::kUpdateListenState: |
| + case GeolocationMessage::kRequestRefresh: |
| + NOTREACHED() << "Engine received unexpected geolocation type."; |
| + break; |
| + case GeolocationMessage::TYPE_NOT_SET: |
| + NOTREACHED(); |
| + break; |
| + } |
| + callback.Run(net::OK); |
| +} |
| + |
| +} // namespace engine |
| +} // namespace blimp |