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

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: Addresses kmarshall's #14 comments Created 4 years, 5 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..167e43fe9718b7adaffded3ed4a978f6ec9c4a9b
--- /dev/null
+++ b/blimp/client/feature/geolocation_feature.cc
@@ -0,0 +1,138 @@
+// 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 "blimp/common/proto/geolocation.pb.h"
Kevin M 2016/08/01 20:51:04 Already included in .h
CJ 2016/08/02 00:36:01 Done.
+#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);
+ 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());
+ DCHECK(location_provider_);
Kevin M 2016/08/01 20:51:04 You can DCHECK location_provider_ in the ctor, but
CJ 2016/08/02 00:36:00 Done.
+
+ const GeolocationMessage& geolocation_message = message->geolocation();
+ switch (geolocation_message.type_case()) {
+ case GeolocationMessage::kSetInterestLevel: {
+ const GeolocationSetInterestLevelMessage& set_level_message =
Kevin M 2016/08/01 20:51:04 You can inline this and remove the braces
CJ 2016/08/02 00:36:01 What does "inline" mean in this context?
+ 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.";
+ callback.Run(net::ERR_UNEXPECTED);
+ return;
+ }
+ callback.Run(net::OK);
+}
+
+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);
+ }
+}
+
+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());
+}
+
+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

Powered by Google App Engine
This is Rietveld 408576698