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..8e7ffc46cb2a20279665337a354633f59d8213ca |
| --- /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; |
| +} // 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 |
| + // 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); |
|
rgustafson
2012/12/03 19:28:59
Hopefully, we can do better than showing nothing e
vadimt
2012/12/03 20:48:56
That's a cool observation! Done.
|
| + } |
| +} |
| + |
| +void GoogleNowService::StartServerRequest( |
|
rgustafson
2012/12/03 19:28:59
If we support non-location specific cards when we
vadimt
2012/12/03 20:48:56
Correct; but I assume, we still need to try gettin
rgustafson
2012/12/04 00:38:01
Yes, but its nice to get as much as you can with t
|
| + 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. |
| + 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 = 300000; |
| + return true; |
| +} |
| + |
| +void GoogleNowService::ShowNotifications( |
| + const ServerResponse& server_response) { |
| + // TODO(vadimt): Implement via using Chrome Notifications. |
| +} |