Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "blimp/engine/feature/geolocation/engine_geolocation_feature.h" | |
| 6 | |
| 7 #include "base/memory/weak_ptr.h" | |
| 8 #include "blimp/common/create_blimp_message.h" | |
| 9 #include "blimp/common/proto/blimp_message.pb.h" | |
| 10 #include "blimp/common/proto/geolocation.pb.h" | |
| 11 #include "content/public/browser/location_provider.h" | |
| 12 #include "content/public/common/geoposition.h" | |
| 13 #include "net/base/net_errors.h" | |
| 14 | |
| 15 namespace blimp { | |
| 16 namespace engine { | |
| 17 namespace { | |
| 18 | |
| 19 content::Geoposition::ErrorCode GetErrorCode( | |
|
Wez
2016/07/12 21:35:14
nit: why is this GetErrorCode but the next method
CJ
2016/07/13 21:49:20
Done.
| |
| 20 const ErrorMessage::ErrorCode& error_code) { | |
| 21 switch (error_code) { | |
| 22 case ErrorMessage::ERROR_CODE_NONE: | |
| 23 return content::Geoposition::ErrorCode::ERROR_CODE_NONE; | |
| 24 case ErrorMessage::ERROR_CODE_PERMISSION_DENIED: | |
| 25 return content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; | |
| 26 case ErrorMessage::ERROR_CODE_POSITION_UNAVAILABLE: | |
| 27 return content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; | |
| 28 case ErrorMessage::ERROR_CODE_TIMEOUT: | |
| 29 return content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 void ConvertLocationMessage(content::Geoposition* output, | |
| 34 const LocationMessage& location) { | |
|
Wez
2016/07/12 21:35:13
nit: Any reason this can't be:
Geoposition Conver
CJ
2016/07/13 21:49:20
Done.
| |
| 35 output->latitude = location.latitude(); | |
| 36 output->longitude = location.longitude(); | |
| 37 output->altitude = location.altitude(); | |
| 38 output->accuracy = location.accuracy(); | |
| 39 output->altitude_accuracy = location.altitude_accuracy(); | |
| 40 output->heading = location.heading(); | |
| 41 output->speed = location.speed(); | |
| 42 output->timestamp = base::Time::FromJsTime(location.timestamp_millis()); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 EngineGeolocationFeature::EngineGeolocationFeature() : weak_factory_(this) {} | |
| 48 | |
| 49 EngineGeolocationFeature::~EngineGeolocationFeature() {} | |
| 50 | |
| 51 void EngineGeolocationFeature::set_outgoing_message_processor( | |
| 52 std::unique_ptr<BlimpMessageProcessor> message_processor) { | |
| 53 DCHECK(message_processor); | |
| 54 outgoing_message_processor_ = std::move(message_processor); | |
| 55 } | |
| 56 | |
| 57 base::WeakPtr<EngineGeolocationFeature> EngineGeolocationFeature::GetWeakPtr() { | |
| 58 return weak_factory_.GetWeakPtr(); | |
| 59 } | |
| 60 | |
| 61 void EngineGeolocationFeature::UpdateListenState(bool enable_high_accuracy) { | |
| 62 GeolocationMessage* geolocation_message; | |
| 63 std::unique_ptr<BlimpMessage> blimp_message = | |
| 64 CreateBlimpMessage(&geolocation_message); | |
| 65 | |
| 66 UpdateListenStateMessage* details = | |
|
Wez
2016/07/12 21:35:13
nit: Name the variable after the field in the mess
CJ
2016/07/13 21:49:21
Done.
| |
| 67 geolocation_message->mutable_update_listen_state(); | |
| 68 if (enable_high_accuracy) { | |
| 69 details->set_listen_state(UpdateListenStateMessage::ACCURACY_HIGH); | |
| 70 } else { | |
| 71 details->set_listen_state(UpdateListenStateMessage::ACCURACY_LOW); | |
| 72 } | |
| 73 | |
| 74 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
| 75 net::CompletionCallback()); | |
| 76 } | |
| 77 | |
| 78 void EngineGeolocationFeature::StopListenState() { | |
| 79 CHECK(false); | |
|
Wez
2016/07/12 21:35:14
This should break your tests, right? But the try-b
CJ
2016/07/13 21:49:21
Fixed.
| |
| 80 GeolocationMessage* geolocation_message; | |
|
Wez
2016/07/12 21:35:14
nit: Here and elsewhere, initialize this to nullpt
CJ
2016/07/13 21:49:21
Done.
| |
| 81 std::unique_ptr<BlimpMessage> blimp_message = | |
| 82 CreateBlimpMessage(&geolocation_message); | |
| 83 | |
| 84 UpdateListenStateMessage* details = | |
| 85 geolocation_message->mutable_update_listen_state(); | |
| 86 details->set_listen_state(UpdateListenStateMessage::STOPPED); | |
| 87 | |
| 88 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
| 89 net::CompletionCallback()); | |
| 90 } | |
| 91 | |
| 92 void EngineGeolocationFeature::RequestRefresh() { | |
| 93 GeolocationMessage* geolocation_message; | |
| 94 std::unique_ptr<BlimpMessage> blimp_message = | |
| 95 CreateBlimpMessage(&geolocation_message); | |
| 96 | |
| 97 geolocation_message->mutable_request_refresh(); | |
| 98 | |
| 99 outgoing_message_processor_->ProcessMessage(std::move(blimp_message), | |
| 100 net::CompletionCallback()); | |
| 101 } | |
| 102 | |
| 103 void EngineGeolocationFeature::SetUpdateCallback( | |
| 104 const base::Callback<void(const content::Geoposition&)>& callback) { | |
| 105 DCHECK(!callback.is_null()); | |
|
Wez
2016/07/12 21:35:13
See BLP comment - I don't think this check (here a
CJ
2016/07/13 21:49:20
Done.
| |
| 106 | |
| 107 callback_ = callback; | |
| 108 } | |
| 109 | |
| 110 void EngineGeolocationFeature::NotifyCallback( | |
| 111 const content::Geoposition& position) { | |
|
Wez
2016/07/12 21:35:13
Why do you need this method to be part of the Dele
CJ
2016/07/13 21:49:20
Done.
| |
| 112 callback_.Run(position); | |
| 113 } | |
| 114 | |
| 115 void EngineGeolocationFeature::ProcessMessage( | |
| 116 std::unique_ptr<BlimpMessage> message, | |
| 117 const net::CompletionCallback& callback) { | |
| 118 DCHECK(!callback.is_null()); | |
| 119 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); | |
| 120 | |
| 121 const GeolocationMessage& geolocation_message = message->geolocation(); | |
| 122 content::Geoposition output; | |
|
Wez
2016/07/12 21:35:14
You only need output in case of kLocation and kErr
CJ
2016/07/13 21:49:20
Done.
| |
| 123 | |
| 124 switch (geolocation_message.type_case()) { | |
| 125 case GeolocationMessage::kLocation: { | |
| 126 const LocationMessage location = geolocation_message.location(); | |
| 127 ConvertLocationMessage(&output, location); | |
| 128 NotifyCallback(output); | |
| 129 break; | |
| 130 } | |
| 131 case GeolocationMessage::kError: { | |
| 132 const ErrorMessage error_message = geolocation_message.error(); | |
| 133 output.error_message = error_message.error_message(); | |
| 134 output.error_code = GetErrorCode(error_message.error_code()); | |
| 135 NotifyCallback(output); | |
| 136 break; | |
| 137 } | |
| 138 case GeolocationMessage::kUpdateListenState: | |
| 139 case GeolocationMessage::kRequestRefresh: | |
| 140 NOTREACHED() << "Engine received unexpected geolocation type."; | |
|
Wez
2016/07/12 21:35:14
NOTREACHED() means "this line of code will never e
CJ
2016/07/13 21:49:20
Done.
| |
| 141 break; | |
| 142 case GeolocationMessage::TYPE_NOT_SET: | |
|
Wez
2016/07/12 21:35:13
nit: You can combine this with the two other unexp
CJ
2016/07/13 21:49:20
Done.
| |
| 143 NOTREACHED(); | |
| 144 break; | |
| 145 } | |
| 146 callback.Run(net::OK); | |
| 147 } | |
| 148 | |
| 149 } // namespace engine | |
| 150 } // namespace blimp | |
| OLD | NEW |