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/login/login_html_dialog.h"
6
7#include "chrome/browser/chromeos/frame/bubble_frame_view.h"
8#include "chrome/browser/chromeos/frame/bubble_window.h"
9#include "chrome/browser/chromeos/login/helper.h"
10#include "chrome/browser/profiles/profile_manager.h"
11#include "chrome/browser/ui/views/html_dialog_view.h"
12#include "content/common/notification_service.h"
13#include "ui/gfx/native_widget_types.h"
14#include "ui/gfx/rect.h"
15#include "ui/gfx/size.h"
16#include "views/window/window.h"
17
18namespace chromeos {
19
20namespace {
21// Default width/height ratio of screen size.
22const double kDefaultWidthRatio = 0.6;
23const double kDefaultHeightRatio = 0.6;
24
25// Custom HtmlDialogView with disabled context menu.
26class HtmlDialogWithoutContextMenuView : public HtmlDialogView {
27 public:
28  HtmlDialogWithoutContextMenuView(Profile* profile,
29                                   HtmlDialogUIDelegate* delegate)
30      : HtmlDialogView(profile, delegate) {}
31  virtual ~HtmlDialogWithoutContextMenuView() {}
32
33  // TabContentsDelegate implementation.
34  bool HandleContextMenu(const ContextMenuParams& params) {
35    // Disable context menu.
36    return true;
37  }
38};
39
40}  // namespace
41
42///////////////////////////////////////////////////////////////////////////////
43// LoginHtmlDialog, public:
44
45LoginHtmlDialog::LoginHtmlDialog(Delegate* delegate,
46                                 gfx::NativeWindow parent_window,
47                                 const std::wstring& title,
48                                 const GURL& url,
49                                 Style style)
50    : delegate_(delegate),
51      parent_window_(parent_window),
52      title_(title),
53      url_(url),
54      style_(style),
55      bubble_frame_view_(NULL),
56      is_open_(false) {
57  gfx::Rect screen_bounds(chromeos::CalculateScreenBounds(gfx::Size()));
58  width_ = static_cast<int>(kDefaultWidthRatio * screen_bounds.width());
59  height_ = static_cast<int>(kDefaultHeightRatio * screen_bounds.height());
60}
61
62LoginHtmlDialog::~LoginHtmlDialog() {
63  delegate_ = NULL;
64}
65
66void LoginHtmlDialog::Show() {
67  HtmlDialogWithoutContextMenuView* html_view =
68      new HtmlDialogWithoutContextMenuView(ProfileManager::GetDefaultProfile(),
69                                           this);
70  if (style_ & STYLE_BUBBLE) {
71    views::Window* bubble_window = BubbleWindow::Create(
72        parent_window_, gfx::Rect(),
73        static_cast<BubbleWindow::Style>(
74            BubbleWindow::STYLE_XBAR | BubbleWindow::STYLE_THROBBER),
75        html_view);
76    bubble_frame_view_ = static_cast<BubbleFrameView*>(
77        bubble_window->non_client_view()->frame_view());
78  } else {
79    views::Window::CreateChromeWindow(parent_window_, gfx::Rect(), html_view);
80  }
81  if (bubble_frame_view_) {
82    bubble_frame_view_->StartThrobber();
83    notification_registrar_.Add(this,
84                                NotificationType::LOAD_COMPLETED_MAIN_FRAME,
85                                NotificationService::AllSources());
86  }
87  html_view->InitDialog();
88  html_view->window()->Show();
89  is_open_ = true;
90}
91
92void LoginHtmlDialog::SetDialogSize(int width, int height) {
93  DCHECK(width >= 0 && height >= 0);
94  width_ = width;
95  height_ = height;
96}
97
98void LoginHtmlDialog::Observe(NotificationType type,
99                              const NotificationSource& source,
100                              const NotificationDetails& details) {
101  DCHECK(type.value == NotificationType::LOAD_COMPLETED_MAIN_FRAME);
102  if (bubble_frame_view_)
103    bubble_frame_view_->StopThrobber();
104}
105
106///////////////////////////////////////////////////////////////////////////////
107// LoginHtmlDialog, protected:
108
109void LoginHtmlDialog::OnDialogClosed(const std::string& json_retval) {
110  is_open_ = false;
111  notification_registrar_.RemoveAll();
112  if (delegate_)
113    delegate_->OnDialogClosed();
114}
115
116void LoginHtmlDialog::OnCloseContents(TabContents* source,
117                                      bool* out_close_dialog) {
118  if (out_close_dialog)
119    *out_close_dialog = true;
120}
121
122void LoginHtmlDialog::GetDialogSize(gfx::Size* size) const {
123  size->SetSize(width_, height_);
124}
125
126}  // namespace chromeos
127