1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome_frame/turndown_prompt/turndown_prompt.h"
#include <atlbase.h>
#include <shlguid.h>
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_comptr.h"
#include "base/win/win_util.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/google_update_settings.h"
#include "chrome/installer/util/install_util.h"
#include "chrome/installer/util/installation_state.h"
#include "chrome/installer/util/util_constants.h"
#include "chrome_frame/infobars/infobar_manager.h"
#include "chrome_frame/policy_settings.h"
#include "chrome_frame/ready_mode/internal/ready_mode_web_browser_adapter.h"
#include "chrome_frame/ready_mode/internal/url_launcher.h"
#include "chrome_frame/simple_resource_loader.h"
#include "chrome_frame/turndown_prompt/reshow_state.h"
#include "chrome_frame/turndown_prompt/turndown_prompt_content.h"
#include "chrome_frame/utils.h"
#include "grit/chromium_strings.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
namespace {
// Time between showings of the turndown prompt.
const int kTurndownPromptReshowDeltaMinutes = 60 * 24 * 7;
void OnUninstallClicked(UrlLauncher* url_launcher);
// Manages the Turndown UI in response to browsing ChromeFrame-rendered
// pages.
class BrowserObserver : public ReadyModeWebBrowserAdapter::Observer {
public:
BrowserObserver(IWebBrowser2* web_browser,
ReadyModeWebBrowserAdapter* adapter);
// ReadyModeWebBrowserAdapter::Observer implementation
virtual void OnNavigateTo(const string16& url);
virtual void OnRenderInChromeFrame(const string16& url);
virtual void OnRenderInHost(const string16& url);
private:
// Shows the turndown prompt if it hasn't been seen since
// kTurndownPromptReshowDeltaMinutes.
void ShowPrompt();
void Hide();
// Returns a self-managed pointer that is not guaranteed to survive handling
// of Windows events. For safety's sake, retrieve this pointer for each use
// and do not store it for use outside of scope.
InfobarManager* GetInfobarManager();
GURL rendered_url_;
base::win::ScopedComPtr<IWebBrowser2> web_browser_;
ReadyModeWebBrowserAdapter* adapter_;
DISALLOW_COPY_AND_ASSIGN(BrowserObserver);
};
// Implements launching of a URL in an instance of IWebBrowser2.
class UrlLauncherImpl : public UrlLauncher {
public:
explicit UrlLauncherImpl(IWebBrowser2* web_browser);
// UrlLauncher implementation
void LaunchUrl(const string16& url);
private:
base::win::ScopedComPtr<IWebBrowser2> web_browser_;
};
UrlLauncherImpl::UrlLauncherImpl(IWebBrowser2* web_browser) {
DCHECK(web_browser);
web_browser_ = web_browser;
}
void UrlLauncherImpl::LaunchUrl(const string16& url) {
VARIANT flags = { VT_I4 };
V_I4(&flags) = navOpenInNewWindow;
base::win::ScopedBstr location(url.c_str());
HRESULT hr = web_browser_->Navigate(location, &flags, NULL, NULL, NULL);
DLOG_IF(ERROR, FAILED(hr)) << "Failed to invoke Navigate on IWebBrowser2. "
<< "Error: " << hr;
}
BrowserObserver::BrowserObserver(IWebBrowser2* web_browser,
ReadyModeWebBrowserAdapter* adapter)
: web_browser_(web_browser),
adapter_(adapter) {
}
void BrowserObserver::OnNavigateTo(const string16& url) {
if (!net::registry_controlled_domains::SameDomainOrHost(
GURL(url),
rendered_url_,
net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)) {
rendered_url_ = GURL();
Hide();
}
}
void BrowserObserver::OnRenderInChromeFrame(const string16& url) {
ShowPrompt();
rendered_url_ = GURL(url);
}
void BrowserObserver::OnRenderInHost(const string16& url) {
Hide();
rendered_url_ = GURL(url);
}
void BrowserObserver::ShowPrompt() {
turndown_prompt::ReshowState reshow_state(
base::TimeDelta::FromMinutes(kTurndownPromptReshowDeltaMinutes));
// Short-circuit if the prompt shouldn't be shown again yet.
if (!reshow_state.HasReshowDeltaExpired(base::Time::Now()))
return;
// This pointer is self-managed and not guaranteed to survive handling of
// Windows events. For safety's sake, retrieve this pointer for each use and
// do not store it for use outside of scope.
InfobarManager* infobar_manager = GetInfobarManager();
if (infobar_manager) {
// Owned by infobar_content
scoped_ptr<UrlLauncher> url_launcher(new UrlLauncherImpl(web_browser_));
// Owned by infobar_manager
scoped_ptr<InfobarContent> infobar_content(new TurndownPromptContent(
url_launcher.release(),
base::Bind(&OnUninstallClicked,
base::Owned(new UrlLauncherImpl(web_browser_)))));
if (infobar_manager->Show(infobar_content.release(), TOP_INFOBAR)) {
// Update state in the registry that the prompt was shown.
reshow_state.MarkShown(base::Time::Now());
}
}
}
void BrowserObserver::Hide() {
InfobarManager* infobar_manager = GetInfobarManager();
if (infobar_manager)
infobar_manager->HideAll();
}
InfobarManager* BrowserObserver::GetInfobarManager() {
HRESULT hr = NOERROR;
base::win::ScopedComPtr<IOleWindow> ole_window;
hr = DoQueryService(SID_SShellBrowser, web_browser_, ole_window.Receive());
if (FAILED(hr) || ole_window == NULL) {
DLOG(ERROR) << "Failed to query SID_SShellBrowser from IWebBrowser2. "
<< "Error: " << hr;
return NULL;
}
HWND web_browser_hwnd = NULL;
hr = ole_window->GetWindow(&web_browser_hwnd);
if (FAILED(hr) || web_browser_hwnd == NULL) {
DLOG(ERROR) << "Failed to query HWND from IOleWindow. "
<< "Error: " << hr;
return NULL;
}
return InfobarManager::Get(web_browser_hwnd);
}
// Returns true if the module into which this code is linked is installed at
// system-level.
bool IsCurrentModuleSystemLevel() {
base::FilePath dll_path;
if (PathService::Get(base::DIR_MODULE, &dll_path))
return !InstallUtil::IsPerUserInstall(dll_path.value().c_str());
return false;
}
// Attempts to create a ReadyModeWebBrowserAdapter instance.
bool CreateWebBrowserAdapter(ReadyModeWebBrowserAdapter** adapter) {
*adapter = NULL;
CComObject<ReadyModeWebBrowserAdapter>* com_object;
HRESULT hr =
CComObject<ReadyModeWebBrowserAdapter>::CreateInstance(&com_object);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to create instance of ReadyModeWebBrowserAdapter. "
<< "Error: " << hr;
return false;
}
com_object->AddRef();
*adapter = com_object;
return true;
}
// Attempts to install Turndown prompts in the provided web browser.
bool InstallPrompts(IWebBrowser2* web_browser) {
base::win::ScopedComPtr<ReadyModeWebBrowserAdapter, NULL> adapter;
if (!CreateWebBrowserAdapter(adapter.Receive()))
return false;
// Pass ownership of our delegate to the BrowserObserver
scoped_ptr<ReadyModeWebBrowserAdapter::Observer> browser_observer(
new BrowserObserver(web_browser, adapter));
// Owns the BrowserObserver
return adapter->Initialize(web_browser, browser_observer.release());
}
// Launches the Chrome Frame uninstaller in response to user action. This
// implementation should not be used to uninstall MSI-based versions of GCF,
// since those must be removed by way of Windows Installer machinery.
void LaunchChromeFrameUninstaller() {
BrowserDistribution* dist =
BrowserDistribution::GetSpecificDistribution(
BrowserDistribution::CHROME_FRAME);
const bool system_level = IsCurrentModuleSystemLevel();
installer::ProductState product_state;
if (!product_state.Initialize(system_level, dist)) {
DLOG(ERROR) << "Chrome frame isn't installed at "
<< (system_level ? "system" : "user") << " level.";
return;
}
CommandLine uninstall_command(product_state.uninstall_command());
if (uninstall_command.GetProgram().empty()) {
DLOG(ERROR) << "No uninstall command found in registry.";
return;
}
// Force Uninstall silences the prompt to reboot to complete uninstall.
uninstall_command.AppendSwitch(installer::switches::kForceUninstall);
VLOG(1) << "Uninstalling Chrome Frame with command: "
<< uninstall_command.GetCommandLineString();
base::LaunchProcess(uninstall_command, base::LaunchOptions(), NULL);
}
void LaunchLearnMoreURL(UrlLauncher* url_launcher) {
url_launcher->LaunchUrl(SimpleResourceLoader::Get(
IDS_CHROME_FRAME_TURNDOWN_LEARN_MORE_URL));
}
void OnUninstallClicked(UrlLauncher* url_launcher) {
LaunchChromeFrameUninstaller();
LaunchLearnMoreURL(url_launcher);
}
} // namespace
namespace turndown_prompt {
bool IsPromptSuppressed() {
// See if this is an MSI install of GCF or if updates have been disabled.
BrowserDistribution* dist =
BrowserDistribution::GetSpecificDistribution(
BrowserDistribution::CHROME_FRAME);
const bool system_level = IsCurrentModuleSystemLevel();
bool multi_install = false;
installer::ProductState product_state;
if (product_state.Initialize(system_level, dist)) {
if (product_state.is_msi())
return true;
multi_install = product_state.is_multi_install();
}
if (multi_install) {
dist =
BrowserDistribution::GetSpecificDistribution(
BrowserDistribution::CHROME_BINARIES);
}
if (GoogleUpdateSettings::GetAppUpdatePolicy(dist->GetAppGuid(), NULL) ==
GoogleUpdateSettings::UPDATES_DISABLED) {
return true;
}
// See if the prompt is explicitly suppressed via GP.
return PolicySettings::GetInstance()->suppress_turndown_prompt();
}
void Configure(IWebBrowser2* web_browser) {
if (!IsPromptSuppressed())
InstallPrompts(web_browser);
}
} // namespace turndown_prompt
|