Chromium Code Reviews| Index: blimp/client/feature/geolocation_feature.cc |
| diff --git a/blimp/client/feature/geolocation_feature.cc b/blimp/client/feature/geolocation_feature.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..adc1ea23594e5c4e54f55f8d87936e4a25ebbfc8 |
| --- /dev/null |
| +++ b/blimp/client/feature/geolocation_feature.cc |
| @@ -0,0 +1,147 @@ |
| +// 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/client/feature/geolocation_feature.h" |
| + |
| +#include <memory> |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "blimp/common/create_blimp_message.h" |
| +#include "blimp/common/proto/blimp_message.pb.h" |
| +#include "device/geolocation/geoposition.h" |
| +#include "device/geolocation/location_provider.h" |
| +#include "net/base/net_errors.h" |
| + |
| +namespace blimp { |
| +namespace client { |
| + |
| +GeolocationFeature::GeolocationFeature( |
| + std::unique_ptr<device::LocationProvider> location_provider) |
| + : location_provider_(std::move(location_provider)), |
| + completion_callback_(base::Bind(&GeolocationFeature::OnSendComplete, |
| + base::Unretained(this))) { |
| + location_provider_->SetUpdateCallback(base::Bind( |
| + &GeolocationFeature::OnLocationUpdate, base::Unretained(this))); |
| +} |
| + |
| +GeolocationFeature::~GeolocationFeature() {} |
| + |
| +void GeolocationFeature::set_outgoing_message_processor( |
| + std::unique_ptr<BlimpMessageProcessor> processor) { |
| + outgoing_message_processor_ = std::move(processor); |
| + can_send_message_ = true; |
| +} |
| + |
| +void GeolocationFeature::ProcessMessage( |
| + std::unique_ptr<BlimpMessage> message, |
| + const net::CompletionCallback& callback) { |
| + DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); |
| + |
| + int result = net::OK; |
| + const GeolocationMessage& geolocation_message = message->geolocation(); |
| + switch (geolocation_message.type_case()) { |
| + case GeolocationMessage::kSetInterestLevel: { |
|
Kevin M
2016/08/15 20:35:40
No variables defined in this case block; no need t
CJ
2016/08/15 21:59:31
Done.
|
| + SetInterestLevel(geolocation_message.set_interest_level().level()); |
| + break; |
| + } |
| + case GeolocationMessage::kRequestRefresh: |
| + location_provider_->RequestRefresh(); |
| + break; |
| + case GeolocationMessage::kCoordinates: |
| + case GeolocationMessage::kError: |
| + case GeolocationMessage::TYPE_NOT_SET: |
| + result = net::ERR_UNEXPECTED; |
| + break; |
| + } |
| + |
| + if (!callback.is_null()) { |
| + callback.Run(result); |
| + } |
| +} |
| + |
| +void GeolocationFeature::OnLocationUpdate( |
| + const device::LocationProvider* location_provider, |
| + const device::Geoposition& position) { |
| + switch (position.error_code) { |
| + case device::Geoposition::ERROR_CODE_NONE: |
| + SendGeolocationPositionMessage(position); |
| + break; |
| + case device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED: |
| + SendGeolocationErrorMessage(GeolocationErrorMessage::PERMISSION_DENIED, |
| + position.error_message); |
| + break; |
| + case device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE: |
| + SendGeolocationErrorMessage(GeolocationErrorMessage::POSITION_UNAVAILABLE, |
| + position.error_message); |
| + break; |
| + case device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT: |
| + SendGeolocationErrorMessage(GeolocationErrorMessage::TIMEOUT, |
| + position.error_message); |
| + break; |
| + } |
| +} |
| + |
| +void GeolocationFeature::SetInterestLevel( |
| + GeolocationSetInterestLevelMessage::Level level) { |
| + switch (level) { |
| + case GeolocationSetInterestLevelMessage::HIGH_ACCURACY: |
| + location_provider_->StartProvider(true); |
| + break; |
| + case GeolocationSetInterestLevelMessage::LOW_ACCURACY: |
| + location_provider_->StartProvider(false); |
| + break; |
| + case GeolocationSetInterestLevelMessage::NO_INTEREST: |
| + location_provider_->StopProvider(); |
| + break; |
| + } |
| +} |
| + |
| +void GeolocationFeature::SendGeolocationPositionMessage( |
| + const device::Geoposition& position) { |
| + GeolocationMessage* geolocation_message = nullptr; |
| + std::unique_ptr<BlimpMessage> blimp_message = |
| + CreateBlimpMessage(&geolocation_message); |
| + GeolocationCoordinatesMessage* coordinates = |
|
Kevin M
2016/08/15 20:35:40
nit: move this to the top of the cluster of coordi
CJ
2016/08/15 21:59:31
Done.
|
| + geolocation_message->mutable_coordinates(); |
| + |
| + coordinates->set_latitude(position.latitude); |
| + coordinates->set_longitude(position.longitude); |
| + coordinates->set_altitude(position.altitude); |
| + coordinates->set_accuracy(position.accuracy); |
| + coordinates->set_altitude_accuracy(position.altitude_accuracy); |
| + coordinates->set_heading(position.heading); |
| + coordinates->set_speed(position.speed); |
| + |
| + if (can_send_message_) { |
| + can_send_message_ = false; |
| + outgoing_message_processor_->ProcessMessage(std::move(blimp_message), |
| + completion_callback_); |
| + } |
| +} |
| + |
| +void GeolocationFeature::SendGeolocationErrorMessage( |
| + const GeolocationErrorMessage::ErrorCode& error_code, |
| + const std::string& error_message) { |
| + GeolocationMessage* geolocation_message = nullptr; |
| + std::unique_ptr<BlimpMessage> blimp_message = |
| + CreateBlimpMessage(&geolocation_message); |
| + |
| + GeolocationErrorMessage* error = geolocation_message->mutable_error(); |
| + error->set_error_code(error_code); |
| + error->set_error_message(error_message); |
| + |
| + if (can_send_message_) { |
| + can_send_message_ = false; |
| + outgoing_message_processor_->ProcessMessage(std::move(blimp_message), |
| + completion_callback_); |
| + } |
| +} |
| + |
| +void GeolocationFeature::OnSendComplete(int result) { |
|
Kevin M
2016/08/15 20:35:40
What about sending an update that was previously s
CJ
2016/08/15 21:59:32
Done.
|
| + can_send_message_ = true; |
| +} |
| + |
| +} // namespace client |
| +} // namespace blimp |