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#ifndef CHROME_BROWSER_GOOGLE_GOOGLE_UPDATE_WIN_H_
6#define CHROME_BROWSER_GOOGLE_GOOGLE_UPDATE_WIN_H_
7
8#include "base/basictypes.h"
9#include "base/memory/ref_counted.h"
10#include "base/strings/string16.h"
11#include "google_update/google_update_idl.h"
12
13namespace base {
14class MessageLoop;
15}
16
17namespace views {
18class Widget;
19}
20
21// The status of the upgrade. UPGRADE_STARTED and UPGRADE_CHECK_STARTED are
22// internal states and will not be reported as results to the listener.
23// These values are used for a histogram. Do not reorder.
24enum GoogleUpdateUpgradeResult {
25  // The upgrade has started.
26  UPGRADE_STARTED = 0,
27  // A check for upgrade has been initiated.
28  UPGRADE_CHECK_STARTED = 1,
29  // An update is available.
30  UPGRADE_IS_AVAILABLE = 2,
31  // The upgrade happened successfully.
32  UPGRADE_SUCCESSFUL = 3,
33  // No need to upgrade, Chrome is up to date.
34  UPGRADE_ALREADY_UP_TO_DATE = 4,
35  // An error occurred.
36  UPGRADE_ERROR = 5,
37  NUM_UPGRADE_RESULTS
38};
39
40// These values are used for a histogram. Do not reorder.
41enum GoogleUpdateErrorCode {
42  // The upgrade completed successfully (or hasn't been started yet).
43  GOOGLE_UPDATE_NO_ERROR = 0,
44  // Google Update only supports upgrading if Chrome is installed in the default
45  // location. This error will appear for developer builds and with
46  // installations unzipped to random locations.
47  CANNOT_UPGRADE_CHROME_IN_THIS_DIRECTORY = 1,
48  // Failed to create Google Update JobServer COM class.
49  GOOGLE_UPDATE_JOB_SERVER_CREATION_FAILED = 2,
50  // Failed to create Google Update OnDemand COM class.
51  GOOGLE_UPDATE_ONDEMAND_CLASS_NOT_FOUND = 3,
52  // Google Update OnDemand COM class reported an error during a check for
53  // update (or while upgrading).
54  GOOGLE_UPDATE_ONDEMAND_CLASS_REPORTED_ERROR = 4,
55  // A call to GetResults failed.
56  GOOGLE_UPDATE_GET_RESULT_CALL_FAILED = 5,
57  // A call to GetVersionInfo failed.
58  GOOGLE_UPDATE_GET_VERSION_INFO_FAILED = 6,
59  // An error occurred while upgrading (or while checking for update).
60  // Check the Google Update log in %TEMP% for more details.
61  GOOGLE_UPDATE_ERROR_UPDATING = 7,
62  // Updates can not be downloaded because the administrator has disabled all
63  // types of updating.
64  GOOGLE_UPDATE_DISABLED_BY_POLICY = 8,
65  // Updates can not be downloaded because the administrator has disabled
66  // manual (on-demand) updates.  Automatic background updates are allowed.
67  GOOGLE_UPDATE_DISABLED_BY_POLICY_AUTO_ONLY = 9,
68  NUM_ERROR_CODES
69};
70
71// The GoogleUpdateStatusListener interface is used by components to receive
72// notifications about the results of an Google Update operation.
73class GoogleUpdateStatusListener {
74 public:
75  // This function is called when Google Update has finished its operation and
76  // wants to notify us about the results. |results| represents what the end
77  // state is, |error_code| represents what error occurred, |error_message| is a
78  // string version of the same (might be blank) and |version| specifies what
79  // new version Google Update detected (or installed). This value can be a
80  // blank string, if the version tag in the Update{} block (in Google Update's
81  // server config for Chrome) is blank.
82  virtual void OnReportResults(GoogleUpdateUpgradeResult results,
83                               GoogleUpdateErrorCode error_code,
84                               const base::string16& error_message,
85                               const base::string16& version) = 0;
86};
87
88////////////////////////////////////////////////////////////////////////////////
89//
90// The Google Update class is responsible for communicating with Google Update
91// and get it to perform operations on our behalf (for example, CheckForUpdate).
92// This class will report back to its parent via the GoogleUpdateStatusListener
93// interface and will delete itself after reporting back.
94//
95////////////////////////////////////////////////////////////////////////////////
96class GoogleUpdate : public base::RefCountedThreadSafe<GoogleUpdate> {
97 public:
98  GoogleUpdate();
99
100  // Ask Google Update to see if a new version is available. If the parameter
101  // |install_if_newer| is true then Google Update will also install that new
102  // version.
103  // |window| should point to a foreground window. This is needed to ensure
104  // that Vista/Windows 7 UAC prompts show up in the foreground. It may also
105  // be null.
106  void CheckForUpdate(bool install_if_newer, HWND window);
107
108  // Pass NULL to clear the listener
109  void set_status_listener(GoogleUpdateStatusListener* listener) {
110    listener_ = listener;
111  }
112
113 private:
114  friend class base::RefCountedThreadSafe<GoogleUpdate>;
115
116  virtual ~GoogleUpdate();
117
118  // This function reports failure from the Google Update operation to the
119  // listener.
120  // Note, after this function completes, this object will have deleted itself.
121  bool ReportFailure(HRESULT hr, GoogleUpdateErrorCode error_code,
122                     const base::string16& error_message,
123                     base::MessageLoop* main_loop);
124
125  // The update check needs to run on another thread than the main thread, and
126  // therefore CheckForUpdate will delegate to this function. |main_loop| points
127  // to the message loop that the response must come from.
128  // |window| should point to a foreground window. This is needed to ensure that
129  // Vista/Windows 7 UAC prompts show up in the foreground. It may also be null.
130  void InitiateGoogleUpdateCheck(bool install_if_newer, HWND window,
131                                 base::MessageLoop* main_loop);
132
133  // This function reports the results of the GoogleUpdate operation to the
134  // listener. If results indicates an error, the |error_code| and
135  // |error_message| will indicate which error occurred.
136  // Note, after this function completes, this object will have deleted itself.
137  void ReportResults(GoogleUpdateUpgradeResult results,
138                     GoogleUpdateErrorCode error_code,
139                     const base::string16& error_message);
140
141  // Which version string Google Update found (if a new one was available).
142  // Otherwise, this will be blank.
143  base::string16 version_available_;
144
145  // The listener who is interested in finding out the result of the operation.
146  GoogleUpdateStatusListener* listener_;
147
148  DISALLOW_COPY_AND_ASSIGN(GoogleUpdate);
149};
150
151#endif  // CHROME_BROWSER_GOOGLE_GOOGLE_UPDATE_WIN_H_
152