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

Unified Diff: blimp/engine/feature/geolocation/engine_geolocation_feature.cc

Issue 2091023006: Adds EngineGeolocationFeature for Blimp Geolocation project. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addresses Wez's #29 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/engine/feature/geolocation/engine_geolocation_feature.cc
diff --git a/blimp/engine/feature/geolocation/engine_geolocation_feature.cc b/blimp/engine/feature/geolocation/engine_geolocation_feature.cc
new file mode 100644
index 0000000000000000000000000000000000000000..72c8b5df356faff728d6ed4f4745db6c40f80f79
--- /dev/null
+++ b/blimp/engine/feature/geolocation/engine_geolocation_feature.cc
@@ -0,0 +1,163 @@
+// 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/engine/feature/geolocation/engine_geolocation_feature.h"
+
+#include "base/memory/ptr_util.h"
+#include "base/memory/weak_ptr.h"
+#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/browser/geolocation_delegate.h"
+#include "content/public/browser/location_provider.h"
+#include "content/public/common/geoposition.h"
+#include "net/base/net_errors.h"
+
+namespace blimp {
+namespace engine {
+namespace {
+
+// A provider of services needed by Geolocation.
Wez 2016/07/15 01:46:17 nit: Suggest something more like "content::Geoloca
CJ 2016/07/18 21:11:56 Done.
+class BlimpGeolocationDelegate : public content::GeolocationDelegate {
+ public:
+ explicit BlimpGeolocationDelegate(
+ base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate) {
+ feature_delegate_ = feature_delegate;
+ }
+
+ bool UseNetworkLocationProviders() final { return false; }
Wez 2016/07/15 01:46:17 BTW I had no idea we were using final in C++ now.
CJ 2016/07/18 21:11:56 Acknowledged.
+
+ std::unique_ptr<content::LocationProvider> OverrideSystemLocationProvider()
+ final {
+ BlimpLocationProvider* location_provider =
+ new BlimpLocationProvider(feature_delegate_);
+ return base::WrapUnique(location_provider);
Wez 2016/07/15 01:46:17 nit: Just put the new inside WrapUnique()
CJ 2016/07/18 21:11:56 Done.
+ }
+
+ private:
+ base::WeakPtr<BlimpLocationProvider::Delegate> feature_delegate_;
+
+ DISALLOW_COPY_AND_ASSIGN(BlimpGeolocationDelegate);
+};
+
+content::Geoposition::ErrorCode ConvertErrorCode(
+ const GeolocationErrorMessage::ErrorCode& error_code) {
+ switch (error_code) {
+ case GeolocationErrorMessage::PERMISSION_DENIED:
+ return content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED;
+ case GeolocationErrorMessage::POSITION_UNAVAILABLE:
+ return content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE;
+ case GeolocationErrorMessage::TIMEOUT:
+ return content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT;
+ }
+}
+
+content::Geoposition ConvertLocationMessage(
+ const GeolocationCoordinatesMessage& coordinates) {
+ content::Geoposition output;
+ output.latitude = coordinates.latitude();
+ output.longitude = coordinates.longitude();
+ output.altitude = coordinates.altitude();
+ output.accuracy = coordinates.accuracy();
+ output.altitude_accuracy = coordinates.altitude_accuracy();
+ output.heading = coordinates.heading();
+ output.speed = coordinates.speed();
+ output.timestamp = base::Time::Now();
+ output.error_code = content::Geoposition::ErrorCode::ERROR_CODE_NONE;
+ return output;
+}
+
+} // namespace
+
+EngineGeolocationFeature::EngineGeolocationFeature() : weak_factory_(this) {}
+
+EngineGeolocationFeature::~EngineGeolocationFeature() {}
+
+void EngineGeolocationFeature::set_outgoing_message_processor(
+ std::unique_ptr<BlimpMessageProcessor> message_processor) {
+ DCHECK(message_processor);
+ outgoing_message_processor_ = std::move(message_processor);
+}
+
+content::GeolocationDelegate*
+EngineGeolocationFeature::CreateGeolocationDelegate() {
+ return new BlimpGeolocationDelegate(GetWeakPtr());
Wez 2016/07/15 01:46:17 Just call weak_factory_.GetweakPtr() directly here
CJ 2016/07/18 21:11:56 Done.
+}
+
+base::WeakPtr<EngineGeolocationFeature> EngineGeolocationFeature::GetWeakPtr() {
+ return weak_factory_.GetWeakPtr();
+}
+
+void EngineGeolocationFeature::RequestAccuracy(
+ GeolocationSetInterestLevelMessage::Level level) {
+ GeolocationMessage* geolocation_message = nullptr;
+ std::unique_ptr<BlimpMessage> blimp_message =
+ CreateBlimpMessage(&geolocation_message);
+
+ GeolocationSetInterestLevelMessage* geolocation_interest =
+ geolocation_message->mutable_set_interest_level();
+ geolocation_interest->set_level(level);
+
+ outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
+ net::CompletionCallback());
+}
+
+void EngineGeolocationFeature::RequestRefresh() {
+ GeolocationMessage* geolocation_message = nullptr;
+ std::unique_ptr<BlimpMessage> blimp_message =
+ CreateBlimpMessage(&geolocation_message);
+
+ geolocation_message->mutable_request_refresh();
+
+ outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
+ net::CompletionCallback());
+}
+
+void EngineGeolocationFeature::SetUpdateCallback(
+ const base::Callback<void(const content::Geoposition&)>& callback) {
+ geoposition_received_callback_ = callback;
+}
+
+void EngineGeolocationFeature::ProcessMessage(
+ std::unique_ptr<BlimpMessage> message,
+ const net::CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
+ DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case());
+
+ const GeolocationMessage& geolocation_message = message->geolocation();
+
+ switch (geolocation_message.type_case()) {
+ case GeolocationMessage::kCoordinates: {
+ const GeolocationCoordinatesMessage& location =
+ geolocation_message.coordinates();
+ content::Geoposition output = ConvertLocationMessage(location);
+ NotifyCallback(output);
+ break;
+ }
+ case GeolocationMessage::kError: {
+ const GeolocationErrorMessage& error_message =
+ geolocation_message.error();
+ content::Geoposition output;
+ output.error_message = error_message.error_message();
+ output.error_code = ConvertErrorCode(error_message.error_code());
+ NotifyCallback(output);
+ break;
+ }
+ case GeolocationMessage::kSetInterestLevel:
+ case GeolocationMessage::kRequestRefresh:
+ case GeolocationMessage::TYPE_NOT_SET:
+ DLOG(FATAL) << "Client sent unexpected message type."
+ << geolocation_message.type_case();
+ break;
+ }
+ callback.Run(net::OK);
+}
+
+void EngineGeolocationFeature::NotifyCallback(
+ const content::Geoposition& position) {
+ geoposition_received_callback_.Run(position);
+}
+
+} // namespace engine
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698