File: target_handler.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (158 lines) | stat: -rw-r--r-- 5,340 bytes parent folder | download | duplicates (5)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "headless/lib/browser/protocol/target_handler.h"

#include <ranges>
#include <string_view>

#include "build/build_config.h"
#include "headless/lib/browser/headless_browser_context_impl.h"
#include "headless/lib/browser/headless_browser_impl.h"
#include "headless/lib/browser/headless_web_contents_impl.h"
#include "headless/public/headless_window_state.h"
#include "ui/gfx/geometry/rect.h"

namespace headless {
namespace protocol {

TargetHandler::TargetHandler(HeadlessBrowserImpl* browser)
    : browser_(browser) {}

TargetHandler::~TargetHandler() = default;

void TargetHandler::Wire(UberDispatcher* dispatcher) {
  Target::Dispatcher::wire(dispatcher, this);
}

Response TargetHandler::Disable() {
  while (!hidden_web_contents_.empty()) {
    // Destroy all existing hidden targets when session is closed. Some of them
    // can be already closed.
    auto target_id = *hidden_web_contents_.begin();
    auto agent_host = content::DevToolsAgentHost::GetForId(target_id);
    if (agent_host) {
      // The target is still alive, so destroy it.
      agent_host->Close();
    }
    hidden_web_contents_.erase(hidden_web_contents_.begin());
  }
  return Response::Success();
}

Response TargetHandler::CreateTarget(
    const std::string& url,
    std::optional<int> left,
    std::optional<int> top,
    std::optional<int> width,
    std::optional<int> height,
    std::optional<std::string> window_state,
    std::optional<std::string> context_id,
    std::optional<bool> enable_begin_frame_control,
    std::optional<bool> new_window,
    std::optional<bool> background,
    std::optional<bool> for_tab,
    std::optional<bool> hidden,
    std::string* out_target_id) {
#if BUILDFLAG(IS_MAC)
  if (enable_begin_frame_control.value_or(false)) {
    return Response::ServerError(
        "BeginFrameControl is not supported on MacOS yet");
  }
#endif

  std::optional<HeadlessWindowState> headless_window_state;
  if (window_state) {
    headless_window_state = GetWindowStateFromProtocol(*window_state);
    if (!headless_window_state) {
      return Response::InvalidParams("Invalid target window state: " +
                                     *window_state);
    }
  }

  HeadlessBrowserContext* context;
  if (context_id.has_value()) {
    context = browser_->GetBrowserContextForId(context_id.value());
    if (!context)
      return Response::InvalidParams("browserContextId");
  } else {
    context = browser_->GetDefaultBrowserContext();
    if (!context) {
      return Response::ServerError(
          "You specified no |browserContextId|, but "
          "there is no default browser context set on "
          "HeadlessBrowser");
    }
  }

  GURL gurl(url);
  if (gurl.is_empty()) {
    gurl = GURL(url::kAboutBlankURL);
  }

  if (hidden.value_or(false)) {
    if (for_tab.value_or(false)) {
      return protocol::Response::InvalidParams(
          "Hidden target cannot be created for tab");
    }
    if (new_window) {
      return protocol::Response::InvalidParams(
          "Hidden target cannot be created in a new window");
    }
    if (!background.value_or(true)) {
      return protocol::Response::InvalidParams(
          "Hidden target can be created only in background");
    }

    // Create a hidden target.
    HeadlessWebContentsImpl* web_contents_impl = HeadlessWebContentsImpl::From(
        context->CreateWebContentsBuilder().SetInitialURL(gurl).Build());

    *out_target_id = content::DevToolsAgentHost::GetOrCreateFor(
                         web_contents_impl->web_contents())
                         ->GetId();
    // Keep hidden target's ID in the hidden_web_contents_ to close it when the
    // session is closed.
    hidden_web_contents_.insert(*out_target_id);
    return Response::Success();
  }

  const gfx::Rect target_window_bounds(
      left.value_or(0), top.value_or(0),
      width.value_or(browser_->options()->window_size.width()),
      height.value_or(browser_->options()->window_size.height()));

  const HeadlessWindowState target_window_state =
      headless_window_state.value_or(HeadlessWindowState::kNormal);

  HeadlessWebContentsImpl* web_contents_impl = HeadlessWebContentsImpl::From(
      context->CreateWebContentsBuilder()
          .SetInitialURL(gurl)
          .SetWindowBounds(target_window_bounds)
          .SetWindowState(target_window_state)
          .SetEnableBeginFrameControl(
              enable_begin_frame_control.value_or(false))
          .Build());

  content::WebContents* wc = web_contents_impl->web_contents();
  auto devtools_agent_host =
      for_tab.value_or(false)
          ? content::DevToolsAgentHost::GetOrCreateForTab(wc)
          : content::DevToolsAgentHost::GetOrCreateFor(wc);
  *out_target_id = devtools_agent_host->GetId();
  return Response::Success();
}

Response TargetHandler::CloseTarget(const std::string& target_id,
                                    bool* out_success) {
  auto agent_host = content::DevToolsAgentHost::GetForId(target_id);
  if (!agent_host) {
    return Response::InvalidParams("No target found for targetId");
  }
  *out_success = agent_host->Close();
  return Response::Success();
}

}  // namespace protocol
}  // namespace headless