login_html_dialog.cc revision 513209b27ff55e2841eac0e4120199c23acce758
1// Copyright (c) 2010 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/login/login_html_dialog.h"
6
7#include "chrome/browser/chromeos/frame/bubble_window.h"
8#include "chrome/browser/chromeos/login/helper.h"
9#include "chrome/browser/profile_manager.h"
10#include "chrome/browser/views/html_dialog_view.h"
11#include "gfx/native_widget_types.h"
12#include "gfx/rect.h"
13#include "gfx/size.h"
14#include "views/window/window.h"
15
16namespace chromeos {
17
18namespace {
19// Default width/height ratio of screen size.
20const double kDefaultWidthRatio = 0.6;
21const double kDefaultHeightRatio = 0.6;
22
23// Custom HtmlDialogView with disabled context menu.
24class HtmlDialogWithoutContextMenuView : public HtmlDialogView {
25 public:
26  HtmlDialogWithoutContextMenuView(Profile* profile,
27                                   HtmlDialogUIDelegate* delegate)
28      : HtmlDialogView(profile, delegate) {}
29  virtual ~HtmlDialogWithoutContextMenuView() {}
30
31  // TabContentsDelegate implementation.
32  bool HandleContextMenu(const ContextMenuParams& params) {
33    // Disable context menu.
34    return true;
35  }
36};
37
38}  // namespace
39
40///////////////////////////////////////////////////////////////////////////////
41// LoginHtmlDialog, public:
42
43LoginHtmlDialog::LoginHtmlDialog(Delegate* delegate,
44                                 gfx::NativeWindow parent_window,
45                                 const std::wstring& title,
46                                 const GURL& url,
47                                 Style style)
48    : delegate_(delegate),
49      parent_window_(parent_window),
50      title_(title),
51      url_(url),
52      style_(style) {
53  gfx::Rect screen_bounds(chromeos::CalculateScreenBounds(gfx::Size()));
54  width_ = static_cast<int>(kDefaultWidthRatio * screen_bounds.width());
55  height_ = static_cast<int>(kDefaultHeightRatio * screen_bounds.height());
56}
57
58LoginHtmlDialog::~LoginHtmlDialog() {
59  delegate_ = NULL;
60}
61
62void LoginHtmlDialog::Show() {
63  HtmlDialogWithoutContextMenuView* html_view =
64      new HtmlDialogWithoutContextMenuView(ProfileManager::GetDefaultProfile(),
65                                           this);
66  switch (style_) {
67    case STYLE_BUBBLE:
68      chromeos::BubbleWindow::Create(parent_window_,
69                                     gfx::Rect(),
70                                     chromeos::BubbleWindow::STYLE_XBAR,
71                                     html_view);
72      break;
73    case STYLE_GENERIC:
74    default:
75      views::Window::CreateChromeWindow(parent_window_,
76                                        gfx::Rect(),
77                                        html_view);
78      break;
79  }
80  html_view->InitDialog();
81  html_view->window()->Show();
82}
83
84void LoginHtmlDialog::SetDialogSize(int width, int height) {
85  DCHECK(width >= 0 && height >= 0);
86  width_ = width;
87  height_ = height;
88}
89
90///////////////////////////////////////////////////////////////////////////////
91// LoginHtmlDialog, protected:
92
93void LoginHtmlDialog::OnDialogClosed(const std::string& json_retval) {
94  if (delegate_)
95    delegate_->OnDialogClosed();
96}
97
98void LoginHtmlDialog::OnCloseContents(TabContents* source,
99                                      bool* out_close_dialog) {
100  if (out_close_dialog)
101    *out_close_dialog = true;
102}
103
104void LoginHtmlDialog::GetDialogSize(gfx::Size* size) const {
105  size->SetSize(width_, height_);
106}
107
108}  // namespace chromeos
109