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

Side by Side Diff: content/browser/geolocation/mock_location_provider.cc

Issue 2028823002: Refactor to make BlimpLocationProvider accessible to content layer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addresses kmarshall's and mvanouwerkerk's comments + code clean up Created 4 years, 6 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file implements a mock location provider and the factory functions for 5 // This file implements a mock location provider and the factory functions for
6 // various ways of creating it. 6 // various ways of creating it.
7 7
8 #include "content/browser/geolocation/mock_location_provider.h" 8 #include "content/browser/geolocation/mock_location_provider.h"
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
17 17
18 namespace content { 18 namespace content {
19 MockLocationProvider* MockLocationProvider::instance_ = NULL;
20 19
21 MockLocationProvider::MockLocationProvider(MockLocationProvider** self_ref) 20 MockLocationProvider::MockLocationProvider()
22 : state_(STOPPED), 21 : state_(STOPPED),
23 is_permission_granted_(false), 22 is_permission_granted_(false),
24 self_ref_(self_ref), 23 provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
25 provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
26 CHECK(self_ref_);
27 CHECK(*self_ref_ == NULL);
28 *self_ref_ = this;
29 }
30 24
31 MockLocationProvider::~MockLocationProvider() { 25 MockLocationProvider::~MockLocationProvider() {}
32 CHECK(*self_ref_ == this);
33 *self_ref_ = NULL;
34 }
35 26
36 void MockLocationProvider::HandlePositionChanged(const Geoposition& position) { 27 void MockLocationProvider::HandlePositionChanged(const Geoposition& position) {
37 if (provider_task_runner_->BelongsToCurrentThread()) { 28 if (provider_task_runner_->BelongsToCurrentThread()) {
38 // The location arbitrator unit tests rely on this method running 29 // The location arbitrator unit tests rely on this method running
39 // synchronously. 30 // synchronously.
40 position_ = position; 31 position_ = position;
41 NotifyCallback(position_); 32 NotifyCallback(position_);
42 } else { 33 } else {
43 provider_task_runner_->PostTask( 34 provider_task_runner_->PostTask(
44 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, 35 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged,
(...skipping 18 matching lines...) Expand all
63 is_permission_granted_ = true; 54 is_permission_granted_ = true;
64 } 55 }
65 56
66 // Mock location provider that automatically calls back its client at most 57 // Mock location provider that automatically calls back its client at most
67 // once, when StartProvider or OnPermissionGranted is called. Use 58 // once, when StartProvider or OnPermissionGranted is called. Use
68 // |requires_permission_to_start| to select which event triggers the callback. 59 // |requires_permission_to_start| to select which event triggers the callback.
69 class AutoMockLocationProvider : public MockLocationProvider { 60 class AutoMockLocationProvider : public MockLocationProvider {
70 public: 61 public:
71 AutoMockLocationProvider(bool has_valid_location, 62 AutoMockLocationProvider(bool has_valid_location,
72 bool requires_permission_to_start) 63 bool requires_permission_to_start)
73 : MockLocationProvider(&instance_), 64 : weak_factory_(this),
74 weak_factory_(this),
75 requires_permission_to_start_(requires_permission_to_start), 65 requires_permission_to_start_(requires_permission_to_start),
76 listeners_updated_(false) { 66 listeners_updated_(false) {
77 if (has_valid_location) { 67 if (has_valid_location) {
78 position_.accuracy = 3; 68 position_.accuracy = 3;
79 position_.latitude = 4.3; 69 position_.latitude = 4.3;
80 position_.longitude = -7.8; 70 position_.longitude = -7.8;
81 // Webkit compares the timestamp to wall clock time, so we need it to be 71 // Webkit compares the timestamp to wall clock time, so we need it to be
82 // contemporary. 72 // contemporary.
83 position_.timestamp = base::Time::Now(); 73 position_.timestamp = base::Time::Now();
84 } else { 74 } else {
(...skipping 16 matching lines...) Expand all
101 } 91 }
102 92
103 void UpdateListenersIfNeeded() { 93 void UpdateListenersIfNeeded() {
104 if (!listeners_updated_) { 94 if (!listeners_updated_) {
105 listeners_updated_ = true; 95 listeners_updated_ = true;
106 base::ThreadTaskRunnerHandle::Get()->PostTask( 96 base::ThreadTaskRunnerHandle::Get()->PostTask(
107 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, 97 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged,
108 weak_factory_.GetWeakPtr(), position_)); 98 weak_factory_.GetWeakPtr(), position_));
109 } 99 }
110 } 100 }
111 101
Kevin M 2016/06/09 20:36:44 make these private
CJ 2016/06/09 23:07:06 Done.
112 base::WeakPtrFactory<MockLocationProvider> weak_factory_; 102 base::WeakPtrFactory<MockLocationProvider> weak_factory_;
Kevin M 2016/06/09 20:36:44 I know this isn't your module, but here are a few
CJ 2016/06/09 23:07:06 Done.
113 const bool requires_permission_to_start_; 103 const bool requires_permission_to_start_;
114 bool listeners_updated_; 104 bool listeners_updated_;
115 }; 105 };
116 106
117 LocationProvider* NewMockLocationProvider() { 107 LocationProvider* NewMockLocationProvider() {
118 return new MockLocationProvider(&MockLocationProvider::instance_); 108 return new MockLocationProvider;
119 } 109 }
120 110
121 LocationProvider* NewAutoSuccessMockLocationProvider() { 111 LocationProvider* NewAutoSuccessMockLocationProvider() {
122 return new AutoMockLocationProvider(true, false); 112 return new AutoMockLocationProvider(true, false);
123 } 113 }
124 114
125 LocationProvider* NewAutoFailMockLocationProvider() { 115 LocationProvider* NewAutoFailMockLocationProvider() {
126 return new AutoMockLocationProvider(false, false); 116 return new AutoMockLocationProvider(false, false);
127 } 117 }
128 118
129 LocationProvider* NewAutoSuccessMockNetworkLocationProvider() { 119 LocationProvider* NewAutoSuccessMockNetworkLocationProvider() {
130 return new AutoMockLocationProvider(true, true); 120 return new AutoMockLocationProvider(true, true);
131 } 121 }
132 122
133 } // namespace content 123 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698