login_html_dialog.cc revision 731df977c0511bca2206b5f333555b1205ff1f43
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 float kDefaultWidthRatio = 0.8;
21const float kDefaultHeightRatio = 0.8;
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    : style_(style),
49      delegate_(delegate),
50      parent_window_(parent_window),
51      title_(title),
52      url_(url) {
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_, gfx::Rect(), html_view);
69      break;
70    case STYLE_GENERIC:
71    default:
72      views::Window::CreateChromeWindow(parent_window_, gfx::Rect(), html_view);
73      break;
74  }
75  html_view->InitDialog();
76  html_view->window()->Show();
77}
78
79void LoginHtmlDialog::SetDialogSize(int width, int height) {
80  DCHECK(width >= 0 && height >= 0);
81  width_ = width;
82  height_ = height;
83}
84
85///////////////////////////////////////////////////////////////////////////////
86// LoginHtmlDialog, protected:
87
88void LoginHtmlDialog::OnDialogClosed(const std::string& json_retval) {
89  if (delegate_)
90    delegate_->OnDialogClosed();
91}
92
93void LoginHtmlDialog::OnCloseContents(TabContents* source,
94                                      bool* out_close_dialog) {
95  if (out_close_dialog)
96    *out_close_dialog = true;
97}
98
99void LoginHtmlDialog::GetDialogSize(gfx::Size* size) const {
100  size->SetSize(width_, height_);
101}
102
103}  // namespace chromeos
104