2014-10-31 18:17:05 +00:00
|
|
|
// 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
|
2013-05-02 15:43:23 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/browser.h"
|
2013-05-02 15:43:23 +00:00
|
|
|
|
2015-04-14 07:55:41 +00:00
|
|
|
#include "atom/browser/mac/atom_application.h"
|
|
|
|
#include "atom/browser/mac/atom_application_delegate.h"
|
2016-05-05 07:26:44 +00:00
|
|
|
#include "atom/browser/mac/dict_util.h"
|
2014-06-25 03:55:33 +00:00
|
|
|
#include "atom/browser/native_window.h"
|
|
|
|
#include "atom/browser/window_list.h"
|
2016-03-21 18:24:25 +00:00
|
|
|
#include "base/mac/bundle_locations.h"
|
2015-04-14 07:55:41 +00:00
|
|
|
#include "base/mac/foundation_util.h"
|
2013-06-19 05:41:54 +00:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2015-04-14 07:55:41 +00:00
|
|
|
#include "brightray/common/application_info.h"
|
2013-05-02 15:43:23 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2013-05-30 11:24:47 +00:00
|
|
|
void Browser::Focus() {
|
2013-06-26 09:22:24 +00:00
|
|
|
[[AtomApplication sharedApplication] activateIgnoringOtherApps:YES];
|
2013-05-30 11:24:47 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 11:30:19 +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) {
|
2015-12-15 09:17:24 +00:00
|
|
|
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() {
|
2015-12-30 00:35:54 +00:00
|
|
|
[[NSDocumentController sharedDocumentController] clearRecentDocuments:nil];
|
2014-11-17 08:13:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 17:55:09 +00:00
|
|
|
bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol) {
|
2016-05-07 18:07:58 +00:00
|
|
|
NSString* identifier = [base::mac::MainBundle() bundleIdentifier];
|
|
|
|
if (!identifier)
|
|
|
|
return false;
|
|
|
|
|
2016-05-08 17:50:17 +00:00
|
|
|
if (!Browser::IsDefaultProtocolClient(protocol))
|
|
|
|
return false;
|
|
|
|
|
2016-05-07 18:07:58 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
// On Mac OS X, 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;
|
|
|
|
for (CFIndex i = 0; i < CFArrayGetCount(bundleList); i++) {
|
|
|
|
other = (CFStringRef)CFArrayGetValueAtIndex(bundleList, i);
|
|
|
|
if (![identifier isEqualToString: (__bridge NSString *)other]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OSStatus return_code = LSSetDefaultHandlerForURLScheme(protocol_cf, other);
|
|
|
|
return return_code == noErr;
|
2016-03-24 17:55:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 18:24:25 +00:00
|
|
|
bool Browser::SetAsDefaultProtocolClient(const std::string& protocol) {
|
|
|
|
if (protocol.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NSString* identifier = [base::mac::MainBundle() bundleIdentifier];
|
|
|
|
if (!identifier)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
|
|
|
|
OSStatus return_code =
|
|
|
|
LSSetDefaultHandlerForURLScheme(base::mac::NSToCFCast(protocol_ns),
|
|
|
|
base::mac::NSToCFCast(identifier));
|
|
|
|
return return_code == noErr;
|
|
|
|
}
|
|
|
|
|
2016-04-25 05:17:01 +00:00
|
|
|
bool Browser::IsDefaultProtocolClient(const std::string& protocol) {
|
|
|
|
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
|
|
|
|
2016-04-25 05:17:01 +00:00
|
|
|
CFStringRef bundle =
|
|
|
|
LSCopyDefaultHandlerForURLScheme(base::mac::NSToCFCast(protocol_ns));
|
|
|
|
NSString* bundleId = static_cast<NSString*>(
|
|
|
|
base::mac::CFTypeRefToNSObjectAutorelease(bundle));
|
|
|
|
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 =
|
|
|
|
[bundleId caseInsensitiveCompare:identifier];
|
|
|
|
return result == NSOrderedSame;
|
|
|
|
}
|
|
|
|
|
2015-11-03 07:09:31 +00:00
|
|
|
void Browser::SetAppUserModelID(const base::string16& name) {
|
|
|
|
}
|
|
|
|
|
2016-05-05 03:26:23 +00:00
|
|
|
void Browser::SetUserActivity(
|
|
|
|
const std::string& type,
|
2016-05-05 07:26:44 +00:00
|
|
|
const base::DictionaryValue& user_info) {
|
2016-05-05 07:38:47 +00:00
|
|
|
[[AtomApplication sharedApplication]
|
|
|
|
setCurrentActivity:base::SysUTF8ToNSString(type)
|
|
|
|
withUserInfo:DictionaryValueToNSDictionary(user_info)];
|
2016-05-03 22:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Browser::GetCurrentActivityType() {
|
2016-05-05 07:38:47 +00:00
|
|
|
NSUserActivity* userActivity =
|
2016-05-05 03:26:23 +00:00
|
|
|
[[AtomApplication sharedApplication] getCurrentActivity];
|
2016-05-05 07:38:47 +00:00
|
|
|
return base::SysNSStringToUTF8(userActivity.activityType);
|
2016-04-30 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 07:26:44 +00:00
|
|
|
bool Browser::ContinueUserActivity(
|
|
|
|
const std::string& type,
|
|
|
|
const base::DictionaryValue& user_info) {
|
|
|
|
bool prevent_default = false;
|
|
|
|
FOR_EACH_OBSERVER(BrowserObserver,
|
|
|
|
observers_,
|
|
|
|
OnContinueUserActivity(&prevent_default, type, user_info));
|
|
|
|
return prevent_default;
|
|
|
|
}
|
|
|
|
|
2013-12-05 02:26:01 +00:00
|
|
|
std::string Browser::GetExecutableFileVersion() const {
|
2015-04-14 07:55:41 +00:00
|
|
|
return brightray::GetApplicationVersion();
|
2013-06-19 05:41:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 02:42:04 +00:00
|
|
|
std::string Browser::GetExecutableFileProductName() const {
|
2015-04-14 07:55:41 +00:00
|
|
|
return brightray::GetApplicationName();
|
2013-12-05 02:42:04 +00:00
|
|
|
}
|
|
|
|
|
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:(NSRequestUserAttentionType)type];
|
2013-08-06 08:19:56 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 13:59:05 +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) {
|
|
|
|
NSDockTile *tile = [[AtomApplication sharedApplication] dockTile];
|
|
|
|
[tile setBadgeLabel:base::SysUTF8ToNSString(label)];
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:02:56 +00:00
|
|
|
void Browser::DockDownloadFinished(const std::string& filePath) {
|
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: @"com.apple.DownloadFileFinished"
|
|
|
|
object: base::SysUTF8ToNSString(filePath)];
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:39:31 +00:00
|
|
|
std::string Browser::DockGetBadgeText() {
|
|
|
|
NSDockTile *tile = [[AtomApplication sharedApplication] dockTile];
|
|
|
|
return base::SysNSStringToUTF8([tile badgeLabel]);
|
|
|
|
}
|
|
|
|
|
2014-06-25 03:55:33 +00:00
|
|
|
void Browser::DockHide() {
|
|
|
|
WindowList* list = WindowList::GetInstance();
|
|
|
|
for (WindowList::iterator it = list->begin(); it != list->end(); ++it)
|
|
|
|
[(*it)->GetNativeWindow() setCanHide:NO];
|
|
|
|
|
|
|
|
ProcessSerialNumber psn = { 0, kCurrentProcess };
|
|
|
|
TransformProcessType(&psn, kProcessTransformToUIElementApplication);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Browser::DockShow() {
|
2015-12-17 20:00:04 +00:00
|
|
|
BOOL active = [[NSRunningApplication currentApplication] isActive];
|
2014-06-25 03:55:33 +00:00
|
|
|
ProcessSerialNumber psn = { 0, kCurrentProcess };
|
2015-12-17 20:00:04 +00:00
|
|
|
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) {
|
2015-12-17 20:00:04 +00:00
|
|
|
[app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
|
|
|
|
break;
|
|
|
|
}
|
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(), ^{
|
2015-12-17 20:00:04 +00:00
|
|
|
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];
|
|
|
|
});
|
2015-12-17 20:00:04 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
|
|
|
|
}
|
2014-06-25 03:55:33 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 15:04:31 +00:00
|
|
|
void Browser::DockSetMenu(ui::MenuModel* model) {
|
2014-11-23 21:52:59 +00:00
|
|
|
AtomApplicationDelegate* delegate = (AtomApplicationDelegate*)[NSApp delegate];
|
2014-11-16 15:04:31 +00:00
|
|
|
[delegate setApplicationDockMenu:model];
|
|
|
|
}
|
|
|
|
|
2016-01-23 23:30:14 +00:00
|
|
|
void Browser::DockSetIcon(const gfx::Image& image) {
|
2016-02-01 08:06:09 +00:00
|
|
|
[[AtomApplication sharedApplication]
|
2016-01-23 23:30:14 +00:00
|
|
|
setApplicationIconImage:image.AsNSImage()];
|
|
|
|
}
|
|
|
|
|
2013-05-02 15:43:23 +00:00
|
|
|
} // namespace atom
|