File: devtools_frontend.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 (286 lines) | stat: -rw-r--r-- 10,381 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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/webui/examples/browser/devtools/devtools_frontend.h"

#include <map>
#include <memory>
#include <string_view>

#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/uuid.h"
#include "base/values.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_frontend_host.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "ipc/ipc_channel.h"
#include "ui/webui/examples/browser/devtools/devtools_server.h"
#include "url/gurl.h"

namespace webui_examples {

namespace {

static GURL GetFrontendURL() {
  return GURL(
      base::StringPrintf("http://127.0.0.1:%d/devtools/devtools_app.html",
                         devtools::GetHttpHandlerPort()));
}

// This constant should be in sync with
// the constant
// kMaxMessageChunkSize in chrome/browser/devtools/devtools_ui_bindings.cc.
constexpr size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4;

}  // namespace

class DevToolsFrontend::AgentHostClient
    : public content::WebContentsObserver,
      public content::DevToolsAgentHostClient {
 public:
  AgentHostClient(content::WebContents* devtools_contents,
                  content::WebContents* inspected_contents)
      : content::WebContentsObserver(devtools_contents),
        devtools_contents_(devtools_contents),
        inspected_contents_(inspected_contents) {}
  AgentHostClient(const AgentHostClient&) = delete;
  AgentHostClient& operator=(const AgentHostClient&) = delete;
  ~AgentHostClient() override = default;

  // content::DevToolsAgentHostClient
  void DispatchProtocolMessage(content::DevToolsAgentHost* agent_host,
                               base::span<const uint8_t> message) override {
    std::string_view str_message(reinterpret_cast<const char*>(message.data()),
                                 message.size());
    if (str_message.length() < kMaxMessageChunkSize) {
      CallClientFunction("DevToolsAPI", "dispatchMessage",
                         base::Value(std::string(str_message)));
    } else {
      size_t total_size = str_message.length();
      for (size_t pos = 0; pos < str_message.length();
           pos += kMaxMessageChunkSize) {
        std::string_view str_message_chunk =
            str_message.substr(pos, kMaxMessageChunkSize);

        CallClientFunction(
            "DevToolsAPI", "dispatchMessageChunk",
            base::Value(std::string(str_message_chunk)),
            base::Value(base::NumberToString(pos ? 0 : total_size)));
      }
    }
  }

  void AgentHostClosed(content::DevToolsAgentHost* agent_host) override {}

  void Attach() {
    if (agent_host_)
      agent_host_->DetachClient(this);

    agent_host_ =
        content::DevToolsAgentHost::GetOrCreateFor(inspected_contents_);
    agent_host_->AttachClient(this);
  }

  void CallClientFunction(
      const std::string& object_name,
      const std::string& method_name,
      base::Value arg1 = {},
      base::Value arg2 = {},
      base::Value arg3 = {},
      base::OnceCallback<void(base::Value)> cb = base::NullCallback()) {
    content::RenderFrameHost* host = devtools_contents_->GetPrimaryMainFrame();
    host->AllowInjectingJavaScript();

    base::Value::List arguments;
    if (!arg1.is_none()) {
      arguments.Append(std::move(arg1));
      if (!arg2.is_none()) {
        arguments.Append(std::move(arg2));
        if (!arg3.is_none()) {
          arguments.Append(std::move(arg3));
        }
      }
    }

    host->ExecuteJavaScriptMethod(base::ASCIIToUTF16(object_name),
                                  base::ASCIIToUTF16(method_name),
                                  std::move(arguments), std::move(cb));
  }

 private:
  // content::WebContentsObserver:
  void ReadyToCommitNavigation(
      content::NavigationHandle* navigation_handle) override {
    content::RenderFrameHost* frame = navigation_handle->GetRenderFrameHost();
    // TODO(crbug.com/40185886): With MPArch there may be multiple main
    // frames. This caller was converted automatically to the primary main frame
    // to preserve its semantics. Follow up to confirm correctness.
    if (navigation_handle->IsInPrimaryMainFrame()) {
      frontend_host_ = content::DevToolsFrontendHost::Create(
          frame, base::BindRepeating(
                     &AgentHostClient::HandleMessageFromDevToolsFrontend,
                     base::Unretained(this)));
      return;
    }
    std::string origin =
        navigation_handle->GetURL().DeprecatedGetOriginAsURL().spec();
    auto it = extensions_api_.find(origin);
    if (it == extensions_api_.end())
      return;
    std::string script = base::StringPrintf(
        "%s(\"%s\")", it->second.c_str(),
        base::Uuid::GenerateRandomV4().AsLowercaseString().c_str());
    content::DevToolsFrontendHost::SetupExtensionsAPI(frame, script);
  }

  void HandleMessageFromDevToolsFrontend(base::Value::Dict message) {
    const std::string* method = message.FindString("method");
    if (!method)
      return;

    int request_id = message.FindInt("id").value_or(0);
    base::Value::List* params_value = message.FindList("params");

    // Since we've received message by value, we can take the list.
    base::Value::List params;
    if (params_value) {
      params = std::move(*params_value);
    }

    if (*method == "dispatchProtocolMessage" && params.size() == 1) {
      const std::string* protocol_message = params[0].GetIfString();
      if (!agent_host_ || !protocol_message)
        return;
      agent_host_->DispatchProtocolMessage(
          this, base::as_byte_span(*protocol_message));
    } else if (*method == "loadCompleted") {
      CallClientFunction("DevToolsAPI", "setUseSoftMenu", base::Value(true));
    } else if (*method == "loadNetworkResource" && params.size() == 3) {
      // TODO(robliao): Add support for this if necessary.
      NOTREACHED();
    } else if (*method == "getPreferences") {
      SendMessageAck(request_id, base::Value(std::move(preferences_)));
      return;
    } else if (*method == "getHostConfig") {
      base::Value::Dict response_dict;

      // Chrome's DevToolsUIBindings sets feature flag values to this
      // devToolsVeLogging dictionary, but they're not accessible from //ui.
      // Just set the default values instead.
      base::Value::Dict ve_logging_dict;
      ve_logging_dict.Set("enabled", true);
      ve_logging_dict.Set("testing", false);
      response_dict.Set("devToolsVeLogging", std::move(ve_logging_dict));

      SendMessageAck(request_id, base::Value(std::move(response_dict)));
      return;
    } else if (*method == "setPreference") {
      if (params.size() < 2)
        return;
      const std::string* name = params[0].GetIfString();

      // We're just setting params[1] as a value anyways, so just make sure it's
      // the type we want, but don't worry about getting it.
      if (!name || !params[1].is_string())
        return;

      preferences_.Set(*name, std::move(params[1]));
    } else if (*method == "removePreference") {
      const std::string* name = params[0].GetIfString();
      if (!name)
        return;
      preferences_.Remove(*name);
    } else if (*method == "requestFileSystems") {
      CallClientFunction("DevToolsAPI", "fileSystemsLoaded",
                         base::Value(base::Value::Type::LIST));
    } else if (*method == "reattach") {
      if (!agent_host_)
        return;
      agent_host_->DetachClient(this);
      agent_host_->AttachClient(this);
    } else if (*method == "registerExtensionsAPI") {
      if (params.size() < 2)
        return;
      const std::string* origin = params[0].GetIfString();
      const std::string* script = params[1].GetIfString();
      if (!origin || !script)
        return;
      extensions_api_[*origin + "/"] = *script;
    } else {
      return;
    }

    if (request_id)
      SendMessageAck(request_id, {});
  }

  void SendMessageAck(int request_id, base::Value arg) {
    CallClientFunction("DevToolsAPI", "embedderMessageAck",
                       base::Value(request_id), std::move(arg));
  }

  const raw_ptr<content::WebContents> devtools_contents_;
  const raw_ptr<content::WebContents> inspected_contents_;

  scoped_refptr<content::DevToolsAgentHost> agent_host_;
  std::unique_ptr<content::DevToolsFrontendHost> frontend_host_;

  std::map<std::string, std::string> extensions_api_;

  base::Value::Dict preferences_;
};

class DevToolsFrontend::Pointer : public content::WebContentsUserData<Pointer> {
 public:
  ~Pointer() override = default;

