Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / location / location_api.cc
blobc406ff68f67c4c47dff9eca5ac9847a656cd148a
1 // Copyright (c) 2013 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.
5 #include "chrome/browser/extensions/api/location/location_api.h"
7 #include "chrome/browser/extensions/api/location/location_manager.h"
8 #include "chrome/common/extensions/api/location.h"
9 #include "extensions/common/error_utils.h"
11 // TODO(vadimt): add tests.
13 namespace location = extensions::api::location;
14 namespace WatchLocation = location::WatchLocation;
15 namespace ClearWatch = location::ClearWatch;
17 namespace extensions {
19 const char kMustBePositive[] = "'*' must be 0 or greater.";
20 const char kMinDistanceInMeters[] = "minDistanceInMeters";
21 const char kMinTimeInMilliseconds[] = "minTimeInMilliseconds";
23 bool IsNegative(double* value) {
24 return value && *value < 0.0;
27 bool LocationWatchLocationFunction::RunSync() {
28 scoped_ptr<WatchLocation::Params> params(
29 WatchLocation::Params::Create(*args_));
30 EXTENSION_FUNCTION_VALIDATE(params.get());
32 double* min_distance_in_meters =
33 params->request_info.min_distance_in_meters.get();
34 if (IsNegative(min_distance_in_meters)) {
35 error_ = ErrorUtils::FormatErrorMessage(
36 kMustBePositive,
37 kMinDistanceInMeters);
38 return false;
41 double* min_time_in_milliseconds =
42 params->request_info.min_time_in_milliseconds.get();
43 if (IsNegative(min_time_in_milliseconds)) {
44 error_ = ErrorUtils::FormatErrorMessage(
45 kMustBePositive,
46 kMinTimeInMilliseconds);
47 return false;
50 // TODO(vadimt): validate and use params->request_info.maximumAge
51 LocationManager::Get(browser_context())
52 ->AddLocationRequest(extension_id(),
53 params->name,
54 min_distance_in_meters,
55 min_time_in_milliseconds);
57 return true;
60 bool LocationClearWatchFunction::RunSync() {
61 scoped_ptr<ClearWatch::Params> params(
62 ClearWatch::Params::Create(*args_));
63 EXTENSION_FUNCTION_VALIDATE(params.get());
65 LocationManager::Get(browser_context())
66 ->RemoveLocationRequest(extension_id(), params->name);
68 return true;
71 } // namespace extensions