-
Notifications
You must be signed in to change notification settings - Fork 467
/
root_window_views.cc
562 lines (466 loc) · 15.4 KB
/
root_window_views.cc
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// Copyright (c) 2016 The Chromium Embedded Framework 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 "tests/cefclient/browser/root_window_views.h"
#include <memory>
#include "include/base/cef_build.h"
#include "include/base/cef_callback.h"
#include "include/cef_app.h"
#include "include/wrapper/cef_helpers.h"
#include "tests/cefclient/browser/client_handler_std.h"
#include "tests/cefclient/browser/client_prefs.h"
namespace client {
namespace {
// Images that are loaded early and cached.
static const char* kDefaultImageCache[] = {"menu_icon", "window_icon"};
} // namespace
RootWindowViews::RootWindowViews(bool use_alloy_style)
: RootWindow(use_alloy_style) {}
RootWindowViews::~RootWindowViews() {
REQUIRE_MAIN_THREAD();
}
void RootWindowViews::SetTitlebarHeight(const std::optional<float>& height) {
if (window_) {
window_->SetTitlebarHeight(height);
}
}
void RootWindowViews::Init(RootWindow::Delegate* delegate,
std::unique_ptr<RootWindowConfig> config,
const CefBrowserSettings& settings) {
DCHECK(delegate);
DCHECK(config->command_line);
DCHECK(!config->with_osr); // Windowless rendering is not supported.
DCHECK(!initialized_);
delegate_ = delegate;
config_ = std::move(config);
CreateClientHandler(config_->url);
initialized_ = true;
delegate_->GetRequestContext(base::BindOnce(
[](scoped_refptr<RootWindowViews> self,
const CefBrowserSettings& settings,
CefRefPtr<CefRequestContext> request_context) {
// Continue initialization on the UI thread.
self->InitOnUIThread(settings, request_context);
},
scoped_refptr<RootWindowViews>(this), settings));
}
void RootWindowViews::InitAsPopup(RootWindow::Delegate* delegate,
bool with_controls,
bool with_osr,
const CefPopupFeatures& popupFeatures,
CefWindowInfo& windowInfo,
CefRefPtr<CefClient>& client,
CefBrowserSettings& settings) {
CEF_REQUIRE_UI_THREAD();
DCHECK(delegate);
DCHECK(!with_osr); // Windowless rendering is not supported.
DCHECK(!initialized_);
delegate_ = delegate;
DCHECK(!config_);
config_ = std::make_unique<RootWindowConfig>();
config_->use_views = true;
config_->use_alloy_style = IsAlloyStyle();
config_->with_controls = with_controls;
if (popupFeatures.xSet) {
initial_bounds_.x = popupFeatures.x;
}
if (popupFeatures.ySet) {
initial_bounds_.y = popupFeatures.y;
}
if (popupFeatures.widthSet) {
initial_bounds_.width = popupFeatures.width;
}
if (popupFeatures.heightSet) {
initial_bounds_.height = popupFeatures.height;
}
CreateClientHandler(std::string());
initialized_ = true;
// The Window will be created in ViewsWindow::OnPopupBrowserViewCreated().
client = client_handler_;
}
void RootWindowViews::Show(ShowMode mode) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI, base::BindOnce(&RootWindowViews::Show, this, mode));
return;
}
if (!window_) {
return;
}
window_->Show();
switch (mode) {
case ShowMinimized:
window_->Minimize();
break;
case ShowMaximized:
window_->Maximize();
break;
default:
break;
}
}
void RootWindowViews::Hide() {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI, base::BindOnce(&RootWindowViews::Hide, this));
return;
}
if (window_) {
window_->Hide();
}
}
void RootWindowViews::SetBounds(int x, int y, size_t width, size_t height) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI, base::BindOnce(&RootWindowViews::SetBounds, this, x, y,
width, height));
return;
}
if (window_) {
window_->SetBounds(
CefRect(x, y, static_cast<int>(width), static_cast<int>(height)));
}
}
void RootWindowViews::Close(bool force) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI, base::BindOnce(&RootWindowViews::Close, this, force));
return;
}
if (window_) {
window_->Close(force);
}
}
void RootWindowViews::SetDeviceScaleFactor(float device_scale_factor) {
REQUIRE_MAIN_THREAD();
// Windowless rendering is not supported.
NOTREACHED();
}
float RootWindowViews::GetDeviceScaleFactor() const {
REQUIRE_MAIN_THREAD();
// Windowless rendering is not supported.
NOTREACHED();
return 0.0;
}
CefRefPtr<CefBrowser> RootWindowViews::GetBrowser() const {
REQUIRE_MAIN_THREAD();
return browser_;
}
ClientWindowHandle RootWindowViews::GetWindowHandle() const {
REQUIRE_MAIN_THREAD();
#if defined(OS_LINUX)
// ClientWindowHandle is a GtkWidget* on Linux and we don't have one of those.
return nullptr;
#else
if (browser_) {
return browser_->GetHost()->GetWindowHandle();
}
return kNullWindowHandle;
#endif
}
bool RootWindowViews::WithControls() {
DCHECK(initialized_);
return config_->with_controls;
}
bool RootWindowViews::InitiallyHidden() {
CEF_REQUIRE_UI_THREAD();
#if defined(OS_MAC)
// Hidden show state is only supported on MacOS.
if (initial_show_state_ == CEF_SHOW_STATE_HIDDEN) {
return true;
}
#endif
return config_->initially_hidden;
}
CefRefPtr<CefWindow> RootWindowViews::GetParentWindow() {
CEF_REQUIRE_UI_THREAD();
return config_->parent_window;
}
CefRect RootWindowViews::GetInitialBounds() {
CEF_REQUIRE_UI_THREAD();
return initial_bounds_;
}
cef_show_state_t RootWindowViews::GetInitialShowState() {
CEF_REQUIRE_UI_THREAD();
return initial_show_state_;
}
scoped_refptr<ImageCache> RootWindowViews::GetImageCache() {
CEF_REQUIRE_UI_THREAD();
return image_cache_;
}
void RootWindowViews::OnViewsWindowCreated(CefRefPtr<ViewsWindow> window) {
CEF_REQUIRE_UI_THREAD();
DCHECK(!window_);
window_ = window;
window_->SetAlwaysOnTop(config_->always_on_top);
}
void RootWindowViews::OnViewsWindowClosing(CefRefPtr<ViewsWindow> window) {
CEF_REQUIRE_UI_THREAD();
DCHECK(window_);
if (!window_->SupportsWindowRestore()) {
return;
}
cef_show_state_t show_state;
std::optional<CefRect> dip_bounds;
if (window_->GetWindowRestorePreferences(show_state, dip_bounds)) {
prefs::SaveWindowRestorePreferences(show_state, dip_bounds);
}
}
void RootWindowViews::OnViewsWindowDestroyed(CefRefPtr<ViewsWindow> window) {
CEF_REQUIRE_UI_THREAD();
window_ = nullptr;
// Continue on the main thread.
MAIN_POST_CLOSURE(
base::BindOnce(&RootWindowViews::NotifyViewsWindowDestroyed, this));
}
void RootWindowViews::OnViewsWindowActivated(CefRefPtr<ViewsWindow> window) {
CEF_REQUIRE_UI_THREAD();
// Continue on the main thread.
MAIN_POST_CLOSURE(
base::BindOnce(&RootWindowViews::NotifyViewsWindowActivated, this));
}
ViewsWindow::Delegate* RootWindowViews::GetDelegateForPopup(
CefRefPtr<CefClient> client) {
CEF_REQUIRE_UI_THREAD();
// |handler| was created in RootWindowViews::InitAsPopup().
ClientHandlerStd* handler = static_cast<ClientHandlerStd*>(client.get());
RootWindowViews* root_window =
static_cast<RootWindowViews*>(handler->delegate());
// May be nullptr when using the default popup behavior.
if (root_window) {
// Transfer some state to the child RootWindowViews.
root_window->image_cache_ = image_cache_;
}
return root_window;
}
void RootWindowViews::OnTest(int test_id) {
if (!CURRENTLY_ON_MAIN_THREAD()) {
// Execute this method on the main thread.
MAIN_POST_CLOSURE(base::BindOnce(&RootWindowViews::OnTest, this, test_id));
return;
}
delegate_->OnTest(this, test_id);
}
void RootWindowViews::OnExit() {
if (!CURRENTLY_ON_MAIN_THREAD()) {
// Execute this method on the main thread.
MAIN_POST_CLOSURE(base::BindOnce(&RootWindowViews::OnExit, this));
return;
}
delegate_->OnExit(this);
}
void RootWindowViews::OnBrowserCreated(CefRefPtr<CefBrowser> browser) {
REQUIRE_MAIN_THREAD();
DCHECK(!browser_);
browser_ = browser;
}
void RootWindowViews::OnBrowserClosing(CefRefPtr<CefBrowser> browser) {
REQUIRE_MAIN_THREAD();
// Nothing to do here.
}
void RootWindowViews::OnBrowserClosed(CefRefPtr<CefBrowser> browser) {
REQUIRE_MAIN_THREAD();
if (browser_) {
DCHECK_EQ(browser->GetIdentifier(), browser_->GetIdentifier());
browser_ = nullptr;
}
client_handler_->DetachDelegate();
client_handler_ = nullptr;
browser_destroyed_ = true;
NotifyDestroyedIfDone();
}
void RootWindowViews::OnSetAddress(const std::string& url) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI,
base::BindOnce(&RootWindowViews::OnSetAddress, this, url));
return;
}
if (window_) {
window_->SetAddress(url);
}
}
void RootWindowViews::OnSetTitle(const std::string& title) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI,
base::BindOnce(&RootWindowViews::OnSetTitle, this, title));
return;
}
if (window_) {
window_->SetTitle(title);
}
}
void RootWindowViews::OnSetFavicon(CefRefPtr<CefImage> image) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI,
base::BindOnce(&RootWindowViews::OnSetFavicon, this, image));
return;
}
if (window_) {
window_->SetFavicon(image);
}
}
void RootWindowViews::OnSetFullscreen(bool fullscreen) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI, base::BindOnce(&RootWindowViews::OnSetFullscreen, this,
fullscreen));
return;
}
if (window_) {
window_->SetFullscreen(fullscreen);
}
}
void RootWindowViews::OnAutoResize(const CefSize& new_size) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI,
base::BindOnce(&RootWindowViews::OnAutoResize, this, new_size));
return;
}
bool has_position = false;
CefPoint position;
if (position_on_resize_) {
// Position the window centered on and immediately below the source.
const int x_offset = (initial_bounds_.width - new_size.width) / 2;
position.Set(initial_bounds_.x + x_offset,
initial_bounds_.y + initial_bounds_.height);
has_position = true;
// Don't change the window position on future resizes.
position_on_resize_ = false;
}
if (window_) {
window_->SetBrowserSize(new_size, has_position, position);
window_->Show();
}
}
void RootWindowViews::OnSetLoadingState(bool isLoading,
bool canGoBack,
bool canGoForward) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI,
base::BindOnce(&RootWindowViews::OnSetLoadingState, this,
isLoading, canGoBack, canGoForward));
return;
}
if (window_) {
window_->SetLoadingState(isLoading, canGoBack, canGoForward);
if (isLoading) {
// Reset to the default window icon when loading begins.
window_->SetFavicon(
delegate_->GetImageCache()->GetCachedImage("window_icon"));
}
}
}
void RootWindowViews::OnSetDraggableRegions(
const std::vector<CefDraggableRegion>& regions) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI, base::BindOnce(&RootWindowViews::OnSetDraggableRegions,
this, regions));
return;
}
if (window_) {
window_->SetDraggableRegions(regions);
}
}
void RootWindowViews::OnTakeFocus(bool next) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI,
base::BindOnce(&RootWindowViews::OnTakeFocus, this, next));
return;
}
if (window_) {
window_->TakeFocus(next);
}
}
void RootWindowViews::OnBeforeContextMenu(CefRefPtr<CefMenuModel> model) {
CEF_REQUIRE_UI_THREAD();
if (window_) {
window_->OnBeforeContextMenu(model);
}
}
void RootWindowViews::CreateClientHandler(const std::string& url) {
DCHECK(!client_handler_);
client_handler_ = new ClientHandlerStd(this, config_->with_controls, url);
client_handler_->set_download_favicon_images(true);
}
void RootWindowViews::InitOnUIThread(
const CefBrowserSettings& settings,
CefRefPtr<CefRequestContext> request_context) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute this method on the UI thread.
CefPostTask(TID_UI, base::BindOnce(&RootWindowViews::InitOnUIThread, this,
settings, request_context));
return;
}
if (config_->initially_hidden && !config_->source_bounds.IsEmpty()) {
// The window will be sized and positioned in OnAutoResize().
initial_bounds_ = config_->source_bounds;
position_on_resize_ = true;
} else if (!config_->bounds.IsEmpty()) {
// Initial state was specified via the config object.
initial_bounds_ = config_->bounds;
initial_show_state_ = config_->show_state;
} else if (ViewsWindow::SupportsWindowRestore(config_->window_type)) {
// Initial state may be specified via the command-line or global
// preferences.
std::optional<CefRect> bounds;
if (prefs::LoadWindowRestorePreferences(initial_show_state_, bounds) &&
bounds) {
initial_bounds_ = *bounds;
}
}
image_cache_ = delegate_->GetImageCache();
// Populate the default image cache.
ImageCache::ImageInfoSet image_set;
for (auto& i : kDefaultImageCache) {
image_set.push_back(ImageCache::ImageInfo::Create2x(i));
}
image_cache_->LoadImages(
image_set, base::BindOnce(&RootWindowViews::CreateViewsWindow, this,
settings, request_context));
}
void RootWindowViews::CreateViewsWindow(
const CefBrowserSettings& settings,
CefRefPtr<CefRequestContext> request_context,
const ImageCache::ImageSet& images) {
CEF_REQUIRE_UI_THREAD();
DCHECK(!window_);
#ifndef NDEBUG
// Make sure the default images loaded successfully.
DCHECK_EQ(images.size(), std::size(kDefaultImageCache));
for (size_t i = 0U; i < std::size(kDefaultImageCache); ++i) {
DCHECK(images[i]) << "Default image " << i << " failed to load";
}
#endif
// Create the ViewsWindow. It will show itself after creation.
ViewsWindow::Create(config_->window_type, this, client_handler_, config_->url,
settings, request_context, config_->command_line);
}
void RootWindowViews::NotifyViewsWindowDestroyed() {
REQUIRE_MAIN_THREAD();
window_destroyed_ = true;
NotifyDestroyedIfDone();
}
void RootWindowViews::NotifyViewsWindowActivated() {
REQUIRE_MAIN_THREAD();
delegate_->OnRootWindowActivated(this);
}
void RootWindowViews::NotifyDestroyedIfDone() {
// Notify once both the window and the browser have been destroyed.
if (window_destroyed_ && browser_destroyed_) {
// The delegate may be holding the last reference to |this|, so take a
// reference here to keep |this| alive until after the method completes.
scoped_refptr<RootWindow> self = this;
delegate_->OnRootWindowDestroyed(this);
if (!config_->close_callback.is_null()) {
std::move(config_->close_callback).Run();
}
}
}
} // namespace client