constrained_html_delegate_win.cc revision dc0f95d653279beabeb9817299e2902918ba123e
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/ui/webui/constrained_html_ui.h"
6
7#include "chrome/browser/profiles/profile.h"
8#include "chrome/browser/ui/views/tab_contents/tab_contents_container.h"
9#include "chrome/browser/ui/webui/html_dialog_tab_contents_delegate.h"
10#include "chrome/browser/ui/webui/html_dialog_ui.h"
11#include "content/browser/tab_contents/tab_contents.h"
12#include "ui/gfx/rect.h"
13#include "views/view.h"
14#include "views/widget/widget_win.h"
15#include "views/window/window_delegate.h"
16
17class ConstrainedHtmlDelegateWin : public TabContentsContainer,
18                                   public ConstrainedHtmlUIDelegate,
19                                   public ConstrainedWindowDelegate,
20                                   public HtmlDialogTabContentsDelegate {
21 public:
22  ConstrainedHtmlDelegateWin(Profile* profile,
23                             HtmlDialogUIDelegate* delegate);
24  ~ConstrainedHtmlDelegateWin();
25
26  // ConstrainedHtmlUIDelegate interface.
27  virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate();
28  virtual void OnDialogClose();
29
30  // ConstrainedWindowDelegate (aka views::WindowDelegate) interface.
31  virtual bool CanResize() const { return true; }
32  virtual views::View* GetContentsView() {
33    return this;
34  }
35  virtual void WindowClosing() {
36    html_delegate_->OnDialogClosed("");
37  }
38
39  // HtmlDialogTabContentsDelegate interface.
40  void MoveContents(TabContents* source, const gfx::Rect& pos) {}
41  void ToolbarSizeChanged(TabContents* source, bool is_animating) {}
42  void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {}
43
44  // Overridden from TabContentsContainer.
45  virtual gfx::Size GetPreferredSize() {
46    gfx::Size size;
47    html_delegate_->GetDialogSize(&size);
48    return size;
49  }
50
51  virtual void ViewHierarchyChanged(bool is_add,
52                                    views::View* parent,
53                                    views::View* child) {
54    TabContentsContainer::ViewHierarchyChanged(is_add, parent, child);
55    if (is_add && child == this) {
56      ChangeTabContents(&html_tab_contents_);
57    }
58  }
59
60  void set_window(ConstrainedWindow* window) {
61    window_ = window;
62  }
63
64 private:
65  TabContents html_tab_contents_;
66
67  HtmlDialogUIDelegate* html_delegate_;
68
69  // The constrained window that owns |this|.  Saved so we can close it later.
70  ConstrainedWindow* window_;
71};
72
73ConstrainedHtmlDelegateWin::ConstrainedHtmlDelegateWin(
74    Profile* profile,
75    HtmlDialogUIDelegate* delegate)
76    : HtmlDialogTabContentsDelegate(profile),
77      html_tab_contents_(profile, NULL, MSG_ROUTING_NONE, NULL, NULL),
78      html_delegate_(delegate),
79      window_(NULL) {
80  CHECK(delegate);
81  html_tab_contents_.set_delegate(this);
82
83  // Set |this| as a property so the ConstrainedHtmlUI can retrieve it.
84  ConstrainedHtmlUI::GetPropertyAccessor().SetProperty(
85      html_tab_contents_.property_bag(), this);
86  html_tab_contents_.controller().LoadURL(delegate->GetDialogContentURL(),
87                                          GURL(),
88                                          PageTransition::START_PAGE);
89}
90
91ConstrainedHtmlDelegateWin::~ConstrainedHtmlDelegateWin() {
92}
93
94HtmlDialogUIDelegate* ConstrainedHtmlDelegateWin::GetHtmlDialogUIDelegate() {
95  return html_delegate_;
96}
97
98void ConstrainedHtmlDelegateWin::OnDialogClose() {
99  window_->CloseConstrainedWindow();
100}
101
102// static
103void ConstrainedHtmlUI::CreateConstrainedHtmlDialog(
104    Profile* profile,
105    HtmlDialogUIDelegate* delegate,
106    TabContents* container) {
107  ConstrainedHtmlDelegateWin* constrained_delegate =
108      new ConstrainedHtmlDelegateWin(profile, delegate);
109  ConstrainedWindow* constrained_window =
110      container->CreateConstrainedDialog(constrained_delegate);
111  constrained_delegate->set_window(constrained_window);
112}
113