Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(620)

Side by Side Diff: chrome/browser/ui/webui/metroizer_ui.cc

Issue 10539169: Prototype version of the first-run dialog for Windows 8 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed some of reviewer's remarks. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 #include "chrome/browser/ui/webui/metroizer_ui.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/path_service.h"
10 #include "chrome/browser/first_run/first_run.h"
11 #include "chrome/browser/prefs/pref_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h"
17 #include "chrome/browser/shell_integration.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/common/url_constants.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/browser/web_contents_delegate.h"
22 #include "content/public/browser/web_ui.h"
23 #include "content/public/browser/web_ui_message_handler.h"
24 #include "grit/browser_resources.h"
25 #include "grit/generated_resources.h"
26 #include "grit/locale_settings.h"
27 #include "ui/base/l10n/l10n_font_util.h"
28 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/gfx/font.h"
30 #include "ui/web_dialogs/web_dialog_delegate.h"
31
32 #ifdef OS_WIN
33 #include "chrome/installer/util/install_util.h"
34 #endif
35
36 using content::BrowserThread;
37 using content::WebContents;
38 using content::WebUIMessageHandler;
39
40 namespace {
41
42 ChromeWebUIDataSource * CreateMetroizerUIHTMLSource() {
43 ChromeWebUIDataSource * data_source = new ChromeWebUIDataSource(
44 chrome::kChromeUIMetroFlowHost);
45 data_source->AddLocalizedString("page-title", IDS_METRO_FLOW_TAB_TITLE);
46 data_source->AddLocalizedString("flowTitle", IDS_METRO_FLOW_TITLE_SHORT);
47 data_source->AddLocalizedString("flowDescription",
48 IDS_METRO_FLOW_DESCRIPTION);
49 data_source->AddLocalizedString("flowNext",
50 IDS_METRO_FLOW_SET_DEFAULT);
51 data_source->AddLocalizedString("flowCancel",
52 IDS_METRO_FLOW_SET_DEFAULT_CANCEL);
53 data_source->set_json_path("strings.js");
54 data_source->add_resource_path("metroize.js", IDR_METROIZE_JS);
55 data_source->set_default_resource(IDR_METROIZE_HTML);
56 return data_source;
57 }
58
59 class MetroizeHandler : public WebUIMessageHandler,
60 public base::SupportsWeakPtr<MetroizeHandler>,
61 public ShellIntegration::DefaultWebClientObserver {
62 public:
63 MetroizeHandler();
64 virtual ~MetroizeHandler();
65
66 // WebUIMessageHandler implementation.
67 virtual void RegisterMessages() OVERRIDE;
68
69 // ShellIntegration::DefaultWebClientObserver implementation.
70 virtual void SetDefaultWebClientUIState(
71 ShellIntegration::DefaultWebClientUIState state,
72 ShellIntegration::SetDefaultWebClientResult call_result) OVERRIDE;
73 virtual bool IsInteractiveSetDefaultPermitted() OVERRIDE;
74
75 private:
76 void HandleLaunchMetroizeProcedure(const ListValue* args);
77 void HandleReturnToChrome(const ListValue* args);
78 void CloseMetroizeWindow();
79
80 #ifdef OS_WIN
81 void ActivateMetroChrome();
82 #endif
83
84 scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_;
85
86 DISALLOW_COPY_AND_ASSIGN(MetroizeHandler);
87 };
88
89 MetroizeHandler::MetroizeHandler() {
90 default_browser_worker_ = new ShellIntegration::DefaultBrowserWorker(this);
91 }
92
93 MetroizeHandler::~MetroizeHandler() {
94 if (default_browser_worker_.get())
95 default_browser_worker_->ObserverDestroyed();
96 }
97
98 void MetroizeHandler::RegisterMessages() {
99 web_ui()->RegisterMessageCallback(
100 "Metroize:LaunchSetDefaultBrowserFlow",
101 base::Bind(&MetroizeHandler::HandleLaunchMetroizeProcedure,
102 base::Unretained(this)));
103 web_ui()->RegisterMessageCallback(
104 "Metroize:ReturnToBrowser",
105 base::Bind(&MetroizeHandler::HandleReturnToChrome,
106 base::Unretained(this)));
107 }
108
109 void MetroizeHandler::HandleLaunchMetroizeProcedure(const ListValue* args) {
110 if (default_browser_worker_.get())
111 default_browser_worker_->StartSetAsDefault();
112 }
113
114 void MetroizeHandler::HandleReturnToChrome(const ListValue* args) {
115 CloseMetroizeWindow();
116 }
117
118 void MetroizeHandler::CloseMetroizeWindow() {
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
120 WebContents* contents = web_ui()->GetWebContents();
121 if (contents) {
122 content::WebContentsDelegate* delegate = contents->GetDelegate();
123 if (delegate) {
124 if (!delegate->IsPopupOrPanel(contents)) {
125 Browser* browser = browser::FindBrowserWithWebContents(contents);
126 if (browser)
127 browser->ShowSyncSetup(SyncPromoUI::SOURCE_START_PAGE);
128 }
129 delegate->CloseContents(contents);
130 }
131 }
132 }
133
134 #ifdef OS_WIN
135 void MetroizeHandler::ActivateMetroChrome() {
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
137
138 FilePath cur_chrome_exe;
139 bool sentinel_removed = false;
140 if (PathService::Get(base::FILE_EXE, &cur_chrome_exe) &&
141 first_run::IsChromeFirstRun() &&
142 InstallUtil::IsPerUserInstall(cur_chrome_exe.value().c_str())) {
143 // If this is per-user install, we will have to remove the sentinel file
144 // to assure the user goes through the intended 'first-run flow'.
145 sentinel_removed = first_run::RemoveSentinel();
146 }
147
148 if (ShellIntegration::ActivateMetroChrome()) {
149 // If Metro Chrome has been activated, we should close this process.
150 // We are restarting as metro now.
151 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
152 base::Bind(&BrowserList::CloseAllBrowsersWithProfile,
153 Profile::FromWebUI(web_ui())));
154 } else {
155 // This will return false if the operation failed for any reason,
156 // including invocation under a Windows version earlier than 8.
157 // In such case we simply close the window and carry on.
158 if (sentinel_removed)
159 first_run::CreateSentinel();
160
161 BrowserThread::PostTask(
162 BrowserThread::UI, FROM_HERE,
163 base::Bind(&MetroizeHandler::CloseMetroizeWindow,
164 base::Unretained(this)));
165 }
166 }
167 #endif
168
169 void MetroizeHandler::SetDefaultWebClientUIState(
170 ShellIntegration::DefaultWebClientUIState state,
171 ShellIntegration::SetDefaultWebClientResult call_result) {
172 // The callback is expected to be invoked once the procedure has completed.
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
174 if (state == ShellIntegration::STATE_NOT_DEFAULT &&
175 call_result == ShellIntegration::RESULT_SET_DEFAULT_OK) {
176 // The operation concluded, but Chrome is still not the default.
177 // If the call has succeeded, this suggests user has decided not to make
178 // chrome the default. We fold this UI and move on.
179 CloseMetroizeWindow();
180 } else if (state == ShellIntegration::STATE_IS_DEFAULT) {
181 // This is a noop under systems other than Windows.
182 #ifdef OS_WIN
183 BrowserThread::PostTask(
184 BrowserThread::FILE, FROM_HERE,
185 base::Bind(&MetroizeHandler::ActivateMetroChrome,
186 base::Unretained(this)));
187 #endif
188 }
189 }
190
191 bool MetroizeHandler::IsInteractiveSetDefaultPermitted() {
192 return true;
193 }
194
195 class MetroizerDialogImpl : public ui::WebDialogDelegate {
196 public:
197 MetroizerDialogImpl(Profile* profile, Browser* browser);
198 void ShowDialog();
199
200 protected:
201 // Overridden from WebDialogDelegate:
202 virtual ui::ModalType GetDialogModalType() const OVERRIDE {
grt (UTC plus 2) 2012/06/19 20:43:09 chromium style: do not inline virtual methods; see
motek. 2012/06/20 16:02:15 Done.
203 return ui::MODAL_TYPE_SYSTEM;
204 }
205
206 virtual string16 GetDialogTitle() const OVERRIDE {
207 return l10n_util::GetStringUTF16(IDS_METRO_FLOW_TAB_TITLE);
208 }
209
210 virtual GURL GetDialogContentURL() const OVERRIDE {
211 std::string url_string(chrome::kChromeUIMetroFlowURL);
212 return GURL(url_string);
213 }
214
215 virtual void GetWebUIMessageHandlers(
216 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {
217 }
218
219 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE;
220
221 virtual std::string GetDialogArgs() const OVERRIDE {
222 return "[]";
223 }
224
225 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE {
226 delete this;
227 }
228
229 virtual void OnCloseContents(WebContents* source, bool* out_close_dialog)
230 OVERRIDE {
231 *out_close_dialog = true;
232 }
233
234 virtual bool ShouldShowDialogTitle() const OVERRIDE {
235 return true;
236 }
237
238 virtual bool HandleContextMenu(
239 const content::ContextMenuParams& params) OVERRIDE {
240 return true;
241 }
242
243 private:
244 Profile* profile_;
245 Browser* browser_;
246
247 DISALLOW_COPY_AND_ASSIGN(MetroizerDialogImpl);
248 };
249
250 MetroizerDialogImpl::MetroizerDialogImpl(Profile* profile, Browser* browser)
251 : profile_(profile), browser_(browser) {
252 }
253
254 void MetroizerDialogImpl::ShowDialog() {
255 browser_->BrowserShowWebDialog(this, NULL);
256 }
257
258 void MetroizerDialogImpl::GetDialogSize(gfx::Size* size) const {
259 PrefService* prefs = profile_->GetPrefs();
260 gfx::Font approximate_web_font(
261 prefs->GetString(prefs::kWebKitSansSerifFontFamily),
262 prefs->GetInteger(prefs::kWebKitDefaultFontSize));
263
264 *size = ui::GetLocalizedContentsSizeForFont(
265 IDS_METRO_FLOW_WIDTH_CHARS, IDS_METRO_FLOW_HEIGHT_LINES,
266 approximate_web_font);
267 }
268
269 static void ShowMetroizerInDialog(Profile* profile, Browser* browser) {
270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
271 MetroizerDialogImpl* dialog = new MetroizerDialogImpl(profile, browser);
272 dialog->ShowDialog();
273 }
274
275 static void AddMetroizerTab(Browser* browser) {
276 GURL url(chrome::kChromeUIMetroFlowURL);
277 browser::NavigateParams params(browser->GetSingletonTabNavigateParams(url));
278 params.path_behavior = browser::NavigateParams::IGNORE_AND_NAVIGATE;
279 browser->ShowSingletonTabOverwritingNTP(params);
280 }
281
282 } // namespace
283
284 MetroizerUI::MetroizerUI(content::WebUI* web_ui) : WebUIController(web_ui) {
285 web_ui->AddMessageHandler(new MetroizeHandler());
286 ChromeURLDataManager::AddDataSource(Profile::FromWebUI(web_ui),
287 CreateMetroizerUIHTMLSource());
288 }
289
290 // static
291 void MetroizerUI::Show(Profile* profile, Browser* browser, bool dialog) {
292 if (dialog) {
293 // Show the UI.
294 BrowserThread::PostTask(
295 BrowserThread::UI, FROM_HERE,
296 base::Bind(&ShowMetroizerInDialog, profile, browser));
297 } else {
298 BrowserThread::PostTask(
299 BrowserThread::UI, FROM_HERE,
300 base::Bind(&AddMetroizerTab, browser));
301 }
302 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698