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..1f45ea0d5cea71c02b4c016a9bef1d6abdbef420 |
| --- /dev/null |
| +++ b/chrome/browser/ui/google_now/google_now_service.cc |
| @@ -0,0 +1,117 @@ |
| +// 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" |
|
sky
2012/12/06 16:40:54
nit: newline between 5 and 6.
vadimt
2012/12/06 20:43:16
Done.
|
| + |
| +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. |
| +// 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 |
| +} // namespace |
| + |
| +struct GoogleNowService::ServerResponse { |
| + // TODO(vadimt): Populate this structure with real fields. |
| + int next_request_delay_ms; |
| +}; |
|
sky
2012/12/06 16:40:54
next_request_delay_ms -> TimeDelta.
vadimt
2012/12/06 20:43:16
Done.
|
| + |
| +GoogleNowService::GoogleNowService(Profile* profile) |
| + : profile_(profile) { |
| + DCHECK(profile_); |
| +} |
| + |
| +GoogleNowService::~GoogleNowService() { |
| +} |
| + |
| +void GoogleNowService::Init() { |
| + // If Google Now integration is enabled for the profile, start the first cards |
| + // update. |
| + if (IsGoogleNowEnabled()) |
| + UpdateCards(); |
| +} |
| + |
| +void GoogleNowService::StartWaitingForNextUpdate(int64 delay_ms) { |
|
sky
2012/12/06 16:40:54
Order of methods in .cc should match that of .h
vadimt
2012/12/06 20:43:16
Done.
|
| + 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 |
| + // enabled 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()); |
| +} |
| + |
| +void GoogleNowService::OnLocationObtained(const Geoposition& position) { |
| + DCHECK(IsGoogleNowEnabled()); |
| + DCHECK(!next_update_timer_.IsRunning()); |
| + |
| + StartServerRequest(position); |
| +} |
| + |
| +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. |
| + // TODO(vadimt): Consider exponential backoff with randomized jitter. |
| + StartWaitingForNextUpdate(kDefaultPollingPeriodMs); |
| + } |
| +} |
| + |
| +bool GoogleNowService::ParseServerResponse(const URLRequest* request, |
|
sky
2012/12/06 16:40:54
If you can't fit everything on one line, then one
vadimt
2012/12/06 20:43:16
Done.
|
| + int num_bytes, ServerResponse* server_response) { |
| + // TODO(vadimt): Do real parsing. |
| + server_response->next_request_delay_ms = kDefaultPollingPeriodMs; |
| + return true; |
| +} |
| + |
| +void GoogleNowService::ShowNotifications( |
| + const ServerResponse& server_response) { |
| + // TODO(vadimt): Implement using Chrome Notifications. |
| +} |