File: browser_extension_window_controller.cc

package info (click to toggle)
chromium 140.0.7339.80-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,201,348 kB
  • sloc: cpp: 35,092,378; ansic: 7,161,671; javascript: 4,199,703; python: 1,441,798; asm: 949,904; xml: 747,409; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,119; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (325 lines) | stat: -rw-r--r-- 10,840 bytes parent folder | download | duplicates (4)
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
// 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.

#include "chrome/browser/extensions/browser_extension_window_controller.h"

#include <optional>
#include <string>

#include "base/check_deref.h"
#include "base/notimplemented.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/window_controller_list.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/tabs/tab_list_interface.h"
#include "chrome/common/extensions/api/tabs.h"
#include "chrome/common/webui_url_constants.h"
#include "components/sessions/core/session_id.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_handlers/incognito_info.h"
#include "extensions/common/manifest_handlers/options_page_info.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "ui/base/base_window.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
#include "chrome/browser/ui/singleton_tabs.h"
#endif

namespace extensions {

namespace {

constexpr char kAlwaysOnTopKey[] = "alwaysOnTop";
constexpr char kFocusedKey[] = "focused";
constexpr char kHeightKey[] = "height";
constexpr char kIncognitoKey[] = "incognito";
constexpr char kLeftKey[] = "left";
constexpr char kShowStateKey[] = "state";
constexpr char kTopKey[] = "top";
constexpr char kWidthKey[] = "width";
constexpr char kWindowTypeKey[] = "type";
constexpr char kShowStateValueNormal[] = "normal";
constexpr char kShowStateValueMinimized[] = "minimized";
constexpr char kShowStateValueMaximized[] = "maximized";
constexpr char kShowStateValueFullscreen[] = "fullscreen";
#if !BUILDFLAG(IS_ANDROID)
constexpr char kShowStateValueLockedFullscreen[] = "locked-fullscreen";
#endif

api::tabs::WindowType GetTabsWindowType(const BrowserWindowInterface* browser) {
#if BUILDFLAG(IS_ANDROID)
  return api::tabs::WindowType::kNormal;
#else
  using BrowserType = BrowserWindowInterface::Type;
  const BrowserType type = browser->GetType();
  if (type == BrowserType::TYPE_DEVTOOLS) {
    return api::tabs::WindowType::kDevtools;
  }
  // Browser::TYPE_APP_POPUP is considered 'popup' rather than 'app' since
  // chrome.windows.create({type: 'popup'}) uses
  // Browser::CreateParams::CreateForAppPopup().
  if (type == BrowserType::TYPE_POPUP || type == BrowserType::TYPE_APP_POPUP) {
    return api::tabs::WindowType::kPopup;
  }
  if (type == BrowserType::TYPE_APP) {
    return api::tabs::WindowType::kApp;
  }
  return api::tabs::WindowType::kNormal;
#endif
}

}  // anonymous namespace

DEFINE_USER_DATA(BrowserExtensionWindowController);

BrowserExtensionWindowController::BrowserExtensionWindowController(
    BrowserWindowInterface* browser)
    : WindowController(browser->GetWindow(), browser->GetProfile()),
      browser_(CHECK_DEREF(browser)),
#if !BUILDFLAG(IS_ANDROID)
      window_(CHECK_DEREF(browser->GetBrowserForMigrationOnly()->window())),
      tab_list_(CHECK_DEREF(TabListInterface::From(browser))),
#endif  // !BUILDFLAG(IS_ANDROID)
      session_id_(browser->GetSessionID()),
      window_type_(GetTabsWindowType(browser)),
      scoped_data_holder_(browser->GetUnownedUserDataHost(), *this) {
  WindowControllerList::GetInstance()->AddExtensionWindow(this);
}

BrowserExtensionWindowController::~BrowserExtensionWindowController() {
  WindowControllerList::GetInstance()->RemoveExtensionWindow(this);
}

BrowserExtensionWindowController* BrowserExtensionWindowController::From(
    BrowserWindowInterface* browser_window_interface) {
  return ui::ScopedUnownedUserData<BrowserExtensionWindowController>::Get(
      browser_window_interface->GetUnownedUserDataHost());
}

int BrowserExtensionWindowController::GetWindowId() const {
  return static_cast<int>(session_id_.id());
}

std::string BrowserExtensionWindowController::GetWindowTypeText() const {
  return api::tabs::ToString(window_type_);
}

void BrowserExtensionWindowController::SetFullscreenMode(
    bool is_fullscreen,
    const GURL& extension_url) const {
#if BUILDFLAG(IS_ANDROID)
  NOTIMPLEMENTED();
#else
  if (window_->IsFullscreen() != is_fullscreen) {
    GetBrowser()->ToggleFullscreenModeWithExtension(extension_url);
  }
#endif
}

bool BrowserExtensionWindowController::CanClose(Reason* reason) const {
#if BUILDFLAG(IS_ANDROID)
  NOTIMPLEMENTED();
#else
  // Don't let an extension remove the window if the user is dragging tabs
  // in that window.
  if (!window_->IsTabStripEditable()) {
    *reason = WindowController::REASON_NOT_EDITABLE;
    return false;
  }
#endif
  return true;
}

BrowserWindowInterface*
BrowserExtensionWindowController::GetBrowserWindowInterface() {
  return &browser_.get();
}

#if !BUILDFLAG(IS_ANDROID)
Browser* BrowserExtensionWindowController::GetBrowser() const {
  return browser_->GetBrowserForMigrationOnly();
}
#endif

bool BrowserExtensionWindowController::IsDeleteScheduled() const {
#if BUILDFLAG(IS_ANDROID)
  NOTIMPLEMENTED();
  return false;
#else
  return GetBrowser()->is_delete_scheduled();
#endif
}

content::WebContents* BrowserExtensionWindowController::GetActiveTab() const {
#if BUILDFLAG(IS_ANDROID)
  NOTIMPLEMENTED();
  return nullptr;
#else
  // In some situations, especially tests, there may not be an active tab.
  tabs::TabInterface* active_tab = tab_list_->GetActiveTab();
  return active_tab ? active_tab->GetContents() : nullptr;
#endif
}

bool BrowserExtensionWindowController::HasEditableTabStrip() const {
#if BUILDFLAG(IS_ANDROID)
  NOTIMPLEMENTED();
  return true;
#else
  return window_->IsTabStripEditable();
#endif
}

int BrowserExtensionWindowController::GetTabCount() const {
#if BUILDFLAG(IS_ANDROID)
  NOTIMPLEMENTED();
  return 0;
#else
  return tab_list_->GetTabCount();
#endif
}

content::WebContents* BrowserExtensionWindowController::GetWebContentsAt(
    int i) const {
#if BUILDFLAG(IS_ANDROID)
  NOTIMPLEMENTED();
  return nullptr;
#else
  return tab_list_->GetTab(i)->GetContents();
#endif
}

bool BrowserExtensionWindowController::IsVisibleToTabsAPIForExtension(
    const Extension* extension,
    bool allow_dev_tools_windows) const {
  // TODO(joelhockey): We are assuming that the caller is webui when |extension|
  // is null and allowing access to all windows. It would be better if we could
  // pass in mojom::ContextType or some way to detect caller type.
  // Platform apps can only see their own windows.
  if (extension && extension->is_platform_app()) {
    return false;
  }

  return (window_type_ != api::tabs::WindowType::kDevtools) ||
         allow_dev_tools_windows;
}

base::Value::Dict
BrowserExtensionWindowController::CreateWindowValueForExtension(
    const Extension* extension,
    PopulateTabBehavior populate_tab_behavior,
    mojom::ContextType context) const {
  base::Value::Dict dict;

  dict.Set(extension_misc::kId, session_id_.id());
  dict.Set(kWindowTypeKey, GetWindowTypeText());
  dict.Set(kFocusedKey, window()->IsActive());
  dict.Set(kIncognitoKey, profile()->IsOffTheRecord());
  dict.Set(kAlwaysOnTopKey,
           window()->GetZOrderLevel() == ui::ZOrderLevel::kFloatingWindow);

  const std::string_view window_state = [&]() {
    if (window()->IsMinimized()) {
      return kShowStateValueMinimized;
    } else if (window()->IsFullscreen()) {
#if !BUILDFLAG(IS_ANDROID)
      if (platform_util::IsBrowserLockedFullscreen(GetBrowser())) {
        return kShowStateValueLockedFullscreen;
      }
#endif
      return kShowStateValueFullscreen;
    } else if (window()->IsMaximized()) {
      return kShowStateValueMaximized;
    }
    return kShowStateValueNormal;
  }();
  dict.Set(kShowStateKey, window_state);

  const gfx::Rect bounds = window()->IsMinimized()
                               ? window()->GetRestoredBounds()
                               : window()->GetBounds();
  dict.Set(kLeftKey, bounds.x());
  dict.Set(kTopKey, bounds.y());
  dict.Set(kWidthKey, bounds.width());
  dict.Set(kHeightKey, bounds.height());

  if (populate_tab_behavior == kPopulateTabs) {
    dict.Set(ExtensionTabUtil::kTabsKey, CreateTabList(extension, context));
  }

  return dict;
}

base::Value::List BrowserExtensionWindowController::CreateTabList(
    const Extension* extension,
    mojom::ContextType context) const {
  base::Value::List tab_list;

#if BUILDFLAG(IS_ANDROID)
  NOTIMPLEMENTED();
#else
  const int tab_count = tab_list_->GetTabCount();

  for (int i = 0; i < tab_count; ++i) {
    content::WebContents* web_contents = tab_list_->GetTab(i)->GetContents();
    CHECK(web_contents);
    const ExtensionTabUtil::ScrubTabBehavior scrub_tab_behavior =
        ExtensionTabUtil::GetScrubTabBehavior(extension, context, web_contents);
    tab_list.Append(
        ExtensionTabUtil::CreateTabObject(web_contents, scrub_tab_behavior,
                                          extension, &tab_list_.get(), i)
            .ToValue());
  }
#endif

  return tab_list;
}

bool BrowserExtensionWindowController::OpenOptionsPage(
    const Extension* extension,
    const GURL& url,
    bool open_in_tab) {
  DCHECK(OptionsPageInfo::HasOptionsPage(extension));

#if BUILDFLAG(IS_ANDROID)
  NOTIMPLEMENTED();
#else
  // Force the options page to open in non-OTR window if the extension is not
  // running in split mode, because it won't be able to save settings from OTR.
  // This version of OpenOptionsPage() can be called from an OTR window via e.g.
  // the action menu, since that's not initiated by the extension.
  Browser* browser_to_use = GetBrowser();
  std::optional<chrome::ScopedTabbedBrowserDisplayer> displayer;
  if (profile()->IsOffTheRecord() && !IncognitoInfo::IsSplitMode(extension)) {
    displayer.emplace(profile()->GetOriginalProfile());
    browser_to_use = displayer->browser();
  }

  // We need to respect path differences because we don't want opening the
  // options page to close a page that might be open to extension content.
  // However, if the options page opens inside the chrome://extensions page, we
  // can override an existing page.
  // Note: ref behavior is to ignore.
  ShowSingletonTabOverwritingNTP(browser_to_use, url,
                                 open_in_tab
                                     ? NavigateParams::RESPECT
                                     : NavigateParams::IGNORE_AND_NAVIGATE);
#endif

  return true;
}

bool BrowserExtensionWindowController::SupportsTabs() {
  return window_type_ != api::tabs::WindowType::kDevtools;
}

}  // namespace extensions