electron/shell/browser/browser_linux.cc

244 lines
7 KiB
C++
Raw Normal View History

// Copyright (c) 2013 GitHub, Inc.
2014-04-25 09:49:37 +00:00
// Use of this source code is governed by the MIT license that can be
2014-01-02 14:47:54 +00:00
// found in the LICENSE file.
#include "shell/browser/browser.h"
2014-01-02 14:47:54 +00:00
2017-10-05 04:30:14 +00:00
#include <fcntl.h>
2017-10-07 03:32:46 +00:00
#include <stdlib.h>
2014-01-02 14:47:54 +00:00
2017-10-05 04:30:14 +00:00
#include "base/command_line.h"
2017-10-07 03:32:46 +00:00
#include "base/environment.h"
2017-10-05 04:30:14 +00:00
#include "base/process/launch.h"
#include "electron/electron_version.h"
#include "shell/browser/native_window.h"
#include "shell/browser/window_list.h"
#include "shell/common/application_info.h"
#if defined(USE_X11)
#include "shell/browser/linux/unity_service.h"
#include "ui/gtk/gtk_util.h"
#endif
2014-01-02 14:47:54 +00:00
namespace electron {
2014-01-02 14:47:54 +00:00
2017-10-07 03:32:46 +00:00
const char kXdgSettings[] = "xdg-settings";
const char kXdgSettingsDefaultSchemeHandler[] = "default-url-scheme-handler";
// The use of the ForTesting flavors is a hack workaround to avoid having to
// patch these as friends into the associated guard classes.
class LaunchXdgUtilityScopedAllowBaseSyncPrimitives
: public base::ScopedAllowBaseSyncPrimitivesForTesting {};
class GetXdgAppOutputScopedAllowBlocking
: public base::ScopedAllowBlockingForTesting {};
2017-10-07 03:32:46 +00: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 01:55:30 +00:00
if (devnull < 0)
return false;
2017-10-07 03:32:46 +00:00
base::LaunchOptions options;
options.fds_to_remap.emplace_back(devnull, STDIN_FILENO);
2017-10-07 03:32:46 +00:00
base::Process process = base::LaunchProcess(argv, options);
close(devnull);
2018-04-18 01:55:30 +00:00
if (!process.IsValid())
return false;
LaunchXdgUtilityScopedAllowBaseSyncPrimitives allow_base_sync_primitives;
2017-10-07 03:32:46 +00:00
return process.WaitForExit(exit_code);
}
base::Optional<std::string> GetXdgAppOutput(
const std::vector<std::string>& argv) {
std::string reply;
int success_code;
GetXdgAppOutputScopedAllowBlocking allow_blocking;
bool ran_ok = base::GetAppOutputWithExitCode(base::CommandLine(argv), &reply,
&success_code);
if (!ran_ok || success_code != EXIT_SUCCESS)
return base::Optional<std::string>();
return base::make_optional(reply);
}
2017-10-07 03:32:46 +00:00
bool SetDefaultWebClient(const std::string& protocol) {
std::unique_ptr<base::Environment> env(base::Environment::Create());
std::vector<std::string> argv = {kXdgSettings, "set"};
2017-10-07 03:32:46 +00:00
if (!protocol.empty()) {
argv.emplace_back(kXdgSettingsDefaultSchemeHandler);
argv.emplace_back(protocol);
2017-10-07 03:32:46 +00:00
}
std::string desktop_name;
if (!env->GetVar("CHROME_DESKTOP", &desktop_name)) {
return false;
}
argv.emplace_back(desktop_name);
2017-10-07 03:32:46 +00:00
int exit_code;
bool ran_ok = LaunchXdgUtility(argv, &exit_code);
return ran_ok && exit_code == EXIT_SUCCESS;
}
2017-10-05 04:30:14 +00:00
2020-07-28 23:43:43 +00:00
void Browser::Focus(gin::Arguments* args) {
2014-01-02 14:47:54 +00:00
// Focus on the first visible window.
for (auto* const window : WindowList::GetWindows()) {
2014-01-02 14:47:54 +00:00
if (window->IsVisible()) {
window->Focus(true);
break;
}
}
}
2018-04-18 01:55:30 +00:00
void Browser::AddRecentDocument(const base::FilePath& path) {}
2014-11-17 05:05:06 +00:00
2018-04-18 01:55:30 +00:00
void Browser::ClearRecentDocuments() {}
2014-11-17 08:13:47 +00:00
2018-04-18 01:55:30 +00:00
void Browser::SetAppUserModelID(const base::string16& name) {}
2016-08-16 05:54:30 +00:00
bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,
2020-07-28 23:43:43 +00:00
gin::Arguments* args) {
2017-10-03 15:10:38 +00:00
return SetDefaultWebClient(protocol);
}
2017-10-05 04:30:14 +00:00
bool Browser::IsDefaultProtocolClient(const std::string& protocol,
2020-07-28 23:43:43 +00:00
gin::Arguments* args) {
std::unique_ptr<base::Environment> env(base::Environment::Create());
2018-04-18 01:55:30 +00:00
if (protocol.empty())
return false;
std::string desktop_name;
if (!env->GetVar("CHROME_DESKTOP", &desktop_name))
return false;
const std::vector<std::string> argv = {kXdgSettings, "check",
kXdgSettingsDefaultSchemeHandler,
protocol, desktop_name};
const auto output = GetXdgAppOutput(argv);
if (!output)
2018-04-18 01:55:30 +00:00
return false;
// Allow any reply that starts with "yes".
return base::StartsWith(output.value(), "yes", base::CompareCase::SENSITIVE);
}
// Todo implement
bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
2020-07-28 23:43:43 +00:00
gin::Arguments* args) {
2016-04-25 05:17:01 +00:00
return false;
}
base::string16 Browser::GetApplicationNameForProtocol(const GURL& url) {
const std::vector<std::string> argv = {
"xdg-mime", "query", "default",
std::string("x-scheme-handler/") + url.scheme()};
return base::ASCIIToUTF16(GetXdgAppOutput(argv).value_or(std::string()));
}
bool Browser::SetBadgeCount(int count) {
if (IsUnityRunning()) {
unity::SetDownloadCount(count);
2016-07-02 01:36:11 +00:00
badge_count_ = count;
return true;
} else {
return false;
}
2016-07-01 08:39:01 +00:00
}
2018-04-18 01:55:30 +00:00
void Browser::SetLoginItemSettings(LoginItemSettings settings) {}
2017-01-27 19:24:46 +00:00
Browser::LoginItemSettings Browser::GetLoginItemSettings(
2017-03-29 19:29:52 +00:00
const LoginItemSettings& options) {
return LoginItemSettings();
}
2014-01-02 14:47:54 +00:00
std::string Browser::GetExecutableFileVersion() const {
2018-10-24 10:49:10 +00:00
return GetApplicationVersion();
2014-01-02 14:47:54 +00:00
}
std::string Browser::GetExecutableFileProductName() const {
2018-10-24 10:49:10 +00:00
return GetApplicationName();
2014-01-02 14:47:54 +00:00
}
2016-07-01 08:39:01 +00:00
bool Browser::IsUnityRunning() {
return unity::IsRunning();
}
2019-03-14 20:39:52 +00:00
bool Browser::IsEmojiPanelSupported() {
return false;
}
void Browser::ShowAboutPanel() {
const auto& opts = about_panel_options_;
if (!opts.is_dict()) {
LOG(WARNING) << "Called showAboutPanel(), but didn't use "
"setAboutPanelSettings() first";
return;
}
GtkWidget* dialogWidget = gtk_about_dialog_new();
GtkAboutDialog* dialog = GTK_ABOUT_DIALOG(dialogWidget);
const std::string* str;
const base::Value* val;
if ((str = opts.FindStringKey("applicationName"))) {
gtk_about_dialog_set_program_name(dialog, str->c_str());
}
if ((str = opts.FindStringKey("applicationVersion"))) {
gtk_about_dialog_set_version(dialog, str->c_str());
}
if ((str = opts.FindStringKey("copyright"))) {
gtk_about_dialog_set_copyright(dialog, str->c_str());
}
if ((str = opts.FindStringKey("website"))) {
gtk_about_dialog_set_website(dialog, str->c_str());
}
if ((str = opts.FindStringKey("iconPath"))) {
GError* error = nullptr;
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);
if (error != nullptr) {
g_warning("%s", error->message);
g_clear_error(&error);
} else {
gtk_about_dialog_set_logo(dialog, icon);
g_clear_object(&icon);
}
}
if ((val = opts.FindListKey("authors"))) {
std::vector<const char*> cstrs;
for (const auto& authorVal : val->GetList()) {
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());
}
}
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialogWidget);
}
void Browser::SetAboutPanelOptions(base::DictionaryValue options) {
about_panel_options_ = std::move(options);
}
} // namespace electron