Index: chrome/browser/ui/google_now/google_now_service.cc |
diff --git a/chrome/browser/ui/google_now/google_now_service.cc b/chrome/browser/ui/google_now/google_now_service.cc |
index 1f45ea0d5cea71c02b4c016a9bef1d6abdbef420..867e2a8ade96377a3681bedbb6139964f264fc69 100644 |
--- a/chrome/browser/ui/google_now/google_now_service.cc |
+++ b/chrome/browser/ui/google_now/google_now_service.cc |
@@ -3,20 +3,25 @@ |
// found in the LICENSE file. |
#include "chrome/browser/ui/google_now/google_now_service.h" |
+#include "content/public/browser/geolocation.h" |
#include "content/public/common/geoposition.h" |
+using base::Bind; |
using base::TimeDelta; |
using content::Geoposition; |
using net::URLRequest; |
namespace { |
+// TODO(vadimt): Figure out the values of the constants. |
+ |
// Period for polling for Google Now cards to use when the period from the |
// server is not available. |
-// TODO(vadimt): Figure out the value. |
// TODO(vadimt): Figure out the consequences for LBS. |
// TODO(vadimt): Consider triggers other than the timer for refreshing the |
// position, such as waking from sleep. |
const int kDefaultPollingPeriodMs = 300000; // 5 minutes |
+// Time allocated to a geolocation request. |
+const int kGeolocationRequestTimeoutMs = 60000; // 1 min |
skare_
2012/12/07 23:39:00
nit here and above: 2 spaces between code and comm
vadimt
2012/12/08 00:04:23
Done.
|
} // namespace |
struct GoogleNowService::ServerResponse { |
@@ -25,7 +30,8 @@ struct GoogleNowService::ServerResponse { |
}; |
GoogleNowService::GoogleNowService(Profile* profile) |
- : profile_(profile) { |
+ : profile_(profile), |
+ ALLOW_THIS_IN_INITIALIZER_LIST(geolocation_request_weak_factory_(this)) { |
DCHECK(profile_); |
} |
@@ -49,6 +55,8 @@ void GoogleNowService::StartWaitingForNextUpdate(int64 delay_ms) { |
void GoogleNowService::OnWaitingForNextUpdateEnds() { |
DCHECK(IsGoogleNowEnabled()); |
DCHECK(!next_update_timer_.IsRunning()); |
+ DCHECK(!geolocation_request_timer_.IsRunning()); |
+ DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs()); |
UpdateCards(); |
} |
@@ -67,17 +75,38 @@ void GoogleNowService::UpdateCards() { |
} |
void GoogleNowService::StartObtainingGeolocation() { |
- // TODO(vadimt): Implement via making a geolocation request. |
- OnLocationObtained(Geoposition()); |
+ DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs()); |
+ content::RequestLocationUpdate(Bind( |
+ &GoogleNowService::OnLocationObtained, |
+ geolocation_request_weak_factory_.GetWeakPtr())); |
+ |
+ DCHECK(!geolocation_request_timer_.IsRunning()); |
+ geolocation_request_timer_.Start(FROM_HERE, |
+ TimeDelta::FromMilliseconds(kGeolocationRequestTimeoutMs), |
+ this, &GoogleNowService::OnLocationRequestTimeout); |
} |
void GoogleNowService::OnLocationObtained(const Geoposition& position) { |
DCHECK(IsGoogleNowEnabled()); |
DCHECK(!next_update_timer_.IsRunning()); |
+ DCHECK(geolocation_request_timer_.IsRunning()); |
+ DCHECK(geolocation_request_weak_factory_.HasWeakPtrs()); |
+ geolocation_request_weak_factory_.InvalidateWeakPtrs(); |
+ geolocation_request_timer_.Stop(); |
StartServerRequest(position); |
} |
+void GoogleNowService::OnLocationRequestTimeout() { |
+ DCHECK(IsGoogleNowEnabled()); |
+ DCHECK(!next_update_timer_.IsRunning()); |
+ DCHECK(!geolocation_request_timer_.IsRunning()); |
+ DCHECK(geolocation_request_weak_factory_.HasWeakPtrs()); |
+ |
+ geolocation_request_weak_factory_.InvalidateWeakPtrs(); |
+ StartServerRequest(Geoposition()); |
+} |
+ |
void GoogleNowService::StartServerRequest( |
const content::Geoposition& position) { |
// TODO(vadimt): Implement via making URLRequest to the server. |
skare_
2012/12/07 23:39:00
should this be implemented in the same CL? or are
vadimt
2012/12/08 00:04:23
It's recommended to make reviews small in Chrome.
|
@@ -88,6 +117,10 @@ void GoogleNowService::OnServerRequestCompleted(URLRequest* request, |
int num_bytes) { |
DCHECK(IsGoogleNowEnabled()); |
DCHECK(!next_update_timer_.IsRunning()); |
+ DCHECK(!geolocation_request_timer_.IsRunning()); |
+ // TODO(vadimt): Uncomment the check below once OnServerRequestCompleted is |
+ // called asynchronously. |
+ // DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs()); |
ServerResponse server_response; |
// TODO(vadimt): Check request's status. |