OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/browser/upgrade_detector.h" | 5 #include "chrome/browser/upgrade_detector.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_version_info.h" | 8 #include "base/file_version_info.h" |
9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "base/task.h" | 11 #include "base/task.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "base/version.h" | 13 #include "base/version.h" |
14 #include "chrome/app/chrome_version_info.h" | 14 #include "chrome/app/chrome_version_info.h" |
15 #include "chrome/browser/chrome_thread.h" | 15 #include "chrome/browser/chrome_thread.h" |
16 #include "chrome/browser/pref_service.h" | 16 #include "chrome/browser/pref_service.h" |
17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/notification_service.h" | 18 #include "chrome/common/notification_service.h" |
19 #include "chrome/common/notification_type.h" | 19 #include "chrome/common/notification_type.h" |
20 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
21 #include "chrome/installer/util/browser_distribution.h" | 21 #include "chrome/installer/util/browser_distribution.h" |
22 | 22 |
23 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
24 #include "chrome/installer/util/install_util.h" | 24 #include "chrome/installer/util/install_util.h" |
25 #elif defined(OS_LINUX) | 25 #elif defined(OS_MACOSX) |
| 26 #include "chrome/browser/cocoa/keystone_glue.h" |
| 27 #elif defined(OS_POSIX) |
26 #include "base/process_util.h" | 28 #include "base/process_util.h" |
27 #include "chrome/installer/util/version.h" | 29 #include "chrome/installer/util/version.h" |
28 #endif | 30 #endif |
29 | 31 |
30 // TODO(finnur): For the stable channel we want to check daily and notify | 32 // TODO(finnur): For the stable channel we want to check daily and notify |
31 // the user if more than 2 weeks have passed since the upgrade happened | 33 // the user if more than 2 weeks have passed since the upgrade happened |
32 // (without a reboot). For the dev channel however, I want quicker feedback | 34 // (without a reboot). For the dev channel however, I want quicker feedback |
33 // on how the feature works so I'm checking every hour and notifying the | 35 // on how the feature works so I'm checking every hour and notifying the |
34 // user immediately. | 36 // user immediately. |
35 | 37 |
36 namespace { | 38 namespace { |
37 | 39 |
38 // How often to check for an upgrade. | 40 // How often to check for an upgrade. |
39 const int kCheckForUpgradeEveryMs = 60 * 60 * 1000; // 1 hour. | 41 const int kCheckForUpgradeEveryMs = 60 * 60 * 1000; // 1 hour. |
40 | 42 |
41 // How long to wait before notifying the user about the upgrade. | 43 // How long to wait before notifying the user about the upgrade. |
42 const int kNotifyUserAfterMs = 0; | 44 const int kNotifyUserAfterMs = 0; |
43 | 45 |
44 // The thread to run the upgrade detection code on. We use FILE for Linux | 46 // The thread to run the upgrade detection code on. We use FILE for Linux |
45 // because we don't want to block the UI thread while launching a background | 47 // because we don't want to block the UI thread while launching a background |
46 // process and reading its output. | 48 // process and reading its output; on the Mac, checking for an upgrade |
| 49 // requires reading a file. |
47 const ChromeThread::ID kDetectUpgradeTaskID = | 50 const ChromeThread::ID kDetectUpgradeTaskID = |
48 #if defined(OS_LINUX) | 51 #if defined(OS_POSIX) |
49 ChromeThread::FILE; | 52 ChromeThread::FILE; |
50 #else | 53 #else |
51 ChromeThread::UI; | 54 ChromeThread::UI; |
52 #endif | 55 #endif |
53 | 56 |
54 // This task checks the currently running version of Chrome against the | 57 // This task checks the currently running version of Chrome against the |
55 // installed version. If the installed version is newer, it runs the passed | 58 // installed version. If the installed version is newer, it runs the passed |
56 // callback task. Otherwise it just deletes the task. | 59 // callback task. Otherwise it just deletes the task. |
57 class DetectUpgradeTask : public Task { | 60 class DetectUpgradeTask : public Task { |
58 public: | 61 public: |
59 explicit DetectUpgradeTask(Task* upgrade_detected_task) | 62 explicit DetectUpgradeTask(Task* upgrade_detected_task) |
60 : upgrade_detected_task_(upgrade_detected_task) { | 63 : upgrade_detected_task_(upgrade_detected_task) { |
61 } | 64 } |
62 | 65 |
63 virtual ~DetectUpgradeTask() { | 66 virtual ~DetectUpgradeTask() { |
64 if (upgrade_detected_task_) { | 67 if (upgrade_detected_task_) { |
65 // This has to get deleted on the same thread it was created. | 68 // This has to get deleted on the same thread it was created. |
66 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, | 69 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
67 new DeleteTask<Task>(upgrade_detected_task_)); | 70 new DeleteTask<Task>(upgrade_detected_task_)); |
68 } | 71 } |
69 } | 72 } |
70 | 73 |
71 virtual void Run() { | 74 virtual void Run() { |
72 DCHECK(ChromeThread::CurrentlyOn(kDetectUpgradeTaskID)); | 75 DCHECK(ChromeThread::CurrentlyOn(kDetectUpgradeTaskID)); |
73 | 76 |
74 #if defined(OS_WIN) || defined(OS_LINUX) | |
75 using installer::Version; | 77 using installer::Version; |
| 78 scoped_ptr<Version> installed_version; |
76 | 79 |
77 #if defined(OS_WIN) | 80 #if defined(OS_WIN) |
78 // Get the version of the currently *installed* instance of Chrome, | 81 // Get the version of the currently *installed* instance of Chrome, |
79 // which might be newer than the *running* instance if we have been | 82 // which might be newer than the *running* instance if we have been |
80 // upgraded in the background. | 83 // upgraded in the background. |
81 scoped_ptr<Version> installed_version(InstallUtil::GetChromeVersion(false)); | 84 installed_version.reset(InstallUtil::GetChromeVersion(false)); |
82 if (!installed_version.get()) { | 85 if (!installed_version.get()) { |
83 // User level Chrome is not installed, check system level. | 86 // User level Chrome is not installed, check system level. |
84 installed_version.reset(InstallUtil::GetChromeVersion(true)); | 87 installed_version.reset(InstallUtil::GetChromeVersion(true)); |
85 } | 88 } |
86 #elif defined(OS_LINUX) | 89 #elif defined(OS_MACOSX) |
| 90 installed_version.reset( |
| 91 Version::GetVersionFromString( |
| 92 keystone_glue::CurrentlyInstalledVersion())); |
| 93 #elif defined(OS_POSIX) |
| 94 // POSIX but not Mac OS X: Linux, etc. |
87 CommandLine command_line(*CommandLine::ForCurrentProcess()); | 95 CommandLine command_line(*CommandLine::ForCurrentProcess()); |
88 command_line.AppendSwitch(switches::kProductVersion); | 96 command_line.AppendSwitch(switches::kProductVersion); |
89 std::string reply; | 97 std::string reply; |
90 if (!base::GetAppOutput(command_line, &reply)) { | 98 if (!base::GetAppOutput(command_line, &reply)) { |
91 DLOG(ERROR) << "Failed to get current file version"; | 99 DLOG(ERROR) << "Failed to get current file version"; |
92 return; | 100 return; |
93 } | 101 } |
94 | 102 |
95 scoped_ptr<Version> installed_version( | 103 installed_version.reset(Version::GetVersionFromString(ASCIIToUTF16(reply))); |
96 Version::GetVersionFromString(ASCIIToUTF16(reply))); | |
97 #endif | 104 #endif |
98 | 105 |
99 // Get the version of the currently *running* instance of Chrome. | 106 // Get the version of the currently *running* instance of Chrome. |
100 scoped_ptr<FileVersionInfo> version(chrome_app::GetChromeVersionInfo()); | 107 scoped_ptr<FileVersionInfo> version(chrome_app::GetChromeVersionInfo()); |
101 if (version.get() == NULL) { | 108 if (version.get() == NULL) { |
102 NOTREACHED() << "Failed to get current file version"; | 109 NOTREACHED() << "Failed to get current file version"; |
103 return; | 110 return; |
104 } | 111 } |
105 | 112 |
106 scoped_ptr<Version> running_version( | 113 scoped_ptr<Version> running_version( |
107 Version::GetVersionFromString(WideToUTF16(version->file_version()))); | 114 Version::GetVersionFromString(WideToUTF16(version->file_version()))); |
108 if (running_version.get() == NULL) { | 115 if (running_version.get() == NULL) { |
109 NOTREACHED() << "Failed to parse version info"; | 116 NOTREACHED() << "Failed to parse version info"; |
110 return; | 117 return; |
111 } | 118 } |
112 | 119 |
113 // |installed_version| may be NULL when the user downgrades on Linux (by | 120 // |installed_version| may be NULL when the user downgrades on Linux (by |
114 // switching from dev to beta channel, for example). The user needs a | 121 // switching from dev to beta channel, for example). The user needs a |
115 // restart in this case as well. See http://crbug.com/46547 | 122 // restart in this case as well. See http://crbug.com/46547 |
116 if (!installed_version.get() || | 123 if (!installed_version.get() || |
117 installed_version->IsHigherThan(running_version.get())) { | 124 installed_version->IsHigherThan(running_version.get())) { |
118 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, | 125 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
119 upgrade_detected_task_); | 126 upgrade_detected_task_); |
120 upgrade_detected_task_ = NULL; | 127 upgrade_detected_task_ = NULL; |
121 } | 128 } |
122 #else // !(defined(OS_WIN) || defined(OS_LINUX)) | |
123 DCHECK(kNotifyUserAfterMs >= 0); // Avoid error: var defined but not used. | |
124 NOTIMPLEMENTED(); | |
125 #endif | |
126 } | 129 } |
127 | 130 |
128 private: | 131 private: |
129 Task* upgrade_detected_task_; | 132 Task* upgrade_detected_task_; |
130 }; | 133 }; |
131 | 134 |
132 } // namespace | 135 } // namespace |
133 | 136 |
134 // static | 137 // static |
135 void UpgradeDetector::RegisterPrefs(PrefService* prefs) { | 138 void UpgradeDetector::RegisterPrefs(PrefService* prefs) { |
136 prefs->RegisterBooleanPref(prefs::kRestartLastSessionOnShutdown, false); | 139 prefs->RegisterBooleanPref(prefs::kRestartLastSessionOnShutdown, false); |
137 } | 140 } |
138 | 141 |
139 UpgradeDetector::UpgradeDetector() | 142 UpgradeDetector::UpgradeDetector() |
140 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 143 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
141 notify_upgrade_(false) { | 144 notify_upgrade_(false) { |
142 // Upgrade notifications work on Windows (only Google Chrome) and Linux | 145 // Windows: only enable upgrade notifications for official builds. |
143 // (chromium and Google Chrome). | 146 // Mac: only enable them if the updater (Keystone) is present. |
144 #if (defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)) || defined(OS_LINUX) | 147 // Linux (and other POSIX): always enable regardless of branding. |
145 detect_upgrade_timer_.Start( | 148 #if (defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)) || defined(OS_POSIX) |
146 base::TimeDelta::FromMilliseconds(kCheckForUpgradeEveryMs), | 149 #if defined(OS_MACOSX) |
147 this, &UpgradeDetector::CheckForUpgrade); | 150 if (keystone_glue::KeystoneEnabled()) |
| 151 #endif |
| 152 { |
| 153 detect_upgrade_timer_.Start( |
| 154 base::TimeDelta::FromMilliseconds(kCheckForUpgradeEveryMs), |
| 155 this, &UpgradeDetector::CheckForUpgrade); |
| 156 } |
148 #endif | 157 #endif |
149 } | 158 } |
150 | 159 |
151 UpgradeDetector::~UpgradeDetector() { | 160 UpgradeDetector::~UpgradeDetector() { |
152 } | 161 } |
153 | 162 |
154 void UpgradeDetector::CheckForUpgrade() { | 163 void UpgradeDetector::CheckForUpgrade() { |
155 method_factory_.RevokeAll(); | 164 method_factory_.RevokeAll(); |
156 Task* callback_task = | 165 Task* callback_task = |
157 method_factory_.NewRunnableMethod(&UpgradeDetector::UpgradeDetected); | 166 method_factory_.NewRunnableMethod(&UpgradeDetector::UpgradeDetected); |
(...skipping 19 matching lines...) Expand all Loading... |
177 } | 186 } |
178 | 187 |
179 void UpgradeDetector::NotifyOnUpgrade() { | 188 void UpgradeDetector::NotifyOnUpgrade() { |
180 notify_upgrade_ = true; | 189 notify_upgrade_ = true; |
181 | 190 |
182 NotificationService::current()->Notify( | 191 NotificationService::current()->Notify( |
183 NotificationType::UPGRADE_RECOMMENDED, | 192 NotificationType::UPGRADE_RECOMMENDED, |
184 Source<UpgradeDetector>(this), | 193 Source<UpgradeDetector>(this), |
185 NotificationService::NoDetails()); | 194 NotificationService::NoDetails()); |
186 } | 195 } |
OLD | NEW |