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