Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(696)

Side by Side Diff: chrome/browser/ui/google_now/google_now_service.cc

Issue 11434116: Geolocation request for Google Now (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cosmetics. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/google_now/google_now_service.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "content/public/browser/browser_thread.h"
rgustafson 2012/12/06 20:10:34 Is this used?
vadimt 2012/12/06 21:05:18 Done.
7 #include "content/public/browser/geolocation.h"
6 #include "content/public/common/geoposition.h" 8 #include "content/public/common/geoposition.h"
7 9
10 using base::Bind;
8 using base::TimeDelta; 11 using base::TimeDelta;
9 using content::Geoposition; 12 using content::Geoposition;
10 using net::URLRequest; 13 using net::URLRequest;
11 14
12 namespace { 15 namespace {
16 // TODO(vadimt): Figure out the values of the constants.
17
13 // 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
14 // server is not available. 19 // server is not available.
15 // TODO(vadimt): Figure out the value.
16 // TODO(vadimt): Figure out the consequences for LBS. 20 // TODO(vadimt): Figure out the consequences for LBS.
17 // TODO(vadimt): Consider triggers other than the timer for refreshing the 21 // TODO(vadimt): Consider triggers other than the timer for refreshing the
18 // position, such as waking from sleep. 22 // position, such as waking from sleep.
19 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 min
20 } // namespace 26 } // namespace
21 27
22 struct GoogleNowService::ServerResponse { 28 struct GoogleNowService::ServerResponse {
23 // TODO(vadimt): Populate this structure with real fields. 29 // TODO(vadimt): Populate this structure with real fields.
24 int next_request_delay_ms; 30 int next_request_delay_ms;
25 }; 31 };
26 32
27 GoogleNowService::GoogleNowService(Profile* profile) 33 GoogleNowService::GoogleNowService(Profile* profile)
28 : profile_(profile) { 34 : profile_(profile),
35 ALLOW_THIS_IN_INITIALIZER_LIST(geolocation_request_weak_factory_(this)) {
29 DCHECK(profile_); 36 DCHECK(profile_);
30 } 37 }
31 38
32 GoogleNowService::~GoogleNowService() { 39 GoogleNowService::~GoogleNowService() {
33 } 40 }
34 41
35 void GoogleNowService::Init() { 42 void GoogleNowService::Init() {
36 // 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
37 // update. 44 // update.
38 if (IsGoogleNowEnabled()) 45 if (IsGoogleNowEnabled())
39 UpdateCards(); 46 UpdateCards();
40 } 47 }
41 48
42 void GoogleNowService::StartWaitingForNextUpdate(int64 delay_ms) { 49 void GoogleNowService::StartWaitingForNextUpdate(int64 delay_ms) {
43 DCHECK(!next_update_timer_.IsRunning()); 50 DCHECK(!next_update_timer_.IsRunning());
44 51
45 next_update_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms), 52 next_update_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms),
46 this, &GoogleNowService::OnWaitingForNextUpdateEnds); 53 this, &GoogleNowService::OnWaitingForNextUpdateEnds);
47 } 54 }
48 55
49 void GoogleNowService::OnWaitingForNextUpdateEnds() { 56 void GoogleNowService::OnWaitingForNextUpdateEnds() {
50 DCHECK(IsGoogleNowEnabled()); 57 DCHECK(IsGoogleNowEnabled());
51 DCHECK(!next_update_timer_.IsRunning()); 58 DCHECK(!next_update_timer_.IsRunning());
59 DCHECK(!geolocation_request_timer_.IsRunning());
60 DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs());
52 61
53 UpdateCards(); 62 UpdateCards();
54 } 63 }
55 64
56 bool GoogleNowService::IsGoogleNowEnabled() const { 65 bool GoogleNowService::IsGoogleNowEnabled() const {
57 // TODO(vadimt): Return a value indicating whether Google Now integration is 66 // TODO(vadimt): Return a value indicating whether Google Now integration is
58 // enabled for 'profile_'. 67 // enabled for 'profile_'.
59 // TODO(vadimt): Process enabling and disabling Google Now integration while 68 // TODO(vadimt): Process enabling and disabling Google Now integration while
60 // the service is running. 69 // the service is running.
61 return true; 70 return true;
62 } 71 }
63 72
64 void GoogleNowService::UpdateCards() { 73 void GoogleNowService::UpdateCards() {
65 // Start obtaining geolocation for the server's request. 74 // Start obtaining geolocation for the server's request.
66 StartObtainingGeolocation(); 75 StartObtainingGeolocation();
67 } 76 }
68 77
69 void GoogleNowService::StartObtainingGeolocation() { 78 void GoogleNowService::StartObtainingGeolocation() {
70 // TODO(vadimt): Implement via making a geolocation request. 79 DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs());
71 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);
72 } 88 }
73 89
74 void GoogleNowService::OnLocationObtained(const Geoposition& position) { 90 void GoogleNowService::OnLocationObtained(const Geoposition& position) {
75 DCHECK(IsGoogleNowEnabled()); 91 DCHECK(IsGoogleNowEnabled());
76 DCHECK(!next_update_timer_.IsRunning()); 92 DCHECK(!next_update_timer_.IsRunning());
93 DCHECK(geolocation_request_timer_.IsRunning());
94 DCHECK(geolocation_request_weak_factory_.HasWeakPtrs());
rgustafson 2012/12/06 20:10:34 If the location is obtained, where are the ptrs in
vadimt 2012/12/06 21:05:18 Another great catch! It's done by the framework af
77 95
96 geolocation_request_timer_.Stop();
78 StartServerRequest(position); 97 StartServerRequest(position);
79 } 98 }
80 99
100 void GoogleNowService::OnLocationRequestTimeout() {
101 DCHECK(IsGoogleNowEnabled());
102 DCHECK(!next_update_timer_.IsRunning());
103 DCHECK(!geolocation_request_timer_.IsRunning());
104 DCHECK(geolocation_request_weak_factory_.HasWeakPtrs());
105
106 geolocation_request_weak_factory_.InvalidateWeakPtrs();
107 StartServerRequest(Geoposition());
108 }
109
81 void GoogleNowService::StartServerRequest( 110 void GoogleNowService::StartServerRequest(
82 const content::Geoposition& position) { 111 const content::Geoposition& position) {
83 // TODO(vadimt): Implement via making URLRequest to the server. 112 // TODO(vadimt): Implement via making URLRequest to the server.
84 OnServerRequestCompleted(NULL, 0); 113 OnServerRequestCompleted(NULL, 0);
85 } 114 }
86 115
87 void GoogleNowService::OnServerRequestCompleted(URLRequest* request, 116 void GoogleNowService::OnServerRequestCompleted(URLRequest* request,
88 int num_bytes) { 117 int num_bytes) {
89 DCHECK(IsGoogleNowEnabled()); 118 DCHECK(IsGoogleNowEnabled());
90 DCHECK(!next_update_timer_.IsRunning()); 119 DCHECK(!next_update_timer_.IsRunning());
120 DCHECK(!geolocation_request_timer_.IsRunning());
121 // TODO(vadimt): Uncomment the check below once OnServerRequestCompleted is
122 // called asynchronously.
123 // DCHECK(!geolocation_request_weak_factory_.HasWeakPtrs());
91 124
92 ServerResponse server_response; 125 ServerResponse server_response;
93 // TODO(vadimt): Check request's status. 126 // TODO(vadimt): Check request's status.
94 if (ParseServerResponse(request, num_bytes, &server_response)) { 127 if (ParseServerResponse(request, num_bytes, &server_response)) {
95 ShowNotifications(server_response); 128 ShowNotifications(server_response);
96 // Once the cards are shown, schedule next cards update after the delay 129 // Once the cards are shown, schedule next cards update after the delay
97 // suggested by the server. 130 // suggested by the server.
98 StartWaitingForNextUpdate(server_response.next_request_delay_ms); 131 StartWaitingForNextUpdate(server_response.next_request_delay_ms);
99 } else { 132 } else {
100 // If the server response is bad, schedule next cards update after the 133 // If the server response is bad, schedule next cards update after the
101 // default delay. 134 // default delay.
102 // TODO(vadimt): Consider exponential backoff with randomized jitter. 135 // TODO(vadimt): Consider exponential backoff with randomized jitter.
103 StartWaitingForNextUpdate(kDefaultPollingPeriodMs); 136 StartWaitingForNextUpdate(kDefaultPollingPeriodMs);
104 } 137 }
105 } 138 }
106 139
107 bool GoogleNowService::ParseServerResponse(const URLRequest* request, 140 bool GoogleNowService::ParseServerResponse(const URLRequest* request,
108 int num_bytes, ServerResponse* server_response) { 141 int num_bytes, ServerResponse* server_response) {
109 // TODO(vadimt): Do real parsing. 142 // TODO(vadimt): Do real parsing.
110 server_response->next_request_delay_ms = kDefaultPollingPeriodMs; 143 server_response->next_request_delay_ms = kDefaultPollingPeriodMs;
111 return true; 144 return true;
112 } 145 }
113 146
114 void GoogleNowService::ShowNotifications( 147 void GoogleNowService::ShowNotifications(
115 const ServerResponse& server_response) { 148 const ServerResponse& server_response) {
116 // TODO(vadimt): Implement using Chrome Notifications. 149 // TODO(vadimt): Implement using Chrome Notifications.
117 } 150 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/google_now/google_now_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698