summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/constrained_html_dialog_win.cc
blob: 2cdc81c2f3205487951c20def2407eebc68c5bcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/views/constrained_html_dialog_win.h"

#include "chrome/browser/dom_ui/html_dialog_ui.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/renderer_host/render_view_host_delegate.h"
#include "chrome/browser/renderer_host/render_widget_host_view_win.h"
#include "chrome/browser/renderer_host/site_instance.h"
#include "chrome/common/bindings_policy.h"
#include "chrome/common/renderer_preferences.h"
#include "views/widget/widget_win.h"
#include "views/window/window_delegate.h"

class ConstrainedHtmlDialogHostView : public views::NativeViewHost {
 public:
  explicit ConstrainedHtmlDialogHostView(ConstrainedHtmlDialogWin* dialog)
      : dialog_(dialog),
        initialized_(false) {
  }

  virtual void ViewHierarchyChanged(bool is_add,
                                    views::View* parent,
                                    views::View* child) {
    NativeViewHost::ViewHierarchyChanged(is_add, parent, child);
    if (is_add && GetWidget() && !initialized_) {
      dialog_->Init(GetWidget()->GetNativeView());
      initialized_ = true;
    }
  }

 private:
  ConstrainedHtmlDialogWin* dialog_;
  bool initialized_;
};

ConstrainedHtmlDialogWin::ConstrainedHtmlDialogWin(
    Profile* profile,
    HtmlDialogUIDelegate* delegate)
    : ConstrainedHtmlDialog(profile, delegate) {
  CHECK(delegate);
  native_view_host_ = new ConstrainedHtmlDialogHostView(this);

  // Size the native view to match the delegate preferred size.
  gfx::Size size;
  delegate->GetDialogSize(&size);
  native_view_host_->SetPreferredSize(size);

  // Create a site instance for the correct URL.
  dialog_url_ = delegate->GetDialogContentURL();
  site_instance_ =
      SiteInstance::CreateSiteInstanceForURL(profile, dialog_url_);
}

ConstrainedHtmlDialogWin::~ConstrainedHtmlDialogWin() {
}

ConstrainedWindowDelegate*
ConstrainedHtmlDialogWin::GetConstrainedWindowDelegate() {
  return this;
}

bool ConstrainedHtmlDialogWin::CanResize() const {
  return true;
}

views::View* ConstrainedHtmlDialogWin::GetContentsView() {
  return native_view_host_;
}

void ConstrainedHtmlDialogWin::WindowClosing() {
  html_dialog_ui_delegate()->OnDialogClosed("");
}

int ConstrainedHtmlDialogWin::GetBrowserWindowID() const {
  return 0;
}

ViewType::Type ConstrainedHtmlDialogWin::GetRenderViewType() const {
  return ViewType::HTML_DIALOG_UI;
}

RendererPreferences ConstrainedHtmlDialogWin::GetRendererPrefs(
    Profile* profile) const {
  return RendererPreferences();
}

void ConstrainedHtmlDialogWin::ProcessDOMUIMessage(
    const ViewHostMsg_DomMessage_Params& params) {
  DOMUI::ProcessDOMUIMessage(params);
}

void ConstrainedHtmlDialogWin::Init(gfx::NativeView parent_view) {
  render_view_host_.reset(new RenderViewHost(site_instance_,
                                             this,
                                             MSG_ROUTING_NONE,
                                             NULL));
  render_view_host_->AllowBindings(BindingsPolicy::DOM_UI);
  render_widget_host_view_ =
      new RenderWidgetHostViewWin(render_view_host_.get());
  render_view_host_->set_view(render_widget_host_view_);
  render_view_host_->CreateRenderView(string16());
  render_view_host_->NavigateToURL(dialog_url_);
  HWND hwnd = render_widget_host_view_->Create(parent_view);
  render_widget_host_view_->ShowWindow(SW_SHOW);
  native_view_host_->Attach(hwnd);

  InitializeDOMUI(render_view_host_.get());
}

// static
ConstrainedHtmlDialog* ConstrainedHtmlDialog::CreateConstrainedHTMLDialog(
    Profile* profile, HtmlDialogUIDelegate* delegate) {
  return new ConstrainedHtmlDialogWin(profile, delegate);
}