2014-10-31 21:17:05 +03:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 13:49:37 +04:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2014-01-02 18:47:54 +04:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 23:46:59 +03:00
|
|
|
#include "shell/browser/browser.h"
|
2014-01-02 18:47:54 +04:00
|
|
|
|
2017-10-05 07:30:14 +03:00
|
|
|
#include <fcntl.h>
|
2017-10-07 06:32:46 +03:00
|
|
|
#include <stdlib.h>
|
2014-01-02 18:47:54 +04:00
|
|
|
|
2024-09-27 01:09:42 +03:00
|
|
|
#if BUILDFLAG(IS_LINUX)
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#endif
|
|
|
|
|
2017-10-05 07:30:14 +03:00
|
|
|
#include "base/command_line.h"
|
2017-10-07 06:32:46 +03:00
|
|
|
#include "base/environment.h"
|
2017-10-05 07:30:14 +03:00
|
|
|
#include "base/process/launch.h"
|
2024-08-24 01:15:45 +03:00
|
|
|
#include "base/strings/strcat.h"
|
2019-06-20 00:31:55 +03:00
|
|
|
#include "electron/electron_version.h"
|
2024-03-22 16:00:21 +03:00
|
|
|
#include "shell/browser/javascript_environment.h"
|
2019-06-19 23:46:59 +03:00
|
|
|
#include "shell/browser/native_window.h"
|
|
|
|
#include "shell/browser/window_list.h"
|
|
|
|
#include "shell/common/application_info.h"
|
2024-03-22 16:00:21 +03:00
|
|
|
#include "shell/common/gin_converters/login_item_settings_converter.h"
|
2022-11-17 22:59:23 +03:00
|
|
|
#include "shell/common/thread_restrictions.h"
|
2017-10-02 17:21:30 +03:00
|
|
|
|
2022-02-10 05:58:52 +03:00
|
|
|
#if BUILDFLAG(IS_LINUX)
|
2019-10-29 01:12:35 +03:00
|
|
|
#include "shell/browser/linux/unity_service.h"
|
2017-10-02 17:21:30 +03:00
|
|
|
#endif
|
2014-01-02 18:47:54 +04:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2024-06-11 01:00:20 +03:00
|
|
|
namespace {
|
|
|
|
|
2017-10-07 06:32:46 +03:00
|
|
|
const char kXdgSettings[] = "xdg-settings";
|
|
|
|
const char kXdgSettingsDefaultSchemeHandler[] = "default-url-scheme-handler";
|
|
|
|
|
2019-11-07 04:50:33 +03:00
|
|
|
// The use of the ForTesting flavors is a hack workaround to avoid having to
|
|
|
|
// patch these as friends into the associated guard classes.
|
2023-03-17 16:27:41 +03:00
|
|
|
class [[maybe_unused, nodiscard]] LaunchXdgUtilityScopedAllowBaseSyncPrimitives
|
2023-03-23 03:09:13 +03:00
|
|
|
: public base::ScopedAllowBaseSyncPrimitivesForTesting {};
|
2019-11-07 04:50:33 +03:00
|
|
|
|
2017-10-07 06:32:46 +03:00
|
|
|
bool LaunchXdgUtility(const std::vector<std::string>& argv, int* exit_code) {
|
|
|
|
*exit_code = EXIT_FAILURE;
|
|
|
|
int devnull = open("/dev/null", O_RDONLY);
|
2018-04-18 04:55:30 +03:00
|
|
|
if (devnull < 0)
|
|
|
|
return false;
|
2017-10-07 06:32:46 +03:00
|
|
|
|
|
|
|
base::LaunchOptions options;
|
2019-09-13 17:26:59 +03:00
|
|
|
options.fds_to_remap.emplace_back(devnull, STDIN_FILENO);
|
2017-10-07 06:32:46 +03:00
|
|
|
|
|
|
|
base::Process process = base::LaunchProcess(argv, options);
|
|
|
|
close(devnull);
|
|
|
|
|
2018-04-18 04:55:30 +03:00
|
|
|
if (!process.IsValid())
|
|
|
|
return false;
|
2019-11-07 04:50:33 +03:00
|
|
|
LaunchXdgUtilityScopedAllowBaseSyncPrimitives allow_base_sync_primitives;
|
2017-10-07 06:32:46 +03:00
|
|
|
return process.WaitForExit(exit_code);
|
|
|
|
}
|
|
|
|
|
2024-01-11 01:23:35 +03:00
|
|
|
std::optional<std::string> GetXdgAppOutput(
|
2019-11-07 04:50:33 +03:00
|
|
|
const std::vector<std::string>& argv) {
|
|
|
|
std::string reply;
|
|
|
|
int success_code;
|
2022-11-17 22:59:23 +03:00
|
|
|
ScopedAllowBlockingForElectron allow_blocking;
|
2019-11-07 04:50:33 +03:00
|
|
|
bool ran_ok = base::GetAppOutputWithExitCode(base::CommandLine(argv), &reply,
|
|
|
|
&success_code);
|
|
|
|
|
|
|
|
if (!ran_ok || success_code != EXIT_SUCCESS)
|
2024-01-11 01:23:35 +03:00
|
|
|
return std::optional<std::string>();
|
2019-11-07 04:50:33 +03:00
|
|
|
|
2024-01-11 01:23:35 +03:00
|
|
|
return std::make_optional(reply);
|
2019-11-07 04:50:33 +03:00
|
|
|
}
|
|
|
|
|
2017-10-07 06:32:46 +03:00
|
|
|
bool SetDefaultWebClient(const std::string& protocol) {
|
2021-06-04 03:23:06 +03:00
|
|
|
auto env = base::Environment::Create();
|
2017-10-07 06:32:46 +03:00
|
|
|
|
2019-11-07 04:50:33 +03:00
|
|
|
std::vector<std::string> argv = {kXdgSettings, "set"};
|
2017-10-07 06:32:46 +03:00
|
|
|
if (!protocol.empty()) {
|
2019-09-13 17:26:59 +03:00
|
|
|
argv.emplace_back(kXdgSettingsDefaultSchemeHandler);
|
2019-11-07 04:50:33 +03:00
|
|
|
argv.emplace_back(protocol);
|
2017-10-07 06:32:46 +03:00
|
|
|
}
|
2019-10-29 01:12:35 +03:00
|
|
|
std::string desktop_name;
|
|
|
|
if (!env->GetVar("CHROME_DESKTOP", &desktop_name)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-07 04:50:33 +03:00
|
|
|
argv.emplace_back(desktop_name);
|
2017-10-07 06:32:46 +03:00
|
|
|
|
|
|
|
int exit_code;
|
|
|
|
bool ran_ok = LaunchXdgUtility(argv, &exit_code);
|
|
|
|
return ran_ok && exit_code == EXIT_SUCCESS;
|
|
|
|
}
|
2017-10-05 07:30:14 +03:00
|
|
|
|
2024-06-11 01:00:20 +03:00
|
|
|
} // namespace
|
|
|
|
|
2018-04-18 04:55:30 +03:00
|
|
|
void Browser::AddRecentDocument(const base::FilePath& path) {}
|
2014-11-17 08:05:06 +03:00
|
|
|
|
2018-04-18 04:55:30 +03:00
|
|
|
void Browser::ClearRecentDocuments() {}
|
2014-11-17 11:13:47 +03:00
|
|
|
|
2016-08-16 08:54:30 +03:00
|
|
|
bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,
|
2020-07-29 02:43:43 +03:00
|
|
|
gin::Arguments* args) {
|
2017-10-03 18:10:38 +03:00
|
|
|
return SetDefaultWebClient(protocol);
|
2016-03-21 21:24:25 +03:00
|
|
|
}
|
|
|
|
|
2017-10-05 07:30:14 +03:00
|
|
|
bool Browser::IsDefaultProtocolClient(const std::string& protocol,
|
2020-07-29 02:43:43 +03:00
|
|
|
gin::Arguments* args) {
|
2021-06-04 03:23:06 +03:00
|
|
|
auto env = base::Environment::Create();
|
2017-10-07 07:36:18 +03:00
|
|
|
|
2018-04-18 04:55:30 +03:00
|
|
|
if (protocol.empty())
|
|
|
|
return false;
|
2017-10-07 07:36:18 +03:00
|
|
|
|
2019-10-29 01:12:35 +03:00
|
|
|
std::string desktop_name;
|
|
|
|
if (!env->GetVar("CHROME_DESKTOP", &desktop_name))
|
|
|
|
return false;
|
2019-11-07 04:50:33 +03:00
|
|
|
const std::vector<std::string> argv = {kXdgSettings, "check",
|
|
|
|
kXdgSettingsDefaultSchemeHandler,
|
|
|
|
protocol, desktop_name};
|
2017-10-07 07:36:18 +03:00
|
|
|
// Allow any reply that starts with "yes".
|
2024-04-24 05:13:59 +03:00
|
|
|
const std::optional<std::string> output = GetXdgAppOutput(argv);
|
|
|
|
return output && output->starts_with("yes");
|
2017-10-02 17:21:30 +03:00
|
|
|
}
|
|
|
|
|
2017-10-07 06:40:58 +03:00
|
|
|
// Todo implement
|
2017-10-02 17:21:30 +03:00
|
|
|
bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
|
2020-07-29 02:43:43 +03:00
|
|
|
gin::Arguments* args) {
|
2016-04-25 08:17:01 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:18:45 +03:00
|
|
|
std::u16string Browser::GetApplicationNameForProtocol(const GURL& url) {
|
2019-11-07 04:50:33 +03:00
|
|
|
const std::vector<std::string> argv = {
|
|
|
|
"xdg-mime", "query", "default",
|
2024-08-24 01:15:45 +03:00
|
|
|
base::StrCat({"x-scheme-handler/", url.scheme_piece()})};
|
2019-11-07 04:50:33 +03:00
|
|
|
|
|
|
|
return base::ASCIIToUTF16(GetXdgAppOutput(argv).value_or(std::string()));
|
|
|
|
}
|
|
|
|
|
2024-01-11 01:23:35 +03:00
|
|
|
bool Browser::SetBadgeCount(std::optional<int> count) {
|
2021-01-21 08:45:06 +03:00
|
|
|
if (IsUnityRunning() && count.has_value()) {
|
|
|
|
unity::SetDownloadCount(count.value());
|
|
|
|
badge_count_ = count.value();
|
2016-07-01 16:18:39 +03:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-01 11:39:01 +03:00
|
|
|
}
|
|
|
|
|
2018-04-18 04:55:30 +03:00
|
|
|
void Browser::SetLoginItemSettings(LoginItemSettings settings) {}
|
2016-07-11 20:57:53 +03:00
|
|
|
|
2024-03-22 16:00:21 +03:00
|
|
|
v8::Local<v8::Value> Browser::GetLoginItemSettings(
|
2017-03-29 22:29:52 +03:00
|
|
|
const LoginItemSettings& options) {
|
2024-03-22 16:00:21 +03:00
|
|
|
LoginItemSettings settings;
|
|
|
|
return gin::ConvertToV8(JavascriptEnvironment::GetIsolate(), settings);
|
2016-07-11 20:57:53 +03:00
|
|
|
}
|
|
|
|
|
2014-01-02 18:47:54 +04:00
|
|
|
std::string Browser::GetExecutableFileVersion() const {
|
2018-10-24 13:49:10 +03:00
|
|
|
return GetApplicationVersion();
|
2014-01-02 18:47:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Browser::GetExecutableFileProductName() const {
|
2018-10-24 13:49:10 +03:00
|
|
|
return GetApplicationName();
|
2014-01-02 18:47:54 +04:00
|
|
|
}
|
|
|
|
|
2016-07-01 11:39:01 +03:00
|
|
|
bool Browser::IsUnityRunning() {
|
2016-06-26 02:55:24 +03:00
|
|
|
return unity::IsRunning();
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:39:52 +03:00
|
|
|
bool Browser::IsEmojiPanelSupported() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-14 01:12:24 +03:00
|
|
|
void Browser::ShowAboutPanel() {
|
2019-06-25 21:31:14 +03:00
|
|
|
const auto& opts = about_panel_options_;
|
2019-08-05 17:53:40 +03:00
|
|
|
|
|
|
|
GtkWidget* dialogWidget = gtk_about_dialog_new();
|
|
|
|
GtkAboutDialog* dialog = GTK_ABOUT_DIALOG(dialogWidget);
|
|
|
|
|
2019-06-25 21:31:14 +03:00
|
|
|
const std::string* str;
|
2023-03-01 01:26:00 +03:00
|
|
|
const base::Value::List* list;
|
2019-06-25 21:31:14 +03:00
|
|
|
|
2023-03-01 01:26:00 +03:00
|
|
|
if ((str = opts.FindString("applicationName"))) {
|
2019-06-25 21:31:14 +03:00
|
|
|
gtk_about_dialog_set_program_name(dialog, str->c_str());
|
|
|
|
}
|
2023-03-01 01:26:00 +03:00
|
|
|
if ((str = opts.FindString("applicationVersion"))) {
|
2019-06-25 21:31:14 +03:00
|
|
|
gtk_about_dialog_set_version(dialog, str->c_str());
|
|
|
|
}
|
2023-03-01 01:26:00 +03:00
|
|
|
if ((str = opts.FindString("copyright"))) {
|
2019-06-25 21:31:14 +03:00
|
|
|
gtk_about_dialog_set_copyright(dialog, str->c_str());
|
|
|
|
}
|
2023-03-01 01:26:00 +03:00
|
|
|
if ((str = opts.FindString("website"))) {
|
2019-06-25 21:31:14 +03:00
|
|
|
gtk_about_dialog_set_website(dialog, str->c_str());
|
|
|
|
}
|
2023-03-01 01:26:00 +03:00
|
|
|
if ((str = opts.FindString("iconPath"))) {
|
2018-11-14 01:12:24 +03:00
|
|
|
GError* error = nullptr;
|
2019-06-26 08:17:30 +03:00
|
|
|
constexpr int width = 64; // width of about panel icon in pixels
|
|
|
|
constexpr int height = 64; // height of about panel icon in pixels
|
|
|
|
|
|
|
|
// set preserve_aspect_ratio to true
|
|
|
|
GdkPixbuf* icon =
|
|
|
|
gdk_pixbuf_new_from_file_at_size(str->c_str(), width, height, &error);
|
2018-11-14 01:12:24 +03:00
|
|
|
if (error != nullptr) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_clear_error(&error);
|
|
|
|
} else {
|
|
|
|
gtk_about_dialog_set_logo(dialog, icon);
|
|
|
|
g_clear_object(&icon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 01:26:00 +03:00
|
|
|
if ((list = opts.FindList("authors"))) {
|
2019-06-25 21:31:14 +03:00
|
|
|
std::vector<const char*> cstrs;
|
2023-03-01 01:26:00 +03:00
|
|
|
for (const auto& authorVal : *list) {
|
2019-06-25 21:31:14 +03:00
|
|
|
if (authorVal.is_string()) {
|
|
|
|
cstrs.push_back(authorVal.GetString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cstrs.empty()) {
|
|
|
|
LOG(WARNING) << "No author strings found in 'authors' array";
|
|
|
|
} else {
|
|
|
|
cstrs.push_back(nullptr); // null-terminated char* array
|
|
|
|
gtk_about_dialog_set_authors(dialog, cstrs.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 17:46:35 +03:00
|
|
|
// destroy the widget when it closes
|
|
|
|
g_signal_connect_swapped(dialogWidget, "response",
|
|
|
|
G_CALLBACK(gtk_widget_destroy), dialogWidget);
|
|
|
|
|
|
|
|
gtk_widget_show_all(dialogWidget);
|
2018-11-14 01:12:24 +03:00
|
|
|
}
|
|
|
|
|
2022-07-05 18:25:18 +03:00
|
|
|
void Browser::SetAboutPanelOptions(base::Value::Dict options) {
|
2023-03-01 01:26:00 +03:00
|
|
|
about_panel_options_ = std::move(options);
|
2018-11-14 01:12:24 +03:00
|
|
|
}
|
|
|
|
|
2014-01-02 18:47:54 +04:00
|
|
|
} // namespace electron
|