electron/shell/browser/ui/gtk/app_indicator_icon.cc
electron-roller[bot] 61374019c0
chore: bump chromium to 103.0.5046.0 (main) (#33906)
* chore: bump chromium in DEPS to 103.0.5020.0

* chore: bump chromium in DEPS to 103.0.5022.0

* chore: bump chromium in DEPS to 103.0.5024.0

* chore: update patches

* 3587410: [Printing] Remove JobEventDetails

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3587410

* chore: bump chromium in DEPS to 103.0.5026.0

* chore: update patches

* 3577218: WebUI: Delete webui_resources.grd and related GN targets.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3577218

* chore: bump chromium in DEPS to 103.0.5028.0

* chore: update patches

* 3579297: Convert UpdatePrintSettings() to use non-deprecated base::Value APIs.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3579297

* 3560622: serial: Add SerialPort.forget() method

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3560622

* 3581708: Restore original display when moving from tab-fullscreen to browser-fullscreen.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3581708

* chore: fix authorization flags lint error

* 3583363: Remove net wrappers around base/strings/escape.h

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3583363

* fixup! 3560622: serial: Add SerialPort.forget() method

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3560622

* 3587589: Reland "Propagate the MIME type from DownloadTargetDeterminer to DownloadItem"

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3587589

* 3584006: Remove IsRenderViewLive from content public

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3584006

* 3596174: [api] Remove APIs for resurrecting finalizers

Ref: https://chromium-review.googlesource.com/c/v8/v8/+/3596174

* 3368244: Hook SnapshotForContentAnalysis renderer API to scan system prints

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3368244

* chore: bump chromium in DEPS to 103.0.5030.0

* chore: update patches

* chore: bump chromium in DEPS to 103.0.5032.0

* chore: bump chromium in DEPS to 103.0.5034.0

* chore: bump chromium in DEPS to 103.0.5036.0

* chore: update patches

* 3586363: Introduce PrintRenderFrame.PrintWithParams() for batch printing to PDF

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3586363

* 3593199: Remove content::PermissionType references and replace them with blink::PermissionType

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3593199

* 3368244: Hook SnapshotForContentAnalysis renderer API to scan system prints

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3368244

* chore: lint

* chore: bump chromium in DEPS to 103.0.5038.0

* chore: update patches

* fixup! 3560622: serial: Add SerialPort.forget() method

* 3606495: mac screen capture: add metric

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3606495

* chore: bump chromium in DEPS to 103.0.5040.0

* chore: update patches

* 3590840: Add IPs to DnsOverHttpsServerConfig

https://chromium-review.googlesource.com/c/chromium/src/+/3590840

* stub functions for ElectronSerialDelegate and SerialChooserController to fix link

* 3566412: [base] Remove base/android dependency on post_task.h and officially remove post_task.h!

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3566412

* 3347944: [content] No longer hand-off whole MainFunctionParams to BrowserMainParts

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3347944

* fixup! 3566412: [base] Remove base/android dependency on post_task.h and off…
e3ea3e1
…icially remove post_task.h!

* chore: update process_singleton patches for content::GetIOThreadTaskRunner({})

Ref: 2015655: [BrowserThread] Migrate co/pub/br and co/br/scheduler to the new API | https://chromium-review.googlesource.com/c/chromium/src/+/2015655

* chore: migrate base::DeleteSoon to content::GetUIThreadTaskRunner({})->DeleteSoon

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3566412

* chore: remove duplicate functions for RevokePortPermissionWebInitiated & GetPortInfo

* chore: migrate Linux/Windows methods off of post_task.h

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3566412

* 64908: Stop building legacy SwiftShader GL in Chromium

https://swiftshader-review.googlesource.com/c/SwiftShader/+/64908

* 3573245: Added Themed versions of RoundedRectBackground and RoundedRectBorder.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/3573245

* chore: bump chromium in DEPS to 103.0.5042.0

* chore: update patches

* 3571804: [api] Advance API deprecation for V8 version v10.2

https://chromium-review.googlesource.com/c/v8/v8/+/3571804

* fixup! 3571804: [api] Advance API deprecation for V8 version v10.2

* build: fix run-clang-format extension matching

* lint

* fix windows build

* how is clang-format still not working for me

* chore: update patches

* 3623985: Replace ad-hoc SetPublicFirstPartySets calls with method in ContentBrowserClient.

https://chromium-review.googlesource.com/c/chromium/src/+/3623985

* no need to implement WillProvidePublicFirstPartySets; the default is false

* 3601036: [QT] Introduce ui/views/linux_ui/linux_ui_factory.*

https://chromium-review.googlesource.com/c/chromium/src/+/3601036

* 3583363: Remove net wrappers around base/strings/escape.h

https://chromium-review.googlesource.com/c/chromium/src/+/3583363

* lint

* chore: bump chromium in DEPS to 103.0.5044.0

* fix conflicts

* chore: update patches

* upgrade nan

* pin version of nan in tests

* replace my hacky deprecated override fix with the fix from upstream

* revert runtime dcheck in v8

* pin nan version at root too

* refactor: tell gyp to use c++17 when building with our node

* Revert "refactor: tell gyp to use c++17 when building with our node"

This reverts commit 41a03a5799a8f40f31555d73d20ea865acfcd192.

* Undo the reversion of 41a03a5799a8f40f31555d73d20ea865acfcd192.

This reverts commit 54440abc598153bd7e259be4a908f0ecc0b33348.

* disable sequential/test-cpu-prof-kill for now

* also sequential/test-diagnostic-dir-cpu-prof

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Keeley Hammond <khammond@slack-corp.com>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: VerteDinde <vertedinde@electronjs.org>
Co-authored-by: Jeremy Rose <japthorp@slack-corp.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Co-authored-by: Jeremy Rose <jeremya@chromium.org>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2022-05-17 12:48:40 -04:00

380 lines
13 KiB
C++

// 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 "shell/browser/ui/gtk/app_indicator_icon.h"
#include <dlfcn.h>
#include <gtk/gtk.h>
#include <algorithm>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/environment.h"
#include "base/files/file_util.h"
#include "base/hash/md5.h"
#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "content/public/browser/browser_thread.h"
#include "shell/browser/ui/gtk/app_indicator_icon_menu.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/base/models/menu_model.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
namespace {
typedef enum {
APP_INDICATOR_CATEGORY_APPLICATION_STATUS,
APP_INDICATOR_CATEGORY_COMMUNICATIONS,
APP_INDICATOR_CATEGORY_SYSTEM_SERVICES,
APP_INDICATOR_CATEGORY_HARDWARE,
APP_INDICATOR_CATEGORY_OTHER
} AppIndicatorCategory;
typedef enum {
APP_INDICATOR_STATUS_PASSIVE,
APP_INDICATOR_STATUS_ACTIVE,
APP_INDICATOR_STATUS_ATTENTION
} AppIndicatorStatus;
typedef AppIndicator* (*app_indicator_new_func)(const gchar* id,
const gchar* icon_name,
AppIndicatorCategory category);
typedef AppIndicator* (*app_indicator_new_with_path_func)(
const gchar* id,
const gchar* icon_name,
AppIndicatorCategory category,
const gchar* icon_theme_path);
typedef void (*app_indicator_set_status_func)(AppIndicator* self,
AppIndicatorStatus status);
typedef void (*app_indicator_set_attention_icon_full_func)(
AppIndicator* self,
const gchar* icon_name,
const gchar* icon_desc);
typedef void (*app_indicator_set_menu_func)(AppIndicator* self, GtkMenu* menu);
typedef void (*app_indicator_set_icon_full_func)(AppIndicator* self,
const gchar* icon_name,
const gchar* icon_desc);
typedef void (*app_indicator_set_icon_theme_path_func)(
AppIndicator* self,
const gchar* icon_theme_path);
bool g_attempted_load = false;
bool g_opened = false;
// Retrieved functions from libappindicator.
app_indicator_new_func app_indicator_new = nullptr;
app_indicator_new_with_path_func app_indicator_new_with_path = nullptr;
app_indicator_set_status_func app_indicator_set_status = nullptr;
app_indicator_set_attention_icon_full_func
app_indicator_set_attention_icon_full = nullptr;
app_indicator_set_menu_func app_indicator_set_menu = nullptr;
app_indicator_set_icon_full_func app_indicator_set_icon_full = nullptr;
app_indicator_set_icon_theme_path_func app_indicator_set_icon_theme_path =
nullptr;
void EnsureLibAppIndicatorLoaded() {
if (g_attempted_load)
return;
g_attempted_load = true;
std::string lib_name =
"libappindicator" + base::NumberToString(GTK_MAJOR_VERSION) + ".so";
void* indicator_lib = dlopen(lib_name.c_str(), RTLD_LAZY);
if (!indicator_lib) {
lib_name += ".1";
indicator_lib = dlopen(lib_name.c_str(), RTLD_LAZY);
}
if (!indicator_lib)
return;
g_opened = true;
app_indicator_new = reinterpret_cast<app_indicator_new_func>(
dlsym(indicator_lib, "app_indicator_new"));
app_indicator_new_with_path =
reinterpret_cast<app_indicator_new_with_path_func>(
dlsym(indicator_lib, "app_indicator_new_with_path"));
app_indicator_set_status = reinterpret_cast<app_indicator_set_status_func>(
dlsym(indicator_lib, "app_indicator_set_status"));
app_indicator_set_attention_icon_full =
reinterpret_cast<app_indicator_set_attention_icon_full_func>(
dlsym(indicator_lib, "app_indicator_set_attention_icon_full"));
app_indicator_set_menu = reinterpret_cast<app_indicator_set_menu_func>(
dlsym(indicator_lib, "app_indicator_set_menu"));
app_indicator_set_icon_full =
reinterpret_cast<app_indicator_set_icon_full_func>(
dlsym(indicator_lib, "app_indicator_set_icon_full"));
app_indicator_set_icon_theme_path =
reinterpret_cast<app_indicator_set_icon_theme_path_func>(
dlsym(indicator_lib, "app_indicator_set_icon_theme_path"));
}
// Writes |bitmap| to a file at |path|. Returns true if successful.
bool WriteFile(const base::FilePath& path, const SkBitmap& bitmap) {
std::vector<unsigned char> png_data;
if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data))
return false;
int bytes_written = base::WriteFile(
path, reinterpret_cast<char*>(&png_data[0]), png_data.size());
return (bytes_written == static_cast<int>(png_data.size()));
}
void DeleteTempDirectory(const base::FilePath& dir_path) {
if (dir_path.empty())
return;
base::DeletePathRecursively(dir_path);
}
} // namespace
namespace electron {
namespace gtkui {
AppIndicatorIcon::AppIndicatorIcon(std::string id,
const gfx::ImageSkia& image,
const std::u16string& tool_tip)
: id_(id) {
auto env = base::Environment::Create();
desktop_env_ = base::nix::GetDesktopEnvironment(env.get());
EnsureLibAppIndicatorLoaded();
tool_tip_ = base::UTF16ToUTF8(tool_tip);
SetIcon(image);
}
AppIndicatorIcon::~AppIndicatorIcon() {
if (icon_) {
app_indicator_set_status(icon_, APP_INDICATOR_STATUS_PASSIVE);
g_object_unref(icon_);
base::ThreadPool::PostTask(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce(&DeleteTempDirectory, temp_dir_));
}
}
// static
bool AppIndicatorIcon::CouldOpen() {
EnsureLibAppIndicatorLoaded();
return g_opened;
}
void AppIndicatorIcon::SetIcon(const gfx::ImageSkia& image) {
if (!g_opened)
return;
++icon_change_count_;
// Copy the bitmap because it may be freed by the time it's accessed in
// another thread.
SkBitmap safe_bitmap = *image.bitmap();
const base::TaskTraits kTraits = {
base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN};
if (desktop_env_ == base::nix::DESKTOP_ENVIRONMENT_KDE4 ||
desktop_env_ == base::nix::DESKTOP_ENVIRONMENT_KDE5) {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, kTraits,
base::BindOnce(AppIndicatorIcon::WriteKDE4TempImageOnWorkerThread,
safe_bitmap, temp_dir_),
base::BindOnce(&AppIndicatorIcon::SetImageFromFile,
weak_factory_.GetWeakPtr()));
} else {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, kTraits,
base::BindOnce(AppIndicatorIcon::WriteUnityTempImageOnWorkerThread,
safe_bitmap, icon_change_count_, id_),
base::BindOnce(&AppIndicatorIcon::SetImageFromFile,
weak_factory_.GetWeakPtr()));
}
}
void AppIndicatorIcon::SetToolTip(const std::u16string& tool_tip) {
DCHECK(!tool_tip_.empty());
tool_tip_ = base::UTF16ToUTF8(tool_tip);
UpdateClickActionReplacementMenuItem();
}
void AppIndicatorIcon::UpdatePlatformContextMenu(ui::MenuModel* model) {
if (!g_opened)
return;
menu_model_ = model;
// The icon is created asynchronously so it might not exist when the menu is
// set.
if (icon_)
SetMenu();
}
void AppIndicatorIcon::RefreshPlatformContextMenu() {
menu_->Refresh();
}
// static
AppIndicatorIcon::SetImageFromFileParams
AppIndicatorIcon::WriteKDE4TempImageOnWorkerThread(
const SkBitmap& bitmap,
const base::FilePath& existing_temp_dir) {
base::FilePath temp_dir = existing_temp_dir;
if (temp_dir.empty() &&
!base::CreateNewTempDirectory(base::FilePath::StringType(), &temp_dir)) {
LOG(WARNING) << "Could not create temporary directory";
return SetImageFromFileParams();
}
base::FilePath icon_theme_path = temp_dir.AppendASCII("icons");
// On KDE4, an image located in a directory ending with
// "icons/hicolor/22x22/apps" can be used as the app indicator image because
// "/usr/share/icons/hicolor/22x22/apps" exists.
base::FilePath image_dir =
icon_theme_path.AppendASCII("hicolor").AppendASCII("22x22").AppendASCII(
"apps");
if (!base::CreateDirectory(image_dir))
return SetImageFromFileParams();
// On KDE4, the name of the image file for each different looking bitmap must
// be unique. It must also be unique across runs of Chrome.
std::vector<unsigned char> bitmap_png_data;
if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bitmap_png_data)) {
LOG(WARNING) << "Could not encode icon";
return SetImageFromFileParams();
}
base::MD5Digest digest;
base::MD5Sum(reinterpret_cast<char*>(&bitmap_png_data[0]),
bitmap_png_data.size(), &digest);
std::string icon_name = base::StringPrintf(
"electron_app_indicator2_%s", base::MD5DigestToBase16(digest).c_str());
// If |bitmap| is smaller than 22x22, KDE does some really ugly resizing.
// Pad |bitmap| with transparent pixels to make it 22x22.
const int kMinimalSize = 22;
SkBitmap scaled_bitmap;
scaled_bitmap.allocN32Pixels(std::max(bitmap.width(), kMinimalSize),
std::max(bitmap.height(), kMinimalSize));
scaled_bitmap.eraseARGB(0, 0, 0, 0);
SkCanvas canvas(scaled_bitmap);
canvas.drawImage(bitmap.asImage(),
(scaled_bitmap.width() - bitmap.width()) / 2,
(scaled_bitmap.height() - bitmap.height()) / 2);
base::FilePath image_path = image_dir.Append(icon_name + ".png");
if (!WriteFile(image_path, scaled_bitmap))
return SetImageFromFileParams();
SetImageFromFileParams params;
params.parent_temp_dir = temp_dir;
params.icon_theme_path = icon_theme_path.value();
params.icon_name = icon_name;
return params;
}
// static
AppIndicatorIcon::SetImageFromFileParams
AppIndicatorIcon::WriteUnityTempImageOnWorkerThread(const SkBitmap& bitmap,
int icon_change_count,
const std::string& id) {
// Create a new temporary directory for each image on Unity since using a
// single temporary directory seems to have issues when changing icons in
// quick succession.
base::FilePath temp_dir;
if (!base::CreateNewTempDirectory(base::FilePath::StringType(), &temp_dir)) {
LOG(WARNING) << "Could not create temporary directory";
return SetImageFromFileParams();
}
std::string icon_name =
base::StringPrintf("%s_%d", id.c_str(), icon_change_count);
base::FilePath image_path = temp_dir.Append(icon_name + ".png");
SetImageFromFileParams params;
if (WriteFile(image_path, bitmap)) {
params.parent_temp_dir = temp_dir;
params.icon_theme_path = temp_dir.value();
params.icon_name = icon_name;
}
return params;
}
void AppIndicatorIcon::SetImageFromFile(const SetImageFromFileParams& params) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (params.icon_theme_path.empty())
return;
if (!icon_) {
icon_ =
app_indicator_new_with_path(id_.c_str(), params.icon_name.c_str(),
APP_INDICATOR_CATEGORY_APPLICATION_STATUS,
params.icon_theme_path.c_str());
app_indicator_set_status(icon_, APP_INDICATOR_STATUS_ACTIVE);
SetMenu();
} else {
app_indicator_set_icon_theme_path(icon_, params.icon_theme_path.c_str());
app_indicator_set_icon_full(icon_, params.icon_name.c_str(), "icon");
}
if (temp_dir_ != params.parent_temp_dir) {
base::ThreadPool::PostTask(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce(&DeleteTempDirectory, temp_dir_));
temp_dir_ = params.parent_temp_dir;
}
}
void AppIndicatorIcon::SetMenu() {
menu_ = std::make_unique<AppIndicatorIconMenu>(menu_model_);
UpdateClickActionReplacementMenuItem();
app_indicator_set_menu(icon_, menu_->GetGtkMenu());
}
void AppIndicatorIcon::UpdateClickActionReplacementMenuItem() {
// The menu may not have been created yet.
if (!menu_.get())
return;
if (!delegate()->HasClickAction() && menu_model_)
return;
DCHECK(!tool_tip_.empty());
menu_->UpdateClickActionReplacementMenuItem(
tool_tip_.c_str(),
base::BindRepeating(
&AppIndicatorIcon::OnClickActionReplacementMenuItemActivated,
base::Unretained(this)));
}
void AppIndicatorIcon::OnClickActionReplacementMenuItemActivated() {
if (delegate())
delegate()->OnClick();
}
} // namespace gtkui
} // namespace electron