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 const int kDefaultPollingPeriodMs = 300000; | |
16 } // namespace | |
17 | |
18 struct GoogleNowService::ServerResponse { | |
19 // TODO(vadimt): Populate this structure with real fields. | |
20 int next_request_delay_ms; | |
21 }; | |
22 | |
23 GoogleNowService::GoogleNowService(Profile* profile) | |
24 : profile_(profile) { | |
25 DCHECK(profile_); | |
26 } | |
27 | |
28 void GoogleNowService::Init() { | |
29 // If Google Now integration is enabled for the profile, start the first cards | |
30 // update. | |
31 if (IsGoogleNowEnabled()) | |
32 UpdateCards(); | |
33 } | |
34 | |
35 void GoogleNowService::StartWaitingForNextUpdate(int64 delay_ms) { | |
36 DCHECK(!next_update_timer_.IsRunning()); | |
37 | |
38 next_update_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms), | |
39 this, &GoogleNowService::OnWaitingForNextUpdateEnds); | |
40 } | |
41 | |
42 void GoogleNowService::OnWaitingForNextUpdateEnds() { | |
43 DCHECK(IsGoogleNowEnabled()); | |
44 DCHECK(!next_update_timer_.IsRunning()); | |
45 | |
46 UpdateCards(); | |
47 } | |
48 | |
49 bool GoogleNowService::IsGoogleNowEnabled() const { | |
50 // TODO(vadimt): Return a value indicating whether Google Now integration is | |
51 // enable for 'profile_'. | |
52 // TODO(vadimt): Process enabling and disabling Google Now integration while | |
53 // the service is running. | |
54 return true; | |
55 } | |
56 | |
57 void GoogleNowService::UpdateCards() { | |
58 // Start obtaining geolocation for the server's request. | |
59 StartObtainingGeolocation(); | |
60 } | |
61 | |
62 void GoogleNowService::StartObtainingGeolocation() { | |
63 // TODO(vadimt): Implement via making a geolocation request. | |
64 OnLocationObtained(Geoposition()); | |
65 } | |
66 | |
67 bool GoogleNowService::IsLocationGood(const content::Geoposition& position) { | |
68 // TODO(vadimt): Check the location's fields to see whether it's usable. | |
69 return true; | |
70 } | |
71 | |
72 void GoogleNowService::OnLocationObtained(const Geoposition& position) { | |
73 DCHECK(IsGoogleNowEnabled()); | |
74 DCHECK(!next_update_timer_.IsRunning()); | |
75 | |
76 if (IsLocationGood(position)) { | |
77 StartServerRequest(position); | |
78 } else { | |
79 // If the location is bad, schedule next cards update. | |
80 StartWaitingForNextUpdate(kDefaultPollingPeriodMs); | |
rgustafson
2012/12/03 19:28:59
Hopefully, we can do better than showing nothing e
vadimt
2012/12/03 20:48:56
That's a cool observation! Done.
| |
81 } | |
82 } | |
83 | |
84 void GoogleNowService::StartServerRequest( | |
rgustafson
2012/12/03 19:28:59
If we support non-location specific cards when we
vadimt
2012/12/03 20:48:56
Correct; but I assume, we still need to try gettin
rgustafson
2012/12/04 00:38:01
Yes, but its nice to get as much as you can with t
| |
85 const content::Geoposition& position) { | |
86 // TODO(vadimt): Implement via making URLRequest to the server. | |
87 OnServerRequestCompleted(NULL, 0); | |
88 } | |
89 | |
90 void GoogleNowService::OnServerRequestCompleted(URLRequest* request, | |
91 int num_bytes) { | |
92 DCHECK(IsGoogleNowEnabled()); | |
93 DCHECK(!next_update_timer_.IsRunning()); | |
94 | |
95 ServerResponse server_response; | |
96 // TODO(vadimt): Check request's status. | |
97 if (ParseServerResponse(request, num_bytes, &server_response)) { | |
98 ShowNotifications(server_response); | |
99 // Once the cards are shown, schedule next cards update after the delay | |
100 // suggested by the server. | |
101 StartWaitingForNextUpdate(server_response.next_request_delay_ms); | |
102 } else { | |
103 // If the server response is bad, schedule next cards update after the | |
104 // default delay. | |
105 StartWaitingForNextUpdate(kDefaultPollingPeriodMs); | |
106 } | |
107 } | |
108 | |
109 bool GoogleNowService::ParseServerResponse(const URLRequest* request, | |
110 int num_bytes, ServerResponse* server_response) { | |
111 // TODO(vadimt): Do real parsing. | |
112 server_response->next_request_delay_ms = 300000; | |
113 return true; | |
114 } | |
115 | |
116 void GoogleNowService::ShowNotifications( | |
117 const ServerResponse& server_response) { | |
118 // TODO(vadimt): Implement via using Chrome Notifications. | |
119 } | |
OLD | NEW |