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