2016-10-17 20:14:44 +02:00
|
|
|
// Copyright 2013 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2013-11-08 17:06:06 +01:00
|
|
|
|
2019-02-12 19:43:44 +01:00
|
|
|
#include "libcef/browser/devtools/devtools_frontend.h"
|
2013-11-08 17:06:06 +01:00
|
|
|
|
2016-10-17 20:14:44 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2020-06-13 02:54:08 +02:00
|
|
|
#include <iomanip>
|
2017-07-27 01:19:27 +02:00
|
|
|
#include <utility>
|
|
|
|
|
2017-02-28 23:40:28 +01:00
|
|
|
#include "libcef/browser/browser_context.h"
|
2019-02-12 19:43:44 +01:00
|
|
|
#include "libcef/browser/devtools/devtools_manager_delegate.h"
|
2016-10-17 20:14:44 +02:00
|
|
|
#include "libcef/browser/net/devtools_scheme_handler.h"
|
2020-06-13 02:54:08 +02:00
|
|
|
#include "libcef/common/cef_switches.h"
|
2020-06-28 23:05:36 +02:00
|
|
|
#include "libcef/common/task_runner_manager.h"
|
2013-11-08 17:06:06 +01:00
|
|
|
|
2019-06-05 17:12:29 +02:00
|
|
|
#include "base/base64.h"
|
2020-06-13 02:54:08 +02:00
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/files/file_util.h"
|
2017-07-27 01:19:27 +02:00
|
|
|
#include "base/guid.h"
|
2014-09-04 19:53:40 +02:00
|
|
|
#include "base/json/json_reader.h"
|
2014-12-13 21:18:31 +01:00
|
|
|
#include "base/json/json_writer.h"
|
2016-06-21 00:59:23 +02:00
|
|
|
#include "base/json/string_escape.h"
|
2016-10-17 20:14:44 +02:00
|
|
|
#include "base/macros.h"
|
2017-07-27 01:19:27 +02:00
|
|
|
#include "base/memory/ptr_util.h"
|
2014-09-04 19:53:40 +02:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2020-08-29 00:39:23 +02:00
|
|
|
#include "base/strings/string_util.h"
|
2015-03-04 02:00:13 +01:00
|
|
|
#include "base/strings/stringprintf.h"
|
2013-12-17 23:04:35 +01:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2018-10-02 14:14:11 +02:00
|
|
|
#include "base/task/post_task.h"
|
2015-03-04 02:00:13 +01:00
|
|
|
#include "base/values.h"
|
2020-07-01 02:57:00 +02:00
|
|
|
#include "chrome/browser/profiles/profile.h"
|
2017-02-28 23:40:28 +01:00
|
|
|
#include "chrome/common/pref_names.h"
|
|
|
|
#include "components/prefs/scoped_user_pref_update.h"
|
2016-10-17 20:14:44 +02:00
|
|
|
#include "content/public/browser/browser_context.h"
|
2018-10-02 14:14:11 +02:00
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
2015-03-04 02:00:13 +01:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2019-06-05 17:12:29 +02:00
|
|
|
#include "content/public/browser/file_url_loader.h"
|
2017-07-27 01:19:27 +02:00
|
|
|
#include "content/public/browser/navigation_handle.h"
|
2014-04-04 18:50:38 +02:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2013-12-17 23:04:35 +01:00
|
|
|
#include "content/public/browser/render_view_host.h"
|
2019-06-05 17:12:29 +02:00
|
|
|
#include "content/public/browser/shared_cors_origin_access_list.h"
|
2016-05-25 01:35:43 +02:00
|
|
|
#include "content/public/browser/storage_partition.h"
|
2013-11-08 17:06:06 +01:00
|
|
|
#include "content/public/browser/web_contents.h"
|
|
|
|
#include "content/public/common/content_client.h"
|
2016-10-17 20:14:44 +02:00
|
|
|
#include "content/public/common/url_constants.h"
|
2019-06-05 17:12:29 +02:00
|
|
|
#include "content/public/common/url_utils.h"
|
2015-11-10 21:18:16 +01:00
|
|
|
#include "ipc/ipc_channel.h"
|
2018-08-09 22:18:24 +02:00
|
|
|
#include "net/base/completion_once_callback.h"
|
2015-03-04 02:00:13 +01:00
|
|
|
#include "net/base/io_buffer.h"
|
|
|
|
#include "net/base/net_errors.h"
|
|
|
|
#include "net/http/http_response_headers.h"
|
2017-05-31 17:33:30 +02:00
|
|
|
#include "net/traffic_annotation/network_traffic_annotation.h"
|
2019-06-05 17:12:29 +02:00
|
|
|
#include "services/network/public/cpp/simple_url_loader.h"
|
|
|
|
#include "services/network/public/cpp/simple_url_loader_stream_consumer.h"
|
2020-10-08 21:54:42 +02:00
|
|
|
#include "services/network/public/cpp/wrapper_shared_url_loader_factory.h"
|
2019-11-12 17:11:44 +01:00
|
|
|
#include "services/network/public/mojom/url_response_head.mojom.h"
|
2020-06-13 02:54:08 +02:00
|
|
|
#include "storage/browser/file_system/native_file_util.h"
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include <windows.h>
|
|
|
|
#elif defined(OS_POSIX)
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2013-11-08 17:06:06 +01:00
|
|
|
|
2015-01-09 18:22:10 +01:00
|
|
|
namespace {
|
|
|
|
|
2021-08-20 01:40:49 +02:00
|
|
|
// This constant should be in sync with the constant in
|
|
|
|
// chrome/browser/devtools/devtools_ui_bindings.cc.
|
|
|
|
constexpr size_t kMaxMessageChunkSize = IPC::Channel::kMaximumMessageSize / 4;
|
|
|
|
|
|
|
|
constexpr int kMaxLogLineLength = 1024;
|
|
|
|
|
2016-10-17 20:14:44 +02:00
|
|
|
static std::string GetFrontendURL() {
|
2018-06-07 18:57:46 +02:00
|
|
|
return base::StringPrintf("%s://%s/devtools_app.html",
|
2017-05-17 11:29:28 +02:00
|
|
|
content::kChromeDevToolsScheme,
|
|
|
|
scheme::kChromeDevToolsHost);
|
2016-10-17 20:14:44 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 01:40:49 +02:00
|
|
|
base::DictionaryValue BuildObjectForResponse(const net::HttpResponseHeaders* rh,
|
|
|
|
bool success,
|
|
|
|
int net_error) {
|
|
|
|
base::DictionaryValue response;
|
2020-03-04 01:29:39 +01:00
|
|
|
int responseCode = 200;
|
|
|
|
if (rh) {
|
|
|
|
responseCode = rh->response_code();
|
|
|
|
} else if (!success) {
|
|
|
|
// In case of no headers, assume file:// URL and failed to load
|
|
|
|
responseCode = 404;
|
|
|
|
}
|
2021-08-20 01:40:49 +02:00
|
|
|
response.SetInteger("statusCode", responseCode);
|
|
|
|
response.SetInteger("netError", net_error);
|
|
|
|
response.SetString("netErrorName", net::ErrorToString(net_error));
|
2019-06-05 17:12:29 +02:00
|
|
|
|
|
|
|
auto headers = std::make_unique<base::DictionaryValue>();
|
|
|
|
size_t iterator = 0;
|
|
|
|
std::string name;
|
|
|
|
std::string value;
|
|
|
|
// TODO(caseq): this probably needs to handle duplicate header names
|
|
|
|
// correctly by folding them.
|
|
|
|
while (rh && rh->EnumerateHeaderLines(&iterator, &name, &value))
|
|
|
|
headers->SetString(name, value);
|
|
|
|
|
2021-08-20 01:40:49 +02:00
|
|
|
response.Set("headers", std::move(headers));
|
2019-06-05 17:12:29 +02:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2020-06-13 02:54:08 +02:00
|
|
|
void WriteTimestamp(std::stringstream& stream) {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
SYSTEMTIME local_time;
|
|
|
|
GetLocalTime(&local_time);
|
|
|
|
stream << std::setfill('0') << std::setw(2) << local_time.wMonth
|
|
|
|
<< std::setw(2) << local_time.wDay << '/' << std::setw(2)
|
|
|
|
<< local_time.wHour << std::setw(2) << local_time.wMinute
|
|
|
|
<< std::setw(2) << local_time.wSecond << '.' << std::setw(3)
|
|
|
|
<< local_time.wMilliseconds;
|
|
|
|
#elif defined(OS_POSIX)
|
|
|
|
timeval tv;
|
|
|
|
gettimeofday(&tv, nullptr);
|
|
|
|
time_t t = tv.tv_sec;
|
|
|
|
struct tm local_time;
|
|
|
|
localtime_r(&t, &local_time);
|
|
|
|
struct tm* tm_time = &local_time;
|
|
|
|
stream << std::setfill('0') << std::setw(2) << 1 + tm_time->tm_mon
|
|
|
|
<< std::setw(2) << tm_time->tm_mday << '/' << std::setw(2)
|
|
|
|
<< tm_time->tm_hour << std::setw(2) << tm_time->tm_min << std::setw(2)
|
|
|
|
<< tm_time->tm_sec << '.' << std::setw(6) << tv.tv_usec;
|
|
|
|
#else
|
|
|
|
#error Unsupported platform
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogProtocolMessage(const base::FilePath& log_file,
|
|
|
|
ProtocolMessageType type,
|
|
|
|
std::string to_log) {
|
|
|
|
// Track if logging has failed, in which case we don't keep trying.
|
|
|
|
static bool log_error = false;
|
|
|
|
if (log_error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (storage::NativeFileUtil::EnsureFileExists(log_file, nullptr) !=
|
|
|
|
base::File::FILE_OK) {
|
|
|
|
LOG(ERROR) << "Failed to create file " << log_file.value();
|
|
|
|
log_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string type_label;
|
|
|
|
switch (type) {
|
|
|
|
case ProtocolMessageType::METHOD:
|
|
|
|
type_label = "METHOD";
|
|
|
|
break;
|
|
|
|
case ProtocolMessageType::RESULT:
|
|
|
|
type_label = "RESULT";
|
|
|
|
break;
|
|
|
|
case ProtocolMessageType::EVENT:
|
|
|
|
type_label = "EVENT";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream stream;
|
|
|
|
WriteTimestamp(stream);
|
|
|
|
stream << ": " << type_label << ": " << to_log << "\n";
|
|
|
|
const std::string& str = stream.str();
|
2021-06-04 03:34:56 +02:00
|
|
|
if (!base::AppendToFile(log_file, base::StringPiece(str))) {
|
2020-06-13 02:54:08 +02:00
|
|
|
LOG(ERROR) << "Failed to write file " << log_file.value();
|
|
|
|
log_error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 20:14:44 +02:00
|
|
|
} // namespace
|
|
|
|
|
2019-06-05 17:12:29 +02:00
|
|
|
class CefDevToolsFrontend::NetworkResourceLoader
|
|
|
|
: public network::SimpleURLLoaderStreamConsumer {
|
|
|
|
public:
|
|
|
|
NetworkResourceLoader(int stream_id,
|
|
|
|
CefDevToolsFrontend* bindings,
|
|
|
|
std::unique_ptr<network::SimpleURLLoader> loader,
|
|
|
|
network::mojom::URLLoaderFactory* url_loader_factory,
|
|
|
|
int request_id)
|
|
|
|
: stream_id_(stream_id),
|
|
|
|
bindings_(bindings),
|
|
|
|
loader_(std::move(loader)),
|
|
|
|
request_id_(request_id) {
|
|
|
|
loader_->SetOnResponseStartedCallback(base::BindOnce(
|
|
|
|
&NetworkResourceLoader::OnResponseStarted, base::Unretained(this)));
|
|
|
|
loader_->DownloadAsStream(url_loader_factory, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void OnResponseStarted(const GURL& final_url,
|
2019-11-12 17:11:44 +01:00
|
|
|
const network::mojom::URLResponseHead& response_head) {
|
2019-06-05 17:12:29 +02:00
|
|
|
response_headers_ = response_head.headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnDataReceived(base::StringPiece chunk,
|
|
|
|
base::OnceClosure resume) override {
|
|
|
|
base::Value chunkValue;
|
|
|
|
|
|
|
|
bool encoded = !base::IsStringUTF8(chunk);
|
|
|
|
if (encoded) {
|
|
|
|
std::string encoded_string;
|
|
|
|
base::Base64Encode(chunk, &encoded_string);
|
|
|
|
chunkValue = base::Value(std::move(encoded_string));
|
|
|
|
} else {
|
|
|
|
chunkValue = base::Value(chunk);
|
|
|
|
}
|
|
|
|
base::Value id(stream_id_);
|
|
|
|
base::Value encodedValue(encoded);
|
|
|
|
|
2021-08-20 01:40:49 +02:00
|
|
|
bindings_->CallClientFunction("DevToolsAPI", "streamWrite", std::move(id),
|
|
|
|
std::move(chunkValue),
|
|
|
|
std::move(encodedValue));
|
2019-06-05 17:12:29 +02:00
|
|
|
std::move(resume).Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnComplete(bool success) override {
|
2020-03-04 01:29:39 +01:00
|
|
|
auto response = BuildObjectForResponse(response_headers_.get(), success,
|
|
|
|
loader_->NetError());
|
2021-08-20 01:40:49 +02:00
|
|
|
bindings_->SendMessageAck(request_id_, std::move(response));
|
2019-06-05 17:12:29 +02:00
|
|
|
|
|
|
|
bindings_->loaders_.erase(bindings_->loaders_.find(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnRetry(base::OnceClosure start_retry) override { NOTREACHED(); }
|
|
|
|
|
|
|
|
const int stream_id_;
|
|
|
|
CefDevToolsFrontend* const bindings_;
|
|
|
|
std::unique_ptr<network::SimpleURLLoader> loader_;
|
|
|
|
int request_id_;
|
|
|
|
scoped_refptr<net::HttpResponseHeaders> response_headers_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(NetworkResourceLoader);
|
|
|
|
};
|
|
|
|
|
2013-11-08 17:06:06 +01:00
|
|
|
// static
|
|
|
|
CefDevToolsFrontend* CefDevToolsFrontend::Show(
|
2020-09-22 21:54:02 +02:00
|
|
|
AlloyBrowserHostImpl* inspected_browser,
|
2013-11-08 17:06:06 +01:00
|
|
|
const CefWindowInfo& windowInfo,
|
|
|
|
CefRefPtr<CefClient> client,
|
2014-10-11 02:12:01 +02:00
|
|
|
const CefBrowserSettings& settings,
|
2020-06-13 02:54:08 +02:00
|
|
|
const CefPoint& inspect_element_at,
|
|
|
|
base::OnceClosure frontend_destroyed_callback) {
|
2014-04-15 21:02:30 +02:00
|
|
|
CefBrowserSettings new_settings = settings;
|
2017-04-20 21:28:17 +02:00
|
|
|
if (!windowInfo.windowless_rendering_enabled &&
|
|
|
|
CefColorGetA(new_settings.background_color) != SK_AlphaOPAQUE) {
|
|
|
|
// Use white as the default background color for windowed DevTools instead
|
|
|
|
// of the CefSettings.background_color value.
|
2014-04-15 21:02:30 +02:00
|
|
|
new_settings.background_color = SK_ColorWHITE;
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
CefBrowserCreateParams create_params;
|
2021-02-18 02:58:25 +01:00
|
|
|
if (!inspected_browser->is_views_hosted())
|
2016-01-19 21:09:01 +01:00
|
|
|
create_params.window_info.reset(new CefWindowInfo(windowInfo));
|
|
|
|
create_params.client = client;
|
|
|
|
create_params.settings = new_settings;
|
|
|
|
create_params.devtools_opener = inspected_browser;
|
|
|
|
create_params.request_context = inspected_browser->GetRequestContext();
|
2019-10-03 16:17:58 +02:00
|
|
|
create_params.extra_info = inspected_browser->browser_info()->extra_info();
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
CefRefPtr<AlloyBrowserHostImpl> frontend_browser =
|
|
|
|
AlloyBrowserHostImpl::Create(create_params);
|
2013-11-08 17:06:06 +01:00
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
content::WebContents* inspected_contents = inspected_browser->web_contents();
|
2014-10-11 02:12:01 +02:00
|
|
|
|
2013-11-08 17:06:06 +01:00
|
|
|
// CefDevToolsFrontend will delete itself when the frontend WebContents is
|
|
|
|
// destroyed.
|
|
|
|
CefDevToolsFrontend* devtools_frontend = new CefDevToolsFrontend(
|
2020-09-22 21:54:02 +02:00
|
|
|
static_cast<AlloyBrowserHostImpl*>(frontend_browser.get()),
|
2020-06-13 02:54:08 +02:00
|
|
|
inspected_contents, inspect_element_at,
|
|
|
|
std::move(frontend_destroyed_callback));
|
2013-11-08 17:06:06 +01:00
|
|
|
|
|
|
|
// Need to load the URL after creating the DevTools objects.
|
2016-10-17 20:14:44 +02:00
|
|
|
frontend_browser->GetMainFrame()->LoadURL(GetFrontendURL());
|
2013-11-08 17:06:06 +01:00
|
|
|
|
|
|
|
return devtools_frontend;
|
|
|
|
}
|
|
|
|
|
2016-10-17 20:14:44 +02:00
|
|
|
void CefDevToolsFrontend::Activate() {
|
|
|
|
frontend_browser_->ActivateContents(web_contents());
|
|
|
|
}
|
|
|
|
|
2013-11-08 17:06:06 +01:00
|
|
|
void CefDevToolsFrontend::Focus() {
|
2016-06-10 18:43:53 +02:00
|
|
|
frontend_browser_->SetFocus(true);
|
2013-11-08 17:06:06 +01:00
|
|
|
}
|
|
|
|
|
2015-03-04 02:00:13 +01:00
|
|
|
void CefDevToolsFrontend::InspectElementAt(int x, int y) {
|
2016-06-10 18:43:53 +02:00
|
|
|
if (inspect_element_at_.x != x || inspect_element_at_.y != y)
|
|
|
|
inspect_element_at_.Set(x, y);
|
2015-03-04 02:00:13 +01:00
|
|
|
if (agent_host_)
|
2018-03-20 21:15:08 +01:00
|
|
|
agent_host_->InspectElement(inspected_contents_->GetFocusedFrame(), x, y);
|
2015-03-04 02:00:13 +01:00
|
|
|
}
|
|
|
|
|
2013-11-08 17:06:06 +01:00
|
|
|
void CefDevToolsFrontend::Close() {
|
2019-10-01 15:55:16 +02:00
|
|
|
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(&AlloyBrowserHostImpl::CloseBrowser,
|
|
|
|
frontend_browser_.get(), true));
|
2013-11-08 17:06:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CefDevToolsFrontend::CefDevToolsFrontend(
|
2020-09-22 21:54:02 +02:00
|
|
|
AlloyBrowserHostImpl* frontend_browser,
|
2016-06-10 18:43:53 +02:00
|
|
|
content::WebContents* inspected_contents,
|
2020-06-13 02:54:08 +02:00
|
|
|
const CefPoint& inspect_element_at,
|
|
|
|
base::OnceClosure frontend_destroyed_callback)
|
2016-10-17 20:14:44 +02:00
|
|
|
: content::WebContentsObserver(frontend_browser->web_contents()),
|
2013-11-08 17:06:06 +01:00
|
|
|
frontend_browser_(frontend_browser),
|
2015-03-19 23:06:16 +01:00
|
|
|
inspected_contents_(inspected_contents),
|
2016-06-10 18:43:53 +02:00
|
|
|
inspect_element_at_(inspect_element_at),
|
2020-06-13 02:54:08 +02:00
|
|
|
frontend_destroyed_callback_(std::move(frontend_destroyed_callback)),
|
|
|
|
file_manager_(frontend_browser, GetPrefs()),
|
|
|
|
protocol_log_file_(
|
|
|
|
base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
|
|
|
|
switches::kDevToolsProtocolLogFile)),
|
|
|
|
weak_factory_(this) {
|
|
|
|
DCHECK(!frontend_destroyed_callback_.is_null());
|
|
|
|
}
|
2013-11-08 17:06:06 +01:00
|
|
|
|
2019-07-29 23:27:12 +02:00
|
|
|
CefDevToolsFrontend::~CefDevToolsFrontend() {}
|
2013-11-08 17:06:06 +01:00
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
void CefDevToolsFrontend::ReadyToCommitNavigation(
|
|
|
|
content::NavigationHandle* navigation_handle) {
|
|
|
|
content::RenderFrameHost* frame = navigation_handle->GetRenderFrameHost();
|
|
|
|
if (navigation_handle->IsInMainFrame()) {
|
2019-02-01 17:42:40 +01:00
|
|
|
frontend_host_ = content::DevToolsFrontendHost::Create(
|
2021-06-04 03:34:56 +02:00
|
|
|
frame, base::BindRepeating(
|
|
|
|
&CefDevToolsFrontend::HandleMessageFromDevToolsFrontend,
|
2019-02-01 17:42:40 +01:00
|
|
|
base::Unretained(this)));
|
2017-07-27 01:19:27 +02:00
|
|
|
return;
|
2014-09-04 19:53:40 +02:00
|
|
|
}
|
2017-07-27 01:19:27 +02:00
|
|
|
|
|
|
|
std::string origin = navigation_handle->GetURL().GetOrigin().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::GenerateGUID().c_str());
|
|
|
|
content::DevToolsFrontendHost::SetupExtensionsAPI(frame, script);
|
2013-11-08 17:06:06 +01:00
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void CefDevToolsFrontend::DocumentAvailableInMainFrame(
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2015-07-31 22:54:54 +02:00
|
|
|
// Don't call AttachClient multiple times for the same DevToolsAgentHost.
|
|
|
|
// Otherwise it will call AgentHostClosed which closes the DevTools window.
|
|
|
|
// This may happen in cases where the DevTools content fails to load.
|
|
|
|
scoped_refptr<content::DevToolsAgentHost> agent_host =
|
2015-03-19 23:06:16 +01:00
|
|
|
content::DevToolsAgentHost::GetOrCreateFor(inspected_contents_);
|
2015-07-31 22:54:54 +02:00
|
|
|
if (agent_host != agent_host_) {
|
2017-09-06 23:40:58 +02:00
|
|
|
if (agent_host_)
|
|
|
|
agent_host_->DetachClient(this);
|
2015-07-31 22:54:54 +02:00
|
|
|
agent_host_ = agent_host;
|
|
|
|
agent_host_->AttachClient(this);
|
2017-03-16 22:13:06 +01:00
|
|
|
if (!inspect_element_at_.IsEmpty()) {
|
2018-03-20 21:15:08 +01:00
|
|
|
agent_host_->InspectElement(inspected_contents_->GetFocusedFrame(),
|
|
|
|
inspect_element_at_.x, inspect_element_at_.y);
|
2016-10-17 20:14:44 +02:00
|
|
|
}
|
2015-07-31 22:54:54 +02:00
|
|
|
}
|
2015-03-04 02:00:13 +01:00
|
|
|
}
|
|
|
|
|
2014-05-29 19:15:34 +02:00
|
|
|
void CefDevToolsFrontend::WebContentsDestroyed() {
|
2017-09-06 23:40:58 +02:00
|
|
|
if (agent_host_) {
|
2016-06-21 00:59:23 +02:00
|
|
|
agent_host_->DetachClient(this);
|
2017-09-06 23:40:58 +02:00
|
|
|
agent_host_ = nullptr;
|
|
|
|
}
|
2020-06-13 02:54:08 +02:00
|
|
|
std::move(frontend_destroyed_callback_).Run();
|
2013-11-08 17:06:06 +01:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2014-09-04 19:53:40 +02:00
|
|
|
void CefDevToolsFrontend::HandleMessageFromDevToolsFrontend(
|
2021-06-04 03:34:56 +02:00
|
|
|
base::Value message) {
|
2021-08-20 01:40:49 +02:00
|
|
|
if (!message.is_dict())
|
2014-09-04 19:53:40 +02:00
|
|
|
return;
|
2021-08-20 01:40:49 +02:00
|
|
|
const std::string* method = message.FindStringKey("method");
|
|
|
|
if (!method)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int request_id = message.FindIntKey("id").value_or(0);
|
|
|
|
base::Value* params_value = message.FindListKey("params");
|
|
|
|
|
|
|
|
// Since we've received message by value, we can take the list.
|
|
|
|
base::Value::ListStorage params;
|
|
|
|
if (params_value) {
|
|
|
|
params = std::move(*params_value).TakeList();
|
2014-09-04 19:53:40 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 01:40:49 +02:00
|
|
|
if (*method == "dispatchProtocolMessage") {
|
|
|
|
if (params.size() < 1)
|
|
|
|
return;
|
|
|
|
const std::string* protocol_message = params[0].GetIfString();
|
|
|
|
if (!agent_host_ || !protocol_message)
|
2015-11-10 21:18:16 +01:00
|
|
|
return;
|
2020-06-13 02:54:08 +02:00
|
|
|
if (ProtocolLoggingEnabled()) {
|
2021-08-20 01:40:49 +02:00
|
|
|
LogProtocolMessage(ProtocolMessageType::METHOD, *protocol_message);
|
2020-06-13 02:54:08 +02:00
|
|
|
}
|
2020-03-04 01:29:39 +01:00
|
|
|
agent_host_->DispatchProtocolMessage(
|
2021-08-20 01:40:49 +02:00
|
|
|
this, base::as_bytes(base::make_span(*protocol_message)));
|
|
|
|
} else if (*method == "loadCompleted") {
|
2015-08-14 16:41:08 +02:00
|
|
|
web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(
|
2021-04-21 00:52:34 +02:00
|
|
|
u"DevToolsAPI.setUseSoftMenu(true);", base::NullCallback());
|
2021-08-20 01:40:49 +02:00
|
|
|
} else if (*method == "loadNetworkResource") {
|
|
|
|
if (params.size() < 3)
|
|
|
|
return;
|
|
|
|
|
2015-03-04 02:00:13 +01:00
|
|
|
// TODO(pfeldman): handle some of the embedder messages in content.
|
2021-08-20 01:40:49 +02:00
|
|
|
const std::string* url = params[0].GetIfString();
|
|
|
|
const std::string* headers = params[1].GetIfString();
|
|
|
|
absl::optional<const int> stream_id = params[2].GetIfInt();
|
|
|
|
if (!url || !headers || !stream_id.has_value()) {
|
2015-03-04 02:00:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-20 01:40:49 +02:00
|
|
|
GURL gurl(*url);
|
2015-03-04 02:00:13 +01:00
|
|
|
if (!gurl.is_valid()) {
|
|
|
|
base::DictionaryValue response;
|
|
|
|
response.SetInteger("statusCode", 404);
|
2020-03-04 01:29:39 +01:00
|
|
|
response.SetBoolean("urlValid", false);
|
2021-08-20 01:40:49 +02:00
|
|
|
SendMessageAck(request_id, std::move(response));
|
2015-03-04 02:00:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
net::NetworkTrafficAnnotationTag traffic_annotation =
|
|
|
|
net::DefineNetworkTrafficAnnotation(
|
|
|
|
"devtools_handle_front_end_messages", R"(
|
|
|
|
semantics {
|
|
|
|
sender: "Developer Tools"
|
|
|
|
description:
|
|
|
|
"When user opens Developer Tools, the browser may fetch "
|
|
|
|
"additional resources from the network to enrich the debugging "
|
|
|
|
"experience (e.g. source map resources)."
|
|
|
|
trigger: "User opens Developer Tools to debug a web page."
|
|
|
|
data: "Any resources requested by Developer Tools."
|
|
|
|
destination: OTHER
|
|
|
|
}
|
|
|
|
policy {
|
2017-07-27 01:19:27 +02:00
|
|
|
cookies_allowed: YES
|
2017-05-31 17:33:30 +02:00
|
|
|
cookies_store: "user"
|
|
|
|
setting:
|
|
|
|
"It's not possible to disable this feature from settings."
|
|
|
|
chrome_policy {
|
2018-06-08 18:53:10 +02:00
|
|
|
DeveloperToolsAvailability {
|
2017-05-31 17:33:30 +02:00
|
|
|
policy_options {mode: MANDATORY}
|
2018-06-08 18:53:10 +02:00
|
|
|
DeveloperToolsAvailability: 2
|
2017-05-31 17:33:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})");
|
2019-06-05 17:12:29 +02:00
|
|
|
|
2019-07-29 23:27:12 +02:00
|
|
|
// Based on DevToolsUIBindings::LoadNetworkResource.
|
|
|
|
auto resource_request = std::make_unique<network::ResourceRequest>();
|
|
|
|
resource_request->url = gurl;
|
|
|
|
// TODO(caseq): this preserves behavior of URLFetcher-based
|
|
|
|
// implementation. We really need to pass proper first party origin from
|
|
|
|
// the front-end.
|
2020-03-04 01:29:39 +01:00
|
|
|
resource_request->site_for_cookies = net::SiteForCookies::FromUrl(gurl);
|
2021-08-20 01:40:49 +02:00
|
|
|
resource_request->headers.AddHeadersFromString(*headers);
|
2019-07-29 23:27:12 +02:00
|
|
|
|
2020-10-08 21:54:42 +02:00
|
|
|
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory;
|
2019-07-29 23:27:12 +02:00
|
|
|
if (gurl.SchemeIsFile()) {
|
2020-10-08 21:54:42 +02:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> pending_remote =
|
|
|
|
content::CreateFileURLLoaderFactory(
|
|
|
|
base::FilePath() /* profile_path */,
|
|
|
|
nullptr /* shared_cors_origin_access_list */);
|
|
|
|
url_loader_factory = network::SharedURLLoaderFactory::Create(
|
|
|
|
std::make_unique<network::WrapperPendingSharedURLLoaderFactory>(
|
|
|
|
std::move(pending_remote)));
|
2019-07-29 23:27:12 +02:00
|
|
|
} else if (content::HasWebUIScheme(gurl)) {
|
|
|
|
base::DictionaryValue response;
|
|
|
|
response.SetInteger("statusCode", 403);
|
2021-08-20 01:40:49 +02:00
|
|
|
SendMessageAck(request_id, std::move(response));
|
2019-07-29 23:27:12 +02:00
|
|
|
return;
|
2019-06-05 17:12:29 +02:00
|
|
|
} else {
|
2021-06-04 03:34:56 +02:00
|
|
|
auto* partition =
|
|
|
|
web_contents()->GetBrowserContext()->GetStoragePartitionForUrl(gurl);
|
2020-10-08 21:54:42 +02:00
|
|
|
url_loader_factory = partition->GetURLLoaderFactoryForBrowserProcess();
|
2019-06-05 17:12:29 +02:00
|
|
|
}
|
2019-07-29 23:27:12 +02:00
|
|
|
|
|
|
|
auto simple_url_loader = network::SimpleURLLoader::Create(
|
|
|
|
std::move(resource_request), traffic_annotation);
|
|
|
|
auto resource_loader = std::make_unique<NetworkResourceLoader>(
|
2021-08-20 01:40:49 +02:00
|
|
|
*stream_id, this, std::move(simple_url_loader),
|
|
|
|
url_loader_factory.get(), request_id);
|
2019-07-29 23:27:12 +02:00
|
|
|
loaders_.insert(std::move(resource_loader));
|
2015-03-04 02:00:13 +01:00
|
|
|
return;
|
2021-08-20 01:40:49 +02:00
|
|
|
} else if (*method == "getPreferences") {
|
|
|
|
SendMessageAck(
|
|
|
|
request_id,
|
|
|
|
GetPrefs()->GetDictionary(prefs::kDevToolsPreferences)->Clone());
|
2015-04-23 12:03:42 +02:00
|
|
|
return;
|
2021-08-20 01:40:49 +02:00
|
|
|
} else if (*method == "setPreference") {
|
|
|
|
if (params.size() < 2)
|
2015-04-23 12:03:42 +02:00
|
|
|
return;
|
2021-08-20 01:40:49 +02:00
|
|
|
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;
|
|
|
|
|
2017-02-28 23:40:28 +01:00
|
|
|
DictionaryPrefUpdate update(GetPrefs(), prefs::kDevToolsPreferences);
|
2021-08-20 01:40:49 +02:00
|
|
|
update.Get()->SetKey(*name, std::move(params[1]));
|
|
|
|
} else if (*method == "removePreference") {
|
|
|
|
const std::string* name = params[0].GetIfString();
|
|
|
|
if (!name)
|
2015-04-23 12:03:42 +02:00
|
|
|
return;
|
2017-02-28 23:40:28 +01:00
|
|
|
DictionaryPrefUpdate update(GetPrefs(), prefs::kDevToolsPreferences);
|
2021-08-20 01:40:49 +02:00
|
|
|
update.Get()->RemoveKey(*name);
|
|
|
|
} else if (*method == "requestFileSystems") {
|
2015-10-09 17:23:12 +02:00
|
|
|
web_contents()->GetMainFrame()->ExecuteJavaScriptForTests(
|
2021-04-21 00:52:34 +02:00
|
|
|
u"DevToolsAPI.fileSystemsLoaded([]);", base::NullCallback());
|
2021-08-20 01:40:49 +02:00
|
|
|
} else if (*method == "reattach") {
|
2018-02-15 01:12:09 +01:00
|
|
|
if (!agent_host_)
|
|
|
|
return;
|
2017-03-03 23:37:23 +01:00
|
|
|
agent_host_->DetachClient(this);
|
|
|
|
agent_host_->AttachClient(this);
|
2021-08-20 01:40:49 +02:00
|
|
|
} else if (*method == "registerExtensionsAPI") {
|
|
|
|
if (params.size() < 2)
|
2017-07-27 01:19:27 +02:00
|
|
|
return;
|
2021-08-20 01:40:49 +02:00
|
|
|
const std::string* origin = params[0].GetIfString();
|
|
|
|
const std::string* script = params[1].GetIfString();
|
|
|
|
if (!origin || !script)
|
2019-02-12 21:31:52 +01:00
|
|
|
return;
|
2021-08-20 01:40:49 +02:00
|
|
|
extensions_api_[*origin + "/"] = *script;
|
|
|
|
} else if (*method == "save") {
|
|
|
|
if (params.size() < 3)
|
2019-02-12 21:31:52 +01:00
|
|
|
return;
|
2021-08-20 01:40:49 +02:00
|
|
|
const std::string* url = params[0].GetIfString();
|
|
|
|
const std::string* content = params[1].GetIfString();
|
|
|
|
absl::optional<bool> save_as = params[2].GetIfBool();
|
|
|
|
if (!url || !content || !save_as.has_value())
|
|
|
|
return;
|
|
|
|
file_manager_.SaveToFile(*url, *content, *save_as);
|
|
|
|
} else if (*method == "append") {
|
|
|
|
if (params.size() < 2)
|
|
|
|
return;
|
|
|
|
const std::string* url = params[0].GetIfString();
|
|
|
|
const std::string* content = params[1].GetIfString();
|
|
|
|
if (!url || !content)
|
|
|
|
return file_manager_.AppendToFile(*url, *content);
|
2014-12-13 21:18:31 +01:00
|
|
|
} else {
|
2014-09-04 19:53:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-04 02:00:13 +01:00
|
|
|
if (request_id)
|
2021-08-20 01:40:49 +02:00
|
|
|
SendMessageAck(request_id, base::Value());
|
2014-09-04 19:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsFrontend::DispatchProtocolMessage(
|
2017-05-17 11:29:28 +02:00
|
|
|
content::DevToolsAgentHost* agent_host,
|
2020-03-04 01:29:39 +01:00
|
|
|
base::span<const uint8_t> message) {
|
2021-05-05 18:48:07 +02:00
|
|
|
if (!frontend_browser_->GetWebContents() ||
|
|
|
|
frontend_browser_->GetWebContents()->IsBeingDestroyed()) {
|
2021-04-20 18:16:33 +02:00
|
|
|
return;
|
2021-05-05 18:48:07 +02:00
|
|
|
}
|
2021-04-20 18:16:33 +02:00
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
base::StringPiece str_message(reinterpret_cast<const char*>(message.data()),
|
|
|
|
message.size());
|
2020-06-13 02:54:08 +02:00
|
|
|
if (ProtocolLoggingEnabled()) {
|
|
|
|
// Quick check to avoid parsing the JSON object. Events begin with a
|
|
|
|
// "method" value whereas method results begin with an "id" value.
|
2020-08-29 00:39:23 +02:00
|
|
|
LogProtocolMessage(base::StartsWith(str_message, "{\"method\":")
|
2020-06-13 02:54:08 +02:00
|
|
|
? ProtocolMessageType::EVENT
|
|
|
|
: ProtocolMessageType::RESULT,
|
|
|
|
str_message);
|
|
|
|
}
|
2015-01-09 18:22:10 +01:00
|
|
|
|
2021-08-20 01:40:49 +02:00
|
|
|
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) {
|
|
|
|
base::StringPiece 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)));
|
|
|
|
}
|
2015-01-09 18:22:10 +01:00
|
|
|
}
|
2014-09-04 19:53:40 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 01:40:49 +02:00
|
|
|
void CefDevToolsFrontend::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) {
|
|
|
|
std::string javascript;
|
|
|
|
|
|
|
|
web_contents()->GetMainFrame()->AllowInjectingJavaScript();
|
|
|
|
|
|
|
|
base::Value arguments(base::Value::Type::LIST);
|
|
|
|
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));
|
2015-03-04 02:00:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-20 01:40:49 +02:00
|
|
|
web_contents()->GetMainFrame()->ExecuteJavaScriptMethod(
|
|
|
|
base::ASCIIToUTF16(object_name), base::ASCIIToUTF16(method_name),
|
|
|
|
std::move(arguments), std::move(cb));
|
2015-03-04 02:00:13 +01:00
|
|
|
}
|
|
|
|
|
2021-08-20 01:40:49 +02:00
|
|
|
void CefDevToolsFrontend::SendMessageAck(int request_id, base::Value arg) {
|
|
|
|
CallClientFunction("DevToolsAPI", "embedderMessageAck",
|
|
|
|
base::Value(request_id), std::move(arg));
|
2015-03-04 02:00:13 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 02:54:08 +02:00
|
|
|
bool CefDevToolsFrontend::ProtocolLoggingEnabled() const {
|
|
|
|
return !protocol_log_file_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsFrontend::LogProtocolMessage(ProtocolMessageType type,
|
|
|
|
const base::StringPiece& message) {
|
|
|
|
DCHECK(ProtocolLoggingEnabled());
|
|
|
|
|
2021-06-04 03:34:56 +02:00
|
|
|
std::string to_log(message.substr(0, kMaxLogLineLength));
|
2020-06-13 02:54:08 +02:00
|
|
|
|
|
|
|
// Execute in an ordered context that allows blocking.
|
2020-06-28 23:05:36 +02:00
|
|
|
auto task_runner = CefTaskRunnerManager::Get()->GetBackgroundTaskRunner();
|
2020-06-13 02:54:08 +02:00
|
|
|
task_runner->PostTask(
|
|
|
|
FROM_HERE, base::BindOnce(::LogProtocolMessage, protocol_log_file_, type,
|
|
|
|
std::move(to_log)));
|
|
|
|
}
|
|
|
|
|
2014-09-04 19:53:40 +02:00
|
|
|
void CefDevToolsFrontend::AgentHostClosed(
|
2017-12-07 22:44:24 +01:00
|
|
|
content::DevToolsAgentHost* agent_host) {
|
2014-11-19 18:56:37 +01:00
|
|
|
DCHECK(agent_host == agent_host_.get());
|
2017-01-23 18:36:54 +01:00
|
|
|
agent_host_ = nullptr;
|
2013-11-08 17:06:06 +01:00
|
|
|
Close();
|
|
|
|
}
|
2017-02-28 23:40:28 +01:00
|
|
|
|
|
|
|
PrefService* CefDevToolsFrontend::GetPrefs() const {
|
2020-07-01 02:57:00 +02:00
|
|
|
return CefBrowserContext::FromBrowserContext(
|
2017-05-17 11:29:28 +02:00
|
|
|
frontend_browser_->web_contents()->GetBrowserContext())
|
2020-07-01 02:57:00 +02:00
|
|
|
->AsProfile()
|
2017-05-17 11:29:28 +02:00
|
|
|
->GetPrefs();
|
2017-02-28 23:40:28 +01:00
|
|
|
}
|