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

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: Fixes try issue 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 : requires_permission_to_start_(requires_permission_to_start),
74 weak_factory_(this),
75 requires_permission_to_start_(requires_permission_to_start),
76 listeners_updated_(false) { 65 listeners_updated_(false) {
77 if (has_valid_location) { 66 if (has_valid_location) {
78 position_.accuracy = 3; 67 position_.accuracy = 3;
79 position_.latitude = 4.3; 68 position_.latitude = 4.3;
80 position_.longitude = -7.8; 69 position_.longitude = -7.8;
81 // Webkit compares the timestamp to wall clock time, so we need it to be 70 // Webkit compares the timestamp to wall clock time, so we need it to be
82 // contemporary. 71 // contemporary.
83 position_.timestamp = base::Time::Now(); 72 position_.timestamp = base::Time::Now();
84 } else { 73 } else {
85 position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; 74 position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE;
(...skipping 12 matching lines...) Expand all
98 if (requires_permission_to_start_) { 87 if (requires_permission_to_start_) {
99 UpdateListenersIfNeeded(); 88 UpdateListenersIfNeeded();
100 } 89 }
101 } 90 }
102 91
103 void UpdateListenersIfNeeded() { 92 void UpdateListenersIfNeeded() {
104 if (!listeners_updated_) { 93 if (!listeners_updated_) {
105 listeners_updated_ = true; 94 listeners_updated_ = true;
106 base::ThreadTaskRunnerHandle::Get()->PostTask( 95 base::ThreadTaskRunnerHandle::Get()->PostTask(
107 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, 96 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged,
108 weak_factory_.GetWeakPtr(), position_)); 97 base::Unretained(this), position_));
109 } 98 }
110 } 99 }
111 100
112 base::WeakPtrFactory<MockLocationProvider> weak_factory_; 101 private:
113 const bool requires_permission_to_start_; 102 const bool requires_permission_to_start_;
114 bool listeners_updated_; 103 bool listeners_updated_;
104
105 DISALLOW_COPY_AND_ASSIGN(AutoMockLocationProvider);
115 }; 106 };
116 107
117 LocationProvider* NewMockLocationProvider() { 108 LocationProvider* NewMockLocationProvider() {
118 return new MockLocationProvider(&MockLocationProvider::instance_); 109 return new MockLocationProvider;
119 } 110 }
120 111
121 LocationProvider* NewAutoSuccessMockLocationProvider() { 112 LocationProvider* NewAutoSuccessMockLocationProvider() {
122 return new AutoMockLocationProvider(true, false); 113 return new AutoMockLocationProvider(true, false);
123 } 114 }
124 115
125 LocationProvider* NewAutoFailMockLocationProvider() { 116 LocationProvider* NewAutoFailMockLocationProvider() {
126 return new AutoMockLocationProvider(false, false); 117 return new AutoMockLocationProvider(false, false);
127 } 118 }
128 119
129 LocationProvider* NewAutoSuccessMockNetworkLocationProvider() { 120 LocationProvider* NewAutoSuccessMockNetworkLocationProvider() {
130 return new AutoMockLocationProvider(true, true); 121 return new AutoMockLocationProvider(true, true);
131 } 122 }
132 123
133 } // namespace content 124 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/geolocation/mock_location_provider.h ('k') | content/public/browser/content_browser_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698