1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/runtime/browser/geolocation/tizen/location_provider_tizen.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/time/time.h"
11 #include "content/public/common/geoposition.h"
15 LocationProviderTizen::LocationProviderTizen()
17 geolocation_message_loop_(base::MessageLoop::current()),
18 is_permission_granted_(false) {}
20 LocationProviderTizen::~LocationProviderTizen() {
24 bool LocationProviderTizen::StartProvider(bool high_accuracy) {
25 // Tizen location framework doesn't have API that could be used to set
26 // preferred accuracy. Accuracy would be provided by the framework when
27 // position is updated.
28 if (InitLocationManager() && is_permission_granted_)
29 return location_manager_start(manager_) == LOCATIONS_ERROR_NONE;
34 void LocationProviderTizen::StopProvider() {
36 location_manager_unset_position_updated_cb(manager_);
37 location_manager_stop(manager_);
38 location_manager_destroy(manager_);
41 LOG(WARNING) << "Location manager not initialized.";
45 void LocationProviderTizen::GetPosition(content::Geoposition* position) {
46 *position = last_position_;
49 void LocationProviderTizen::RequestRefresh() {
50 // Tizen location framework sends updates automatically. No need to refresh.
53 void LocationProviderTizen::OnPermissionGranted() {
54 is_permission_granted_ = true;
59 bool LocationProviderTizen::InitLocationManager() {
63 // FIXME(shalamov): Tizen location manager throws critical error when
64 // hybrid location method is used. Using GPS until it is fixed.
65 int ret = location_manager_create(LOCATIONS_METHOD_GPS, &manager_);
66 if (ret != LOCATIONS_ERROR_NONE) {
67 LOG(ERROR) << "Cannot create location manager.";
72 ret = location_manager_set_service_state_changed_cb(manager_,
73 &LocationProviderTizen::OnStateChanged,
76 if (ret != LOCATIONS_ERROR_NONE) {
77 location_manager_unset_position_updated_cb(manager_);
78 location_manager_destroy(manager_);
86 void LocationProviderTizen::NotifyLocationProvider() {
89 content::Geoposition pos;
91 ret = location_manager_get_position(manager_,
96 pos.timestamp = base::Time::FromTimeT(timestamp);
98 if (ret != LOCATIONS_ERROR_NONE) {
99 LOG(ERROR) << "Cannot retrieve position from location manager.";
103 location_accuracy_level_e level;
106 ret = location_manager_get_accuracy(manager_,
111 if (ret != LOCATIONS_ERROR_NONE)
112 LOG(ERROR) << "Cannot retrieve position accuracy from location manager.";
114 pos.accuracy = (horizontal / 2) + (vertical / 2);
116 last_position_ = pos;
117 if (geolocation_message_loop_) {
118 base::Closure task = base::Bind(&LocationProviderTizen::NotifyCallback,
119 base::Unretained(this),
121 geolocation_message_loop_->PostTask(FROM_HERE, task);
125 void LocationProviderTizen::OnStateChanged(location_service_state_e state,
128 LocationProviderTizen* impl = static_cast<LocationProviderTizen*>(data);
129 if (state == LOCATIONS_SERVICE_ENABLED)
130 impl->NotifyLocationProvider();
136 __attribute__((visibility("default"))) content::LocationProvider*
137 NewSystemLocationProvider() {
138 return new xwalk::LocationProviderTizen;
141 } // namespace content