| 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/client/core/geolocation/geolocation_feature.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "blimp/common/create_blimp_message.h" | |
| 12 #include "blimp/common/proto/blimp_message.pb.h" | |
| 13 #include "device/geolocation/geoposition.h" | |
| 14 #include "device/geolocation/location_provider.h" | |
| 15 #include "net/base/net_errors.h" | |
| 16 | |
| 17 namespace blimp { | |
| 18 namespace client { | |
| 19 | |
| 20 GeolocationFeature::GeolocationFeature( | |
| 21 std::unique_ptr<device::LocationProvider> location_provider) | |
| 22 : location_provider_(std::move(location_provider)) { | |
| 23 location_provider_->SetUpdateCallback(base::Bind( | |
| 24 &GeolocationFeature::OnLocationUpdate, base::Unretained(this))); | |
| 25 } | |
| 26 | |
| 27 GeolocationFeature::~GeolocationFeature() {} | |
| 28 | |
| 29 void GeolocationFeature::set_outgoing_message_processor( | |
| 30 std::unique_ptr<BlimpMessageProcessor> processor) { | |
| 31 outgoing_message_processor_ = std::move(processor); | |
| 32 am_sending_message_ = false; | |
| 33 } | |
| 34 | |
| 35 void GeolocationFeature::ProcessMessage( | |
| 36 std::unique_ptr<BlimpMessage> message, | |
| 37 const net::CompletionCallback& callback) { | |
| 38 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case()); | |
| 39 | |
| 40 int result = net::OK; | |
| 41 const GeolocationMessage& geolocation_message = message->geolocation(); | |
| 42 switch (geolocation_message.type_case()) { | |
| 43 case GeolocationMessage::kSetInterestLevel: | |
| 44 SetInterestLevel(geolocation_message.set_interest_level().level()); | |
| 45 break; | |
| 46 case GeolocationMessage::kRequestRefresh: | |
| 47 location_provider_->OnPermissionGranted(); | |
| 48 break; | |
| 49 case GeolocationMessage::kCoordinates: | |
| 50 case GeolocationMessage::kError: | |
| 51 case GeolocationMessage::TYPE_NOT_SET: | |
| 52 result = net::ERR_UNEXPECTED; | |
| 53 break; | |
| 54 } | |
| 55 | |
| 56 if (!callback.is_null()) { | |
| 57 callback.Run(result); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 void GeolocationFeature::OnLocationUpdate( | |
| 62 const device::LocationProvider* location_provider, | |
| 63 const device::Geoposition& position) { | |
| 64 DCHECK_EQ(location_provider_.get(), location_provider); | |
| 65 | |
| 66 if (!am_sending_message_) { | |
| 67 switch (position.error_code) { | |
| 68 case device::Geoposition::ERROR_CODE_NONE: | |
| 69 SendGeolocationPositionMessage(position); | |
| 70 break; | |
| 71 case device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED: | |
| 72 SendGeolocationErrorMessage(GeolocationErrorMessage::PERMISSION_DENIED, | |
| 73 position.error_message); | |
| 74 break; | |
| 75 case device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE: | |
| 76 SendGeolocationErrorMessage( | |
| 77 GeolocationErrorMessage::POSITION_UNAVAILABLE, | |
| 78 position.error_message); | |
| 79 break; | |
| 80 case device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT: | |
| 81 SendGeolocationErrorMessage(GeolocationErrorMessage::TIMEOUT, | |
| 82 position.error_message); | |
| 83 break; | |
| 84 } | |
| 85 } else { | |
| 86 need_to_send_message_ = true; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void GeolocationFeature::SetInterestLevel( | |
| 91 GeolocationSetInterestLevelMessage::Level level) { | |
| 92 switch (level) { | |
| 93 case GeolocationSetInterestLevelMessage::HIGH_ACCURACY: | |
| 94 location_provider_->StartProvider(true); | |
| 95 break; | |
| 96 case GeolocationSetInterestLevelMessage::LOW_ACCURACY: | |
| 97 location_provider_->StartProvider(false); | |
| 98 break; | |
| 99 case GeolocationSetInterestLevelMessage::NO_INTEREST: | |
| 100 location_provider_->StopProvider(); | |
| 101 break; | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 void GeolocationFeature::SendGeolocationPositionMessage( | |
| 106 const device::Geoposition& position) { | |
| 107 GeolocationMessage* geolocation_message = nullptr; | |
| 108 std::unique_ptr<BlimpMessage> blimp_message = | |
| 109 CreateBlimpMessage(&geolocation_message); | |
| 110 | |
| 111 GeolocationCoordinatesMessage* coordinates = | |
| 112 geolocation_message->mutable_coordinates(); | |
| 113 coordinates->set_latitude(position.latitude); | |
| 114 coordinates->set_longitude(position.longitude); | |
| 115 coordinates->set_altitude(position.altitude); | |
| 116 coordinates->set_accuracy(position.accuracy); | |
| 117 coordinates->set_altitude_accuracy(position.altitude_accuracy); | |
| 118 coordinates->set_heading(position.heading); | |
| 119 coordinates->set_speed(position.speed); | |
| 120 | |
| 121 am_sending_message_ = true; | |
| 122 outgoing_message_processor_->ProcessMessage( | |
| 123 std::move(blimp_message), | |
| 124 base::Bind(&GeolocationFeature::OnSendComplete, base::Unretained(this))); | |
| 125 } | |
| 126 | |
| 127 void GeolocationFeature::SendGeolocationErrorMessage( | |
| 128 GeolocationErrorMessage::ErrorCode error_code, | |
| 129 const std::string& error_message) { | |
| 130 GeolocationMessage* geolocation_message = nullptr; | |
| 131 std::unique_ptr<BlimpMessage> blimp_message = | |
| 132 CreateBlimpMessage(&geolocation_message); | |
| 133 | |
| 134 GeolocationErrorMessage* error = geolocation_message->mutable_error(); | |
| 135 error->set_error_code(error_code); | |
| 136 error->set_error_message(error_message); | |
| 137 | |
| 138 am_sending_message_ = true; | |
| 139 outgoing_message_processor_->ProcessMessage( | |
| 140 std::move(blimp_message), | |
| 141 base::Bind(&GeolocationFeature::OnSendComplete, base::Unretained(this))); | |
| 142 } | |
| 143 | |
| 144 void GeolocationFeature::OnSendComplete(int result) { | |
| 145 am_sending_message_ = false; | |
| 146 if (need_to_send_message_) { | |
| 147 device::Geoposition new_position = location_provider_->GetPosition(); | |
| 148 if (new_position.Validate()) { | |
| 149 OnLocationUpdate(location_provider_.get(), new_position); | |
| 150 } | |
| 151 need_to_send_message_ = false; | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 } // namespace client | |
| 156 } // namespace blimp | |
| OLD | NEW |