1// Copyright (c) 2011 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/views/update_recommended_message_box.h"
6
7#include "base/utf_string_conversions.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/prefs/pref_service.h"
10#include "chrome/browser/ui/browser_list.h"
11#include "chrome/browser/ui/views/window.h"
12#include "chrome/common/pref_names.h"
13#include "grit/chromium_strings.h"
14#include "grit/generated_resources.h"
15#include "ui/base/l10n/l10n_util.h"
16#include "ui/base/message_box_flags.h"
17#include "views/controls/message_box_view.h"
18#include "views/window/window.h"
19
20#if defined(OS_CHROMEOS)
21#include "chrome/browser/chromeos/cros/cros_library.h"
22#include "chrome/browser/chromeos/cros/power_library.h"
23#endif
24
25////////////////////////////////////////////////////////////////////////////////
26// UpdateRecommendedMessageBox, public:
27
28// static
29void UpdateRecommendedMessageBox::ShowMessageBox(
30    gfx::NativeWindow parent_window) {
31  // When the window closes, it will delete itself.
32  new UpdateRecommendedMessageBox(parent_window);
33}
34
35bool UpdateRecommendedMessageBox::Accept() {
36  // Set the flag to restore the last session on shutdown.
37  PrefService* pref_service = g_browser_process->local_state();
38  pref_service->SetBoolean(prefs::kRestartLastSessionOnShutdown, true);
39
40#if defined(OS_CHROMEOS)
41  chromeos::CrosLibrary::Get()->GetPowerLibrary()->RequestRestart();
42  // If running the Chrome OS build, but we're not on the device, fall through
43#endif
44  BrowserList::CloseAllBrowsersAndExit();
45
46  return true;
47}
48
49int UpdateRecommendedMessageBox::GetDialogButtons() const {
50  return ui::MessageBoxFlags::DIALOGBUTTON_OK |
51         ui::MessageBoxFlags::DIALOGBUTTON_CANCEL;
52}
53
54std::wstring UpdateRecommendedMessageBox::GetDialogButtonLabel(
55    ui::MessageBoxFlags::DialogButton button) const {
56  DCHECK(button == ui::MessageBoxFlags::DIALOGBUTTON_OK ||
57         button == ui::MessageBoxFlags::DIALOGBUTTON_CANCEL);
58  return button == ui::MessageBoxFlags::DIALOGBUTTON_OK ?
59      UTF16ToWide(l10n_util::GetStringUTF16(IDS_RELAUNCH_AND_UPDATE)) :
60      UTF16ToWide(l10n_util::GetStringUTF16(IDS_NOT_NOW));
61}
62
63bool UpdateRecommendedMessageBox::ShouldShowWindowTitle() const {
64#if defined(OS_CHROMEOS)
65  return false;
66#else
67  return true;
68#endif
69}
70
71std::wstring UpdateRecommendedMessageBox::GetWindowTitle() const {
72  return UTF16ToWide(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
73}
74
75void UpdateRecommendedMessageBox::DeleteDelegate() {
76  delete this;
77}
78
79bool UpdateRecommendedMessageBox::IsModal() const {
80  return true;
81}
82
83views::View* UpdateRecommendedMessageBox::GetContentsView() {
84  return message_box_view_;
85}
86
87////////////////////////////////////////////////////////////////////////////////
88// UpdateRecommendedMessageBox, private:
89
90UpdateRecommendedMessageBox::UpdateRecommendedMessageBox(
91    gfx::NativeWindow parent_window) {
92  const int kDialogWidth = 400;
93#if defined(OS_CHROMEOS)
94  const int kProductNameId = IDS_PRODUCT_OS_NAME;
95#else
96  const int kProductNameId = IDS_PRODUCT_NAME;
97#endif
98  const string16 product_name = l10n_util::GetStringUTF16(kProductNameId);
99  // Also deleted when the window closes.
100  message_box_view_ = new views::MessageBoxView(
101      ui::MessageBoxFlags::kFlagHasMessage |
102          ui::MessageBoxFlags::kFlagHasOKButton,
103      UTF16ToWide(l10n_util::GetStringFUTF16(IDS_UPDATE_RECOMMENDED,
104                                             product_name)),
105      std::wstring(),
106      kDialogWidth);
107  browser::CreateViewsWindow(parent_window, gfx::Rect(), this)->Show();
108}
109
110UpdateRecommendedMessageBox::~UpdateRecommendedMessageBox() {
111}
112