2019-02-12 21:31:52 +01:00
|
|
|
// Copyright 2019 The Chromium Embedded Framework Authors. Portions 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.
|
|
|
|
|
|
|
|
#include "libcef/browser/devtools/devtools_file_manager.h"
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
#include "libcef/browser/alloy/alloy_browser_host_impl.h"
|
2019-02-12 21:31:52 +01:00
|
|
|
|
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/files/file_util.h"
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/functional/bind.h"
|
|
|
|
#include "base/functional/callback.h"
|
2019-02-12 21:31:52 +01:00
|
|
|
#include "base/json/json_writer.h"
|
2021-08-20 01:40:49 +02:00
|
|
|
#include "base/json/values_util.h"
|
2019-02-12 21:31:52 +01:00
|
|
|
#include "base/lazy_instance.h"
|
|
|
|
#include "base/path_service.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/task/sequenced_task_runner.h"
|
2021-04-21 00:52:34 +02:00
|
|
|
#include "base/task/thread_pool.h"
|
2019-02-12 21:31:52 +01:00
|
|
|
#include "base/values.h"
|
|
|
|
#include "chrome/common/pref_names.h"
|
|
|
|
#include "components/prefs/scoped_user_pref_update.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
base::LazyInstance<base::FilePath>::Leaky g_last_save_path =
|
|
|
|
LAZY_INSTANCE_INITIALIZER;
|
|
|
|
|
|
|
|
void WriteToFile(const base::FilePath& path, const std::string& content) {
|
|
|
|
DCHECK(!path.empty());
|
|
|
|
base::WriteFile(path, content.c_str(), content.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendToFile(const base::FilePath& path, const std::string& content) {
|
|
|
|
DCHECK(!path.empty());
|
2021-06-04 03:34:56 +02:00
|
|
|
base::AppendToFile(path, base::StringPiece(content));
|
2019-02-12 21:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-09-22 21:54:02 +02:00
|
|
|
CefDevToolsFileManager::CefDevToolsFileManager(
|
|
|
|
AlloyBrowserHostImpl* browser_impl,
|
|
|
|
PrefService* prefs)
|
2019-02-12 21:31:52 +01:00
|
|
|
: browser_impl_(browser_impl),
|
|
|
|
prefs_(prefs),
|
2021-04-21 00:52:34 +02:00
|
|
|
file_task_runner_(
|
|
|
|
base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()})),
|
2019-02-12 21:31:52 +01:00
|
|
|
weak_factory_(this) {}
|
|
|
|
|
|
|
|
void CefDevToolsFileManager::SaveToFile(const std::string& url,
|
|
|
|
const std::string& content,
|
|
|
|
bool save_as) {
|
|
|
|
Save(url, content, save_as,
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(&CefDevToolsFileManager::FileSavedAs,
|
|
|
|
weak_factory_.GetWeakPtr(), url),
|
|
|
|
base::BindOnce(&CefDevToolsFileManager::CanceledFileSaveAs,
|
|
|
|
weak_factory_.GetWeakPtr(), url));
|
2019-02-12 21:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsFileManager::AppendToFile(const std::string& url,
|
|
|
|
const std::string& content) {
|
|
|
|
Append(url, content,
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(&CefDevToolsFileManager::AppendedTo,
|
|
|
|
weak_factory_.GetWeakPtr(), url));
|
2019-02-12 21:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsFileManager::Save(const std::string& url,
|
|
|
|
const std::string& content,
|
|
|
|
bool save_as,
|
2021-06-04 03:34:56 +02:00
|
|
|
SaveCallback saveCallback,
|
|
|
|
CancelCallback cancelCallback) {
|
2019-02-12 21:31:52 +01:00
|
|
|
auto it = saved_files_.find(url);
|
|
|
|
if (it != saved_files_.end() && !save_as) {
|
2021-06-04 03:34:56 +02:00
|
|
|
SaveAsFileSelected(url, content, std::move(saveCallback), it->second);
|
2019-02-12 21:31:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-26 21:30:45 +02:00
|
|
|
const base::Value::Dict& file_map =
|
|
|
|
prefs_->GetDict(prefs::kDevToolsEditedFiles);
|
2019-02-12 21:31:52 +01:00
|
|
|
base::FilePath initial_path;
|
|
|
|
|
2022-09-26 21:30:45 +02:00
|
|
|
if (const base::Value* path_value = file_map.Find(base::MD5String(url))) {
|
2021-08-20 01:40:49 +02:00
|
|
|
absl::optional<base::FilePath> path = base::ValueToFilePath(*path_value);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (path) {
|
2020-07-08 19:23:29 +02:00
|
|
|
initial_path = std::move(*path);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-02-12 21:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (initial_path.empty()) {
|
|
|
|
GURL gurl(url);
|
|
|
|
std::string suggested_file_name =
|
|
|
|
gurl.is_valid() ? gurl.ExtractFileName() : url;
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (suggested_file_name.length() > 64) {
|
2019-02-12 21:31:52 +01:00
|
|
|
suggested_file_name = suggested_file_name.substr(0, 64);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-02-12 21:31:52 +01:00
|
|
|
|
|
|
|
if (!g_last_save_path.Pointer()->empty()) {
|
|
|
|
initial_path = g_last_save_path.Pointer()->DirName().AppendASCII(
|
|
|
|
suggested_file_name);
|
|
|
|
} else {
|
|
|
|
// Use the temp directory. It may be an empty value.
|
|
|
|
base::PathService::Get(base::DIR_TEMP, &initial_path);
|
|
|
|
initial_path = initial_path.AppendASCII(suggested_file_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 21:55:23 +02:00
|
|
|
blink::mojom::FileChooserParams params;
|
2019-02-12 21:31:52 +01:00
|
|
|
params.mode = blink::mojom::FileChooserParams::Mode::kSave;
|
|
|
|
if (!initial_path.empty()) {
|
|
|
|
params.default_file_name = initial_path;
|
|
|
|
if (!initial_path.Extension().empty()) {
|
|
|
|
params.accept_types.push_back(CefString(initial_path.Extension()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 21:55:23 +02:00
|
|
|
browser_impl_->RunFileChooserForBrowser(
|
2021-06-04 03:34:56 +02:00
|
|
|
params,
|
|
|
|
base::BindOnce(&CefDevToolsFileManager::SaveAsDialogDismissed,
|
|
|
|
weak_factory_.GetWeakPtr(), url, content,
|
|
|
|
std::move(saveCallback), std::move(cancelCallback)));
|
2019-02-12 21:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsFileManager::SaveAsDialogDismissed(
|
|
|
|
const std::string& url,
|
|
|
|
const std::string& content,
|
2021-06-04 03:34:56 +02:00
|
|
|
SaveCallback saveCallback,
|
|
|
|
CancelCallback cancelCallback,
|
2019-02-12 21:31:52 +01:00
|
|
|
const std::vector<base::FilePath>& file_paths) {
|
|
|
|
if (file_paths.size() == 1) {
|
2021-06-04 03:34:56 +02:00
|
|
|
SaveAsFileSelected(url, content, std::move(saveCallback), file_paths[0]);
|
2019-02-12 21:31:52 +01:00
|
|
|
} else {
|
2021-06-04 03:34:56 +02:00
|
|
|
std::move(cancelCallback).Run();
|
2019-02-12 21:31:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsFileManager::SaveAsFileSelected(const std::string& url,
|
|
|
|
const std::string& content,
|
2021-06-04 03:34:56 +02:00
|
|
|
SaveCallback callback,
|
2019-02-12 21:31:52 +01:00
|
|
|
const base::FilePath& path) {
|
|
|
|
*g_last_save_path.Pointer() = path;
|
|
|
|
saved_files_[url] = path;
|
|
|
|
|
2023-01-30 18:43:54 +01:00
|
|
|
ScopedDictPrefUpdate update(prefs_, prefs::kDevToolsEditedFiles);
|
|
|
|
update->Set(base::MD5String(url), base::FilePathToValue(path));
|
2019-02-12 21:31:52 +01:00
|
|
|
std::string file_system_path = path.AsUTF8Unsafe();
|
2021-06-04 03:34:56 +02:00
|
|
|
std::move(callback).Run(file_system_path);
|
2019-02-12 21:31:52 +01:00
|
|
|
file_task_runner_->PostTask(FROM_HERE,
|
|
|
|
base::BindOnce(&::WriteToFile, path, content));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsFileManager::FileSavedAs(const std::string& url,
|
|
|
|
const std::string& file_system_path) {
|
|
|
|
base::Value url_value(url);
|
|
|
|
base::Value file_system_path_value(file_system_path);
|
|
|
|
CallClientFunction("DevToolsAPI.savedURL", &url_value,
|
2020-01-15 14:36:24 +01:00
|
|
|
&file_system_path_value, nullptr);
|
2019-02-12 21:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsFileManager::CanceledFileSaveAs(const std::string& url) {
|
|
|
|
base::Value url_value(url);
|
2020-01-15 14:36:24 +01:00
|
|
|
CallClientFunction("DevToolsAPI.canceledSaveURL", &url_value, nullptr,
|
|
|
|
nullptr);
|
2019-02-12 21:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsFileManager::Append(const std::string& url,
|
|
|
|
const std::string& content,
|
2021-06-04 03:34:56 +02:00
|
|
|
AppendCallback callback) {
|
2019-02-12 21:31:52 +01:00
|
|
|
auto it = saved_files_.find(url);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (it == saved_files_.end()) {
|
2019-02-12 21:31:52 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-06-04 03:34:56 +02:00
|
|
|
std::move(callback).Run();
|
2019-02-12 21:31:52 +01:00
|
|
|
file_task_runner_->PostTask(
|
|
|
|
FROM_HERE, base::BindOnce(&::AppendToFile, it->second, content));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsFileManager::AppendedTo(const std::string& url) {
|
|
|
|
base::Value url_value(url);
|
2020-01-15 14:36:24 +01:00
|
|
|
CallClientFunction("DevToolsAPI.appendedToURL", &url_value, nullptr, nullptr);
|
2019-02-12 21:31:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDevToolsFileManager::CallClientFunction(
|
|
|
|
const std::string& function_name,
|
|
|
|
const base::Value* arg1,
|
|
|
|
const base::Value* arg2,
|
|
|
|
const base::Value* arg3) {
|
|
|
|
std::string javascript = function_name + "(";
|
|
|
|
if (arg1) {
|
|
|
|
std::string json;
|
|
|
|
base::JSONWriter::Write(*arg1, &json);
|
|
|
|
javascript.append(json);
|
|
|
|
if (arg2) {
|
|
|
|
base::JSONWriter::Write(*arg2, &json);
|
|
|
|
javascript.append(", ").append(json);
|
|
|
|
if (arg3) {
|
|
|
|
base::JSONWriter::Write(*arg3, &json);
|
|
|
|
javascript.append(", ").append(json);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
javascript.append(");");
|
2022-07-21 19:26:10 +02:00
|
|
|
browser_impl_->web_contents()->GetPrimaryMainFrame()->ExecuteJavaScript(
|
2019-04-16 16:38:48 +02:00
|
|
|
base::UTF8ToUTF16(javascript), base::NullCallback());
|
2019-02-12 21:31:52 +01:00
|
|
|
}
|