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/chromeos/sim_dialog_delegate.h"
6
7#include "base/strings/stringprintf.h"
8#include "chrome/browser/profiles/profile_manager.h"
9#include "chrome/browser/ui/browser_dialogs.h"
10#include "chrome/common/url_constants.h"
11#include "ui/gfx/size.h"
12
13using content::WebContents;
14using content::WebUIMessageHandler;
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// Dialog mode constants.
27const char kSimDialogChangePinMode[]  = "change-pin";
28const char kSimDialogSetLockOnMode[]  = "set-lock-on";
29const char kSimDialogSetLockOffMode[] = "set-lock-off";
30
31}  // namespace
32
33namespace chromeos {
34
35// static
36void SimDialogDelegate::ShowDialog(gfx::NativeWindow owning_window,
37                                   SimDialogMode mode) {
38  chrome::ShowWebDialog(owning_window,
39                        ProfileManager::GetActiveUserProfile(),
40                        new SimDialogDelegate(mode));
41}
42
43SimDialogDelegate::SimDialogDelegate(SimDialogMode dialog_mode)
44    : dialog_mode_(dialog_mode) {
45}
46
47SimDialogDelegate::~SimDialogDelegate() {
48}
49
50ui::ModalType SimDialogDelegate::GetDialogModalType() const {
51  return ui::MODAL_TYPE_SYSTEM;
52}
53
54base::string16 SimDialogDelegate::GetDialogTitle() const {
55  return base::string16();
56}
57
58GURL SimDialogDelegate::GetDialogContentURL() const {
59  if (dialog_mode_ == SIM_DIALOG_UNLOCK) {
60    std::string url_string(chrome::kChromeUISimUnlockURL);
61    return GURL(url_string);
62  } else {
63    std::string mode_value;
64    if (dialog_mode_ == SIM_DIALOG_CHANGE_PIN)
65      mode_value = kSimDialogChangePinMode;
66    else if (dialog_mode_ == SIM_DIALOG_SET_LOCK_ON)
67      mode_value = kSimDialogSetLockOnMode;
68    else
69      mode_value = kSimDialogSetLockOffMode;
70
71    // Create a URL that includes an additional mode (other than Unlock flow).
72    // Possible values for mode are:
73    // change-pin   - use dialog to change PIN, ask for old & new PIN.
74    // set-lock-on  - enable RequirePin restriction.
75    // set-lock-off - disable RequirePin restriction.
76    std::string url_string =
77        std::string(chrome::kChromeUISimUnlockURL) + "?mode=" + mode_value;
78    return GURL(url_string);
79  }
80}
81
82void SimDialogDelegate::GetWebUIMessageHandlers(
83    std::vector<WebUIMessageHandler*>* handlers) const {
84}
85
86void SimDialogDelegate::GetDialogSize(gfx::Size* size) const {
87  // TODO(nkostylev): Set custom size based on locale settings.
88  if (dialog_mode_ == SIM_DIALOG_CHANGE_PIN)
89    size->SetSize(kChangePinWidth , kChangePinHeight);
90  else
91    size->SetSize(kDefaultWidth, kDefaultHeight);
92}
93
94std::string SimDialogDelegate::GetDialogArgs() const {
95  return "[]";
96}
97
98void SimDialogDelegate::OnDialogClosed(const std::string& json_retval) {
99  delete this;
100}
101
102void SimDialogDelegate::OnCloseContents(WebContents* source,
103                                        bool* out_close_dialog) {
104  if (out_close_dialog)
105    *out_close_dialog = true;
106}
107
108bool SimDialogDelegate::ShouldShowDialogTitle() const {
109  return false;
110}
111
112bool SimDialogDelegate::HandleContextMenu(
113    const content::ContextMenuParams& params) {
114  // Disable context menu.
115  return true;
116}
117
118}  // namespace chromeos
119