| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 #include "content/browser/geolocation/gateway_data_provider_common.h" | |
| 6 | |
| 7 #include "base/utf_string_conversions.h" | |
| 8 | |
| 9 GatewayDataProviderCommon::GatewayDataProviderCommon() | |
| 10 : Thread("Geolocation_gateway_provider"), | |
| 11 is_first_scan_complete_(false), | |
| 12 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { | |
| 13 } | |
| 14 | |
| 15 GatewayDataProviderCommon::~GatewayDataProviderCommon() { | |
| 16 // Thread must be stopped before entering destructor chain to avoid race | |
| 17 // conditions; see comment in DeviceDataProvider::Unregister. | |
| 18 DCHECK(!IsRunning()); // Must call StopDataProvider before destroying me. | |
| 19 } | |
| 20 | |
| 21 bool GatewayDataProviderCommon::StartDataProvider() { | |
| 22 DCHECK(CalledOnClientThread()); | |
| 23 DCHECK(!IsRunning()); // StartDataProvider must only be called once. | |
| 24 return Start(); | |
| 25 } | |
| 26 | |
| 27 void GatewayDataProviderCommon::StopDataProvider() { | |
| 28 DCHECK(CalledOnClientThread()); | |
| 29 Stop(); | |
| 30 } | |
| 31 | |
| 32 bool GatewayDataProviderCommon::GetData(GatewayData *data) { | |
| 33 DCHECK(CalledOnClientThread()); | |
| 34 DCHECK(data); | |
| 35 base::AutoLock lock(data_mutex_); | |
| 36 *data = gateway_data_; | |
| 37 // If we've successfully completed a scan, indicate that we have all of the | |
| 38 // data we can get. | |
| 39 return is_first_scan_complete_; | |
| 40 } | |
| 41 | |
| 42 // Thread implementation. | |
| 43 void GatewayDataProviderCommon::Init() { | |
| 44 DCHECK(gateway_api_ == NULL); | |
| 45 gateway_api_.reset(NewGatewayApi()); | |
| 46 if (gateway_api_ == NULL) { | |
| 47 // Error! Can't do scans, so don't try and schedule one. | |
| 48 is_first_scan_complete_ = true; | |
| 49 return; | |
| 50 } | |
| 51 DCHECK(polling_policy_ == NULL); | |
| 52 polling_policy_.reset(NewPollingPolicy()); | |
| 53 DCHECK(polling_policy_ != NULL); | |
| 54 ScheduleNextScan(0); | |
| 55 } | |
| 56 | |
| 57 void GatewayDataProviderCommon::CleanUp() { | |
| 58 // Destroy these instances in the thread on which they were created. | |
| 59 gateway_api_.reset(); | |
| 60 polling_policy_.reset(); | |
| 61 } | |
| 62 | |
| 63 void GatewayDataProviderCommon::DoRouterScanTask() { | |
| 64 bool update_available = false; | |
| 65 GatewayData new_data; | |
| 66 if (!gateway_api_->GetRouterData(&new_data.router_data)) { | |
| 67 ScheduleNextScan(polling_policy_->NoRouterInterval()); | |
| 68 } else { | |
| 69 { | |
| 70 base::AutoLock lock(data_mutex_); | |
| 71 update_available = gateway_data_.DiffersSignificantly(new_data); | |
| 72 gateway_data_ = new_data; | |
| 73 } | |
| 74 ScheduleNextScan(polling_policy_->PollingInterval()); | |
| 75 } | |
| 76 if (update_available || !is_first_scan_complete_) { | |
| 77 is_first_scan_complete_ = true; | |
| 78 NotifyListeners(); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 void GatewayDataProviderCommon::ScheduleNextScan(int interval) { | |
| 83 message_loop()->PostDelayedTask( | |
| 84 FROM_HERE, task_factory_.NewRunnableMethod( | |
| 85 &GatewayDataProviderCommon::DoRouterScanTask), interval); | |
| 86 } | |
| 87 | |
| 88 GatewayPollingPolicyInterface* GatewayDataProviderCommon::NewPollingPolicy() { | |
| 89 return new GenericGatewayPollingPolicy; | |
| 90 } | |
| OLD | NEW |