| 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 #ifndef CHROME_BROWSER_UI_GOOGLE_NOW_GOOGLE_NOW_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_UI_GOOGLE_NOW_GOOGLE_NOW_SERVICE_H_ |
| 6 #define CHROME_BROWSER_UI_GOOGLE_NOW_GOOGLE_NOW_SERVICE_H_ | 6 #define CHROME_BROWSER_UI_GOOGLE_NOW_GOOGLE_NOW_SERVICE_H_ |
| 7 | 7 |
| 8 #include "base/timer.h" | 8 #include "base/timer.h" |
| 9 #include "chrome/browser/profiles/profile_keyed_service.h" | 9 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 10 #include "net/url_request/url_fetcher_delegate.h" |
| 10 | 11 |
| 11 class Profile; | 12 class Profile; |
| 12 | 13 |
| 13 namespace base { | 14 namespace base { |
| 14 class TimeDelta; | 15 class TimeDelta; |
| 15 } | 16 } |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 struct Geoposition; | 19 struct Geoposition; |
| 19 } | 20 } |
| 20 | 21 |
| 21 namespace net { | |
| 22 class URLRequest; | |
| 23 } | |
| 24 | |
| 25 // The Google Now service gets Google Now cards from the server and shows them | 22 // The Google Now service gets Google Now cards from the server and shows them |
| 26 // as Chrome notifications. | 23 // as Chrome notifications. |
| 27 // The service performs periodic updating of Google Now cards. | 24 // The service performs periodic updating of Google Now cards. |
| 28 // Each updating of the cards consists of 3 steps: | 25 // Each updating of the cards consists of 3 steps: |
| 29 // 1. Obtaining the location of the machine (asynchronous); | 26 // 1. Obtaining the location of the machine (asynchronous); |
| 30 // 2. Making a server request (asynchronous); | 27 // 2. Making a server request (asynchronous); |
| 31 // 3. Showing the cards as notifications. | 28 // 3. Showing the cards as notifications. |
| 32 class GoogleNowService : public ProfileKeyedService { | 29 class GoogleNowService : public ProfileKeyedService, |
| 30 private net::URLFetcherDelegate { |
| 33 public: | 31 public: |
| 34 // Must call Init after construction. | 32 // Must call Init after construction. |
| 35 explicit GoogleNowService(Profile* profile); | 33 explicit GoogleNowService(Profile* profile); |
| 36 virtual ~GoogleNowService(); | 34 virtual ~GoogleNowService(); |
| 37 void Init(); | 35 void Init(); |
| 38 | 36 |
| 39 private: | 37 private: |
| 40 // Parsed response from the server. | 38 // Parsed response from the server. |
| 41 struct ServerResponse; | 39 struct ServerResponse; |
| 42 | 40 |
| 43 // Returns true if Google Now integration is enabled for the profile. | 41 // Returns true if Google Now integration is enabled for the profile. |
| 44 bool IsGoogleNowEnabled() const; | 42 bool IsGoogleNowEnabled() const; |
| 45 // Gets new cards from the server and shows them as notifications. | 43 // Gets new cards from the server and shows them as notifications. |
| 46 void UpdateCards(); | 44 void UpdateCards(); |
| 47 | 45 |
| 48 // Schedules next cards update after the specified delay. | 46 // Schedules next cards update after the specified delay. |
| 49 void StartWaitingForNextUpdate(base::TimeDelta delay); | 47 void StartWaitingForNextUpdate(base::TimeDelta delay); |
| 50 void OnWaitingForNextUpdateEnds(); | 48 void OnWaitingForNextUpdateEnds(); |
| 51 | 49 |
| 52 // Starts obtaining location of the machine. | 50 // Starts obtaining location of the machine. |
| 53 void StartObtainingGeolocation(); | 51 void StartObtainingGeolocation(); |
| 54 void OnLocationObtained(const content::Geoposition& position); | 52 void OnLocationObtained(const content::Geoposition& position); |
| 55 void OnLocationRequestTimeout(); | 53 void OnLocationRequestTimeout(); |
| 56 | 54 |
| 55 // Builds a URL for a server request. |
| 56 static std::string BuildRequestURL(const content::Geoposition& position); |
| 57 // Starts downloading cards from the server. If the position's Validate() call | 57 // Starts downloading cards from the server. If the position's Validate() call |
| 58 // returns false, this means that the position is not available. | 58 // returns false, this means that the position is not available. |
| 59 void StartServerRequest(const content::Geoposition& position); | 59 void StartServerRequest(const content::Geoposition& position); |
| 60 void OnServerRequestCompleted(net::URLRequest* request, int num_bytes); | |
| 61 | 60 |
| 62 // Parses server response. Returns true if the parsing was successful. | 61 // Parses server response. Returns true if the parsing was successful. |
| 63 static bool ParseServerResponse(const net::URLRequest* request, | 62 static bool ParseServerResponse(const std::string& response_string, |
| 64 int num_bytes, | |
| 65 ServerResponse* server_response); | 63 ServerResponse* server_response); |
| 66 | 64 |
| 67 // Shows Google Now cards as notifications. | 65 // Shows Google Now cards as notifications. |
| 68 void ShowNotifications(const ServerResponse& server_response); | 66 void ShowNotifications(const ServerResponse& server_response); |
| 69 | 67 |
| 68 // URLFetcherDelegate |
| 69 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 70 |
| 70 // The profile. | 71 // The profile. |
| 71 Profile* const profile_; | 72 Profile* const profile_; |
| 72 // Timer to schedule next cards update. | 73 // Timer to schedule next cards update. |
| 73 base::OneShotTimer<GoogleNowService> next_update_timer_; | 74 base::OneShotTimer<GoogleNowService> next_update_timer_; |
| 74 // Timer to cancel geolocation requests that take too long. | 75 // Timer to cancel geolocation requests that take too long. |
| 75 base::OneShotTimer<GoogleNowService> geolocation_request_timer_; | 76 base::OneShotTimer<GoogleNowService> geolocation_request_timer_; |
| 76 // Weak factory for the geolocation request callback. Used to ensure | 77 // Weak factory for the geolocation request callback. Used to ensure |
| 77 // geolocation request callback is not run after this object is destroyed. | 78 // geolocation request callback is not run after this object is destroyed. |
| 78 base::WeakPtrFactory<GoogleNowService> geolocation_request_weak_factory_; | 79 base::WeakPtrFactory<GoogleNowService> geolocation_request_weak_factory_; |
| 80 // Fetcher for Google Now cards. |
| 81 scoped_ptr<net::URLFetcher> fetcher_; |
| 79 | 82 |
| 80 DISALLOW_COPY_AND_ASSIGN(GoogleNowService); | 83 DISALLOW_COPY_AND_ASSIGN(GoogleNowService); |
| 81 }; | 84 }; |
| 82 | 85 |
| 83 #endif // CHROME_BROWSER_UI_GOOGLE_NOW_GOOGLE_NOW_SERVICE_H_ | 86 #endif // CHROME_BROWSER_UI_GOOGLE_NOW_GOOGLE_NOW_SERVICE_H_ |
| OLD | NEW |