| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/google_now/google_now_service.h" | 5 #include "chrome/browser/ui/google_now/google_now_service.h" |
| 6 | 6 |
| 7 #include "content/public/browser/geolocation.h" |
| 7 #include "content/public/common/geoposition.h" | 8 #include "content/public/common/geoposition.h" |
| 8 | 9 |
| 10 using base::Bind; |
| 9 using base::TimeDelta; | 11 using base::TimeDelta; |
| 10 using content::Geoposition; | 12 using content::Geoposition; |
| 11 using net::URLRequest; | 13 using net::URLRequest; |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 16 // TODO(vadimt): Figure out the values of the constants. |
| 17 |
| 14 // Period for polling for Google Now cards to use when the period from the | 18 // Period for polling for Google Now cards to use when the period from the |
| 15 // server is not available. | 19 // server is not available. |
| 16 // TODO(vadimt): Figure out the value. | |
| 17 // TODO(vadimt): Figure out the consequences for LBS. | 20 // TODO(vadimt): Figure out the consequences for LBS. |
| 18 // TODO(vadimt): Consider triggers other than the timer for refreshing the | 21 // TODO(vadimt): Consider triggers other than the timer for refreshing the |
| 19 // position, such as waking from sleep. | 22 // position, such as waking from sleep. |
| 20 const int kDefaultPollingPeriodMs = 300000; // 5 minutes | 23 const int kDefaultPollingPeriodMs = 300000; // 5 minutes |
| 24 // Time allocated to a geolocation request. |
| 25 const int kGeolocationRequestTimeoutMs = 60000; // 1 minute |
| 21 } // namespace | 26 } // namespace |
| 22 | 27 |
| 23 struct GoogleNowService::ServerResponse { | 28 struct GoogleNowService::ServerResponse { |
| 24 // TODO(vadimt): Populate this structure with real fields. | 29 // TODO(vadimt): Populate this structure with real fields. |
| 25 TimeDelta next_request_delay; | 30 TimeDelta next_request_delay; |
| 26 }; | 31 }; |
| 27 | 32 |
| 28 GoogleNowService::GoogleNowService(Profile* profile) | 33 GoogleNowService::GoogleNowService(Profile* profile) |
| 29 : profile_(profile) { | 34 : profile_(profile), |
| 35 ALLOW_THIS_IN_INITIALIZER_LIST(geolocation_request_weak_factory_(this)) { |
| 30 DCHECK(profile_); | 36 DCHECK(profile_); |
| 31 } | 37 } |
| 32 | 38 |
| 33 GoogleNowService::~GoogleNowService() { | 39 GoogleNowService::~GoogleNowService() { |
| 34 } | 40 } |
| 35 | 41 |
| 36 void GoogleNowService::Init() { | 42 void GoogleNowService::Init() { |
| 37 // If Google Now integration is enabled for the profile, start the first cards | 43 // If Google Now integration is enabled for the profile, start the first cards |
| 38 // update. | 44 // update. |
| 39 if (IsGoogleNowEnabled()) | 45 if (IsGoogleNowEnabled()) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 56 void GoogleNowService::StartWaitingForNextUpdate(TimeDelta delay) { | 62 void GoogleNowService::StartWaitingForNextUpdate(TimeDelta delay) { |
| 57 DCHECK(!next_update_timer_.IsRunning()); | 63 DCHECK(!next_update_timer_.IsRunning()); |
| 58 | 64 |
| 59 next_update_timer_.Start(FROM_HERE, delay, | 65 next_update_timer_.Start(FROM_HERE, delay, |
| 60 this, &GoogleNowService::OnWaitingForNextUpdateEnds); | 66 this, &GoogleNowService::OnWaitingForNextUpdateEnds); |
| 61 } | 67 } |
| 62 | 68 |
| 63 void GoogleNowService::OnWaitingForNextUpdateEnds() { | 69 void GoogleNowService::OnWaitingForNextUpdateEnds() { |
| 64 DCHECK(IsGoogleNowEnabled()); | 70 DCHECK(IsGoogleNowEnabled()); |
| 65 DCHECK(!next_update_timer_.IsRunning()); | 71 DCHECK(!next_update_timer_.IsRunning()); |
| 72 DCHECK(!geolocation_request_timer_.IsRunning()); |
| 73 DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs()); |
| 66 | 74 |
| 67 UpdateCards(); | 75 UpdateCards(); |
| 68 } | 76 } |
| 69 | 77 |
| 70 void GoogleNowService::StartObtainingGeolocation() { | 78 void GoogleNowService::StartObtainingGeolocation() { |
| 71 // TODO(vadimt): Implement via making a geolocation request. | 79 DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs()); |
| 72 OnLocationObtained(Geoposition()); | 80 content::RequestLocationUpdate(Bind( |
| 81 &GoogleNowService::OnLocationObtained, |
| 82 geolocation_request_weak_factory_.GetWeakPtr())); |
| 83 |
| 84 DCHECK(!geolocation_request_timer_.IsRunning()); |
| 85 geolocation_request_timer_.Start(FROM_HERE, |
| 86 TimeDelta::FromMilliseconds(kGeolocationRequestTimeoutMs), |
| 87 this, &GoogleNowService::OnLocationRequestTimeout); |
| 73 } | 88 } |
| 74 | 89 |
| 75 void GoogleNowService::OnLocationObtained(const Geoposition& position) { | 90 void GoogleNowService::OnLocationObtained(const Geoposition& position) { |
| 76 DCHECK(IsGoogleNowEnabled()); | 91 DCHECK(IsGoogleNowEnabled()); |
| 77 DCHECK(!next_update_timer_.IsRunning()); | 92 DCHECK(!next_update_timer_.IsRunning()); |
| 93 DCHECK(geolocation_request_timer_.IsRunning()); |
| 94 DCHECK(geolocation_request_weak_factory_.HasWeakPtrs()); |
| 78 | 95 |
| 96 geolocation_request_weak_factory_.InvalidateWeakPtrs(); |
| 97 geolocation_request_timer_.Stop(); |
| 79 StartServerRequest(position); | 98 StartServerRequest(position); |
| 80 } | 99 } |
| 81 | 100 |
| 101 void GoogleNowService::OnLocationRequestTimeout() { |
| 102 DCHECK(IsGoogleNowEnabled()); |
| 103 DCHECK(!next_update_timer_.IsRunning()); |
| 104 DCHECK(!geolocation_request_timer_.IsRunning()); |
| 105 DCHECK(geolocation_request_weak_factory_.HasWeakPtrs()); |
| 106 |
| 107 geolocation_request_weak_factory_.InvalidateWeakPtrs(); |
| 108 StartServerRequest(Geoposition()); |
| 109 } |
| 110 |
| 82 void GoogleNowService::StartServerRequest( | 111 void GoogleNowService::StartServerRequest( |
| 83 const content::Geoposition& position) { | 112 const content::Geoposition& position) { |
| 84 // TODO(vadimt): Implement via making URLRequest to the server. | 113 // TODO(vadimt): Implement via making URLRequest to the server. |
| 85 OnServerRequestCompleted(NULL, 0); | 114 OnServerRequestCompleted(NULL, 0); |
| 86 } | 115 } |
| 87 | 116 |
| 88 void GoogleNowService::OnServerRequestCompleted(URLRequest* request, | 117 void GoogleNowService::OnServerRequestCompleted(URLRequest* request, |
| 89 int num_bytes) { | 118 int num_bytes) { |
| 90 DCHECK(IsGoogleNowEnabled()); | 119 DCHECK(IsGoogleNowEnabled()); |
| 91 DCHECK(!next_update_timer_.IsRunning()); | 120 DCHECK(!next_update_timer_.IsRunning()); |
| 121 DCHECK(!geolocation_request_timer_.IsRunning()); |
| 122 // TODO(vadimt): Uncomment the check below once OnServerRequestCompleted is |
| 123 // called asynchronously. |
| 124 // DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs()); |
| 92 | 125 |
| 93 ServerResponse server_response; | 126 ServerResponse server_response; |
| 94 // TODO(vadimt): Check request's status. | 127 // TODO(vadimt): Check request's status. |
| 95 if (ParseServerResponse(request, num_bytes, &server_response)) { | 128 if (ParseServerResponse(request, num_bytes, &server_response)) { |
| 96 ShowNotifications(server_response); | 129 ShowNotifications(server_response); |
| 97 // Once the cards are shown, schedule next cards update after the delay | 130 // Once the cards are shown, schedule next cards update after the delay |
| 98 // suggested by the server. | 131 // suggested by the server. |
| 99 StartWaitingForNextUpdate(server_response.next_request_delay); | 132 StartWaitingForNextUpdate(server_response.next_request_delay); |
| 100 } else { | 133 } else { |
| 101 // If the server response is bad, schedule next cards update after the | 134 // If the server response is bad, schedule next cards update after the |
| (...skipping 10 matching lines...) Expand all Loading... |
| 112 // TODO(vadimt): Do real parsing. | 145 // TODO(vadimt): Do real parsing. |
| 113 server_response->next_request_delay = | 146 server_response->next_request_delay = |
| 114 TimeDelta::FromMilliseconds(kDefaultPollingPeriodMs); | 147 TimeDelta::FromMilliseconds(kDefaultPollingPeriodMs); |
| 115 return true; | 148 return true; |
| 116 } | 149 } |
| 117 | 150 |
| 118 void GoogleNowService::ShowNotifications( | 151 void GoogleNowService::ShowNotifications( |
| 119 const ServerResponse& server_response) { | 152 const ServerResponse& server_response) { |
| 120 // TODO(vadimt): Implement using Chrome Notifications. | 153 // TODO(vadimt): Implement using Chrome Notifications. |
| 121 } | 154 } |
| OLD | NEW |