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..802ee1458a3bf2b71ccdc288378ec67d42d50e0b |
| --- /dev/null |
| +++ b/blimp/client/feature/geolocation_feature.cc |
| @@ -0,0 +1,121 @@ |
| +// 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; |
| + // This case shouldn't be hit. Error? |
|
Kevin M
2016/07/20 17:30:15
Use NOTREACHED() instead of a comment.
CJ
2016/07/20 23:22:10
Cool thanks. Are using comments to ask questions a
|
| + case content::Geoposition::ErrorCode::ERROR_CODE_NONE: |
| + 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()); |
| + 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(); |
| + if (set_level_message.has_level()) { |
|
Kevin M
2016/07/20 17:30:15
If it doesn't have a level, doesn't this mean that
CJ
2016/07/20 23:22:10
True. I'm thinking the delegate should handle this
|
| + delegate_->OnGeolocationInterestUpdate(set_level_message.level()); |
| + } |
| + break; |
| + } |
| + case GeolocationMessage::kRequestRefresh: |
| + delegate_->OnRequestRefresh(); |
| + break; |
| + case GeolocationMessage::kCoordinates: |
| + case GeolocationMessage::kError: |
| + case GeolocationMessage::TYPE_NOT_SET: |
| + DLOG(FATAL) << "Engine sent unexpected message type."; |
|
Kevin M
2016/07/20 17:30:15
DLOG only crashes on debug builds. We should fail
CJ
2016/07/20 23:22:10
Done.
|
| + break; |
| + } |
| + callback.Run(net::OK); |
| +} |
| + |
| +} // namespace client |
| +} // namespace blimp |