summaryrefslogtreecommitdiffstats
path: root/ceee/ie/plugin/bho/infobar_window.cc
blob: f7c06c1ecd5e6b04ca9c1acce0f8e5df1a01b52b (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// 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.
//
// Implementation of the manager for infobar windows.

#include "ceee/ie/plugin/bho/infobar_window.h"

#include <atlapp.h>
#include <atlcrack.h>
#include <atlmisc.h>

#include "base/logging.h"
#include "base/scoped_ptr.h"

namespace {

const UINT_PTR kInfobarSlidingTimerId = 1U;
// Interval for sliding the infobar in milliseconds.
const UINT kInfobarSlidingTimerIntervalMs = 50U;
// The step when the infobar is sliding, in pixels.
const int kInfobarSlidingStep = 10;
// The default height of the infobar. See also similar constant in
// ceee/ie/plugin/bho/executor.cc which overrides this one.
const int kInfobarDefaultHeight = 39;

}  // namespace


namespace infobar_api {

InfobarWindow* InfobarWindow::CreateInfobar(InfobarType type,
                                            Delegate* delegate) {
  DCHECK(delegate);
  return NULL == delegate ? NULL : new InfobarWindow(type, delegate);
}

InfobarWindow::InfobarWindow(InfobarType type, Delegate* delegate)
    : type_(type),
      delegate_(delegate),
      show_(false),
      target_height_(1),
      current_height_(1),
      sliding_infobar_(false) {
  DCHECK(delegate);
}

InfobarWindow::~InfobarWindow() {
  Reset();

  if (IsWindow()) {
    DestroyWindow();
  } else {
    NOTREACHED() << "Infobar window was not successfully created.";
  }
}

void InfobarWindow::OnBrowserWindowClose() {
  // Propagate the event to the manager.
  if (delegate_ != NULL)
    delegate_->OnWindowClose(type_);
}

HRESULT InfobarWindow::Show(int max_height, bool slide) {
  if (url_.empty())
    return E_UNEXPECTED;

  StartUpdatingLayout(true, max_height, slide);
  return S_OK;
}

HRESULT InfobarWindow::Hide() {
  // This could be called on the infobar that was not yet shown if show infobar
  // function is called with an empty URL (see CeeeExecutor::ShowInfobar
  // implementation). Therefore we should check if window exists and do not
  // treat this as an error if not.
  if (IsWindow())
    StartUpdatingLayout(false, 0, false);

  return S_OK;
}

HRESULT InfobarWindow::Navigate(const std::wstring& url) {
  // If the CF exists (which means the infobar has already been created) then
  // navigate it. Otherwise just store the URL, it will be passed to the CF when
  // it will be created.
  url_ = url;
  if (chrome_frame_host_)
    chrome_frame_host_->SetUrl(CComBSTR(url_.c_str()));
  return S_OK;
}

void InfobarWindow::ReserveSpace(RECT* rect) {
  DCHECK(rect);
  if (rect == NULL || !show_)
    return;

  switch (type_) {
    case TOP_INFOBAR:
      rect->top += current_height_;
      if (rect->top > rect->bottom)
        rect->top = rect->bottom;
      break;
    case BOTTOM_INFOBAR:
      rect->bottom -= current_height_;
      if (rect->bottom < rect->top)
        rect->bottom = rect->top;
      break;
    default:
      NOTREACHED() << "Unknown InfobarType value.";
      break;
  }
}

void InfobarWindow::UpdatePosition() {
  // Make infobar be consistent with IE window's size.
  // NOTE: Even if currently it is not visible, we still need to update its
  // position, since the contents may need to decide its layout based on the
  // width of the infobar.

  CRect rect = CalculatePosition();
  if (IsWindow())
    MoveWindow(&rect, TRUE);
}

void InfobarWindow::Reset() {
  Hide();

  if (chrome_frame_host_)
    chrome_frame_host_->Teardown();

  DCHECK(!show_ && !sliding_infobar_);
  if (m_hWnd != NULL) {
    DestroyWindow();
    m_hWnd = NULL;
  }
  url_.clear();
  chrome_frame_host_.Release();
}

void InfobarWindow::StartUpdatingLayout(bool show, int max_height, bool slide) {
  if (!IsWindow()) {
    LOG(ERROR) << "Updating infobar layout when window has not been created";
    return;
  }

  show_ = show;
  if (show) {
    int html_content_height = kInfobarDefaultHeight;
    CSize html_content_size(0, 0);
    if (SUCCEEDED(GetContentSize(&html_content_size)) &&
        html_content_size.cy > 0) {
      html_content_height = html_content_size.cy;
    }
    target_height_ = (max_height == 0 || html_content_height < max_height) ?
        html_content_height : max_height;
    if (target_height_ <= 0) {
      target_height_ = 1;
    }
  } else {
    target_height_ = 1;
  }

  if (!slide || !show) {
    current_height_ = target_height_;

    if (sliding_infobar_) {
      KillTimer(kInfobarSlidingTimerId);
      sliding_infobar_ = false;
    }
  } else {
    // If the infobar is visible and sliding effect is requested, we need to
    // start expanding/shrinking the infobar according to its current height.
    current_height_ = CalculateNextHeight();

    if (!sliding_infobar_) {
      SetTimer(kInfobarSlidingTimerId, kInfobarSlidingTimerIntervalMs, NULL);
      sliding_infobar_ = true;
    }
  }

  UpdateLayout();
}

int InfobarWindow::CalculateNextHeight() {
  if (current_height_ < target_height_) {
    return std::min(current_height_ + kInfobarSlidingStep, target_height_);
  } else if (current_height_ > target_height_) {
    return std::max(current_height_ - kInfobarSlidingStep, target_height_);
  } else {
    return current_height_;
  }
}

RECT InfobarWindow::CalculatePosition() {
  CRect rect(0, 0, 0, 0);

  if (NULL == delegate_)
    return rect;
  HWND container_window = delegate_->GetContainerWindow();
  if (container_window == NULL || !::IsWindow(container_window))
    return rect;
  HWND container_parent_window = ::GetParent(container_window);
  if (!::IsWindow(container_parent_window))
    return rect;

  ::GetWindowRect(container_window, &rect);
  ::MapWindowPoints(NULL, container_parent_window,
                    reinterpret_cast<POINT*>(&rect), 2);

  switch (type_) {
    case TOP_INFOBAR:
      if (rect.top + current_height_ < rect.bottom)
        rect.bottom = rect.top + current_height_;
      break;
    case BOTTOM_INFOBAR:
      if (rect.bottom - current_height_ > rect.top)
        rect.top = rect.bottom - current_height_;
      break;
    default:
      NOTREACHED() << "Unknown InfobarType value.";
      break;
  }
  return rect;
}

void InfobarWindow::UpdateLayout() {
  CRect rect = CalculatePosition();
  if (IsWindow()) {
    // Set infobar's z-order, place it at the top, so that it won't be hidden by
    // IE window.
    SetWindowPos(HWND_TOP, &rect, show_ ? SWP_SHOWWINDOW : SWP_HIDEWINDOW);
  }

  HWND container_window = NULL;
  if (delegate_ != NULL)
    container_window = delegate_->GetContainerWindow();
  if (container_window != NULL && ::IsWindow(container_window)) {
    // Call SetWindowPos with SWP_FRAMECHANGED for IE window, then IE
    // window would receive WM_NCCALCSIZE to recalculate its client size.
    ::SetWindowPos(container_window,
                   NULL, 0, 0, 0, 0,
                   SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
                   SWP_FRAMECHANGED);
  }
}

HRESULT InfobarWindow::GetContentSize(SIZE* size) {
  DCHECK(size);
  if (NULL == size)
    return E_POINTER;

  // Set the size to 0 that means we do not know it.
  // TODO(vadimb@google.com): Find how to get the content size from the CF.
  size->cx = 0;
  size->cy = 0;
  return S_OK;
}

LRESULT InfobarWindow::OnTimer(UINT_PTR nIDEvent) {
  DCHECK(nIDEvent == kInfobarSlidingTimerId);
  if (show_ && sliding_infobar_ && current_height_ != target_height_) {
    current_height_ = CalculateNextHeight();
    UpdateLayout();
  } else if (sliding_infobar_) {
    KillTimer(kInfobarSlidingTimerId);
    sliding_infobar_ = false;
  }

  return S_OK;
}

LRESULT InfobarWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) {
  InitializingCoClass<InfobarBrowserWindow>::CreateInitialized(
      CComBSTR(url_.c_str()), this, &chrome_frame_host_);

  if (chrome_frame_host_) {
    chrome_frame_host_->CreateAndShowWindow(m_hWnd);
    AdjustSize();
  }
  return S_OK;
}

void InfobarWindow::OnPaint(CDCHandle dc) {
  RECT rc;
  if (GetUpdateRect(&rc, FALSE)) {
    PAINTSTRUCT ps = {};
    BeginPaint(&ps);

    BOOL ret = GetClientRect(&rc);
    DCHECK(ret);
    FillRect(ps.hdc, &rc, (HBRUSH)GetStockObject(GRAY_BRUSH));
    ::DrawText(ps.hdc, L"Google CEEE. No Chrome Frame found!", -1,
               &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

    EndPaint(&ps);
  }
}

void InfobarWindow::OnSize(UINT type, CSize size) {
  AdjustSize();
}

void InfobarWindow::AdjustSize() {
  if (NULL != chrome_frame_host_) {
    CRect rect;
    GetClientRect(&rect);
    chrome_frame_host_->SetWindowSize(rect.Width(), rect.Height());
  }
}

}  // namespace infobar_api