- add sources.
[platform/framework/web/crosswalk.git] / src / chrome_frame / turndown_prompt / turndown_prompt_content.cc
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.
4
5 #include "chrome_frame/turndown_prompt/turndown_prompt_content.h"
6
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"
10
11 TurndownPromptContent::TurndownPromptContent(
12     UrlLauncher* url_launcher,
13     const base::Closure& uninstall_closure)
14     : url_launcher_(url_launcher),
15       uninstall_closure_(uninstall_closure) {
16 }
17
18 TurndownPromptContent::~TurndownPromptContent() {
19   if (window_ != NULL && window_->IsWindow()) {
20     // The window must discard its ContentFrame pointer at this time.
21     window_->DestroyWindow();
22     window_.reset();
23   }
24 }
25
26 bool TurndownPromptContent::InstallInFrame(Frame* frame) {
27   DCHECK(window_ == NULL);
28   DCHECK(url_launcher_ != NULL);
29
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();
34
35   return window_ != NULL;
36 }
37
38 void TurndownPromptContent::SetDimensions(const RECT& dimensions) {
39   if (window_ != NULL && window_->IsWindow()) {
40     window_->SetWindowPos(HWND_TOP, &dimensions,
41                           ::IsRectEmpty(&dimensions) ? SWP_HIDEWINDOW :
42                                                        SWP_SHOWWINDOW);
43   }
44 }
45
46 bool GetDialogTemplateDimensions(TurndownPromptWindow* window,
47                                  RECT* dimensions) {
48   HRSRC resource = ::FindResource(_AtlBaseModule.GetResourceInstance(),
49                                   MAKEINTRESOURCE(TurndownPromptWindow::IDD),
50                                   RT_DIALOG);
51
52   HGLOBAL handle = NULL;
53   DLGTEMPLATE* dlgtemplate = NULL;
54   _DialogSplitHelper::DLGTEMPLATEEX* dlgtemplateex = NULL;
55
56   if (resource == NULL) {
57     DPLOG(ERROR) << "Failed to find resource for TurndownPromptWindow::IDD";
58     return false;
59   }
60
61   handle = ::LoadResource(_AtlBaseModule.GetResourceInstance(), resource);
62
63   if (handle == NULL) {
64     DPLOG(ERROR) << "Failed to load resource for TurndownPromptWindow::IDD";
65     return false;
66   }
67
68   dlgtemplate = reinterpret_cast<DLGTEMPLATE*>(::LockResource(handle));
69   if (dlgtemplate == NULL) {
70     DPLOG(ERROR) << "Failed to lock resource for TurndownPromptWindow::IDD";
71     return false;
72   }
73
74   if (!_DialogSplitHelper::IsDialogEx(dlgtemplate)) {
75     DLOG(ERROR) << "Resource TurndownPromptWindow::IDD is not a DLGTEMPLATEEX";
76     return false;
77   }
78
79   dlgtemplateex =
80       reinterpret_cast<_DialogSplitHelper::DLGTEMPLATEEX*>(dlgtemplate);
81
82   RECT dlgdimensions = {0, 0, dlgtemplateex->cx, dlgtemplateex->cy};
83   if (!window->MapDialogRect(&dlgdimensions)) {
84     DPLOG(ERROR) << "Failure in MapDialogRect";
85     return false;
86   }
87
88   *dimensions = dlgdimensions;
89   return true;
90 }
91
92 size_t TurndownPromptContent::GetDesiredSize(size_t width, size_t height) {
93   DCHECK(window_ != NULL && window_->IsWindow());
94
95   if (window_ == NULL || !window_->IsWindow()) {
96     return 0;
97   }
98   RECT dialog_dimensions = {0, 0, 0, 0};
99
100   if (GetDialogTemplateDimensions(window_.get(), &dialog_dimensions))
101     return width == 0 ? dialog_dimensions.right : dialog_dimensions.bottom;
102   else
103     return width == 0 ? 0 : 39;
104 }