fake_location_provider.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // This file implements a fake location provider and the factory functions for
  5. // various ways of creating it.
  6. // TODO(lethalantidote): Convert location_arbitrator_impl to use actual mock
  7. // instead of FakeLocationProvider.
  8. #include "services/device/geolocation/fake_location_provider.h"
  9. #include "base/bind.h"
  10. #include "base/callback_helpers.h"
  11. #include "base/compiler_specific.h"
  12. #include "base/location.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/threading/thread_task_runner_handle.h"
  15. namespace device {
  16. FakeLocationProvider::FakeLocationProvider()
  17. : provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
  18. FakeLocationProvider::~FakeLocationProvider() = default;
  19. void FakeLocationProvider::HandlePositionChanged(
  20. const mojom::Geoposition& position) {
  21. if (provider_task_runner_->BelongsToCurrentThread()) {
  22. // The location arbitrator unit tests rely on this method running
  23. // synchronously.
  24. position_ = position;
  25. if (!callback_.is_null())
  26. callback_.Run(this, position_);
  27. } else {
  28. provider_task_runner_->PostTask(
  29. FROM_HERE, base::BindOnce(&FakeLocationProvider::HandlePositionChanged,
  30. base::Unretained(this), position));
  31. }
  32. }
  33. void FakeLocationProvider::SetUpdateCallback(
  34. const LocationProviderUpdateCallback& callback) {
  35. callback_ = callback;
  36. }
  37. void FakeLocationProvider::StartProvider(bool high_accuracy) {
  38. state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY;
  39. }
  40. void FakeLocationProvider::StopProvider() {
  41. state_ = STOPPED;
  42. }
  43. const mojom::Geoposition& FakeLocationProvider::GetPosition() {
  44. return position_;
  45. }
  46. void FakeLocationProvider::OnPermissionGranted() {
  47. is_permission_granted_ = true;
  48. }
  49. } // namespace device