electron/shell/browser/browser_mac.mm

398 lines
13 KiB
Text
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
// found in the LICENSE file.
#include "shell/browser/browser.h"
#include <string>
#include <utility>
#include "base/mac/bundle_locations.h"
#include "base/mac/foundation_util.h"
2016-07-06 18:43:44 +00:00
#include "base/mac/mac_util.h"
#include "base/mac/scoped_cftyperef.h"
2016-07-01 08:39:01 +00:00
#include "base/strings/string_number_conversions.h"
2016-07-02 01:36:11 +00:00
#include "base/strings/sys_string_conversions.h"
2016-05-23 15:49:46 +00:00
#include "net/base/mac/url_conversions.h"
#include "shell/browser/mac/atom_application.h"
#include "shell/browser/mac/atom_application_delegate.h"
#include "shell/browser/mac/dict_util.h"
#include "shell/browser/native_window.h"
#include "shell/browser/window_list.h"
#include "shell/common/application_info.h"
#include "shell/common/gin_helper/arguments.h"
#include "shell/common/platform_util.h"
#include "shell/common/promise_util.h"
#include "ui/gfx/image/image.h"
2016-05-23 15:49:46 +00:00
#include "url/gurl.h"
namespace electron {
2018-02-05 07:13:35 +00:00
void Browser::SetShutdownHandler(base::Callback<bool()> handler) {
[[AtomApplication sharedApplication] setShutdownHandler:std::move(handler)];
}
2013-05-30 11:24:47 +00:00
void Browser::Focus() {
[[AtomApplication sharedApplication] activateIgnoringOtherApps:NO];
2013-05-30 11:24:47 +00:00
}
void Browser::Hide() {
[[AtomApplication sharedApplication] hide:nil];
}
2016-01-30 20:40:32 +00:00
void Browser::Show() {
[[AtomApplication sharedApplication] unhide:nil];
}
2014-11-17 05:05:06 +00:00
void Browser::AddRecentDocument(const base::FilePath& path) {
NSString* path_string = base::mac::FilePathToNSString(path);
if (!path_string)
return;
NSURL* u = [NSURL fileURLWithPath:path_string];
if (!u)
return;
2014-11-17 05:05:06 +00:00
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:u];
}
2014-11-17 08:13:47 +00:00
void Browser::ClearRecentDocuments() {
[[NSDocumentController sharedDocumentController] clearRecentDocuments:nil];
2014-11-17 08:13:47 +00:00
}
2016-08-16 05:54:30 +00:00
bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
gin_helper::Arguments* args) {
NSString* identifier = [base::mac::MainBundle() bundleIdentifier];
if (!identifier)
return false;
2016-08-16 07:40:44 +00:00
if (!Browser::IsDefaultProtocolClient(protocol, args))
return false;
NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
CFStringRef protocol_cf = base::mac::NSToCFCast(protocol_ns);
CFArrayRef bundleList = LSCopyAllHandlersForURLScheme(protocol_cf);
if (!bundleList) {
return false;
}
2016-06-18 13:26:26 +00:00
// On macOS, we can't query the default, but the handlers list seems to put
// Apple's defaults first, so we'll use the first option that isn't our bundle
CFStringRef other = nil;
2017-03-30 20:56:21 +00:00
for (CFIndex i = 0; i < CFArrayGetCount(bundleList); ++i) {
2018-04-20 18:47:04 +00:00
other =
base::mac::CFCast<CFStringRef>(CFArrayGetValueAtIndex(bundleList, i));
if (![identifier isEqualToString:(__bridge NSString*)other]) {
break;
}
}
2018-06-20 03:16:37 +00:00
// No other app was found set it to none instead of setting it back to itself.
if ([identifier isEqualToString:(__bridge NSString*)other]) {
other = base::mac::NSToCFCast(@"None");
}
OSStatus return_code = LSSetDefaultHandlerForURLScheme(protocol_cf, other);
return return_code == noErr;
}
2016-08-16 05:54:30 +00:00
bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,
gin_helper::Arguments* args) {
if (protocol.empty())
return false;
NSString* identifier = [base::mac::MainBundle() bundleIdentifier];
if (!identifier)
return false;
NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
2018-04-20 18:47:04 +00:00
OSStatus return_code = LSSetDefaultHandlerForURLScheme(
base::mac::NSToCFCast(protocol_ns), base::mac::NSToCFCast(identifier));
return return_code == noErr;
}
2016-08-16 05:54:30 +00:00
bool Browser::IsDefaultProtocolClient(const std::string& protocol,
gin_helper::Arguments* args) {
2016-04-25 05:17:01 +00:00
if (protocol.empty())
return false;
NSString* identifier = [base::mac::MainBundle() bundleIdentifier];
if (!identifier)
return false;
NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
2016-04-30 18:17:29 +00:00
base::ScopedCFTypeRef<CFStringRef> bundleId(
LSCopyDefaultHandlerForURLScheme(base::mac::NSToCFCast(protocol_ns)));
2016-04-25 05:17:01 +00:00
if (!bundleId)
return false;
2016-04-30 18:17:29 +00:00
// Ensure the comparison is case-insensitive
2016-04-25 05:17:01 +00:00
// as LS does not persist the case of the bundle id.
NSComparisonResult result =
[base::mac::CFToNSCast(bundleId) caseInsensitiveCompare:identifier];
2016-04-25 05:17:01 +00:00
return result == NSOrderedSame;
}
2018-04-20 18:47:04 +00:00
void Browser::SetAppUserModelID(const base::string16& name) {}
bool Browser::SetBadgeCount(int count) {
chore: bump chromium to 1e9f9a24aa12 (master) (#17880) * chore: bump chromium in DEPS to 1e9f9a24aa12bea9cf194a82a7e249bd1242ec4f * chore: update patches * Make WebContents' theme color a base::Optional<SkColor> https://chromium-review.googlesource.com/c/chromium/src/+/1540022 * update autofill patch for incorrect header includes * Move Shell messages to web_test and rename to BlinkTest. https://chromium-review.googlesource.com/c/chromium/src/+/1525181 * Make PlatformNotificationServiceImpl a KeyedService. https://chromium-review.googlesource.com/c/chromium/src/+/1336150 * Move MediaPlayerId to its own file. https://chromium-review.googlesource.com/c/chromium/src/+/1547057 * Remove net/base/completion_callback.h, which is no longer used https://chromium-review.googlesource.com/c/chromium/src/+/1552821 * AW NS: support file scheme cookies https://chromium-review.googlesource.com/c/chromium/src/+/1533486 * Remove SecurityInfo and adapt remaining consumers https://chromium-review.googlesource.com/c/chromium/src/+/1509455 * Remove deprecated type-specific number to string conversion functions https://chromium-review.googlesource.com/c/chromium/src/+/1545881 * DevTools: Adding new performance histograms for launch of top 4 tools https://chromium-review.googlesource.com/c/chromium/src/+/1506388 * Update include paths for //base/hash/hash.h https://chromium-review.googlesource.com/c/chromium/src/+/1544630 * build: Disable ensure_gn_version gclient hook for mac CI checkout * update patches * use maybe version of v8::String::NewFromTwoByte * bump appveyor image version * fix mac ci hopefully * Convert enum to enum class for MenuAnchorPosition https://chromium-review.googlesource.com/c/chromium/src/+/1530508 * use maybe version of ToObject * RenderViewHost::GetProcess is no longer const * Unrefcount AuthChallengeInfo https://chromium-review.googlesource.com/c/chromium/src/+/1550631 * MenuButtonController takes Button rather than MenuButton https://chromium-review.googlesource.com/c/chromium/src/+/1500935 * add //ui/views_bridge_mac to deps to fix link error * forward declare views::Button in atom::MenuDelegate * more v8 patches * base/{=> hash}/md5.h https://chromium-review.googlesource.com/c/chromium/src/+/1535124 * gfx::{PlatformFontWin => win}::* https://chromium-review.googlesource.com/c/chromium/src/+/1534178 * fix v8 patches * [base] Rename TaskScheduler to ThreadPool https://chromium-review.googlesource.com/c/chromium/src/+/1561552 * use internal_config_base for bytecode_builtins_list_generator avoids windows link errors * FIXME: temporarily disable v8/breakpad integration * FIXME: temporarily disable prevent-will-redirect test * FIXME: disable neon on aarch64 pending crbug.com/953815 * update to account for WebCursor refactor https://chromium-review.googlesource.com/c/chromium/src/+/1562755 * enable stack dumping on appveyor * Revert "FIXME: disable neon on aarch64 pending crbug.com/953815" This reverts commit 57f082026be3d83069f2a2814684abf4dc9e7b53. * fix: remove const qualifiers to match upstream * fix: remove const qualifiers to match upstream in cc files as well * don't throw an error when testing if an object is an object * use non-deprecated Buffer constructor * Remove net::CookieSameSite::DEFAULT_MODE enum value https://chromium-review.googlesource.com/c/chromium/src/+/1567955 * depend on modded dbus-native to work around buffer deprecation https://github.com/sidorares/dbus-native/pull/262 * revert clang roll to fix arm build on linux * fixup! depend on modded dbus-native to work around buffer deprecation need more coffee * update coffee-script * robustify verify-mksnapshot w.r.t. command-line parameters * Revert "robustify verify-mksnapshot w.r.t. command-line parameters" This reverts commit a49af01411f684f6025528d604895c3696e0bc57. * fix mksnapshot by matching args * update patches * TMP: enable rdp on appveyor * Changed ContentBrowserClient::CreateQuotaPermissionContext() to return scoped_refptr. https://chromium-review.googlesource.com/c/chromium/src/+/1569376 * Make content::ResourceType an enum class. https://chromium-review.googlesource.com/c/chromium/src/+/1569345 * fixup! Make content::ResourceType an enum class. * turn off rdp * use net::CompletionRepeatingCallback instead of base::Callback<void(int)> * remove disable_ensure_gn_version_gclient_hook.patch * copy repeating callback instead of std::move * fix lint * add completion_repeating_callback.h include
2019-04-20 17:20:37 +00:00
DockSetBadgeText(count != 0 ? base::NumberToString(count) : "");
2016-07-02 01:36:11 +00:00
badge_count_ = count;
return true;
2016-07-01 08:39:01 +00:00
}
2016-05-23 15:49:46 +00:00
void Browser::SetUserActivity(const std::string& type,
const base::DictionaryValue& user_info,
gin_helper::Arguments* args) {
2016-05-23 15:49:46 +00:00
std::string url_string;
args->GetNext(&url_string);
[[AtomApplication sharedApplication]
setCurrentActivity:base::SysUTF8ToNSString(type)
2016-05-23 15:49:46 +00:00
withUserInfo:DictionaryValueToNSDictionary(user_info)
withWebpageURL:net::NSURLWithGURL(GURL(url_string))];
}
std::string Browser::GetCurrentActivityType() {
NSUserActivity* userActivity =
[[AtomApplication sharedApplication] getCurrentActivity];
return base::SysNSStringToUTF8(userActivity.activityType);
}
void Browser::InvalidateCurrentActivity() {
[[AtomApplication sharedApplication] invalidateCurrentActivity];
}
void Browser::ResignCurrentActivity() {
[[AtomApplication sharedApplication] resignCurrentActivity];
}
void Browser::UpdateCurrentActivity(const std::string& type,
const base::DictionaryValue& user_info) {
2017-09-14 07:12:34 +00:00
[[AtomApplication sharedApplication]
updateCurrentActivity:base::SysUTF8ToNSString(type)
withUserInfo:DictionaryValueToNSDictionary(user_info)];
}
bool Browser::WillContinueUserActivity(const std::string& type) {
bool prevent_default = false;
2017-09-14 07:12:34 +00:00
for (BrowserObserver& observer : observers_)
observer.OnWillContinueUserActivity(&prevent_default, type);
return prevent_default;
}
2017-09-14 07:12:34 +00:00
void Browser::DidFailToContinueUserActivity(const std::string& type,
const std::string& error) {
for (BrowserObserver& observer : observers_)
observer.OnDidFailToContinueUserActivity(type, error);
}
2016-05-23 15:49:46 +00:00
bool Browser::ContinueUserActivity(const std::string& type,
const base::DictionaryValue& user_info) {
bool prevent_default = false;
2017-01-24 03:34:39 +00:00
for (BrowserObserver& observer : observers_)
observer.OnContinueUserActivity(&prevent_default, type, user_info);
return prevent_default;
}
2017-09-14 07:12:34 +00:00
void Browser::UserActivityWasContinued(const std::string& type,
const base::DictionaryValue& user_info) {
for (BrowserObserver& observer : observers_)
observer.OnUserActivityWasContinued(type, user_info);
}
bool Browser::UpdateUserActivityState(const std::string& type,
const base::DictionaryValue& user_info) {
bool prevent_default = false;
for (BrowserObserver& observer : observers_)
observer.OnUpdateUserActivityState(&prevent_default, type, user_info);
return prevent_default;
}
Browser::LoginItemSettings Browser::GetLoginItemSettings(
2017-03-29 19:29:52 +00:00
const LoginItemSettings& options) {
2016-07-07 23:29:09 +00:00
LoginItemSettings settings;
#if defined(MAS_BUILD)
settings.open_at_login = platform_util::GetLoginItemEnabled();
#else
2018-04-20 18:47:04 +00:00
settings.open_at_login =
base::mac::CheckLoginItemStatus(&settings.open_as_hidden);
2016-07-07 23:29:09 +00:00
settings.restore_state = base::mac::WasLaunchedAsLoginItemRestoreState();
settings.opened_at_login = base::mac::WasLaunchedAsLoginOrResumeItem();
settings.opened_as_hidden = base::mac::WasLaunchedAsHiddenLoginItem();
#endif
2016-07-07 23:29:09 +00:00
return settings;
}
void RemoveFromLoginItems() {
// logic to find the login item copied from GetLoginItemForApp in
// base/mac/mac_util.mm
base::ScopedCFTypeRef<LSSharedFileListRef> login_items(
LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL));
if (!login_items.get()) {
LOG(ERROR) << "Couldn't get a Login Items list.";
return;
}
base::scoped_nsobject<NSArray> login_items_array(
base::mac::CFToNSCast(LSSharedFileListCopySnapshot(login_items, NULL)));
NSURL* url = [NSURL fileURLWithPath:[base::mac::MainBundle() bundlePath]];
for (NSUInteger i = 0; i < [login_items_array count]; ++i) {
LSSharedFileListItemRef item =
reinterpret_cast<LSSharedFileListItemRef>(login_items_array[i]);
base::ScopedCFTypeRef<CFErrorRef> error;
CFURLRef item_url_ref =
LSSharedFileListItemCopyResolvedURL(item, 0, error.InitializeInto());
if (!error && item_url_ref) {
base::ScopedCFTypeRef<CFURLRef> item_url(item_url_ref);
if (CFEqual(item_url, url)) {
LSSharedFileListItemRemove(login_items, item);
return;
}
}
}
}
void Browser::SetLoginItemSettings(LoginItemSettings settings) {
#if defined(MAS_BUILD)
if (!platform_util::SetLoginItemEnabled(settings.open_at_login)) {
LOG(ERROR) << "Unable to set login item enabled on sandboxed app.";
}
#else
if (settings.open_at_login) {
2016-07-07 23:29:09 +00:00
base::mac::AddToLoginItems(settings.open_as_hidden);
} else {
RemoveFromLoginItems();
}
#endif
}
2013-12-05 02:26:01 +00:00
std::string Browser::GetExecutableFileVersion() const {
2018-10-24 10:49:10 +00:00
return GetApplicationVersion();
}
std::string Browser::GetExecutableFileProductName() const {
2018-10-24 10:49:10 +00:00
return GetApplicationName();
}
2013-08-06 08:19:56 +00:00
int Browser::DockBounce(BounceType type) {
2015-12-21 02:52:49 +00:00
return [[AtomApplication sharedApplication]
requestUserAttention:static_cast<NSRequestUserAttentionType>(type)];
2013-08-06 08:19:56 +00:00
}
void Browser::DockCancelBounce(int request_id) {
[[AtomApplication sharedApplication] cancelUserAttentionRequest:request_id];
2013-08-06 08:19:56 +00:00
}
void Browser::DockSetBadgeText(const std::string& label) {
2018-04-20 18:47:04 +00:00
NSDockTile* tile = [[AtomApplication sharedApplication] dockTile];
2013-08-06 08:19:56 +00:00
[tile setBadgeLabel:base::SysUTF8ToNSString(label)];
}
void Browser::DockDownloadFinished(const std::string& filePath) {
[[NSDistributedNotificationCenter defaultCenter]
2018-04-20 18:47:04 +00:00
postNotificationName:@"com.apple.DownloadFileFinished"
object:base::SysUTF8ToNSString(filePath)];
}
2013-08-06 08:39:31 +00:00
std::string Browser::DockGetBadgeText() {
2018-04-20 18:47:04 +00:00
NSDockTile* tile = [[AtomApplication sharedApplication] dockTile];
2013-08-06 08:39:31 +00:00
return base::SysNSStringToUTF8([tile badgeLabel]);
}
void Browser::DockHide() {
for (auto* const& window : WindowList::GetWindows())
[window->GetNativeWindow().GetNativeNSWindow() setCanHide:NO];
2018-04-20 18:47:04 +00:00
ProcessSerialNumber psn = {0, kCurrentProcess};
TransformProcessType(&psn, kProcessTransformToUIElementApplication);
}
2016-08-01 22:22:37 +00:00
bool Browser::DockIsVisible() {
// Because DockShow has a slight delay this may not be true immediately
// after that call.
2018-04-20 18:47:04 +00:00
return ([[NSRunningApplication currentApplication] activationPolicy] ==
NSApplicationActivationPolicyRegular);
2016-08-01 22:22:37 +00:00
}
v8::Local<v8::Promise> Browser::DockShow(v8::Isolate* isolate) {
util::Promise<void*> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();
BOOL active = [[NSRunningApplication currentApplication] isActive];
2018-04-20 18:47:04 +00:00
ProcessSerialNumber psn = {0, kCurrentProcess};
if (active) {
2015-12-21 02:52:49 +00:00
// Workaround buggy behavior of TransformProcessType.
// http://stackoverflow.com/questions/7596643/
NSArray* runningApps = [NSRunningApplication
runningApplicationsWithBundleIdentifier:@"com.apple.dock"];
for (NSRunningApplication* app in runningApps) {
[app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
break;
}
__block util::Promise<void*> p = std::move(promise);
2015-12-21 02:52:49 +00:00
dispatch_time_t one_ms = dispatch_time(DISPATCH_TIME_NOW, USEC_PER_SEC);
dispatch_after(one_ms, dispatch_get_main_queue(), ^{
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
2015-12-21 02:52:49 +00:00
dispatch_time_t one_ms = dispatch_time(DISPATCH_TIME_NOW, USEC_PER_SEC);
dispatch_after(one_ms, dispatch_get_main_queue(), ^{
[[NSRunningApplication currentApplication]
activateWithOptions:NSApplicationActivateIgnoringOtherApps];
p.Resolve();
2015-12-21 02:52:49 +00:00
});
});
} else {
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
promise.Resolve();
}
return handle;
}
void Browser::DockSetMenu(AtomMenuModel* model) {
2018-04-20 18:47:04 +00:00
AtomApplicationDelegate* delegate =
(AtomApplicationDelegate*)[NSApp delegate];
2014-11-16 15:04:31 +00:00
[delegate setApplicationDockMenu:model];
}
void Browser::DockSetIcon(const gfx::Image& image) {
2016-02-01 08:06:09 +00:00
[[AtomApplication sharedApplication]
setApplicationIconImage:image.AsNSImage()];
}
2016-10-10 20:30:58 +00:00
void Browser::ShowAboutPanel() {
NSDictionary* options = DictionaryValueToNSDictionary(about_panel_options_);
// Credits must be a NSAttributedString instead of NSString
id credits = options[@"Credits"];
if (credits != nil) {
NSMutableDictionary* mutable_options = [options mutableCopy];
mutable_options[@"Credits"] = [[[NSAttributedString alloc]
initWithString:(NSString*)credits] autorelease];
options = [NSDictionary dictionaryWithDictionary:mutable_options];
}
2016-10-10 20:30:58 +00:00
[[AtomApplication sharedApplication]
orderFrontStandardAboutPanelWithOptions:options];
}
void Browser::SetAboutPanelOptions(const base::DictionaryValue& options) {
about_panel_options_.Clear();
2016-10-12 17:52:59 +00:00
for (const auto& pair : options) {
std::string key = pair.first;
const auto& val = pair.second;
if (!key.empty() && val->is_string()) {
2016-10-12 17:52:59 +00:00
key[0] = base::ToUpperASCII(key[0]);
about_panel_options_.SetString(key, val->GetString());
2016-10-12 17:52:59 +00:00
}
}
2016-10-10 20:30:58 +00:00
}
2019-03-14 20:39:52 +00:00
void Browser::ShowEmojiPanel() {
[[AtomApplication sharedApplication] orderFrontCharacterPalette:nil];
}
bool Browser::IsEmojiPanelSupported() {
return true;
}
} // namespace electron