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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
// Copyright (c) 2012 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/browser/ui/webui/set_as_default_browser_ui.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/shell_integration.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/singleton_tabs.h"
#include "chrome/browser/ui/sync/sync_promo_ui.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/locale_settings.h"
#include "chrome/installer/util/install_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "grit/browser_resources.h"
#include "ui/base/l10n/l10n_font_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/font.h"
#include "ui/views/widget/widget.h"
#include "ui/web_dialogs/web_dialog_delegate.h"
using content::BrowserThread;
using content::WebContents;
using content::WebUIMessageHandler;
namespace {
const char kSetAsDefaultBrowserHistogram[] = "DefaultBrowser.InteractionResult";
// The enum permits registering in UMA the three possible outcomes (do not
// reorder these).
// ACCEPTED: user pressed Next and made Chrome default.
// DECLINED: user simply closed the dialog without making Chrome default.
// REGRETTED: user pressed Next but then elected a different default browser.
enum MakeChromeDefaultResult {
MAKE_CHROME_DEFAULT_ACCEPTED = 0,
MAKE_CHROME_DEFAULT_DECLINED = 1,
MAKE_CHROME_DEFAULT_REGRETTED = 2,
// MAKE_CHROME_DEFAULT_ACCEPTED_IMMERSE = 3, // Deprecated.
MAKE_CHROME_DEFAULT_MAX
};
content::WebUIDataSource* CreateSetAsDefaultBrowserUIHTMLSource() {
content::WebUIDataSource* data_source = content::WebUIDataSource::Create(
chrome::kChromeUIMetroFlowHost);
data_source->AddLocalizedString("page-title", IDS_METRO_FLOW_TAB_TITLE);
data_source->AddLocalizedString("flowTitle", IDS_METRO_FLOW_TITLE_SHORT);
data_source->AddLocalizedString("flowDescription",
IDS_METRO_FLOW_DESCRIPTION);
data_source->AddLocalizedString("flowNext",
IDS_METRO_FLOW_SET_DEFAULT);
data_source->AddLocalizedString("chromeLogoString",
IDS_METRO_FLOW_LOGO_STRING_ALT);
data_source->SetJsonPath("strings.js");
data_source->AddResourcePath("set_as_default_browser.js",
IDR_SET_AS_DEFAULT_BROWSER_JS);
data_source->SetDefaultResource(IDR_SET_AS_DEFAULT_BROWSER_HTML);
return data_source;
}
// A simple class serving as a delegate for passing down the result of the
// interaction.
class ResponseDelegate {
public:
virtual void SetDialogInteractionResult(MakeChromeDefaultResult result) = 0;
protected:
virtual ~ResponseDelegate() { }
};
// Event handler for SetAsDefaultBrowserUI. Capable of setting Chrome as the
// default browser on button click, closing itself and triggering Chrome
// restart.
class SetAsDefaultBrowserHandler
: public WebUIMessageHandler,
public base::SupportsWeakPtr<SetAsDefaultBrowserHandler>,
public ShellIntegration::DefaultWebClientObserver {
public:
explicit SetAsDefaultBrowserHandler(
const base::WeakPtr<ResponseDelegate>& response_delegate);
virtual ~SetAsDefaultBrowserHandler();
// WebUIMessageHandler implementation.
virtual void RegisterMessages() override;
// ShellIntegration::DefaultWebClientObserver implementation.
virtual void SetDefaultWebClientUIState(
ShellIntegration::DefaultWebClientUIState state) override;
virtual void OnSetAsDefaultConcluded(bool close_chrome) override;
virtual bool IsInteractiveSetDefaultPermitted() override;
private:
// Handler for the 'Next' (or 'make Chrome the Metro browser') button.
void HandleLaunchSetDefaultBrowserFlow(const base::ListValue* args);
// Close this web ui.
void ConcludeInteraction(MakeChromeDefaultResult interaction_result);
scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_;
bool set_default_returned_;
bool set_default_result_;
base::WeakPtr<ResponseDelegate> response_delegate_;
DISALLOW_COPY_AND_ASSIGN(SetAsDefaultBrowserHandler);
};
SetAsDefaultBrowserHandler::SetAsDefaultBrowserHandler(
const base::WeakPtr<ResponseDelegate>& response_delegate)
: default_browser_worker_(new ShellIntegration::DefaultBrowserWorker(this)),
set_default_returned_(false), set_default_result_(false),
response_delegate_(response_delegate) {
}
SetAsDefaultBrowserHandler::~SetAsDefaultBrowserHandler() {
default_browser_worker_->ObserverDestroyed();
}
void SetAsDefaultBrowserHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"SetAsDefaultBrowser:LaunchSetDefaultBrowserFlow",
base::Bind(&SetAsDefaultBrowserHandler::HandleLaunchSetDefaultBrowserFlow,
base::Unretained(this)));
}
void SetAsDefaultBrowserHandler::SetDefaultWebClientUIState(
ShellIntegration::DefaultWebClientUIState state) {
// The callback is expected to be invoked once the procedure has completed.
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!set_default_returned_)
return;
if (state == ShellIntegration::STATE_NOT_DEFAULT && set_default_result_) {
// The operation concluded, but Chrome is still not the default.
// If the call has succeeded, this suggests user has decided not to make
// chrome the default.
ConcludeInteraction(MAKE_CHROME_DEFAULT_REGRETTED);
} else if (state == ShellIntegration::STATE_IS_DEFAULT) {
ConcludeInteraction(MAKE_CHROME_DEFAULT_ACCEPTED);
}
// Otherwise, keep the dialog open since the user probably didn't make a
// choice.
}
void SetAsDefaultBrowserHandler::OnSetAsDefaultConcluded(bool call_result) {
set_default_returned_ = true;
set_default_result_ = call_result;
}
bool SetAsDefaultBrowserHandler::IsInteractiveSetDefaultPermitted() {
return true;
}
void SetAsDefaultBrowserHandler::HandleLaunchSetDefaultBrowserFlow(
const base::ListValue* args) {
set_default_returned_ = false;
set_default_result_ = false;
default_browser_worker_->StartSetAsDefault();
}
void SetAsDefaultBrowserHandler::ConcludeInteraction(
MakeChromeDefaultResult interaction_result) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (response_delegate_)
response_delegate_->SetDialogInteractionResult(interaction_result);
WebContents* contents = web_ui()->GetWebContents();
if (contents) {
content::WebContentsDelegate* delegate = contents->GetDelegate();
if (delegate)
delegate->CloseContents(contents);
}
}
// A web dialog delegate implementation for when 'Make Chrome Metro' UI
// is displayed on a dialog.
class SetAsDefaultBrowserDialogImpl : public ui::WebDialogDelegate,
public ResponseDelegate,
public chrome::BrowserListObserver {
public:
SetAsDefaultBrowserDialogImpl(Profile* profile, Browser* browser);
virtual ~SetAsDefaultBrowserDialogImpl();
// Show a modal web dialog with kChromeUIMetroFlowURL page.
void ShowDialog();
protected:
// Overridden from WebDialogDelegate:
virtual ui::ModalType GetDialogModalType() const override;
virtual base::string16 GetDialogTitle() const override;
virtual GURL GetDialogContentURL() const override;
virtual void GetWebUIMessageHandlers(
std::vector<WebUIMessageHandler*>* handlers) const override;
virtual void GetDialogSize(gfx::Size* size) const override;
virtual std::string GetDialogArgs() const override;
virtual void OnDialogClosed(const std::string& json_retval) override;
virtual void OnCloseContents(WebContents* source,
bool* out_close_dialog) override;
virtual bool ShouldShowDialogTitle() const override;
virtual bool HandleContextMenu(
const content::ContextMenuParams& params) override;
// Overridden from ResponseDelegate:
virtual void SetDialogInteractionResult(MakeChromeDefaultResult result);
// Overridden from BrowserListObserver:
virtual void OnBrowserRemoved(Browser* browser) override;
private:
Profile* profile_;
Browser* browser_;
mutable bool owns_handler_;
base::WeakPtrFactory<ResponseDelegate> response_delegate_ptr_factory_;
SetAsDefaultBrowserHandler* handler_;
MakeChromeDefaultResult dialog_interaction_result_;
DISALLOW_COPY_AND_ASSIGN(SetAsDefaultBrowserDialogImpl);
};
SetAsDefaultBrowserDialogImpl::SetAsDefaultBrowserDialogImpl(Profile* profile,
Browser* browser)
: profile_(profile),
browser_(browser),
owns_handler_(true),
response_delegate_ptr_factory_(this),
handler_(new SetAsDefaultBrowserHandler(
response_delegate_ptr_factory_.GetWeakPtr())),
dialog_interaction_result_(MAKE_CHROME_DEFAULT_DECLINED) {
BrowserList::AddObserver(this);
}
SetAsDefaultBrowserDialogImpl::~SetAsDefaultBrowserDialogImpl() {
if (browser_)
BrowserList::RemoveObserver(this);
if (owns_handler_)
delete handler_;
}
void SetAsDefaultBrowserDialogImpl::ShowDialog() {
// Use a NULL parent window to make sure that the dialog will have an item
// in the Windows task bar. The code below will make it highlight if the
// dialog is not in the foreground.
gfx::NativeWindow native_window = chrome::ShowWebDialog(NULL, profile_, this);
views::Widget* widget = views::Widget::GetWidgetForNativeWindow(
native_window);
widget->FlashFrame(true);
}
ui::ModalType SetAsDefaultBrowserDialogImpl::GetDialogModalType() const {
return ui::MODAL_TYPE_SYSTEM;
}
base::string16 SetAsDefaultBrowserDialogImpl::GetDialogTitle() const {
return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE);
}
GURL SetAsDefaultBrowserDialogImpl::GetDialogContentURL() const {
std::string url_string(chrome::kChromeUIMetroFlowURL);
return GURL(url_string);
}
void SetAsDefaultBrowserDialogImpl::GetWebUIMessageHandlers(
std::vector<WebUIMessageHandler*>* handlers) const {
handlers->push_back(handler_);
owns_handler_ = false;
}
void SetAsDefaultBrowserDialogImpl::GetDialogSize(gfx::Size* size) const {
PrefService* prefs = profile_->GetPrefs();
gfx::Font approximate_web_font(
prefs->GetString(prefs::kWebKitSansSerifFontFamily),
prefs->GetInteger(prefs::kWebKitDefaultFontSize));
*size = ui::GetLocalizedContentsSizeForFont(
IDS_METRO_FLOW_WIDTH_CHARS, IDS_METRO_FLOW_HEIGHT_LINES,
approximate_web_font);
}
std::string SetAsDefaultBrowserDialogImpl::GetDialogArgs() const {
return "[]";
}
void SetAsDefaultBrowserDialogImpl::OnDialogClosed(
const std::string& json_retval) {
// Register the user's response in UMA.
UMA_HISTOGRAM_ENUMERATION(kSetAsDefaultBrowserHistogram,
dialog_interaction_result_,
MAKE_CHROME_DEFAULT_MAX);
// If the user explicitly elected *not to* make Chrome default, we won't
// ask again.
if (dialog_interaction_result_ == MAKE_CHROME_DEFAULT_REGRETTED) {
PrefService* prefs = profile_->GetPrefs();
prefs->SetBoolean(prefs::kCheckDefaultBrowser, false);
}
// Carry on with a normal chrome session. For the purpose of surfacing this
// dialog the actual browser window had to remain hidden. Now it's time to
// show it.
if (browser_) {
BrowserWindow* window = browser_->window();
WebContents* contents = browser_->tab_strip_model()->GetActiveWebContents();
window->Show();
if (contents)
contents->SetInitialFocus();
}
delete this;
}
void SetAsDefaultBrowserDialogImpl::OnCloseContents(WebContents* source,
bool* out_close_dialog) {
*out_close_dialog = true;
}
bool SetAsDefaultBrowserDialogImpl::ShouldShowDialogTitle() const {
return true;
}
bool SetAsDefaultBrowserDialogImpl::HandleContextMenu(
const content::ContextMenuParams& params) {
return true;
}
void SetAsDefaultBrowserDialogImpl::SetDialogInteractionResult(
MakeChromeDefaultResult result) {
dialog_interaction_result_ = result;
}
void SetAsDefaultBrowserDialogImpl::OnBrowserRemoved(Browser* browser) {
if (browser_ == browser) {
browser_ = NULL;
BrowserList::RemoveObserver(this);
}
}
} // namespace
SetAsDefaultBrowserUI::SetAsDefaultBrowserUI(content::WebUI* web_ui)
: ui::WebDialogUI(web_ui) {
content::WebUIDataSource::Add(
Profile::FromWebUI(web_ui), CreateSetAsDefaultBrowserUIHTMLSource());
}
// static
void SetAsDefaultBrowserUI::Show(Profile* profile, Browser* browser) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
SetAsDefaultBrowserDialogImpl* dialog =
new SetAsDefaultBrowserDialogImpl(profile, browser);
dialog->ShowDialog();
}
|