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

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

Issue 6597044: Revert 76228 - Move core pieces of geolocation from chrome to content.This is... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 9 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 // This file implements a mock location provider and the factory functions for
6 // various ways of creating it.
7
8 #include "content/browser/geolocation/mock_location_provider.h"
9
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "base/message_loop.h"
13 #include "base/message_loop_proxy.h"
14 #include "base/task.h"
15
16 // The provider will always be destroyed before the thread it runs on, so ref
17 // counting is not required.
18 DISABLE_RUNNABLE_METHOD_REFCOUNT(MockLocationProvider);
19
20 MockLocationProvider* MockLocationProvider::instance_ = NULL;
21
22 MockLocationProvider::MockLocationProvider(MockLocationProvider** self_ref)
23 : state_(STOPPED),
24 self_ref_(self_ref),
25 provider_loop_(base::MessageLoopProxy::CreateForCurrentThread()) {
26 CHECK(self_ref_);
27 CHECK(*self_ref_ == NULL);
28 *self_ref_ = this;
29 }
30
31 MockLocationProvider::~MockLocationProvider() {
32 CHECK(*self_ref_ == this);
33 *self_ref_ = NULL;
34 }
35
36 void MockLocationProvider::HandlePositionChanged(const Geoposition& position) {
37 if (provider_loop_->BelongsToCurrentThread()) {
38 // The location arbitrator unit tests rely on this method running
39 // synchronously.
40 position_ = position;
41 UpdateListeners();
42 } else {
43 Task* task = NewRunnableMethod(
44 this, &MockLocationProvider::HandlePositionChanged, position);
45 provider_loop_->PostTask(FROM_HERE, task);
46 }
47 }
48
49 bool MockLocationProvider::StartProvider(bool high_accuracy) {
50 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY;
51 return true;
52 }
53
54 void MockLocationProvider::StopProvider() {
55 state_ = STOPPED;
56 }
57
58 void MockLocationProvider::GetPosition(Geoposition* position) {
59 *position = position_;
60 }
61
62 void MockLocationProvider::OnPermissionGranted(const GURL& requesting_frame) {
63 permission_granted_url_ = requesting_frame;
64 }
65
66 // Mock location provider that automatically calls back it's client at most
67 // once, when StartProvider or OnPermissionGranted is called. Use
68 // |requires_permission_to_start| to select which event triggers the callback.
69 class AutoMockLocationProvider : public MockLocationProvider {
70 public:
71 AutoMockLocationProvider(bool has_valid_location,
72 bool requires_permission_to_start)
73 : MockLocationProvider(&instance_),
74 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)),
75 requires_permission_to_start_(requires_permission_to_start),
76 listeners_updated_(false) {
77 if (has_valid_location) {
78 position_.accuracy = 3;
79 position_.latitude = 4.3;
80 position_.longitude = -7.8;
81 // Webkit compares the timestamp to wall clock time, so we need it to be
82 // contemporary.
83 position_.timestamp = base::Time::Now();
84 } else {
85 position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE;
86 }
87 }
88 virtual bool StartProvider(bool high_accuracy) {
89 MockLocationProvider::StartProvider(high_accuracy);
90 if (!requires_permission_to_start_) {
91 UpdateListenersIfNeeded();
92 }
93 return true;
94 }
95
96 void OnPermissionGranted(const GURL& requesting_frame) {
97 MockLocationProvider::OnPermissionGranted(requesting_frame);
98 if (requires_permission_to_start_) {
99 UpdateListenersIfNeeded();
100 }
101 }
102
103 void UpdateListenersIfNeeded() {
104 if (!listeners_updated_) {
105 listeners_updated_ = true;
106 MessageLoop::current()->PostTask(
107 FROM_HERE, task_factory_.NewRunnableMethod(
108 &MockLocationProvider::HandlePositionChanged, position_));
109 }
110 }
111
112 ScopedRunnableMethodFactory<MockLocationProvider> task_factory_;
113 const bool requires_permission_to_start_;
114 bool listeners_updated_;
115 };
116
117 LocationProviderBase* NewMockLocationProvider() {
118 return new MockLocationProvider(&MockLocationProvider::instance_);
119 }
120
121 LocationProviderBase* NewAutoSuccessMockLocationProvider() {
122 return new AutoMockLocationProvider(true, false);
123 }
124
125 LocationProviderBase* NewAutoFailMockLocationProvider() {
126 return new AutoMockLocationProvider(false, false);
127 }
128
129 LocationProviderBase* NewAutoSuccessMockNetworkLocationProvider() {
130 return new AutoMockLocationProvider(true, true);
131 }
OLDNEW
« no previous file with comments | « content/browser/geolocation/mock_location_provider.h ('k') | content/browser/geolocation/network_location_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698