OLD | NEW |
| (Empty) |
1 // Copyright 2010 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 // Functions related to the sending the setup pings. The functionality provided | |
17 // by this class is used in by install, handoff, and update features. | |
18 // This is an overview of how the setup pings work. | |
19 // In the case of install, the execution flow includes the following steps: | |
20 // elevation if needed, setting up Omaha, and installing the applications | |
21 // specified in the tag. | |
22 // The code guarantees that an EVENT_INSTALL_COMPLETE(2) ping for Omaha is sent | |
23 // in all cases, except trivial errors that may happen before the execution flow | |
24 // reaches the Install function. | |
25 // A EVENT_INSTALL_COMPLETE(2) ping for the apps is also sent in all cases. | |
26 // | |
27 // Where the code fails affects how the pings are generated and sent, as | |
28 // following: | |
29 // * if the elevation was required but the elevated process failed to run, | |
30 // then both pings are sent from the medium integrity /install | |
31 // process. | |
32 // * if the Omaha setup code ran but it errored out or the handoff failed to | |
33 // launch, then both pings are sent from the /install process or the | |
34 // elevated /install process if elevation was successful. The pings will be | |
35 // split in two different http transactions in the case setup completed | |
36 // successfully but it failed to handoff. | |
37 // * if the /handoff process launched but an error occured in the handoff | |
38 // process itself, then the Omaha "2" ping is sent from the /install process | |
39 // and the apps "2" ping is sent from the /handoff process. | |
40 // The apps ping is only sent if the handoff code did not proceed far enough | |
41 // to create a bundle of applications. Beyond that point, the bundle takes | |
42 // over the responsibility of sending "2" pings for each app in the bundle. | |
43 // | |
44 // There is an IPC mechanism between /install and /handoff processes based | |
45 // on detected input idle to avoid overlapping error handling and | |
46 // displaying redundant error messages in different processes. Usually ping | |
47 // handling, error handling, and displaying error UI is done in the same layer. | |
48 // When an error happens in the chain of /install, elevated install, | |
49 // and /handoff processes, then UI is displayed by one of these processes only | |
50 // if the child process did not display UI. Since UI is displayed in the | |
51 // /handoff process in both the success and error cases, this information can't | |
52 // be useful to handle the pings, therefore pings only rely on a weaker | |
53 // guarantee, which is whether the child process has launched or not. | |
54 | |
55 // TODO(omaha): unify the install and bundle pings mechanisms. There is | |
56 // no facility to cancel the install pings in the current implementation. | |
57 | |
58 // TODO(omaha): use a pimpl to avoid the dependency on UpdateRequest. | |
59 | |
60 #ifndef OMAHA_COMMON_PING_H_ | |
61 #define OMAHA_COMMON_PING_H_ | |
62 | |
63 #include <windows.h> | |
64 #include <atlstr.h> | |
65 #include <utility> | |
66 #include <vector> | |
67 #include "base/basictypes.h" | |
68 #include "base/scoped_ptr.h" | |
69 #include "omaha/common/ping_event.h" | |
70 #include "omaha/common/update_request.h" | |
71 #include "omaha/common/web_services_client.h" | |
72 #include "third_party/gtest/include/gtest/gtest.h" | |
73 | |
74 namespace omaha { | |
75 | |
76 struct CommandLineExtraArgs; | |
77 class App; | |
78 | |
79 class Ping { | |
80 public: | |
81 Ping(bool is_machine, | |
82 const CString& session_id, | |
83 const CString& install_source); | |
84 ~Ping(); | |
85 | |
86 // TODO(omaha): Consider moving everything except the functionality that | |
87 // actually sends the pings out of the Ping class into builder classes. A | |
88 // dependency on the model App is not desirable here. | |
89 void BuildRequest(const App* app, bool is_update_check); | |
90 | |
91 // Loads app data from a location other than the Omaha state machine. | |
92 void LoadAppDataFromExtraArgs(const CommandLineExtraArgs& extra_args); | |
93 void LoadAppDataFromRegistry(const std::vector<CString>& apps); | |
94 void LoadOmahaDataFromRegistry(); | |
95 | |
96 // Builds pings for Omaha or apps loaded previously. | |
97 void BuildOmahaPing(const CString& version, | |
98 const CString& next_version, | |
99 const PingEventPtr& ping_event); | |
100 | |
101 void BuildOmahaPing(const CString& version, | |
102 const CString& next_version, | |
103 const PingEventPtr& ping_event1, | |
104 const PingEventPtr& ping_event2); | |
105 | |
106 void BuildAppsPing(const PingEventPtr& ping_event); | |
107 | |
108 // Serializes a ping request as a string. | |
109 HRESULT BuildRequestString(CString* request_string) const; | |
110 | |
111 // Sends the ping events. The pings could be sent out-of-process, | |
112 // using the installed Omaha or in-process, if the out-of-process delivery | |
113 // fails. | |
114 // | |
115 // Sending pings is attempted out-of-process first, with a timeout | |
116 // of 60 seconds, after which the in-process delivery kicks in. The pinging | |
117 // process pinging is terminated before the in-process pinging is attempted | |
118 // in order to avoid duplicate pings and prevent run away processes. | |
119 // | |
120 // The 'is_fire_and_forget' argument only applies to the out-of-process | |
121 // delivery mechanism. This allows the execution flow to return to the caller | |
122 // as soon as possible and it is useful for sending success pings. | |
123 // The in-process pinging is always blocking. | |
124 // | |
125 // If the caller is local system and a user is logged on, the function | |
126 // impersonatates that user. | |
127 // | |
128 // The function returns S_OK if the ping was successfully sent using either | |
129 // mechanism. | |
130 HRESULT Send(bool is_fire_and_forget); | |
131 | |
132 // Sends all persisted pings. Deletes successful or expired pings. | |
133 static HRESULT SendPersistedPings(bool is_machine); | |
134 | |
135 // Sends a ping string to the server, in-process. The ping_string must be web | |
136 // safe base64 encoded and it will be decoded before the ping is sent. | |
137 static HRESULT HandlePing(bool is_machine, const CString& ping_string); | |
138 | |
139 private: | |
140 FRIEND_TEST(PingTest, BuildOmahaPing); | |
141 FRIEND_TEST(PingTest, BuildOmahaPingWithSessionOverride); | |
142 FRIEND_TEST(PingTest, BuildAppsPing); | |
143 FRIEND_TEST(PingTest, BuildAppsPingFromRegistry); | |
144 FRIEND_TEST(PingTest, SendString); | |
145 FRIEND_TEST(PingTest, SendInProcess); | |
146 FRIEND_TEST(PingTest, IsPingExpired_PastTime); | |
147 FRIEND_TEST(PingTest, IsPingExpired_CurrentTime); | |
148 FRIEND_TEST(PingTest, IsPingExpired_FutureTime); | |
149 FRIEND_TEST(PingTest, LoadPersistedPings_NoPersistedPings); | |
150 FRIEND_TEST(PingTest, LoadPersistedPings); | |
151 FRIEND_TEST(PingTest, PersistPing); | |
152 FRIEND_TEST(PingTest, DeletePersistedPing); | |
153 FRIEND_TEST(PingTest, PersistPing_Load_Delete); | |
154 FRIEND_TEST(PingTest, SendPersistedPings); | |
155 FRIEND_TEST(PingTest, DISABLED_SendUsingGoogleUpdate); | |
156 | |
157 typedef std::vector<std::pair<time64, CString> > PingsVector; | |
158 static const TCHAR* const kRegKeyPing; | |
159 static const time64 kPingExpiry100ns; | |
160 | |
161 // Sends pings using the installed GoogleUpdate, which runs in the | |
162 // ping mode. the function waits for the pings to be sent if wait_timeout_ms | |
163 // is not zero. Returns S_OK if the pings have been successfully sent. | |
164 HRESULT SendUsingGoogleUpdate(const CString& request_string, | |
165 DWORD wait_timeout_ms) const; | |
166 | |
167 // Sends ping events in process. Returns S_OK if the pings have been | |
168 // sent to the server and the server response is 200 OK; | |
169 HRESULT SendInProcess(const CString& request_string) const; | |
170 | |
171 xml::request::App BuildOmahaApp(const CString& version, | |
172 const CString& next_version) const; | |
173 | |
174 // Persistent Ping utility functions. | |
175 static CString GetPingRegPath(bool is_machine); | |
176 static HRESULT LoadPersistedPings(bool is_machine, PingsVector* pings); | |
177 static bool IsPingExpired(time64 persisted_time); | |
178 static HRESULT DeletePersistedPing(bool is_machine, time64 persisted_time); | |
179 static HRESULT PersistPing(bool is_machine, const CString& ping_string); | |
180 | |
181 // Sends a string to the server. | |
182 static HRESULT SendString(bool is_machine, | |
183 const HeadersVector& headers, | |
184 const CString& request_string); | |
185 | |
186 bool is_machine_; | |
187 | |
188 // Information about apps. | |
189 struct AppData { | |
190 CString app_id; | |
191 CString language; | |
192 CString brand_code; | |
193 CString client_id; | |
194 CString installation_id; | |
195 CString pv; | |
196 CString experiment_labels; | |
197 }; | |
198 std::vector<AppData> apps_data_; | |
199 AppData omaha_data_; | |
200 | |
201 scoped_ptr<xml::UpdateRequest> ping_request_; | |
202 | |
203 DISALLOW_COPY_AND_ASSIGN(Ping); | |
204 }; | |
205 | |
206 } // namespace omaha | |
207 | |
208 #endif // OMAHA_COMMON_PING_H_ | |
209 | |
OLD | NEW |