| OLD | NEW |
| (Empty) |
| 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 | |
| 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/bind.h" | |
| 11 #include "base/bind_helpers.h" | |
| 12 #include "base/compiler_specific.h" | |
| 13 #include "base/location.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 MockLocationProvider::MockLocationProvider() | |
| 21 : state_(STOPPED), | |
| 22 is_permission_granted_(false), | |
| 23 provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} | |
| 24 | |
| 25 MockLocationProvider::~MockLocationProvider() {} | |
| 26 | |
| 27 void MockLocationProvider::HandlePositionChanged(const Geoposition& position) { | |
| 28 if (provider_task_runner_->BelongsToCurrentThread()) { | |
| 29 // The location arbitrator unit tests rely on this method running | |
| 30 // synchronously. | |
| 31 position_ = position; | |
| 32 NotifyCallback(position_); | |
| 33 } else { | |
| 34 provider_task_runner_->PostTask( | |
| 35 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, | |
| 36 base::Unretained(this), position)); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 bool MockLocationProvider::StartProvider(bool high_accuracy) { | |
| 41 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY; | |
| 42 return true; | |
| 43 } | |
| 44 | |
| 45 void MockLocationProvider::StopProvider() { | |
| 46 state_ = STOPPED; | |
| 47 } | |
| 48 | |
| 49 void MockLocationProvider::GetPosition(Geoposition* position) { | |
| 50 *position = position_; | |
| 51 } | |
| 52 | |
| 53 void MockLocationProvider::OnPermissionGranted() { | |
| 54 is_permission_granted_ = true; | |
| 55 } | |
| 56 | |
| 57 // Mock location provider that automatically calls back its client at most | |
| 58 // once, when StartProvider or OnPermissionGranted is called. Use | |
| 59 // |requires_permission_to_start| to select which event triggers the callback. | |
| 60 class AutoMockLocationProvider : public MockLocationProvider { | |
| 61 public: | |
| 62 AutoMockLocationProvider(bool has_valid_location, | |
| 63 bool requires_permission_to_start) | |
| 64 : requires_permission_to_start_(requires_permission_to_start), | |
| 65 listeners_updated_(false) { | |
| 66 if (has_valid_location) { | |
| 67 position_.accuracy = 3; | |
| 68 position_.latitude = 4.3; | |
| 69 position_.longitude = -7.8; | |
| 70 // Webkit compares the timestamp to wall clock time, so we need it to be | |
| 71 // contemporary. | |
| 72 position_.timestamp = base::Time::Now(); | |
| 73 } else { | |
| 74 position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | |
| 75 } | |
| 76 } | |
| 77 bool StartProvider(bool high_accuracy) override { | |
| 78 MockLocationProvider::StartProvider(high_accuracy); | |
| 79 if (!requires_permission_to_start_) { | |
| 80 UpdateListenersIfNeeded(); | |
| 81 } | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 void OnPermissionGranted() override { | |
| 86 MockLocationProvider::OnPermissionGranted(); | |
| 87 if (requires_permission_to_start_) { | |
| 88 UpdateListenersIfNeeded(); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void UpdateListenersIfNeeded() { | |
| 93 if (!listeners_updated_) { | |
| 94 listeners_updated_ = true; | |
| 95 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 96 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, | |
| 97 base::Unretained(this), position_)); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 private: | |
| 102 const bool requires_permission_to_start_; | |
| 103 bool listeners_updated_; | |
| 104 | |
| 105 DISALLOW_COPY_AND_ASSIGN(AutoMockLocationProvider); | |
| 106 }; | |
| 107 | |
| 108 LocationProvider* NewMockLocationProvider() { | |
| 109 return new MockLocationProvider; | |
| 110 } | |
| 111 | |
| 112 LocationProvider* NewAutoSuccessMockLocationProvider() { | |
| 113 return new AutoMockLocationProvider(true, false); | |
| 114 } | |
| 115 | |
| 116 LocationProvider* NewAutoFailMockLocationProvider() { | |
| 117 return new AutoMockLocationProvider(false, false); | |
| 118 } | |
| 119 | |
| 120 LocationProvider* NewAutoSuccessMockNetworkLocationProvider() { | |
| 121 return new AutoMockLocationProvider(true, true); | |
| 122 } | |
| 123 | |
| 124 } // namespace content | |
| OLD | NEW |