summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/location/location_api.cc
blob: 81b6132337756674b00e4a43cafd88220c549342 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/location/location_api.h"

#include "chrome/browser/extensions/api/location/location_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/location.h"
#include "extensions/common/error_utils.h"

// TODO(vadimt): add tests.

namespace location = extensions::api::location;
namespace WatchLocation = location::WatchLocation;
namespace ClearWatch = location::ClearWatch;

namespace extensions {

const char kMustBePositive[] = "'*' must be 0 or greater.";
const char kMinDistanceInMeters[] = "minDistanceInMeters";
const char kMinTimeInMilliseconds[] = "minTimeInMilliseconds";

bool IsNegative(double* value) {
  return value && *value < 0.0;
}

bool LocationWatchLocationFunction::RunImpl() {
  scoped_ptr<WatchLocation::Params> params(
      WatchLocation::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());

  double* min_distance_in_meters =
      params->request_info.min_distance_in_meters.get();
  if (IsNegative(min_distance_in_meters)) {
    error_ = ErrorUtils::FormatErrorMessage(
        kMustBePositive,
        kMinDistanceInMeters);
    return false;
  }

  double* min_time_in_milliseconds =
      params->request_info.min_time_in_milliseconds.get();
  if (IsNegative(min_time_in_milliseconds)) {
    error_ = ErrorUtils::FormatErrorMessage(
        kMustBePositive,
        kMinTimeInMilliseconds);
    return false;
  }

  // TODO(vadimt): validate and use params->request_info.maximumAge
  LocationManager::Get(GetProfile())
      ->AddLocationRequest(extension_id(),
                           params->name,
                           min_distance_in_meters,
                           min_time_in_milliseconds);

  return true;
}

bool LocationClearWatchFunction::RunImpl() {
  scoped_ptr<ClearWatch::Params> params(
      ClearWatch::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());

  LocationManager::Get(GetProfile())
      ->RemoveLocationRequest(extension_id(), params->name);

  return true;
}

}  // namespace extensions