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..5b06a3ed4ca458e5b1f90f401d2c89baaf6ab955 |
| --- /dev/null |
| +++ b/blimp/client/feature/geolocation_feature.cc |
| @@ -0,0 +1,119 @@ |
| +// 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 <utility> |
| + |
| +#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/common/geoposition.h" |
| +#include "net/base/net_errors.h" |
| + |
| +namespace blimp { |
| +namespace client { |
| + |
| +GeolocationFeature::GeolocationFeature() {} |
| + |
| +GeolocationFeature::~GeolocationFeature() {} |
| + |
| +void GeolocationFeature::set_outgoing_message_processor( |
| + std::unique_ptr<BlimpMessageProcessor> processor) { |
| + outgoing_message_processor_ = std::move(processor); |
| +} |
| + |
| +void GeolocationFeature::SetDelegate(GeolocationFeatureDelegate* delegate) { |
| + DCHECK(delegate); |
| + |
| + delegate_ = delegate; |
| +} |
| + |
| +void GeolocationFeature::OnLocationUpdate( |
| + const content::Geoposition& position) { |
| + if (position.error_code == content::Geoposition::ERROR_CODE_NONE) { |
| + SendGeolocationPositionMessage(position); |
| + } else { |
| + SendGeolocationErrorMessage(position); |
| + } |
| +} |
| + |
| +void GeolocationFeature::SendGeolocationPositionMessage( |
| + const content::Geoposition& position) { |
| + GeolocationMessage* geolocation_message = nullptr; |
| + std::unique_ptr<BlimpMessage> blimp_message = |
| + CreateBlimpMessage(&geolocation_message); |
| + GeolocationCoordinatesMessage* coordinates = |
| + 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); |
| + |
| + outgoing_message_processor_->ProcessMessage(std::move(blimp_message), |
| + net::CompletionCallback()); |
| +} |
| + |
| +void GeolocationFeature::SendGeolocationErrorMessage( |
| + const content::Geoposition& position) { |
| + GeolocationMessage* geolocation_message = nullptr; |
| + std::unique_ptr<BlimpMessage> blimp_message = |
| + CreateBlimpMessage(&geolocation_message); |
| + |
| + GeolocationErrorMessage* error = geolocation_message->mutable_error(); |
| + switch (position.error_code) { |
| + case content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED: |
| + error->set_error_code(GeolocationErrorMessage::PERMISSION_DENIED); |
| + break; |
| + case content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE: |
| + error->set_error_code(GeolocationErrorMessage::POSITION_UNAVAILABLE); |
| + break; |
| + case content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT: |
| + error->set_error_code(GeolocationErrorMessage::TIMEOUT); |
| + break; |
| + case content::Geoposition::ErrorCode::ERROR_CODE_NONE: |
| + NOTREACHED() << "ERROR_CODE_NONE GIVEN."; |
|
Wez
2016/07/22 01:35:08
nit: No need for the additional text; NOTREACHED()
CJ
2016/07/22 19:22:35
Done.
|
| + break; |
| + } |
| + |
| + error->set_error_message(position.error_message); |
| + |
| + outgoing_message_processor_->ProcessMessage(std::move(blimp_message), |
| + net::CompletionCallback()); |
| +} |
| + |
| +void GeolocationFeature::ProcessMessage( |
| + std::unique_ptr<BlimpMessage> message, |
| + const net::CompletionCallback& callback) { |
| + DCHECK(!callback.is_null()); |
|
Wez
2016/07/22 01:35:08
As discussed in the Engine*Feature CL, the API doe
CJ
2016/07/22 19:22:35
Done.
|
| + DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); |
| + DCHECK(delegate_); |
| + |
| + const GeolocationMessage& geolocation_message = message->geolocation(); |
| + switch (geolocation_message.type_case()) { |
| + case GeolocationMessage::kSetInterestLevel: { |
| + const GeolocationSetInterestLevelMessage& set_level_message = |
| + geolocation_message.set_interest_level(); |
| + delegate_->OnGeolocationInterestUpdate(set_level_message.level()); |
| + break; |
| + } |
| + case GeolocationMessage::kRequestRefresh: |
| + delegate_->OnRequestRefresh(); |
| + break; |
| + case GeolocationMessage::kCoordinates: |
| + case GeolocationMessage::kError: |
| + case GeolocationMessage::TYPE_NOT_SET: |
| + callback.Run(net::ERR_UNEXPECTED); |
| + return; |
| + } |
| + callback.Run(net::OK); |
| +} |
| + |
| +} // namespace client |
| +} // namespace blimp |