1 // Copyright 2013 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.
5 #include "chrome_frame/turndown_prompt/turndown_prompt_content.h"
7 #include "base/logging.h"
8 #include "chrome_frame/ready_mode/internal/url_launcher.h"
9 #include "chrome_frame/turndown_prompt/turndown_prompt_window.h"
11 TurndownPromptContent::TurndownPromptContent(
12 UrlLauncher
* url_launcher
,
13 const base::Closure
& uninstall_closure
)
14 : url_launcher_(url_launcher
),
15 uninstall_closure_(uninstall_closure
) {
18 TurndownPromptContent::~TurndownPromptContent() {
19 if (window_
!= NULL
&& window_
->IsWindow()) {
20 // The window must discard its ContentFrame pointer at this time.
21 window_
->DestroyWindow();
26 bool TurndownPromptContent::InstallInFrame(Frame
* frame
) {
27 DCHECK(window_
== NULL
);
28 DCHECK(url_launcher_
!= NULL
);
30 // Pass ownership of our url_launcher_ to the window.
31 window_
= TurndownPromptWindow::CreateInstance(
32 frame
, url_launcher_
.release(), uninstall_closure_
);
33 uninstall_closure_
.Reset();
35 return window_
!= NULL
;
38 void TurndownPromptContent::SetDimensions(const RECT
& dimensions
) {
39 if (window_
!= NULL
&& window_
->IsWindow()) {
40 window_
->SetWindowPos(HWND_TOP
, &dimensions
,
41 ::IsRectEmpty(&dimensions
) ? SWP_HIDEWINDOW
:
46 bool GetDialogTemplateDimensions(TurndownPromptWindow
* window
,
48 HRSRC resource
= ::FindResource(_AtlBaseModule
.GetResourceInstance(),
49 MAKEINTRESOURCE(TurndownPromptWindow::IDD
),
52 HGLOBAL handle
= NULL
;
53 DLGTEMPLATE
* dlgtemplate
= NULL
;
54 _DialogSplitHelper::DLGTEMPLATEEX
* dlgtemplateex
= NULL
;
56 if (resource
== NULL
) {
57 DPLOG(ERROR
) << "Failed to find resource for TurndownPromptWindow::IDD";
61 handle
= ::LoadResource(_AtlBaseModule
.GetResourceInstance(), resource
);
64 DPLOG(ERROR
) << "Failed to load resource for TurndownPromptWindow::IDD";
68 dlgtemplate
= reinterpret_cast<DLGTEMPLATE
*>(::LockResource(handle
));
69 if (dlgtemplate
== NULL
) {
70 DPLOG(ERROR
) << "Failed to lock resource for TurndownPromptWindow::IDD";
74 if (!_DialogSplitHelper::IsDialogEx(dlgtemplate
)) {
75 DLOG(ERROR
) << "Resource TurndownPromptWindow::IDD is not a DLGTEMPLATEEX";
80 reinterpret_cast<_DialogSplitHelper::DLGTEMPLATEEX
*>(dlgtemplate
);
82 RECT dlgdimensions
= {0, 0, dlgtemplateex
->cx
, dlgtemplateex
->cy
};
83 if (!window
->MapDialogRect(&dlgdimensions
)) {
84 DPLOG(ERROR
) << "Failure in MapDialogRect";
88 *dimensions
= dlgdimensions
;
92 size_t TurndownPromptContent::GetDesiredSize(size_t width
, size_t height
) {
93 DCHECK(window_
!= NULL
&& window_
->IsWindow());
95 if (window_
== NULL
|| !window_
->IsWindow()) {
98 RECT dialog_dimensions
= {0, 0, 0, 0};
100 if (GetDialogTemplateDimensions(window_
.get(), &dialog_dimensions
))
101 return width
== 0 ? dialog_dimensions
.right
: dialog_dimensions
.bottom
;
103 return width
== 0 ? 0 : 39;