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..641b95999066dac097eb0f4ce48d22d9c440bc90 |
--- /dev/null |
+++ b/blimp/client/feature/geolocation_feature.cc |
@@ -0,0 +1,137 @@ |
+// 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) { |
+ DCHECK(location_provider); |
Wez
2016/08/02 01:18:10
nit: Strictly you don't need this, since you de-re
CJ
2016/08/03 20:06:03
Done.
|
+ location_provider_ = std::move(location_provider); |
Wez
2016/08/02 01:18:10
nit: Move this std::move() to be a member-initiali
CJ
2016/08/03 20:06:03
Done.
|
+ 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); |
+} |
+ |
+void GeolocationFeature::ProcessMessage( |
+ std::unique_ptr<BlimpMessage> message, |
+ const net::CompletionCallback& callback) { |
+ DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); |
+ |
+ const GeolocationMessage& geolocation_message = message->geolocation(); |
+ switch (geolocation_message.type_case()) { |
+ case GeolocationMessage::kSetInterestLevel: { |
+ const GeolocationSetInterestLevelMessage& set_level_message = |
+ geolocation_message.set_interest_level(); |
+ OnGeolocationInterestUpdate(set_level_message.level()); |
+ break; |
+ } |
+ case GeolocationMessage::kRequestRefresh: |
+ location_provider_->RequestRefresh(); |
+ break; |
+ case GeolocationMessage::kCoordinates: |
+ case GeolocationMessage::kError: |
+ case GeolocationMessage::TYPE_NOT_SET: |
+ DLOG(FATAL) << "Unsupported message type."; |
Wez
2016/08/02 01:18:10
nit: This should be "Invalid message type." - "Uns
CJ
2016/08/03 20:06:03
Done.
|
+ callback.Run(net::ERR_UNEXPECTED); |
+ return; |
+ } |
+ callback.Run(net::OK); |
Wez
2016/08/02 01:18:11
nit: Consider using ScopedTaskRunner for this.
As
CJ
2016/08/03 20:06:03
Done.
|
+} |
+ |
+void GeolocationFeature::OnLocationUpdate( |
+ const device::LocationProvider* location_provider, |
+ const device::Geoposition& position) { |
+ if (position.error_code == device::Geoposition::ERROR_CODE_NONE) { |
+ SendGeolocationPositionMessage(position); |
+ } else { |
+ SendGeolocationErrorMessage(position.error_code, position.error_message); |
Wez
2016/08/02 01:18:10
If you change the signature of SendGeolocationErro
CJ
2016/08/03 20:06:03
Done.
|
+ } |
+} |
+ |
+void GeolocationFeature::OnGeolocationInterestUpdate( |
+ 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 = |
+ 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()); |
Wez
2016/08/02 01:18:10
nit: Perhaps we should register a completion callb
CJ
2016/08/03 20:06:03
How does one know if the network is ready?
Wez
2016/08/09 01:40:33
By passing a non-null CompletionCallback and setti
CJ
2016/08/09 21:40:37
Done.
|
+} |
+ |
+void GeolocationFeature::SendGeolocationErrorMessage( |
+ const device::Geoposition::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(); |
+ switch (error_code) { |
+ case device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED: |
+ error->set_error_code(GeolocationErrorMessage::PERMISSION_DENIED); |
+ break; |
+ case device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE: |
+ error->set_error_code(GeolocationErrorMessage::POSITION_UNAVAILABLE); |
+ break; |
+ case device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT: |
+ error->set_error_code(GeolocationErrorMessage::TIMEOUT); |
+ break; |
+ case device::Geoposition::ErrorCode::ERROR_CODE_NONE: |
+ NOTREACHED(); |
+ break; |
+ } |
+ |
+ error->set_error_message(error_message); |
+ |
+ outgoing_message_processor_->ProcessMessage(std::move(blimp_message), |
+ net::CompletionCallback()); |
+} |
+ |
+} // namespace client |
+} // namespace blimp |