Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7ba54712b0d27d4fc5c4e9368414c9c6ef45b337 |
| --- /dev/null |
| +++ b/chrome/browser/ui/google_now/google_now_service.cc |
| @@ -0,0 +1,119 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/google_now/google_now_service.h" |
| +#include "content/public/common/geoposition.h" |
| + |
| +using base::TimeDelta; |
| +using content::Geoposition; |
| +using net::URLRequest; |
| + |
| +namespace { |
| +// Period for polling for Google Now cards to use when the period from the |
| +// server is not available. |
| +const int kDefaultPollingPeriodMs = 300000; |
|
skare_
2012/12/01 03:00:14
(optional) add comment
... = 300000; // 5 minutes
vadimt
2012/12/03 18:48:07
Done.
|
| +} // namespace |
| + |
| +struct GoogleNowService::ServerResponse { |
| + // TODO(vadimt): Populate this structure with real fields. |
| + int next_request_delay_ms; |
| +}; |
| + |
| +GoogleNowService::GoogleNowService(Profile* profile) |
| + : profile_(profile) { |
| + DCHECK(profile_); |
| +} |
| + |
| +void GoogleNowService::Init() { |
| + // If Google Now integration is enabled for the profile, start the first cards |
|
skare_
2012/12/01 03:00:14
nit: code sort of explains itself here but feel fr
vadimt
2012/12/03 18:48:07
Thanks! I'll keep the comment, more info doesn't h
|
| + // update. |
| + if (IsGoogleNowEnabled()) |
| + UpdateCards(); |
| +} |
| + |
| +void GoogleNowService::StartWaitingForNextUpdate(int64 delay_ms) { |
| + DCHECK(!next_update_timer_.IsRunning()); |
| + |
| + next_update_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms), |
| + this, &GoogleNowService::OnWaitingForNextUpdateEnds); |
| +} |
| + |
| +void GoogleNowService::OnWaitingForNextUpdateEnds() { |
| + DCHECK(IsGoogleNowEnabled()); |
| + DCHECK(!next_update_timer_.IsRunning()); |
| + |
| + UpdateCards(); |
| +} |
| + |
| +bool GoogleNowService::IsGoogleNowEnabled() const { |
| + // TODO(vadimt): Return a value indicating whether Google Now integration is |
| + // enable for 'profile_'. |
| + // TODO(vadimt): Process enabling and disabling Google Now integration while |
| + // the service is running. |
| + return true; |
| +} |
| + |
| +void GoogleNowService::UpdateCards() { |
| + // Start obtaining geolocation for the server's request. |
| + StartObtainingGeolocation(); |
| +} |
| + |
| +void GoogleNowService::StartObtainingGeolocation() { |
| + // TODO(vadimt): Implement via making a geolocation request. |
| + OnLocationObtained(Geoposition()); |
| +} |
| + |
| +bool GoogleNowService::IsLocationGood(const content::Geoposition& position) { |
| + // TODO(vadimt): Check the location's fields to see whether it's usable. |
| + return true; |
| +} |
| + |
| +void GoogleNowService::OnLocationObtained(const Geoposition& position) { |
| + DCHECK(IsGoogleNowEnabled()); |
| + DCHECK(!next_update_timer_.IsRunning()); |
| + |
| + if (IsLocationGood(position)) { |
| + StartServerRequest(position); |
| + } else { |
| + // If the location is bad, schedule next cards update. |
| + StartWaitingForNextUpdate(kDefaultPollingPeriodMs); |
| + } |
| +} |
| + |
| +void GoogleNowService::StartServerRequest( |
| + const content::Geoposition& position) { |
| + // TODO(vadimt): Implement via making URLRequest to the server. |
| + OnServerRequestCompleted(NULL, 0); |
| +} |
| + |
| +void GoogleNowService::OnServerRequestCompleted(URLRequest* request, |
| + int num_bytes) { |
| + DCHECK(IsGoogleNowEnabled()); |
| + DCHECK(!next_update_timer_.IsRunning()); |
| + |
| + ServerResponse server_response; |
| + // TODO(vadimt): Check request's status. |
| + if (ParseServerResponse(request, num_bytes, &server_response)) { |
| + ShowNotifications(server_response); |
| + // Once the cards are shown, schedule next cards update after the delay |
| + // suggested by the server. |
| + StartWaitingForNextUpdate(server_response.next_request_delay_ms); |
| + } else { |
| + // If the server response is bad, schedule next cards update after the |
| + // default delay. |
|
skare_
2012/12/01 03:00:14
You may want exponential backoff with randomized j
vadimt
2012/12/03 18:48:07
Done.
|
| + StartWaitingForNextUpdate(kDefaultPollingPeriodMs); |
| + } |
| +} |
| + |
| +bool GoogleNowService::ParseServerResponse(const URLRequest* request, |
| + int num_bytes, ServerResponse* server_response) { |
| + // TODO(vadimt): Do real parsing. |
| + server_response->next_request_delay_ms = 10000;// TODO 300000; |
|
skare_
2012/12/01 03:00:14
I know this will come from the response, but maybe
vadimt
2012/12/03 18:48:07
Done.
|
| + return true; |
| +} |
| + |
| +void GoogleNowService::ShowNotifications( |
| + const ServerResponse& server_response) { |
| + // TODO(vadimt): Implement via using Chrome Notifications. |
|
skare_
2012/12/01 03:00:14
nit: just one (either) of "via using"
vadimt
2012/12/03 18:48:07
Done.
|
| +} |