OLD | NEW |
(Empty) | |
| 1 // Copyright 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 #ifndef IOS_CHROME_BROWSER_OMAHA_OMAHA_SERVICE_H_ |
| 6 #define IOS_CHROME_BROWSER_OMAHA_OMAHA_SERVICE_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/singleton.h" |
| 14 #include "base/scoped_observer.h" |
| 15 #include "base/timer/timer.h" |
| 16 #include "base/version.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" |
| 18 |
| 19 namespace base { |
| 20 class DictionaryValue; |
| 21 } |
| 22 |
| 23 namespace net { |
| 24 class URLFetcher; |
| 25 class URLRequestContextGetter; |
| 26 } |
| 27 |
| 28 struct UpgradeRecommendedDetails; |
| 29 |
| 30 // This service handles the communication with the Omaha server. It also |
| 31 // handles all the scheduling necessary to contact the server regularly. |
| 32 // All methods, but the constructor, |GetInstance| and |Start| methods, must be |
| 33 // called from the IO thread. |
| 34 class OmahaService : public net::URLFetcherDelegate { |
| 35 public: |
| 36 // Called when an upgrade is recommended. |
| 37 using UpgradeRecommendedCallback = |
| 38 base::Callback<void(const UpgradeRecommendedDetails&)>; |
| 39 |
| 40 // Starts the service. Also set the |URLRequestContextGetter| necessary to |
| 41 // access the Omaha server. This method should only be called once. |
| 42 static void Start(net::URLRequestContextGetter* request_context_getter, |
| 43 const UpgradeRecommendedCallback& callback); |
| 44 |
| 45 // Returns debug information about the omaha service. |
| 46 static void GetDebugInformation( |
| 47 const base::Callback<void(base::DictionaryValue*)> callback); |
| 48 |
| 49 private: |
| 50 // For tests: |
| 51 friend class OmahaServiceTest; |
| 52 friend class OmahaServiceInternalTest; |
| 53 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, PingMessageTest); |
| 54 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, |
| 55 PingMessageTestWithUnknownInstallDate); |
| 56 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, InstallEventMessageTest); |
| 57 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, SendPingFailure); |
| 58 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, SendPingSuccess); |
| 59 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, SendInstallEventSuccess); |
| 60 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, SendPingReceiveUpdate); |
| 61 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, PersistStatesTest); |
| 62 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, BackoffTest); |
| 63 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, NonSpammingTest); |
| 64 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, ActivePingAfterInstallEventTest); |
| 65 FRIEND_TEST_ALL_PREFIXES(OmahaServiceTest, InstallRetryTest); |
| 66 FRIEND_TEST_ALL_PREFIXES(OmahaServiceInternalTest, |
| 67 PingMessageTestWithProfileData); |
| 68 // For the singleton: |
| 69 friend struct base::DefaultSingletonTraits<OmahaService>; |
| 70 friend class base::Singleton<OmahaService>; |
| 71 |
| 72 // Enum for the |GetPingContent| and |GetNextPingRequestId| method. |
| 73 enum PingContent { |
| 74 INSTALL_EVENT, |
| 75 USAGE_PING, |
| 76 }; |
| 77 |
| 78 // Initialize the timer. Used on startup. |
| 79 void Initialize(); |
| 80 |
| 81 // net::URLFetcherDelegate |
| 82 void OnURLFetchComplete(const net::URLFetcher* fetcher) override; |
| 83 |
| 84 // Raw GetInstance method. Necessary for using singletons. |
| 85 static OmahaService* GetInstance(); |
| 86 |
| 87 // Private constructor, only used by the singleton. |
| 88 OmahaService(); |
| 89 // Private constructor, only used for tests. |
| 90 explicit OmahaService(bool schedule); |
| 91 ~OmahaService() override; |
| 92 |
| 93 // Returns the time to wait before next attempt. |
| 94 static base::TimeDelta GetBackOff(uint8_t number_of_tries); |
| 95 |
| 96 void set_upgrade_recommended_callback( |
| 97 const UpgradeRecommendedCallback& callback) { |
| 98 upgrade_recommended_callback_ = callback; |
| 99 } |
| 100 |
| 101 // Sends a ping to the Omaha server. |
| 102 void SendPing(); |
| 103 |
| 104 // Method that will either start sending a ping to the server, or schedule |
| 105 // itself to be called again when the next ping must be send. |
| 106 void SendOrScheduleNextPing(); |
| 107 |
| 108 // Persists the state of the service. |
| 109 void PersistStates(); |
| 110 |
| 111 // Returns the XML representation of the ping message to send to the Omaha |
| 112 // server. If |sendInstallEvent| is true, the message will contain an |
| 113 // installation complete event. |
| 114 std::string GetPingContent(const std::string& requestId, |
| 115 const std::string& sessionId, |
| 116 const std::string& versionName, |
| 117 const std::string& channelName, |
| 118 const base::Time& installationTime, |
| 119 PingContent pingContent); |
| 120 |
| 121 // Returns the xml representation of the ping message to send to the Omaha |
| 122 // server. Use the current state of the service to compute the right message. |
| 123 std::string GetCurrentPingContent(); |
| 124 |
| 125 // Computes debugging information and fill |result|. |
| 126 void GetDebugInformationOnIOThread( |
| 127 const base::Callback<void(base::DictionaryValue*)> callback); |
| 128 |
| 129 // Returns whether the next ping to send must a an install/update ping. If |
| 130 // |true|, the next ping must use |GetInstallRetryRequestId| as identifier |
| 131 // for the request and must include a X-RequestAge header. |
| 132 bool IsNextPingInstallRetry(); |
| 133 |
| 134 // Returns the request identifier to use for the next ping. If it is an |
| 135 // install/update retry, it will return the identifier used on the initial |
| 136 // request. If this is not the case, returns a random id. |
| 137 // |send_install_event| must be true if the next ping is a install/update |
| 138 // event, in that case, the identifier will be stored so that it can be |
| 139 // reused until the ping is successful. |
| 140 std::string GetNextPingRequestId(PingContent ping_content); |
| 141 |
| 142 // Stores the given request id to be reused on install/update retry. |
| 143 void SetInstallRetryRequestId(const std::string& request_id); |
| 144 |
| 145 // Clears the stored request id for a installation/update ping retry. Must be |
| 146 // called after a successfull installation/update ping. |
| 147 void ClearInstallRetryRequestId(); |
| 148 |
| 149 // Clears the all persistent state. Should only be used for testing. |
| 150 static void ClearPersistentStateForTests(); |
| 151 |
| 152 // To communicate with the Omaha server. |
| 153 std::unique_ptr<net::URLFetcher> fetcher_; |
| 154 net::URLRequestContextGetter* request_context_getter_; |
| 155 |
| 156 // The timer that call this object back when needed. |
| 157 base::OneShotTimer timer_; |
| 158 |
| 159 // Whether to schedule pings. This is only false for tests. |
| 160 const bool schedule_; |
| 161 |
| 162 // The install date of the application. This is fetched in |Initialize| on |
| 163 // the main thread and cached for use on the IO thread. |
| 164 int64_t application_install_date_; |
| 165 |
| 166 // The time at which the last ping was sent. |
| 167 base::Time last_sent_time_; |
| 168 |
| 169 // The time at which to send the next ping. |
| 170 base::Time next_tries_time_; |
| 171 |
| 172 // The timestamp of the ping to send. |
| 173 base::Time current_ping_time_; |
| 174 |
| 175 // Last version for which an installation ping has been sent. |
| 176 base::Version last_sent_version_; |
| 177 |
| 178 // The language in use at start up. |
| 179 std::string locale_lang_; |
| 180 |
| 181 // Number of tries of the last ping. |
| 182 uint8_t number_of_tries_; |
| 183 |
| 184 // Whether the ping currently being sent is an install (new or update) ping. |
| 185 bool sending_install_event_; |
| 186 |
| 187 // Called to notify that upgrade is recommended. |
| 188 UpgradeRecommendedCallback upgrade_recommended_callback_; |
| 189 |
| 190 DISALLOW_COPY_AND_ASSIGN(OmahaService); |
| 191 }; |
| 192 |
| 193 #endif // IOS_CHROME_BROWSER_OMAHA_OMAHA_SERVICE_H_ |
OLD | NEW |