Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/google_now/google_now_service.h" | 5 #include "chrome/browser/ui/google_now/google_now_service.h" |
| 6 #include "content/public/browser/geolocation.h" | |
| 6 #include "content/public/common/geoposition.h" | 7 #include "content/public/common/geoposition.h" |
| 7 | 8 |
| 9 using base::Bind; | |
| 8 using base::TimeDelta; | 10 using base::TimeDelta; |
| 9 using content::Geoposition; | 11 using content::Geoposition; |
| 10 using net::URLRequest; | 12 using net::URLRequest; |
| 11 | 13 |
| 12 namespace { | 14 namespace { |
| 15 // TODO(vadimt): Figure out the values of the constants. | |
| 16 | |
| 13 // Period for polling for Google Now cards to use when the period from the | 17 // Period for polling for Google Now cards to use when the period from the |
| 14 // server is not available. | 18 // server is not available. |
| 15 // TODO(vadimt): Figure out the value. | |
| 16 // TODO(vadimt): Figure out the consequences for LBS. | 19 // TODO(vadimt): Figure out the consequences for LBS. |
| 17 // TODO(vadimt): Consider triggers other than the timer for refreshing the | 20 // TODO(vadimt): Consider triggers other than the timer for refreshing the |
| 18 // position, such as waking from sleep. | 21 // position, such as waking from sleep. |
| 19 const int kDefaultPollingPeriodMs = 300000; // 5 minutes | 22 const int kDefaultPollingPeriodMs = 300000; // 5 minutes |
| 23 // Time allocated to a geolocation request. | |
| 24 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.
| |
| 20 } // namespace | 25 } // namespace |
| 21 | 26 |
| 22 struct GoogleNowService::ServerResponse { | 27 struct GoogleNowService::ServerResponse { |
| 23 // TODO(vadimt): Populate this structure with real fields. | 28 // TODO(vadimt): Populate this structure with real fields. |
| 24 int next_request_delay_ms; | 29 int next_request_delay_ms; |
| 25 }; | 30 }; |
| 26 | 31 |
| 27 GoogleNowService::GoogleNowService(Profile* profile) | 32 GoogleNowService::GoogleNowService(Profile* profile) |
| 28 : profile_(profile) { | 33 : profile_(profile), |
| 34 ALLOW_THIS_IN_INITIALIZER_LIST(geolocation_request_weak_factory_(this)) { | |
| 29 DCHECK(profile_); | 35 DCHECK(profile_); |
| 30 } | 36 } |
| 31 | 37 |
| 32 GoogleNowService::~GoogleNowService() { | 38 GoogleNowService::~GoogleNowService() { |
| 33 } | 39 } |
| 34 | 40 |
| 35 void GoogleNowService::Init() { | 41 void GoogleNowService::Init() { |
| 36 // If Google Now integration is enabled for the profile, start the first cards | 42 // If Google Now integration is enabled for the profile, start the first cards |
| 37 // update. | 43 // update. |
| 38 if (IsGoogleNowEnabled()) | 44 if (IsGoogleNowEnabled()) |
| 39 UpdateCards(); | 45 UpdateCards(); |
| 40 } | 46 } |
| 41 | 47 |
| 42 void GoogleNowService::StartWaitingForNextUpdate(int64 delay_ms) { | 48 void GoogleNowService::StartWaitingForNextUpdate(int64 delay_ms) { |
| 43 DCHECK(!next_update_timer_.IsRunning()); | 49 DCHECK(!next_update_timer_.IsRunning()); |
| 44 | 50 |
| 45 next_update_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms), | 51 next_update_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms), |
| 46 this, &GoogleNowService::OnWaitingForNextUpdateEnds); | 52 this, &GoogleNowService::OnWaitingForNextUpdateEnds); |
| 47 } | 53 } |
| 48 | 54 |
| 49 void GoogleNowService::OnWaitingForNextUpdateEnds() { | 55 void GoogleNowService::OnWaitingForNextUpdateEnds() { |
| 50 DCHECK(IsGoogleNowEnabled()); | 56 DCHECK(IsGoogleNowEnabled()); |
| 51 DCHECK(!next_update_timer_.IsRunning()); | 57 DCHECK(!next_update_timer_.IsRunning()); |
| 58 DCHECK(!geolocation_request_timer_.IsRunning()); | |
| 59 DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs()); | |
| 52 | 60 |
| 53 UpdateCards(); | 61 UpdateCards(); |
| 54 } | 62 } |
| 55 | 63 |
| 56 bool GoogleNowService::IsGoogleNowEnabled() const { | 64 bool GoogleNowService::IsGoogleNowEnabled() const { |
| 57 // TODO(vadimt): Return a value indicating whether Google Now integration is | 65 // TODO(vadimt): Return a value indicating whether Google Now integration is |
| 58 // enabled for 'profile_'. | 66 // enabled for 'profile_'. |
| 59 // TODO(vadimt): Process enabling and disabling Google Now integration while | 67 // TODO(vadimt): Process enabling and disabling Google Now integration while |
| 60 // the service is running. | 68 // the service is running. |
| 61 return true; | 69 return true; |
| 62 } | 70 } |
| 63 | 71 |
| 64 void GoogleNowService::UpdateCards() { | 72 void GoogleNowService::UpdateCards() { |
| 65 // Start obtaining geolocation for the server's request. | 73 // Start obtaining geolocation for the server's request. |
| 66 StartObtainingGeolocation(); | 74 StartObtainingGeolocation(); |
| 67 } | 75 } |
| 68 | 76 |
| 69 void GoogleNowService::StartObtainingGeolocation() { | 77 void GoogleNowService::StartObtainingGeolocation() { |
| 70 // TODO(vadimt): Implement via making a geolocation request. | 78 DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs()); |
| 71 OnLocationObtained(Geoposition()); | 79 content::RequestLocationUpdate(Bind( |
| 80 &GoogleNowService::OnLocationObtained, | |
| 81 geolocation_request_weak_factory_.GetWeakPtr())); | |
| 82 | |
| 83 DCHECK(!geolocation_request_timer_.IsRunning()); | |
| 84 geolocation_request_timer_.Start(FROM_HERE, | |
| 85 TimeDelta::FromMilliseconds(kGeolocationRequestTimeoutMs), | |
| 86 this, &GoogleNowService::OnLocationRequestTimeout); | |
| 72 } | 87 } |
| 73 | 88 |
| 74 void GoogleNowService::OnLocationObtained(const Geoposition& position) { | 89 void GoogleNowService::OnLocationObtained(const Geoposition& position) { |
| 75 DCHECK(IsGoogleNowEnabled()); | 90 DCHECK(IsGoogleNowEnabled()); |
| 76 DCHECK(!next_update_timer_.IsRunning()); | 91 DCHECK(!next_update_timer_.IsRunning()); |
| 92 DCHECK(geolocation_request_timer_.IsRunning()); | |
| 93 DCHECK(geolocation_request_weak_factory_.HasWeakPtrs()); | |
| 77 | 94 |
| 95 geolocation_request_weak_factory_.InvalidateWeakPtrs(); | |
| 96 geolocation_request_timer_.Stop(); | |
| 78 StartServerRequest(position); | 97 StartServerRequest(position); |
| 79 } | 98 } |
| 80 | 99 |
| 100 void GoogleNowService::OnLocationRequestTimeout() { | |
| 101 DCHECK(IsGoogleNowEnabled()); | |
| 102 DCHECK(!next_update_timer_.IsRunning()); | |
| 103 DCHECK(!geolocation_request_timer_.IsRunning()); | |
| 104 DCHECK(geolocation_request_weak_factory_.HasWeakPtrs()); | |
| 105 | |
| 106 geolocation_request_weak_factory_.InvalidateWeakPtrs(); | |
| 107 StartServerRequest(Geoposition()); | |
| 108 } | |
| 109 | |
| 81 void GoogleNowService::StartServerRequest( | 110 void GoogleNowService::StartServerRequest( |
| 82 const content::Geoposition& position) { | 111 const content::Geoposition& position) { |
| 83 // TODO(vadimt): Implement via making URLRequest to the server. | 112 // 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.
| |
| 84 OnServerRequestCompleted(NULL, 0); | 113 OnServerRequestCompleted(NULL, 0); |
| 85 } | 114 } |
| 86 | 115 |
| 87 void GoogleNowService::OnServerRequestCompleted(URLRequest* request, | 116 void GoogleNowService::OnServerRequestCompleted(URLRequest* request, |
| 88 int num_bytes) { | 117 int num_bytes) { |
| 89 DCHECK(IsGoogleNowEnabled()); | 118 DCHECK(IsGoogleNowEnabled()); |
| 90 DCHECK(!next_update_timer_.IsRunning()); | 119 DCHECK(!next_update_timer_.IsRunning()); |
| 120 DCHECK(!geolocation_request_timer_.IsRunning()); | |
| 121 // TODO(vadimt): Uncomment the check below once OnServerRequestCompleted is | |
| 122 // called asynchronously. | |
| 123 // DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs()); | |
| 91 | 124 |
| 92 ServerResponse server_response; | 125 ServerResponse server_response; |
| 93 // TODO(vadimt): Check request's status. | 126 // TODO(vadimt): Check request's status. |
| 94 if (ParseServerResponse(request, num_bytes, &server_response)) { | 127 if (ParseServerResponse(request, num_bytes, &server_response)) { |
| 95 ShowNotifications(server_response); | 128 ShowNotifications(server_response); |
| 96 // Once the cards are shown, schedule next cards update after the delay | 129 // Once the cards are shown, schedule next cards update after the delay |
| 97 // suggested by the server. | 130 // suggested by the server. |
| 98 StartWaitingForNextUpdate(server_response.next_request_delay_ms); | 131 StartWaitingForNextUpdate(server_response.next_request_delay_ms); |
| 99 } else { | 132 } else { |
| 100 // If the server response is bad, schedule next cards update after the | 133 // If the server response is bad, schedule next cards update after the |
| 101 // default delay. | 134 // default delay. |
| 102 // TODO(vadimt): Consider exponential backoff with randomized jitter. | 135 // TODO(vadimt): Consider exponential backoff with randomized jitter. |
| 103 StartWaitingForNextUpdate(kDefaultPollingPeriodMs); | 136 StartWaitingForNextUpdate(kDefaultPollingPeriodMs); |
| 104 } | 137 } |
| 105 } | 138 } |
| 106 | 139 |
| 107 bool GoogleNowService::ParseServerResponse(const URLRequest* request, | 140 bool GoogleNowService::ParseServerResponse(const URLRequest* request, |
| 108 int num_bytes, ServerResponse* server_response) { | 141 int num_bytes, ServerResponse* server_response) { |
| 109 // TODO(vadimt): Do real parsing. | 142 // TODO(vadimt): Do real parsing. |
| 110 server_response->next_request_delay_ms = kDefaultPollingPeriodMs; | 143 server_response->next_request_delay_ms = kDefaultPollingPeriodMs; |
| 111 return true; | 144 return true; |
| 112 } | 145 } |
| 113 | 146 |
| 114 void GoogleNowService::ShowNotifications( | 147 void GoogleNowService::ShowNotifications( |
| 115 const ServerResponse& server_response) { | 148 const ServerResponse& server_response) { |
| 116 // TODO(vadimt): Implement using Chrome Notifications. | 149 // TODO(vadimt): Implement using Chrome Notifications. |
| 117 } | 150 } |
| OLD | NEW |