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/chromeos/sim_dialog_delegate.h"
6
7#include "base/stringprintf.h"
8#include "chrome/browser/chromeos/frame/bubble_window.h"
9#include "chrome/browser/chromeos/login/user_manager.h"
10#include "chrome/browser/profiles/profile_manager.h"
11#include "chrome/browser/ui/browser.h"
12#include "chrome/browser/ui/browser_list.h"
13#include "chrome/browser/ui/views/html_dialog_view.h"
14#include "chrome/common/url_constants.h"
15
16namespace {
17
18// Default width/height of the dialog.
19const int kDefaultWidth = 350;
20const int kDefaultHeight = 225;
21
22// Width/height for the change PIN dialog mode.
23const int kChangePinWidth = 350;
24const int kChangePinHeight = 245;
25
26// URL that includes additional mode (other than Unlock flow) that we're using
27// dialog for. Possible values:
28// change-pin   - use dialog to change PIN, ask for old & new PIN.
29// set-lock-on  - enable RequirePin restriction.
30// set-lock-off - disable RequirePin restriction.
31// In general SIM unlock case sim-unlock URL is loaded w/o parameters.
32const char kSimDialogSpecialModeURL[] = "chrome://sim-unlock/?mode=%s";
33
34// Dialog mode constants.
35const char kSimDialogChangePinMode[]  = "change-pin";
36const char kSimDialogSetLockOnMode[]  = "set-lock-on";
37const char kSimDialogSetLockOffMode[] = "set-lock-off";
38
39// Custom HtmlDialogView with disabled context menu.
40class HtmlDialogWithoutContextMenuView : public HtmlDialogView {
41 public:
42  HtmlDialogWithoutContextMenuView(Profile* profile,
43                                   HtmlDialogUIDelegate* delegate)
44      : HtmlDialogView(profile, delegate) {}
45
46  // TabContentsDelegate implementation.
47  bool HandleContextMenu(const ContextMenuParams& params) {
48    // Disable context menu.
49    return true;
50  }
51};
52
53}  // namespace
54
55namespace chromeos {
56
57// static
58void SimDialogDelegate::ShowDialog(gfx::NativeWindow owning_window,
59                                         SimDialogMode mode) {
60  Profile* profile;
61  if (UserManager::Get()->user_is_logged_in()) {
62    Browser* browser = BrowserList::GetLastActive();
63    DCHECK(browser);
64    profile = browser->profile();
65  } else {
66    profile = ProfileManager::GetDefaultProfile();
67  }
68  HtmlDialogView* html_view = new HtmlDialogWithoutContextMenuView(
69      profile, new SimDialogDelegate(mode));
70  html_view->InitDialog();
71  chromeos::BubbleWindow::Create(owning_window,
72                                 gfx::Rect(),
73                                 chromeos::BubbleWindow::STYLE_GENERIC,
74                                 html_view);
75  html_view->window()->Show();
76}
77
78SimDialogDelegate::SimDialogDelegate(SimDialogMode dialog_mode)
79    : dialog_mode_(dialog_mode) {
80}
81
82SimDialogDelegate::~SimDialogDelegate() {
83}
84
85bool SimDialogDelegate::IsDialogModal() const {
86  return true;
87}
88
89std::wstring SimDialogDelegate::GetDialogTitle() const {
90  return std::wstring();
91}
92
93GURL SimDialogDelegate::GetDialogContentURL() const {
94  if (dialog_mode_ == SIM_DIALOG_UNLOCK) {
95    std::string url_string(chrome::kChromeUISimUnlockURL);
96    return GURL(url_string);
97  } else {
98    std::string mode_value;
99    if (dialog_mode_ == SIM_DIALOG_CHANGE_PIN)
100      mode_value = kSimDialogChangePinMode;
101    else if (dialog_mode_ == SIM_DIALOG_SET_LOCK_ON)
102      mode_value = kSimDialogSetLockOnMode;
103    else
104      mode_value = kSimDialogSetLockOffMode;
105    return GURL(StringPrintf(kSimDialogSpecialModeURL, mode_value.c_str()));
106  }
107}
108
109void SimDialogDelegate::GetWebUIMessageHandlers(
110    std::vector<WebUIMessageHandler*>* handlers) const {
111}
112
113void SimDialogDelegate::GetDialogSize(gfx::Size* size) const {
114  // TODO(nkostylev): Set custom size based on locale settings.
115  if (dialog_mode_ == SIM_DIALOG_CHANGE_PIN)
116    size->SetSize(kChangePinWidth , kChangePinHeight);
117  else
118    size->SetSize(kDefaultWidth, kDefaultHeight);
119}
120
121std::string SimDialogDelegate::GetDialogArgs() const {
122  return "[]";
123}
124
125void SimDialogDelegate::OnDialogClosed(const std::string& json_retval) {
126  delete this;
127}
128
129void SimDialogDelegate::OnCloseContents(TabContents* source,
130                                              bool* out_close_dialog) {
131  if (out_close_dialog)
132    *out_close_dialog = true;
133}
134
135bool SimDialogDelegate::ShouldShowDialogTitle() const {
136  return false;
137}
138
139}  // namespace chromeos
140