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 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_BROWSER_H_
#define CHROME_BROWSER_UI_BROWSER_H_
#include <stdint.h>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "base/functional/callback.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/scoped_observation_traits.h"
#include "base/supports_user_data.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/types/expected.h"
#include "build/build_config.h"
#include "chrome/browser/tab_contents/web_contents_collection.h"
#include "chrome/browser/themes/theme_service_observer.h"
#include "chrome/browser/ui/bookmarks/bookmark_bar.h"
#include "chrome/browser/ui/bookmarks/bookmark_tab_helper_observer.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/browser_window/public/desktop_browser_window_capabilities_delegate.h"
#include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/browser/ui/unload_controller.h"
#include "chrome/browser/ui/unowned_user_data/unowned_user_data_host.h"
#include "chrome/browser/ui/user_education/browser_user_education_interface.h"
#include "components/paint_preview/buildflags/buildflags.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/sessions/core/session_id.h"
#include "components/zoom/zoom_observer.h"
#include "content/public/browser/fullscreen_types.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "extensions/buildflags/buildflags.h"
#include "printing/buildflags/buildflags.h"
#include "third_party/blink/public/mojom/page/draggable_region.mojom-forward.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/ui_base_types.h"
#include "ui/base/window_open_disposition.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#if BUILDFLAG(IS_ANDROID)
#error This file should only be included on desktop.
#endif
#if BUILDFLAG(IS_OZONE)
#include "ui/ozone/public/platform_session_manager.h"
#endif
class BackgroundContents;
class BreadcrumbManagerBrowserAgent;
class BrowserActions;
class BrowserContentSettingBubbleModelDelegate;
class BrowserLiveTabContext;
class BrowserView;
class BrowserWindow;
class BrowserWindowFeatures;
class FindBarController;
class OverscrollPrefManager;
class Profile;
class ScopedKeepAlive;
class ScopedProfileKeepAlive;
class StatusBubble;
class TabStripModelDelegate;
namespace tabs {
class TabInterface;
}
namespace blink {
enum class ProtocolHandlerSecurityLevel;
}
namespace chrome {
class BrowserCommandController;
}
namespace content {
struct DropData;
class NavigationHandle;
class SessionStorageNamespace;
} // namespace content
namespace extensions {
class ExtensionBrowserWindowHelper;
} // namespace extensions
namespace gfx {
class Image;
}
namespace ui {
struct SelectedFileInfo;
}
namespace web_app {
class AppBrowserController;
}
namespace web_modal {
class WebContentsModalDialogHost;
}
namespace views {
class View;
}
// This enum is not a member of `Browser` so that it can be forward
// declared in `unload_controller.h` to avoid circular includes.
enum class BrowserClosingStatus {
kPermitted,
kDeniedByUser,
kDeniedByPolicy,
kDeniedUnloadHandlersNeedTime
};
// An instance of this class represents a single browser window on Desktop. All
// features that are scoped to a browser window should have lifetime semantics
// scoped to an instance of this class, usually via direct or indirect ownership
// of a std::unique_ptr. See BrowserWindowFeatures and TabFeatures.
class Browser : public TabStripModelObserver,
public WebContentsCollection::Observer,
public content::WebContentsDelegate,
public ChromeWebModalDialogManagerDelegate,
public base::SupportsUserData,
public BookmarkTabHelperObserver,
public zoom::ZoomObserver,
public ThemeServiceObserver,
public ui::SelectFileDialog::Listener,
public BrowserWindowInterface,
public DesktopBrowserWindowCapabilitiesDelegate {
public:
// Possible elements of the Browser window.
enum WindowFeature {
FEATURE_NONE = 0,
FEATURE_TITLEBAR = 1 << 0,
FEATURE_TABSTRIP = 1 << 1,
FEATURE_TOOLBAR = 1 << 2,
FEATURE_LOCATIONBAR = 1 << 3,
FEATURE_BOOKMARKBAR = 1 << 4,
// TODO(crbug.com/40639933): Add FEATURE_PAGECONTROLS to describe the
// presence of per-page controls such as Content Settings Icons, which
// should be decoupled from FEATURE_LOCATIONBAR as they have independent
// presence in Web App browsers.
};
// The context for a download blocked notification from
// OkToCloseWithInProgressDownloads.
enum class DownloadCloseType {
// Browser close is not blocked by download state.
kOk,
// The browser is shutting down and there are active downloads
// that would be cancelled.
kBrowserShutdown,
// There are active downloads associated with this incognito profile
// that would be canceled.
kLastWindowInIncognitoProfile,
// There are active downloads associated with this guest session
// that would be canceled.
kLastWindowInGuestSession,
};
// Represents the result of the user being warned before closing the browser.
// See WarnBeforeClosingCallback and WarnBeforeClosing() below.
enum class WarnBeforeClosingResult { kOkToClose, kDoNotClose };
// Represents the result of a browser creation request.
enum class CreationStatus {
kOk,
kErrorNoProcess,
kErrorProfileUnsuitable,
kErrorLoadingKiosk,
};
// Represents the source of a browser creation request.
enum class CreationSource {
kUnknown,
kSessionRestore,
kStartupCreator,
kLastAndUrlsStartupPref,
kDeskTemplate,
};
// Represents the reasons for force showing bookmark bar.
enum ForceShowBookmarkBarFlag {
kNone = 0,
kTabGroupsTutorialActive = 1 << 0,
kTabGroupSaved = 1 << 1,
};
// Represents whether a value was known to be explicitly specified.
enum class ValueSpecified { kUnknown, kSpecified, kUnspecified };
// The default value for a browser's `restore_id` param.
static constexpr int kDefaultRestoreId = 0;
// Callback that receives the result of a user being warned about closing a
// browser window (for example, if closing the window would interrupt a
// download). The parameter is whether the close should proceed.
using WarnBeforeClosingCallback =
base::OnceCallback<void(WarnBeforeClosingResult)>;
struct CreateParams {
explicit CreateParams(Profile* profile, bool user_gesture);
CreateParams(Type type, Profile* profile, bool user_gesture);
CreateParams(const CreateParams& other);
CreateParams& operator=(const CreateParams& other);
~CreateParams();
static CreateParams CreateForApp(const std::string& app_name,
bool trusted_source,
const gfx::Rect& window_bounds,
Profile* profile,
bool user_gesture);
static CreateParams CreateForAppPopup(const std::string& app_name,
bool trusted_source,
const gfx::Rect& window_bounds,
Profile* profile,
bool user_gesture);
static CreateParams CreateForPictureInPicture(const std::string& app_name,
bool trusted_source,
Profile* profile,
bool user_gesture);
static CreateParams CreateForDevTools(Profile* profile);
// The browser type.
Type type;
// The associated profile.
raw_ptr<Profile, AcrossTasksDanglingUntriaged> profile;
// Specifies the browser `is_trusted_source_` value.
bool trusted_source = false;
// Specifies the browser `omit_from_session_restore_` value, whether the new
// Browser should be omitted from being saved/restored by session restore.
bool omit_from_session_restore = false;
// Specifies the browser `should_trigger_session_restore` value. If true, a
// new window opening should be treated like the start of a session (with
// potential session restore, startup URLs, etc.). Otherwise, don't restore
// the session.
bool should_trigger_session_restore = true;
// The bounds of the window to open.
gfx::Rect initial_bounds;
// Whether `initial_bounds.origin()` was explicitly specified, if known.
// Used to disambiguate coordinate (0,0) from an unspecified location when
// parameters originate from the JS Window.open() window features string,
// e.g. window.open(... 'left=0,top=0,...') vs window.open(... 'popup,...').
ValueSpecified initial_origin_specified = ValueSpecified::kUnknown;
// The workspace the window should open in, if the platform supports it.
std::string initial_workspace;
// Whether the window is visible on all workspaces initially, if the
// platform supports it.
bool initial_visible_on_all_workspaces_state = false;
// Whether to enable the tab group feature in the tab strip.
bool are_tab_groups_enabled = true;
ui::mojom::WindowShowState initial_show_state =
ui::mojom::WindowShowState::kDefault;
CreationSource creation_source = CreationSource::kUnknown;
#if BUILDFLAG(IS_CHROMEOS)
// If set, the browser should be created on the display given by
// `display_id`.
std::optional<int64_t> display_id;
#endif
#if BUILDFLAG(IS_LINUX)
// When the browser window is shown, the desktop environment is notified
// using this ID. In response, the desktop will stop playing the "waiting
// for startup" animation (if any).
std::string startup_id;
#endif
#if BUILDFLAG(IS_OZONE)
// Some platforms support session management assisted by the windowing
// system, such as:
// -ChromeOS, where this id is retrieved from the session backing
// storage and used by Ash to restore the browser window state.
// - Ozone/Wayland, with xdg-session-management protocol extension, in
// which case, this id is sent to the Wayland compositor, so it can also
// restore the window state when the window is initialized. Se
// ui/ozone/public/platfrom_session_manager.h for more details.
int32_t restore_id = kDefaultRestoreId;
#endif
// Whether this browser was created by a user gesture. We track this
// specifically for the multi-user case in chromeos where we can place
// windows generated by user gestures differently from ones
// programmatically created.
bool user_gesture;
// Whether this browser was created specifically for dragged tab(s).
bool in_tab_dragging = false;
// Supply a custom BrowserWindow implementation, to be used instead of the
// default. Intended for testing.
raw_ptr<BrowserWindow, DanglingUntriaged> window = nullptr;
// User-set title of this browser window, if there is one.
std::string user_title;
// Only applied when not in forced app mode. True if the browser is
// resizeable.
bool can_resize = true;
// Only applied when not in forced app mode. True if the browser can be
// maximizable.
bool can_maximize = true;
// Only applied when not in forced app mode. True if the browser can enter
// fullscreen.
bool can_fullscreen = true;
// Document Picture in Picture options, specific to TYPE_PICTURE_IN_PICTURE.
std::optional<blink::mojom::PictureInPictureWindowOptions> pip_options;
private:
friend class Browser;
friend class WindowSizerChromeOSTest;
static CreateParams CreateForAppBase(bool is_popup,
const std::string& app_name,
bool trusted_source,
const gfx::Rect& window_bounds,
Profile* profile,
bool user_gesture);
// The application name that is also the name of the window to the shell.
// Do not set this value directly, use CreateForApp/CreateForAppPopup.
// This name will be set for:
// 1) v1 applications launched via an application shortcut or extension API.
// 2) undocked devtool windows.
// 3) popup windows spawned from v1 applications.
std::string app_name;
// When set to true, skip initializing |window_| and everything that depends
// on it.
bool skip_window_init_for_testing = false;
};
// Constructors, Creation, Showing //////////////////////////////////////////
// Creates a browser instance with the provided params. Returns an unowned
// pointer to the created browser.
// Crashes if the requested browser creation is not allowed.
// For example, browser creation will not be allowed for profiles that
// disallow browsing (like sign-in profile on Chrome OS).
//
// Unless |params->window| is specified, a new BrowserWindow will be created
// for the browser - the created BrowserWindow will take the ownership of the
// created Browser instance.
//
// If |params.window| or |params.skip_window_init_for_testing| are set, the
// caller is expected to take the ownership of the created Browser instance.
static Browser* Create(const CreateParams& params);
// WARNING: Use of this is DEPRECATED and exists only to support pre-existing
// browser unittests. Similar to Create() above, however the created browser
// is owned by the caller.
// TODO(crbug.com/417766643): Remove this once all use of Browser in unittests
// has been eliminated.
static std::unique_ptr<Browser> DeprecatedCreateOwnedForTesting(
const CreateParams& params);
// Returns whether a browser window can be created for the specified profile.
static CreationStatus GetCreationStatusForProfile(Profile* profile);
Browser(const Browser&) = delete;
Browser& operator=(const Browser&) = delete;
~Browser() override;
// Set overrides for the initial window bounds and maximized state.
void set_override_bounds(const gfx::Rect& bounds) {
override_bounds_ = bounds;
}
ui::mojom::WindowShowState initial_show_state() const {
return initial_show_state_;
}
void set_initial_show_state(ui::mojom::WindowShowState initial_show_state) {
initial_show_state_ = initial_show_state;
}
// Return true if the initial window bounds have been overridden.
bool bounds_overridden() const { return !override_bounds_.IsEmpty(); }
// Set indicator that this browser is being created via session restore.
// This is used on the Mac (only) to determine animation style when the
// browser window is shown.
void set_is_session_restore(bool is_session_restore) {
creation_source_ = CreationSource::kSessionRestore;
}
bool is_session_restore() const {
return creation_source_ == CreationSource::kSessionRestore;
}
// Tells the browser whether it should skip showing any dialogs that ask the
// user to confirm that they want to close the browser when it is being
// closed.
void set_force_skip_warning_user_on_close(
bool force_skip_warning_user_on_close) {
force_skip_warning_user_on_close_ = force_skip_warning_user_on_close;
}
// Sets whether the UI should be immediately updated when scheduled on a
// test.
void set_update_ui_immediately_for_testing() {
update_ui_immediately_for_testing_ = true;
}
// Accessors ////////////////////////////////////////////////////////////////
const CreateParams& create_params() const { return create_params_; }
Type type() const { return type_; }
const std::string& app_name() const { return app_name_; }
const std::string& user_title() const { return user_title_; }
bool is_trusted_source() const { return is_trusted_source_; }
Profile* profile() const { return profile_; }
gfx::Rect override_bounds() const { return override_bounds_; }
const std::string& initial_workspace() const { return initial_workspace_; }
bool initial_visible_on_all_workspaces_state() const {
return initial_visible_on_all_workspaces_state_;
}
CreationSource creation_source() const { return creation_source_; }
// |window()| will return NULL if called before |CreateBrowserWindow()|
// is done.
BrowserWindow* window() const { return window_; }
// In production code, each instance of Browser will always instantiate an
// instance of BrowserView in the constructor. Some tests instantiate a
// Browser without a BrowserView: this is an anti-pattern and should be
// avoided.
BrowserView& GetBrowserView();
// Never nullptr.
//
// When the last tab is removed, the browser attempts to close, see
// TabStripEmpty().
// TODO(https://crbug.com/331031753): Several existing Browser::Types never
// show a tab strip, yet are forced to work with the tab strip API to deal
// with the previous condition. This creates confusing control flow both for
// the tab strip and this class. One or both of the following should happen:
// (1) tab_strip_model_ should become an optional member.
// (2) Variations of Browser::Type that never show a tab strip should not use
// this class.
TabStripModel* tab_strip_model() const { return tab_strip_model_.get(); }
// Never nullptr.
TabStripModelDelegate* tab_strip_model_delegate() const {
return tab_strip_model_delegate_.get();
}
BrowserActions* browser_actions() const { return browser_actions_.get(); }
chrome::BrowserCommandController* command_controller() {
return command_controller_.get();
}
SessionID session_id() const { return session_id_; }
bool omit_from_session_restore() const { return omit_from_session_restore_; }
bool should_trigger_session_restore() const {
return should_trigger_session_restore_;
}
BrowserContentSettingBubbleModelDelegate*
content_setting_bubble_model_delegate() {
return content_setting_bubble_model_delegate_.get();
}
BrowserLiveTabContext* live_tab_context() { return live_tab_context_.get(); }
const web_app::AppBrowserController* app_controller() const {
return app_controller_.get();
}
web_app::AppBrowserController* app_controller() {
return app_controller_.get();
}
BrowserWindowFeatures* browser_window_features() const {
return features_.get();
}
base::WeakPtr<Browser> AsWeakPtr();
base::WeakPtr<const Browser> AsWeakPtr() const;
// Returns the state of the bookmark bar.
BookmarkBar::State bookmark_bar_state() const { return bookmark_bar_state_; }
// State Storage and Retrieval for UI ///////////////////////////////////////
GURL GetNewTabURL() const;
// Gets the Favicon of the page in the selected tab.
gfx::Image GetCurrentPageIcon() const;
// Gets the title of the window based on the selected tab's title.
// Disables additional formatting when |include_app_name| is false or if the
// window is an app window.
std::u16string GetWindowTitleForCurrentTab(bool include_app_name) const;
// Gets the window title of the tab at |index|.
std::u16string GetWindowTitleForTab(int index) const;
std::u16string GetTitleForTab(int index) const;
// Gets the window title for the current tab, to display in a menu. If the
// title is too long to fit in the required space, the tab title will be
// elided. The result title might still be a larger width than specified, as
// at least a few characters of the title are always shown.
std::u16string GetWindowTitleForMaxWidth(int max_width) const;
// Gets the window title from the provided WebContents.
// Disables additional formatting when |include_app_name| is false or if the
// window is an app window.
std::u16string GetWindowTitleFromWebContents(
bool include_app_name,
content::WebContents* contents) const;
// Prepares a title string for display (removes embedded newlines, etc).
static std::u16string FormatTitleForDisplay(std::u16string title);
// OnBeforeUnload handling //////////////////////////////////////////////////
// Displays any necessary warnings to the user on taking an action that might
// close the browser (for example, warning if there are downloads in progress
// that would be interrupted).
//
// Distinct from HandleBeforeClose() (which calls this method) because
// this method does not consider beforeunload handler, only things the user
// should be prompted about.
//
// If no warnings are needed, the method returns kOkToClose, indicating that
// the close can proceed immediately, and the callback is not called. If the
// method returns kDoNotClose, closing should be handled by |warn_callback|
// (and then only if the callback receives the kOkToClose value).
WarnBeforeClosingResult MaybeWarnBeforeClosing(
WarnBeforeClosingCallback warn_callback);
// Gives beforeunload handlers the chance to cancel the close. Returns the
// closing status. Closing can be denied due to different reasons.
// This function checks if unload handlers are still executing. It further
// may ask the user for permission to close the browser (e.g. if downloads
// are ongoing).
// If this function is called
// * but the user denied closure after being prompted, it returns
// `BrowserClosingStatus::kDeniedByUser`.
// * but the closure is not permitted by policy, it returns
// `BrowserClosingStatus::kDeniedByPolicy`.
// * while the process begun by TryToCloseWindow is in progress, it returns
// `BrowserClosingStatus::kDeniedUnloadHandlersNeedTime`.
//
// If you don't care about beforeunload handlers and just want to prompt the
// user that they might lose an in-progress operation, call
// MaybeWarnBeforeClosing() instead (HandleBeforeClose() also calls this
// method).
BrowserClosingStatus HandleBeforeClose();
// Begins the process of confirming whether the associated browser can be
// closed. If there are no tabs with beforeunload handlers it will immediately
// return false. If |skip_beforeunload| is true, all beforeunload
// handlers will be skipped and window closing will be confirmed without
// showing the prompt, the function will return false as well.
// Otherwise, it starts prompting the user, returns true and will call
// |on_close_confirmed| with the result of the user's decision.
// After calling this function, if the window will not be closed, call
// ResetBeforeUnloadHandlers() to reset all beforeunload handlers; calling
// this function multiple times without an intervening call to
// ResetTryToCloseWindow() will run only the beforeunload handlers
// registered since the previous call.
// Note that if the browser window has been used before, users should always
// have a chance to save their work before the window is closed without
// triggering beforeunload event.
bool TryToCloseWindow(
bool skip_beforeunload,
const base::RepeatingCallback<void(bool)>& on_close_confirmed);
// Clears the results of any beforeunload confirmation dialogs triggered by a
// TryToCloseWindow call.
void ResetTryToCloseWindow();
// Figure out if there are tabs that have beforeunload handlers.
bool TabsNeedBeforeUnloadFired() const;
// Browser closing consists of the following phases:
//
// 1. If the browser has WebContents with before unload handlers, then the
// before unload handlers are processed (this is asynchronous). During this
// phase IsAttemptingToCloseBrowser() returns true. When processing
// completes, the WebContents is removed. Once all WebContents are removed,
// the next phase happens. Note that this phase may be aborted.
// 2. The Browser window is hidden, and a task is posted that results in
// deleting the Browser (Views is responsible for posting the task). This
// phase can not be stopped. During this phase is_delete_scheduled()
// returns true. IsBrowserClosing() is nearly identical to
// is_delete_scheduled(), it's set just before removing the tabs.
//
// Note that there are other cases that may delay closing, such as downloads,
// but that is done before any of these steps.
// TODO(crbug.com/40064092): See about unifying IsBrowserClosing() and
// is_delete_scheduled().
bool IsAttemptingToCloseBrowser() const override;
bool IsBrowserClosing() const;
bool is_delete_scheduled() const { return is_delete_scheduled_; }
// Invoked when the window containing us is closing. Performs the necessary
// cleanup.
void OnWindowClosing();
// In-progress download termination handling /////////////////////////////////
// Indicates whether or not this browser window can be closed, or
// would be blocked by in-progress downloads.
// If executing downloads would be cancelled by this window close,
// then |*num_downloads_blocking| is updated with how many downloads
// would be canceled if the close continued.
DownloadCloseType OkToCloseWithInProgressDownloads(
int* num_downloads_blocking) const;
// External state change handling ////////////////////////////////////////////
// Invoked at the end of a fullscreen transition.
void WindowFullscreenStateChanged();
// Only used on Mac. Called when the top ui style has been changed since this
// may trigger bookmark bar state change.
void FullscreenTopUIStateChanged();
void OnFindBarVisibilityChanged();
// Assorted browser commands ////////////////////////////////////////////////
// NOTE: Within each of the following sections, the IDs are ordered roughly by
// how they appear in the GUI/menus (left to right, top to bottom, etc.).
// See the description of
// FullscreenController::ToggleFullscreenModeWithExtension.
void ToggleFullscreenModeWithExtension(const GURL& extension_url);
// Returns true if the Browser supports the specified feature. The value of
// this varies during the lifetime of the browser. For example, if the window
// is fullscreen this may return a different value. If you only care about
// whether or not it's possible for the browser to support a particular
// feature use |CanSupportWindowFeature|.
bool SupportsWindowFeature(WindowFeature feature) const;
// Returns true if the Browser can support the specified feature. See comment
// in |SupportsWindowFeature| for details on this.
bool CanSupportWindowFeature(WindowFeature feature) const;
// Show various bits of UI
void OpenFile();
void UpdateDownloadShelfVisibility(bool visible);
// Whether the specified WebContents can be saved.
// Saving can be disabled e.g. for the DevTools window.
bool CanSaveContents(content::WebContents* web_contents) const;
// Returns whether favicon should be shown.
bool ShouldDisplayFavicon(content::WebContents* web_contents) const;
/////////////////////////////////////////////////////////////////////////////
// Called by Navigate() when a navigation has occurred in a tab in
// this Browser. Updates the UI for the start of this navigation.
void UpdateUIForNavigationInTab(content::WebContents* contents,
ui::PageTransition transition,
NavigateParams::WindowAction action,
bool user_initiated);
// Used to register a KeepAlive to affect the Chrome lifetime. The KeepAlive
// is registered when the browser is added to the browser list, and unregisted
// when it is removed from it.
void RegisterKeepAlive();
void UnregisterKeepAlive();
// Interface implementations ////////////////////////////////////////////////
// Overridden from TabStripModelObserver:
void OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) override;
void OnTabGroupChanged(const TabGroupChange& change) override;
void TabPinnedStateChanged(TabStripModel* tab_strip_model,
content::WebContents* contents,
int index) override;
void TabGroupedStateChanged(TabStripModel* tab_strip_model,
std::optional<tab_groups::TabGroupId> old_group,
std::optional<tab_groups::TabGroupId> new_group,
tabs::TabInterface* tab,
int index) override;
void TabStripEmpty() override;
void OnSplitTabChanged(const SplitTabChange& change) override;
// Overridden from content::WebContentsDelegate:
void ActivateContents(content::WebContents* contents) override;
void SetTopControlsShownRatio(content::WebContents* web_contents,
float ratio) override;
int GetTopControlsHeight() override;
bool DoBrowserControlsShrinkRendererSize(
content::WebContents* contents) override;
int GetVirtualKeyboardHeight(content::WebContents* contents) override;
void SetTopControlsGestureScrollInProgress(bool in_progress) override;
bool CanOverscrollContent() override;
bool ShouldPreserveAbortedURLs(content::WebContents* source) override;
void SetFocusToLocationBar() override;
bool PreHandleMouseEvent(content::WebContents* source,
const blink::WebMouseEvent& event) override;
void PreHandleDragUpdate(const content::DropData& drop_data,
const gfx::PointF& client_pt) override;
void PreHandleDragExit() override;
content::KeyboardEventProcessingResult PreHandleKeyboardEvent(
content::WebContents* source,
const input::NativeWebKeyboardEvent& event) override;
bool HandleKeyboardEvent(content::WebContents* source,
const input::NativeWebKeyboardEvent& event) override;
bool PreHandleGestureEvent(content::WebContents* source,
const blink::WebGestureEvent& event) override;
bool CanDragEnter(content::WebContents* source,
const content::DropData& data,
blink::DragOperationsMask operations_allowed) override;
void CreateSmsPrompt(content::RenderFrameHost*,
const std::vector<url::Origin>&,
const std::string& one_time_code,
base::OnceClosure on_confirm,
base::OnceClosure on_cancel) override;
bool ShouldAllowRunningInsecureContent(content::WebContents* web_contents,
bool allowed_per_prefs,
const url::Origin& origin,
const GURL& resource_url) override;
void OnDidBlockNavigation(
content::WebContents* web_contents,
const GURL& blocked_url,
blink::mojom::NavigationBlockedReason reason) override;
content::PictureInPictureResult EnterPictureInPicture(
content::WebContents* web_contents) override;
void ExitPictureInPicture() override;
bool IsBackForwardCacheSupported(content::WebContents& web_contents) override;
content::PreloadingEligibility IsPrerender2Supported(
content::WebContents& web_contents,
content::PreloadingTriggerType trigger_type) override;
bool ShouldShowStaleContentOnEviction(content::WebContents* source) override;
void MediaWatchTimeChanged(
const content::MediaPlayerWatchTime& watch_time) override;
std::unique_ptr<content::EyeDropper> OpenEyeDropper(
content::RenderFrameHost* frame,
content::EyeDropperListener* listener) override;
void InitiatePreview(content::WebContents& web_contents,
const GURL& url) override;
bool ShouldUseInstancedSystemMediaControls() const override;
void DraggableRegionsChanged(
const std::vector<blink::mojom::DraggableRegionPtr>& regions,
content::WebContents* contents) override;
std::vector<blink::mojom::RelatedApplicationPtr> GetSavedRelatedApplications(
content::WebContents* web_contents) override;
bool is_type_normal() const { return type_ == TYPE_NORMAL; }
bool is_type_popup() const { return type_ == TYPE_POPUP; }
bool is_type_app() const { return type_ == TYPE_APP; }
bool is_type_app_popup() const { return type_ == TYPE_APP_POPUP; }
bool is_type_devtools() const { return type_ == TYPE_DEVTOOLS; }
#if BUILDFLAG(IS_CHROMEOS)
bool is_type_custom_tab() const { return type_ == TYPE_CUSTOM_TAB; }
#endif
bool is_type_picture_in_picture() const {
return type_ == TYPE_PICTURE_IN_PICTURE;
}
// True when the mouse cursor is locked.
bool IsPointerLocked() const;
// Called each time the browser window is shown.
void OnWindowDidShow();
bool ShouldRunUnloadListenerBeforeClosing(content::WebContents* web_contents);
bool RunUnloadListenerBeforeClosing(content::WebContents* web_contents);
// Sets the browser's user title. Setting it to an empty string clears it.
void SetWindowUserTitle(const std::string& user_title);
// Gets the browser for opening chrome:// pages. This will return the opener
// browser if the current browser is in picture-in-picture mode, otherwise
// returns the current browser.
Browser* GetBrowserForOpeningWebUi();
std::vector<StatusBubble*> GetStatusBubblesForTesting();
UnloadController* GetUnloadControllerForTesting() {
return &unload_controller_;
}
// Sets or clears the flags to force showing bookmark bar.
void SetForceShowBookmarkBarFlag(ForceShowBookmarkBarFlag flag);
void ClearForceShowBookmarkBarFlag(ForceShowBookmarkBarFlag flag);
// BrowserWindowInterface overrides:
views::WebView* GetWebView() override;
Profile* GetProfile() override;
void OpenGURL(const GURL& gurl, WindowOpenDisposition disposition) override;
content::WebContents* OpenURL(
const content::OpenURLParams& params,
base::OnceCallback<void(content::NavigationHandle&)>
navigation_handle_callback) override;
const SessionID& GetSessionID() const override;
TabStripModel* GetTabStripModel() override;
bool IsTabStripVisible() override;
bool ShouldHideUIForFullscreen() const override;
base::CallbackListSubscription RegisterBrowserDidClose(
BrowserDidCloseCallback callback) override;
views::View* TopContainer() override;
base::WeakPtr<BrowserWindowInterface> GetWeakPtr() override;
views::View* LensOverlayView() override;
base::CallbackListSubscription RegisterActiveTabDidChange(
ActiveTabChangeCallback callback) override;
tabs::TabInterface* GetActiveTabInterface() override;
BrowserWindowFeatures& GetFeatures() override;
const BrowserWindowFeatures& GetFeatures() const override;
UnownedUserDataHost& GetUnownedUserDataHost() override;
const UnownedUserDataHost& GetUnownedUserDataHost() const override;
web_modal::WebContentsModalDialogHost*
GetWebContentsModalDialogHostForWindow() override;
bool IsActive() const override;
base::CallbackListSubscription RegisterDidBecomeActive(
DidBecomeActiveCallback callback) override;
base::CallbackListSubscription RegisterDidBecomeInactive(
DidBecomeInactiveCallback callback) override;
ExclusiveAccessManager* GetExclusiveAccessManager() override;
ImmersiveModeController* GetImmersiveModeController() override;
BrowserActions* GetActions() override;
Type GetType() const override;
BrowserUserEducationInterface* GetUserEducationInterface() override;
web_app::AppBrowserController* GetAppBrowserController() override;
std::vector<tabs::TabInterface*> GetAllTabInterfaces() override;
Browser* GetBrowserForMigrationOnly() override;
bool IsTabModalPopupDeprecated() const override;
bool CanShowCallToAction() const override;
std::unique_ptr<ScopedWindowCallToAction> ShowCallToAction() override;
ui::BaseWindow* GetWindow() override;
DesktopBrowserWindowCapabilities* capabilities() override;
const DesktopBrowserWindowCapabilities* capabilities() const override;
// Called by BrowserView.
void set_is_tab_modal_popup_deprecated(bool is_tab_modal_popup_deprecated) {
is_tab_modal_popup_deprecated_ = is_tab_modal_popup_deprecated;
}
// Called by BrowserView on active change for the browser.
void DidBecomeActive();
void DidBecomeInactive();
#if BUILDFLAG(IS_CHROMEOS)
bool IsLockedForOnTask();
void SetLockedForOnTask(bool locked);
#endif
#if BUILDFLAG(IS_OZONE)
const std::optional<ui::PlatformSessionWindowData>& platform_session_data()
const {
return platform_session_data_;
}
#endif
private:
friend class BrowserTest;
friend class ExclusiveAccessTest;
friend class FullscreenControllerInteractiveTest;
FRIEND_TEST_ALL_PREFIXES(AppModeTest, EnableAppModeTest);
FRIEND_TEST_ALL_PREFIXES(BrowserCommandControllerTest,
IsReservedCommandOrKeyIsApp);
FRIEND_TEST_ALL_PREFIXES(BrowserCloseTest, LastIncognito);
FRIEND_TEST_ALL_PREFIXES(BrowserCloseTest, LastRegular);
FRIEND_TEST_ALL_PREFIXES(BrowserCommandControllerTest, AppFullScreen);
FRIEND_TEST_ALL_PREFIXES(BrowserTest, OpenAppWindowLikeNtp);
FRIEND_TEST_ALL_PREFIXES(BrowserTest, AppIdSwitch);
FRIEND_TEST_ALL_PREFIXES(ExclusiveAccessBubbleWindowControllerTest,
DenyExitsFullscreen);
FRIEND_TEST_ALL_PREFIXES(ExclusiveAccessTest,
TabEntersPresentationModeFromWindowed);
FRIEND_TEST_ALL_PREFIXES(BrowserCloseTest, LastGuest);
// Used to describe why a tab is being detached. This is used by
// TabDetachedAtImpl.
enum DetachType {
// Result of TabDetachedAt.
DETACH_TYPE_DETACH,
// Result of TabReplacedAt.
DETACH_TYPE_REPLACE,
// Result of the tab strip not having any significant tabs.
DETACH_TYPE_EMPTY
};
// Describes where the bookmark bar state change originated from.
enum BookmarkBarStateChangeReason {
// From the constructor.
BOOKMARK_BAR_STATE_CHANGE_INIT,
// Change is the result of the active tab changing.
BOOKMARK_BAR_STATE_CHANGE_TAB_SWITCH,
// Change is the result of the bookmark bar pref changing.
BOOKMARK_BAR_STATE_CHANGE_PREF_CHANGE,
// Change is the result of a state change in the active tab.
BOOKMARK_BAR_STATE_CHANGE_TAB_STATE,
// Change is the result of window toggling in/out of fullscreen mode.
BOOKMARK_BAR_STATE_CHANGE_TOGGLE_FULLSCREEN,
// Change is the result of switching the option of showing toolbar in full
// screen. Only used on Mac.
BOOKMARK_BAR_STATE_CHANGE_TOOLBAR_OPTION_CHANGE,
// Change is the result of a force show reason
BOOKMARK_BAR_STATE_CHANGE_FORCE_SHOW,
// Change is the result of a split tab being created or removed.
BOOKMARK_BAR_STATE_CHANGE_SPLIT_TAB_CHANGE,
};
// Tracks whether a tabstrip call to action UI is showing.
class ScopedWindowCallToActionImpl : public ScopedWindowCallToAction {
public:
explicit ScopedWindowCallToActionImpl(Browser* browser);
~ScopedWindowCallToActionImpl() override;
private:
// Owns this.
base::WeakPtr<Browser> browser_;
};
explicit Browser(const CreateParams& params);
// Overridden from content::WebContentsDelegate:
content::WebContents* OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params,
base::OnceCallback<void(content::NavigationHandle&)>
navigation_handle_callback) override;
void NavigationStateChanged(content::WebContents* source,
content::InvalidateTypes changed_flags) override;
void VisibleSecurityStateChanged(content::WebContents* source) override;
content::WebContents* AddNewContents(
content::WebContents* source,
std::unique_ptr<content::WebContents> new_contents,
const GURL& target_url,
WindowOpenDisposition disposition,
const blink::mojom::WindowFeatures& window_features,
bool user_gesture,
bool* was_blocked) override;
void LoadingStateChanged(content::WebContents* source,
bool should_show_loading_ui) override;
void CloseContents(content::WebContents* source) override;
void SetContentsBounds(content::WebContents* source,
const gfx::Rect& bounds) override;
void UpdateTargetURL(content::WebContents* source, const GURL& url) override;
void ContentsMouseEvent(content::WebContents* source,
const ui::Event& event) override;
void ContentsZoomChange(bool zoom_in) override;
bool TakeFocus(content::WebContents* source, bool reverse) override;
void BeforeUnloadFired(content::WebContents* source,
bool proceed,
bool* proceed_to_fire_unload) override;
bool ShouldFocusLocationBarByDefault(content::WebContents* source) override;
bool ShouldFocusPageAfterCrash(content::WebContents* source) override;
void ShowRepostFormWarningDialog(content::WebContents* source) override;
bool IsWebContentsCreationOverridden(
content::RenderFrameHost* opener,
content::SiteInstance* source_site_instance,
content::mojom::WindowContainerType window_container_type,
const GURL& opener_url,
const std::string& frame_name,
const GURL& target_url) override;
content::WebContents* CreateCustomWebContents(
content::RenderFrameHost* opener,
content::SiteInstance* source_site_instance,
bool is_new_browsing_instance,
const GURL& opener_url,
const std::string& frame_name,
const GURL& target_url,
const content::StoragePartitionConfig& partition_config,
content::SessionStorageNamespace* session_storage_namespace) override;
void WebContentsCreated(content::WebContents* source_contents,
int opener_render_process_id,
int opener_render_frame_id,
const std::string& frame_name,
const GURL& target_url,
content::WebContents* new_contents) override;
void RendererUnresponsive(
content::WebContents* source,
content::RenderWidgetHost* render_widget_host,
base::RepeatingClosure hang_monitor_restarter) override;
void RendererResponsive(
content::WebContents* source,
content::RenderWidgetHost* render_widget_host) override;
content::JavaScriptDialogManager* GetJavaScriptDialogManager(
content::WebContents* source) override;
bool GuestSaveFrame(content::WebContents* guest_web_contents) override;
void RunFileChooser(content::RenderFrameHost* render_frame_host,
scoped_refptr<content::FileSelectListener> listener,
const blink::mojom::FileChooserParams& params) override;
void EnumerateDirectory(content::WebContents* web_contents,
scoped_refptr<content::FileSelectListener> listener,
const base::FilePath& path) override;
bool CanUseWindowingControls(
content::RenderFrameHost* requesting_frame) override;
void OnWebApiWindowResizableChanged() override;
bool GetCanResize() override;
void MinimizeFromWebAPI() override;
void MaximizeFromWebAPI() override;
void RestoreFromWebAPI() override;
ui::mojom::WindowShowState GetWindowShowState() const override;
bool CanEnterFullscreenModeForTab(
content::RenderFrameHost* requesting_frame) override;
void EnterFullscreenModeForTab(
content::RenderFrameHost* requesting_frame,
const blink::mojom::FullscreenOptions& options) override;
void ExitFullscreenModeForTab(content::WebContents* web_contents) override;
bool IsFullscreenForTabOrPending(
const content::WebContents* web_contents) override;
content::FullscreenState GetFullscreenState(
const content::WebContents* web_contents) const override;
blink::mojom::DisplayMode GetDisplayMode(
const content::WebContents* web_contents) override;
blink::ProtocolHandlerSecurityLevel GetProtocolHandlerSecurityLevel(
content::RenderFrameHost* requesting_frame) override;
void RegisterProtocolHandler(content::RenderFrameHost* requesting_frame,
const std::string& protocol,
const GURL& url,
bool user_gesture) override;
void UnregisterProtocolHandler(content::RenderFrameHost* requesting_frame,
const std::string& protocol,
const GURL& url,
bool user_gesture) override;
void FindReply(content::WebContents* web_contents,
int request_id,
int number_of_matches,
const gfx::Rect& selection_rect,
int active_match_ordinal,
bool final_update) override;
void RequestPointerLock(content::WebContents* web_contents,
bool user_gesture,
bool last_unlocked_by_target) override;
void LostPointerLock() override;
bool IsWaitingForPointerLockPrompt(
content::WebContents* web_contents) override;
void RequestKeyboardLock(content::WebContents* web_contents,
bool esc_key_locked) override;
void CancelKeyboardLockRequest(content::WebContents* web_contents) override;
void RequestMediaAccessPermission(
content::WebContents* web_contents,
const content::MediaStreamRequest& request,
content::MediaResponseCallback callback) override;
void ProcessSelectAudioOutput(
const content::SelectAudioOutputRequest& request,
content::SelectAudioOutputCallback callback) override;
bool CheckMediaAccessPermission(content::RenderFrameHost* render_frame_host,
const url::Origin& security_origin,
blink::mojom::MediaStreamType type) override;
std::string GetTitleForMediaControls(
content::WebContents* web_contents) override;
#if BUILDFLAG(ENABLE_PRINTING)
void PrintCrossProcessSubframe(
content::WebContents* web_contents,
const gfx::Rect& rect,
int document_cookie,
content::RenderFrameHost* subframe_host) const override;
#endif
#if BUILDFLAG(ENABLE_PAINT_PREVIEW)
void CapturePaintPreviewOfSubframe(
content::WebContents* web_contents,
const gfx::Rect& rect,
const base::UnguessableToken& guid,
content::RenderFrameHost* render_frame_host) override;
#endif
// WebContentsCollection::Observer:
void DidFinishNavigation(
content::WebContents* web_contents,
content::NavigationHandle* navigation_handle) override;
// Overridden from WebContentsModalDialogManagerDelegate:
void SetWebContentsBlocked(content::WebContents* web_contents,
bool blocked) override;
web_modal::WebContentsModalDialogHost* GetWebContentsModalDialogHost()
override;
// Overridden from BookmarkTabHelperObserver:
void URLStarredChanged(content::WebContents* web_contents,
bool starred) override;
// Overridden from ZoomObserver:
void OnZoomControllerDestroyed(
zoom::ZoomController* zoom_controller) override;
void OnZoomChanged(
const zoom::ZoomController::ZoomChangedEventData& data) override;
// Overridden from SelectFileDialog::Listener:
void FileSelected(const ui::SelectedFileInfo& file_info, int index) override;
void FileSelectionCanceled() override;
// Overridden from ThemeServiceObserver:
void OnThemeChanged() override;
// Command and state updating ///////////////////////////////////////////////
// Handle changes to tab strip model.
void OnTabInsertedAt(content::WebContents* contents, int index);
void OnTabClosing(content::WebContents* contents);
void OnTabDetached(content::WebContents* contents, bool was_active);
void OnTabDeactivated(content::WebContents* contents);
void OnActiveTabChanged(content::WebContents* old_contents,
content::WebContents* new_contents,
int index,
int reason);
void OnTabMoved(int from_index, int to_index);
void OnTabReplacedAt(content::WebContents* old_contents,
content::WebContents* new_contents,
int index);
// Handle changes to kDevToolsAvailability preference.
void OnDevToolsAvailabilityChanged();
#if BUILDFLAG(IS_CHROMEOS)
// Handle `on_task_locked_` state changes.
void OnLockedForOnTaskUpdated();
#endif
// UI update coalescing and handling ////////////////////////////////////////
// Asks the toolbar (and as such the location bar) to update its state to
// reflect the current tab's current URL, security state, etc.
// If |should_restore_state| is true, we're switching (back?) to this tab and
// should restore any previous location bar state (such as user editing) as
// well.
void UpdateToolbar(bool should_restore_state);
// Asks the toolbar to layout and redraw to reflect the current security
// state.
void UpdateToolbarSecurityState();
// Does one or both of the following for each bit in |changed_flags|:
// . If the update should be processed immediately, it is.
// . If the update should processed asynchronously (to avoid lots of ui
// updates), then scheduled_updates_ is updated for the |source| and update
// pair and a task is scheduled (assuming it isn't running already)
// that invokes ProcessPendingUIUpdates.
void ScheduleUIUpdate(content::WebContents* source, unsigned changed_flags);
// Processes all pending updates to the UI that have been scheduled by
// ScheduleUIUpdate in scheduled_updates_.
void ProcessPendingUIUpdates();
// Removes all entries from scheduled_updates_ whose source is contents.
void RemoveScheduledUpdatesFor(content::WebContents* contents);
// Getters for UI ///////////////////////////////////////////////////////////
// Returns the list of StatusBubbles from the current toolbar. It is possible
// for this to be empty if called before the toolbar has initialized. In a
// split view, there will be multiple status bubbles with the active one
// listed first.
// TODO(beng): remove this.
std::vector<StatusBubble*> GetStatusBubbles();
// Session restore functions ////////////////////////////////////////////////
// Notifies the history database of the index for all tabs whose index is
// >= index.
void SyncHistoryWithTabs(int index);
// In-progress download termination handling /////////////////////////////////
// Called when the window is closing to check if potential in-progress
// downloads should prevent it from closing.
// Returns true if the window can close, false otherwise.
bool CanCloseWithInProgressDownloads();
// Called when the user has decided whether to proceed or not with the browser
// closure. |cancel_downloads| is true if the downloads should be canceled
// and the browser closed, false if the browser should stay open and the
// downloads running.
void InProgressDownloadResponse(bool cancel_downloads);
// Called when the user has decided whether to proceed or not with the browser
// closure, in case the cookie migration notice was shown. |proceed_closing|
// is true if the browser can be closed.
void CookieMigrationNoticeResponse(bool proceed_closing);
// Called when all warnings have completed when attempting to close the
// browser directly (e.g. via hotkey, close button, terminate signal, etc.)
// Used as a WarnBeforeClosingCallback by HandleBeforeClose().
void FinishWarnBeforeClosing(WarnBeforeClosingResult result);
// Assorted utility functions ///////////////////////////////////////////////
// Sets the specified browser as the delegate of the WebContents and all the
// associated tab helpers that are needed. If |set_delegate| is true, this
// browser object is set as a delegate for |web_contents| components, else
// is is removed as a delegate.
void SetAsDelegate(content::WebContents* web_contents, bool set_delegate);
void TabDetachedAtImpl(content::WebContents* contents,
bool was_active,
DetachType type);
// Updates the loading state for the window and tabstrip.
void UpdateWindowForLoadingStateChanged(content::WebContents* source,
bool should_show_loading_ui);
// Shared code between Reload() and ReloadBypassingCache().
void ReloadInternal(WindowOpenDisposition disposition, bool bypass_cache);
// See comment on SupportsWindowFeatureImpl for info on `check_can_support`.
bool NormalBrowserSupportsWindowFeature(WindowFeature feature,
bool check_can_support) const;
// See comment on SupportsWindowFeatureImpl for info on `check_can_support`.
bool PopupBrowserSupportsWindowFeature(WindowFeature feature,
bool check_can_support) const;
// See comment on SupportsWindowFeatureImpl for info on `check_can_support`.
bool AppPopupBrowserSupportsWindowFeature(WindowFeature feature,
bool check_can_support) const;
// See comment on SupportsWindowFeatureImpl for info on `check_can_support`.
bool AppBrowserSupportsWindowFeature(WindowFeature feature,
bool check_can_support) const;
#if BUILDFLAG(IS_CHROMEOS)
// See comment on SupportsWindowFeatureImpl for info on `check_can_support`.
bool CustomTabBrowserSupportsWindowFeature(WindowFeature feature) const;
#endif
// See comment on SupportsWindowFeatureImpl for info on `check_can_support`.
bool PictureInPictureBrowserSupportsWindowFeature(
WindowFeature feature,
bool check_can_support) const;
// Implementation of SupportsWindowFeature and CanSupportWindowFeature. If
// `check_can_support` is true, this method returns true if this type of
// browser can ever support `feature`, under any conditions; if
// `check_can_support` is false, it returns true if the browser *in its
// current state* (e.g. whether or not it is currently fullscreen) supports
// `feature`.
bool SupportsWindowFeatureImpl(WindowFeature feature,
bool check_can_support) const;
// Resets |bookmark_bar_state_| based on the active tab. Notifies the
// BrowserWindow if necessary.
void UpdateBookmarkBarState(BookmarkBarStateChangeReason reason);
bool ShouldShowBookmarkBar() const;
// Returns true if we can start the shutdown sequence for the browser, i.e.
// the last browser window is being closed.
bool ShouldStartShutdown() const;
// Returns true if a BackgroundContents should be created in response to a
// WebContents::CreateNewWindow() call.
bool ShouldCreateBackgroundContents(
content::SiteInstance* source_site_instance,
const GURL& opener_url,
const std::string& frame_name);
// Creates a BackgroundContents. This should only be called when
// ShouldCreateBackgroundContents() is true.
BackgroundContents* CreateBackgroundContents(
content::SiteInstance* source_site_instance,
content::RenderFrameHost* opener,
const GURL& opener_url,
bool is_new_browsing_instance,
const std::string& frame_name,
const GURL& target_url,
const content::StoragePartitionConfig& partition_config,
content::SessionStorageNamespace* session_storage_namespace);
void UpdateTabGroupSessionDataForTab(
tabs::TabInterface* tab,
std::optional<tab_groups::TabGroupId> group);
// Create `FindBarController` if it does not exist.
// TODO(crbug.com/423956131): Convert to `GetFindBarController` which returns
// existing `FindBarController`.
FindBarController* CreateOrGetFindBarController();
// Returns true if a `FindBarController` exists for this browser.
// TODO(crbug.com/423956131): Remove this function.
bool HasFindBarController();
// Data members /////////////////////////////////////////////////////////////
PrefChangeRegistrar profile_pref_registrar_;
// This Browser's create params.
const CreateParams create_params_;
// This Browser's type.
const Type type_;
// This Browser's profile.
const raw_ptr<Profile, AcrossTasksDanglingUntriaged> profile_;
// Prevent Profile deletion until this browser window is closed.
std::unique_ptr<ScopedProfileKeepAlive> profile_keep_alive_;
// This Browser's window.
raw_ptr<BrowserWindow, DanglingUntriaged> window_;
// The active state of this browser.
bool is_active_ = false;
std::unique_ptr<TabStripModelDelegate> const tab_strip_model_delegate_;
std::unique_ptr<TabStripModel> const tab_strip_model_;
// The application name that is also the name of the window to the shell.
// This name should be set when:
// 1) we launch an application via an application shortcut or extension API.
// 2) we launch an undocked devtool window.
const std::string app_name_;
// True if the source is trusted (i.e. we do not need to show the URL in a
// a popup window). Also used to determine which app windows to save and
// restore on Chrome OS.
bool is_trusted_source_;
// Unique identifier of this browser for session restore. This id is only
// unique within the current session, and is not guaranteed to be unique
// across sessions.
const SessionID session_id_;
// Whether this Browser should be omitted from being saved/restored by session
// restore.
bool omit_from_session_restore_ = false;
// If true, a new window opening should be treated like the start of a session
// (with potential session restore, startup URLs, etc.). Otherwise, don't
// restore the session.
const bool should_trigger_session_restore_;
// UI update coalescing and handling ////////////////////////////////////////
typedef std::map<const content::WebContents*, int> UpdateMap;
// Maps from WebContents to pending UI updates that need to be processed.
// We don't update things like the URL or tab title right away to avoid
// flickering and extra painting.
// See ScheduleUIUpdate and ProcessPendingUIUpdates.
UpdateMap scheduled_updates_;
// In-progress download termination handling /////////////////////////////////
enum CancelDownloadConfirmationState {
NOT_PROMPTED, // We have not asked the user.
WAITING_FOR_RESPONSE, // We have asked the user and have not received a
// reponse yet.
RESPONSE_RECEIVED // The user was prompted and made a decision already.
};
// State used to figure-out whether we should prompt the user for confirmation
// when the browser is closed with in-progress downloads.
CancelDownloadConfirmationState cancel_download_confirmation_state_;
/////////////////////////////////////////////////////////////////////////////
// Override values for the bounds of the window and its maximized or minimized
// state.
// These are supplied by callers that don't want to use the default values.
// The default values are typically loaded from local state (last session),
// obtained from the last window of the same type, or obtained from the
// shell shortcut's startup info.
gfx::Rect override_bounds_;
ui::mojom::WindowShowState initial_show_state_;
const std::string initial_workspace_;
bool initial_visible_on_all_workspaces_state_;
CreationSource creation_source_ = CreationSource::kUnknown;
UnloadController unload_controller_;
// Dialog box used for opening and saving files.
scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
// Helper which implements the ContentSettingBubbleModel interface.
std::unique_ptr<BrowserContentSettingBubbleModelDelegate>
content_setting_bubble_model_delegate_;
// Helper which implements the LiveTabContext interface.
std::unique_ptr<BrowserLiveTabContext> live_tab_context_;
// Helper which handles bookmark app specific browser configuration.
// This must be initialized before |command_controller_| to ensure the correct
// set of commands are enabled.
const std::unique_ptr<web_app::AppBrowserController> app_controller_;
BookmarkBar::State bookmark_bar_state_;
std::unique_ptr<BrowserActions> browser_actions_;
std::unique_ptr<chrome::BrowserCommandController> command_controller_;
// True if the browser window has been shown at least once.
bool window_has_shown_;
std::string user_title_;
// Listens for browser-related breadcrumb events to be added to crash reports.
std::unique_ptr<BreadcrumbManagerBrowserAgent>
breadcrumb_manager_browser_agent_;
std::unique_ptr<ScopedKeepAlive> keep_alive_;
WarnBeforeClosingCallback warn_before_closing_callback_;
// Tells if the browser should skip warning the user when closing the window.
bool force_skip_warning_user_on_close_ = false;
// If true, immediately updates the UI when scheduled.
bool update_ui_immediately_for_testing_ = false;
#if BUILDFLAG(IS_CHROMEOS)
// OnTask is a ChromeOS feature that is not related to web browsers, but
// happens to be implemented using code in //chrome/browser. The feature,
// when enabled, disables certain functionality that a web browser would
// never typically disable.
bool on_task_locked_ = false;
#endif
#if BUILDFLAG(ENABLE_EXTENSIONS)
std::unique_ptr<extensions::ExtensionBrowserWindowHelper>
extension_browser_window_helper_;
#endif
const base::ElapsedTimer creation_timer_;
// The opener browser of the document picture-in-picture browser. Null if the
// current browser is a regular browser.
raw_ptr<Browser> opener_browser_ = nullptr;
WebContentsCollection web_contents_collection_{this};
// If true, the Browser window has been closed and this will be deleted
// shortly (after a PostTask).
bool is_delete_scheduled_ = false;
// Do not use this. Instead, create a views::Widget and use helpers like
// TabDialogManager.
// If true, the browser window was created as a tab modal pop-up. This is
// determined by the NavigateParams::is_tab_modal_popup_deprecated.
bool is_tab_modal_popup_deprecated_ = false;
#if defined(USE_AURA)
std::unique_ptr<OverscrollPrefManager> overscroll_pref_manager_;
#endif
int force_show_bookmark_bar_flags_ = ForceShowBookmarkBarFlag::kNone;
using BrowserDidCloseCallbackList =
base::RepeatingCallbackList<void(BrowserWindowInterface*)>;
BrowserDidCloseCallbackList browser_did_close_callback_list_;
using DidActiveTabChangeCallbackList =
base::RepeatingCallbackList<void(BrowserWindowInterface*)>;
DidActiveTabChangeCallbackList did_active_tab_change_callback_list_;
using DidBecomeActiveCallbackList =
base::RepeatingCallbackList<void(BrowserWindowInterface*)>;
DidBecomeActiveCallbackList did_become_active_callback_list_;
using DidBecomeInactiveCallbackList =
base::RepeatingCallbackList<void(BrowserWindowInterface*)>;
DidBecomeInactiveCallbackList did_become_inactive_callback_list_;
std::unique_ptr<BrowserWindowFeatures> features_;
#if BUILDFLAG(IS_OZONE)
// If supported by the platform, this stores stores data related to the
// windowing system level session. E.g: session and window IDs. See
// ui/ozone/public/platform_session_manager.h for more details.
std::optional<ui::PlatformSessionWindowData> platform_session_data_ =
std::nullopt;
#endif
// Tracks whether a modal UI is showing.
bool showing_call_to_action_ = false;
UnownedUserDataHost unowned_user_data_host_;
// The following factory is used for chrome update coalescing.
base::WeakPtrFactory<Browser> chrome_updater_factory_{this};
// The following factory is used to close the frame at a later time.
base::WeakPtrFactory<Browser> weak_factory_{this};
};
#endif // CHROME_BROWSER_UI_BROWSER_H_
|