Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(837)

Unified Diff: blimp/client/feature/geolocation_feature.cc

Issue 2161223003: Adds GeolocationFeature for Blimp Geolocation project. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@engine_feature_prep
Patch Set: Adding more deps to BUILD for device/geolocation. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..f524b1f562a25b2368aeec5f3dc25c4abda2b8c1
--- /dev/null
+++ b/blimp/client/feature/geolocation_feature.cc
@@ -0,0 +1,154 @@
+// 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: {
+ 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:
+ DLOG(FATAL) << "Invalid message type: " << geolocation_message.type_case()
+ << ".";
+ 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 =
+ 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) {
+ can_send_message_ = true;
+ if (result != net::OK) {
+ device::Geoposition position;
+ location_provider_->GetPosition(&position);
+ OnLocationUpdate(location_provider_.get(), position);
Kevin M 2016/08/10 23:23:34 Won't this just update endlessly?
Wez 2016/08/12 00:39:42 Right; we only want to send a new update following
CJ 2016/08/12 21:57:46 Yeah, I wasn't sure what we are doing in the error
+ }
+}
+
+} // namespace client
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698