  static DevToolsFrontend* Create(content::WebContents* web_contents) {
    CreateForWebContents(web_contents);
    Pointer* ptr = FromWebContents(web_contents);
    return ptr->Get();
  }

  DevToolsFrontend* Get() { return ptr_.get(); }

 private:
  friend class content::WebContentsUserData<Pointer>;
  Pointer(content::WebContents* web_contents)
      : content::WebContentsUserData<Pointer>(*web_contents),
        ptr_(new DevToolsFrontend(web_contents)) {}
  Pointer(const Pointer*) = delete;
  Pointer& operator=(const Pointer&) = delete;

  WEB_CONTENTS_USER_DATA_KEY_DECL();

  std::unique_ptr<DevToolsFrontend> ptr_;
};

WEB_CONTENTS_USER_DATA_KEY_IMPL(DevToolsFrontend::Pointer);

DevToolsFrontend::DevToolsFrontend(content::WebContents* inspected_contents)
    : frontend_url_(GetFrontendURL()),
      inspected_contents_(inspected_contents) {}
DevToolsFrontend::~DevToolsFrontend() = default;

// static
DevToolsFrontend* DevToolsFrontend::CreateAndGet(
    content::WebContents* inspected_contents) {
  return DevToolsFrontend::Pointer::Create(inspected_contents);
}

void DevToolsFrontend::SetDevtoolsWebContents(
    content::WebContents* devtools_contents) {
  devtools_contents_ = devtools_contents;
  agent_host_client_ = std::make_unique<AgentHostClient>(devtools_contents_,
                                                         inspected_contents_);
  agent_host_client_->Attach();
}

}  // namespace webui_examples