Chromium Code Reviews| 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" | |
|
sky
2012/12/06 16:40:54
nit: newline between 5 and 6.
vadimt
2012/12/06 20:43:16
Done.
| |
| 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 }; | |
|
sky
2012/12/06 16:40:54
next_request_delay_ms -> TimeDelta.
vadimt
2012/12/06 20:43:16
Done.
| |
| 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) { | |
|
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.
| |
| 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 // enabled 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 void GoogleNowService::OnLocationObtained(const Geoposition& position) { | |
| 75 DCHECK(IsGoogleNowEnabled()); | |
| 76 DCHECK(!next_update_timer_.IsRunning()); | |
| 77 | |
| 78 StartServerRequest(position); | |
| 79 } | |
| 80 | |
| 81 void GoogleNowService::StartServerRequest( | |
| 82 const content::Geoposition& position) { | |
| 83 // TODO(vadimt): Implement via making URLRequest to the server. | |
| 84 OnServerRequestCompleted(NULL, 0); | |
| 85 } | |
| 86 | |
| 87 void GoogleNowService::OnServerRequestCompleted(URLRequest* request, | |
| 88 int num_bytes) { | |
| 89 DCHECK(IsGoogleNowEnabled()); | |
| 90 DCHECK(!next_update_timer_.IsRunning()); | |
| 91 | |
| 92 ServerResponse server_response; | |
| 93 // TODO(vadimt): Check request's status. | |
| 94 if (ParseServerResponse(request, num_bytes, &server_response)) { | |
| 95 ShowNotifications(server_response); | |
| 96 // Once the cards are shown, schedule next cards update after the delay | |
| 97 // suggested by the server. | |
| 98 StartWaitingForNextUpdate(server_response.next_request_delay_ms); | |
| 99 } else { | |
| 100 // If the server response is bad, schedule next cards update after the | |
| 101 // default delay. | |
| 102 // TODO(vadimt): Consider exponential backoff with randomized jitter. | |
| 103 StartWaitingForNextUpdate(kDefaultPollingPeriodMs); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 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.
| |
| 108 int num_bytes, ServerResponse* server_response) { | |
| 109 // TODO(vadimt): Do real parsing. | |
| 110 server_response->next_request_delay_ms = kDefaultPollingPeriodMs; | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 void GoogleNowService::ShowNotifications( | |
| 115 const ServerResponse& server_response) { | |
| 116 // TODO(vadimt): Implement using Chrome Notifications. | |
| 117 } | |
| OLD | NEW |