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