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

Side by Side 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: 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 unified diff | Download patch
OLDNEW
(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/feature/geolocation_feature.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "blimp/common/create_blimp_message.h"
11 #include "blimp/common/proto/blimp_message.pb.h"
12 #include "blimp/common/proto/geolocation.pb.h"
13 #include "content/public/common/geoposition.h"
14 #include "net/base/net_errors.h"
15
16 namespace blimp {
17 namespace client {
18
19 GeolocationFeature::GeolocationFeature() {}
20
21 GeolocationFeature::~GeolocationFeature() {}
22
23 void GeolocationFeature::set_outgoing_message_processor(
24 std::unique_ptr<BlimpMessageProcessor> processor) {
25 outgoing_message_processor_ = std::move(processor);
26 }
27
28 void GeolocationFeature::SetDelegate(GeolocationFeatureDelegate* delegate) {
29 DCHECK(delegate);
30
31 delegate_ = delegate;
32 }
33
34 void GeolocationFeature::OnLocationUpdate(
35 const content::Geoposition& position) {
36 if (position.error_code == content::Geoposition::ERROR_CODE_NONE) {
37 SendGeolocationPositionMessage(position);
38 } else {
39 SendGeolocationErrorMessage(position);
40 }
41 }
42
43 void GeolocationFeature::SendGeolocationPositionMessage(
44 const content::Geoposition& position) {
45 GeolocationMessage* geolocation_message = nullptr;
46 std::unique_ptr<BlimpMessage> blimp_message =
47 CreateBlimpMessage(&geolocation_message);
48 GeolocationCoordinatesMessage* coordinates =
49 geolocation_message->mutable_coordinates();
50
51 coordinates->set_latitude(position.latitude);
52 coordinates->set_longitude(position.longitude);
53 coordinates->set_altitude(position.altitude);
54 coordinates->set_accuracy(position.accuracy);
55 coordinates->set_altitude_accuracy(position.altitude_accuracy);
56 coordinates->set_heading(position.heading);
57 coordinates->set_speed(position.speed);
58
59 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
60 net::CompletionCallback());
61 }
62
63 void GeolocationFeature::SendGeolocationErrorMessage(
64 const content::Geoposition& position) {
65 GeolocationMessage* geolocation_message = nullptr;
66 std::unique_ptr<BlimpMessage> blimp_message =
67 CreateBlimpMessage(&geolocation_message);
68
69 GeolocationErrorMessage* error = geolocation_message->mutable_error();
70 switch (position.error_code) {
71 case content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED:
72 error->set_error_code(GeolocationErrorMessage::PERMISSION_DENIED);
73 break;
74 case content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE:
75 error->set_error_code(GeolocationErrorMessage::POSITION_UNAVAILABLE);
76 break;
77 case content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT:
78 error->set_error_code(GeolocationErrorMessage::TIMEOUT);
79 break;
80 // This case shouldn't be hit. Error?
Kevin M 2016/07/20 17:30:15 Use NOTREACHED() instead of a comment.
CJ 2016/07/20 23:22:10 Cool thanks. Are using comments to ask questions a
81 case content::Geoposition::ErrorCode::ERROR_CODE_NONE:
82 break;
83 }
84
85 error->set_error_message(position.error_message);
86
87 outgoing_message_processor_->ProcessMessage(std::move(blimp_message),
88 net::CompletionCallback());
89 }
90
91 void GeolocationFeature::ProcessMessage(
92 std::unique_ptr<BlimpMessage> message,
93 const net::CompletionCallback& callback) {
94 DCHECK(!callback.is_null());
95 DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case());
96 DCHECK(delegate_);
97
98 const GeolocationMessage& geolocation_message = message->geolocation();
99 switch (geolocation_message.type_case()) {
100 case GeolocationMessage::kSetInterestLevel: {
101 const GeolocationSetInterestLevelMessage& set_level_message =
102 geolocation_message.set_interest_level();
103 if (set_level_message.has_level()) {
Kevin M 2016/07/20 17:30:15 If it doesn't have a level, doesn't this mean that
CJ 2016/07/20 23:22:10 True. I'm thinking the delegate should handle this
104 delegate_->OnGeolocationInterestUpdate(set_level_message.level());
105 }
106 break;
107 }
108 case GeolocationMessage::kRequestRefresh:
109 delegate_->OnRequestRefresh();
110 break;
111 case GeolocationMessage::kCoordinates:
112 case GeolocationMessage::kError:
113 case GeolocationMessage::TYPE_NOT_SET:
114 DLOG(FATAL) << "Engine sent unexpected message type.";
Kevin M 2016/07/20 17:30:15 DLOG only crashes on debug builds. We should fail
CJ 2016/07/20 23:22:10 Done.
115 break;
116 }
117 callback.Run(net::OK);
118 }
119
120 } // namespace client
121 } // namespace blimp
OLDNEW
« blimp/client/feature/geolocation_feature.h ('K') | « blimp/client/feature/geolocation_feature.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698