browser_mac.mm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/browser.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #import <ServiceManagement/ServiceManagement.h>
  9. #include "base/apple/bridging.h"
  10. #include "base/apple/bundle_locations.h"
  11. #include "base/apple/scoped_cftyperef.h"
  12. #include "base/i18n/rtl.h"
  13. #include "base/mac/mac_util.h"
  14. #include "base/mac/mac_util.mm"
  15. #include "base/strings/sys_string_conversions.h"
  16. #include "chrome/browser/browser_process.h"
  17. #include "net/base/mac/url_conversions.h"
  18. #include "shell/browser/badging/badge_manager.h"
  19. #include "shell/browser/javascript_environment.h"
  20. #include "shell/browser/mac/dict_util.h"
  21. #include "shell/browser/mac/electron_application.h"
  22. #include "shell/browser/mac/electron_application_delegate.h"
  23. #include "shell/browser/native_window.h"
  24. #include "shell/browser/window_list.h"
  25. #include "shell/common/api/electron_api_native_image.h"
  26. #include "shell/common/application_info.h"
  27. #include "shell/common/gin_converters/image_converter.h"
  28. #include "shell/common/gin_helper/arguments.h"
  29. #include "shell/common/gin_helper/dictionary.h"
  30. #include "shell/common/gin_helper/error_thrower.h"
  31. #include "shell/common/gin_helper/promise.h"
  32. #include "shell/common/platform_util.h"
  33. #include "ui/base/resource/resource_scale_factor.h"
  34. #include "ui/gfx/image/image.h"
  35. #include "url/gurl.h"
  36. namespace electron {
  37. namespace {
  38. NSString* GetAppPathForProtocol(const GURL& url) {
  39. NSURL* ns_url = [NSURL
  40. URLWithString:base::SysUTF8ToNSString(url.possibly_invalid_spec())];
  41. base::apple::ScopedCFTypeRef<CFErrorRef> out_err;
  42. base::apple::ScopedCFTypeRef<CFURLRef> openingApp(
  43. LSCopyDefaultApplicationURLForURL(base::apple::NSToCFPtrCast(ns_url),
  44. kLSRolesAll, out_err.InitializeInto()));
  45. if (out_err) {
  46. // likely kLSApplicationNotFoundErr
  47. return nullptr;
  48. }
  49. NSString* app_path = [base::apple::CFToNSPtrCast(openingApp.get()) path];
  50. return app_path;
  51. }
  52. gfx::Image GetApplicationIconForProtocol(NSString* _Nonnull app_path) {
  53. NSImage* image = [[NSWorkspace sharedWorkspace] iconForFile:app_path];
  54. gfx::Image icon(image);
  55. return icon;
  56. }
  57. std::u16string GetAppDisplayNameForProtocol(NSString* app_path) {
  58. NSString* app_display_name =
  59. [[NSFileManager defaultManager] displayNameAtPath:app_path];
  60. return base::SysNSStringToUTF16(app_display_name);
  61. }
  62. #if !IS_MAS_BUILD()
  63. bool CheckLoginItemStatus(bool* is_hidden) {
  64. base::mac::LoginItemsFileList login_items;
  65. if (!login_items.Initialize())
  66. return false;
  67. base::apple::ScopedCFTypeRef<LSSharedFileListItemRef> item(
  68. login_items.GetLoginItemForMainApp());
  69. if (!item.get())
  70. return false;
  71. if (is_hidden)
  72. *is_hidden = base::mac::IsHiddenLoginItem(item.get());
  73. return true;
  74. }
  75. Browser::LoginItemSettings GetLoginItemSettingsDeprecated() {
  76. Browser::LoginItemSettings settings;
  77. settings.open_at_login = CheckLoginItemStatus(&settings.open_as_hidden);
  78. settings.restore_state = base::mac::WasLaunchedAsLoginItemRestoreState();
  79. settings.opened_at_login = base::mac::WasLaunchedAsLoginOrResumeItem();
  80. settings.opened_as_hidden = base::mac::WasLaunchedAsHiddenLoginItem();
  81. return settings;
  82. }
  83. #endif
  84. } // namespace
  85. v8::Local<v8::Promise> Browser::GetApplicationInfoForProtocol(
  86. v8::Isolate* isolate,
  87. const GURL& url) {
  88. gin_helper::Promise<gin_helper::Dictionary> promise(isolate);
  89. v8::Local<v8::Promise> handle = promise.GetHandle();
  90. auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
  91. NSString* ns_app_path = GetAppPathForProtocol(url);
  92. if (!ns_app_path) {
  93. promise.RejectWithErrorMessage(
  94. "Unable to retrieve installation path to app");
  95. return handle;
  96. }
  97. std::u16string app_path = base::SysNSStringToUTF16(ns_app_path);
  98. std::u16string app_display_name = GetAppDisplayNameForProtocol(ns_app_path);
  99. gfx::Image app_icon = GetApplicationIconForProtocol(ns_app_path);
  100. dict.Set("name", app_display_name);
  101. dict.Set("path", app_path);
  102. dict.Set("icon", app_icon);
  103. promise.Resolve(dict);
  104. return handle;
  105. }
  106. void Browser::SetShutdownHandler(base::RepeatingCallback<bool()> handler) {
  107. [[AtomApplication sharedApplication] setShutdownHandler:std::move(handler)];
  108. }
  109. void Browser::Focus(gin::Arguments* args) {
  110. gin_helper::Dictionary opts;
  111. bool steal_focus = false;
  112. if (args->GetNext(&opts)) {
  113. gin_helper::ErrorThrower thrower(args->isolate());
  114. if (!opts.Get("steal", &steal_focus)) {
  115. thrower.ThrowError(
  116. "Expected options object to contain a 'steal' boolean property");
  117. return;
  118. }
  119. }
  120. [[AtomApplication sharedApplication] activateIgnoringOtherApps:steal_focus];
  121. }
  122. void Browser::Hide() {
  123. [[AtomApplication sharedApplication] hide:nil];
  124. }
  125. bool Browser::IsHidden() {
  126. return [[AtomApplication sharedApplication] isHidden];
  127. }
  128. void Browser::Show() {
  129. [[AtomApplication sharedApplication] unhide:nil];
  130. }
  131. void Browser::AddRecentDocument(const base::FilePath& path) {
  132. NSString* path_string = base::apple::FilePathToNSString(path);
  133. if (!path_string)
  134. return;
  135. NSURL* u = [NSURL fileURLWithPath:path_string];
  136. if (!u)
  137. return;
  138. [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:u];
  139. }
  140. void Browser::ClearRecentDocuments() {
  141. [[NSDocumentController sharedDocumentController] clearRecentDocuments:nil];
  142. }
  143. bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol,
  144. gin::Arguments* args) {
  145. NSString* identifier = [base::apple::MainBundle() bundleIdentifier];
  146. if (!identifier)
  147. return false;
  148. if (!Browser::IsDefaultProtocolClient(protocol, args))
  149. return false;
  150. NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
  151. CFStringRef protocol_cf = base::apple::NSToCFPtrCast(protocol_ns);
  152. // TODO(codebytere): Use -[NSWorkspace URLForApplicationToOpenURL:] instead
  153. #pragma clang diagnostic push
  154. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  155. CFArrayRef bundleList = LSCopyAllHandlersForURLScheme(protocol_cf);
  156. #pragma clang diagnostic pop
  157. if (!bundleList) {
  158. return false;
  159. }
  160. // On macOS, we can't query the default, but the handlers list seems to put
  161. // Apple's defaults first, so we'll use the first option that isn't our bundle
  162. CFStringRef other = nil;
  163. for (CFIndex i = 0; i < CFArrayGetCount(bundleList); ++i) {
  164. other =
  165. base::apple::CFCast<CFStringRef>(CFArrayGetValueAtIndex(bundleList, i));
  166. if (![identifier isEqualToString:(__bridge NSString*)other]) {
  167. break;
  168. }
  169. }
  170. // No other app was found set it to none instead of setting it back to itself.
  171. if ([identifier isEqualToString:(__bridge NSString*)other]) {
  172. other = base::apple::NSToCFPtrCast(@"None");
  173. }
  174. OSStatus return_code = LSSetDefaultHandlerForURLScheme(protocol_cf, other);
  175. return return_code == noErr;
  176. }
  177. bool Browser::SetAsDefaultProtocolClient(const std::string& protocol,
  178. gin::Arguments* args) {
  179. if (protocol.empty())
  180. return false;
  181. NSString* identifier = [base::apple::MainBundle() bundleIdentifier];
  182. if (!identifier)
  183. return false;
  184. NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
  185. OSStatus return_code =
  186. LSSetDefaultHandlerForURLScheme(base::apple::NSToCFPtrCast(protocol_ns),
  187. base::apple::NSToCFPtrCast(identifier));
  188. return return_code == noErr;
  189. }
  190. bool Browser::IsDefaultProtocolClient(const std::string& protocol,
  191. gin::Arguments* args) {
  192. if (protocol.empty())
  193. return false;
  194. NSString* identifier = [base::apple::MainBundle() bundleIdentifier];
  195. if (!identifier)
  196. return false;
  197. NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
  198. // TODO(codebytere): Use -[NSWorkspace URLForApplicationToOpenURL:] instead
  199. #pragma clang diagnostic push
  200. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  201. base::apple::ScopedCFTypeRef<CFStringRef> bundleId(
  202. LSCopyDefaultHandlerForURLScheme(
  203. base::apple::NSToCFPtrCast(protocol_ns)));
  204. #pragma clang diagnostic pop
  205. if (!bundleId)
  206. return false;
  207. // Ensure the comparison is case-insensitive
  208. // as LS does not persist the case of the bundle id.
  209. NSComparisonResult result = [base::apple::CFToNSPtrCast(bundleId.get())
  210. caseInsensitiveCompare:identifier];
  211. return result == NSOrderedSame;
  212. }
  213. std::u16string Browser::GetApplicationNameForProtocol(const GURL& url) {
  214. NSString* app_path = GetAppPathForProtocol(url);
  215. if (!app_path) {
  216. return std::u16string();
  217. }
  218. std::u16string app_display_name = GetAppDisplayNameForProtocol(app_path);
  219. return app_display_name;
  220. }
  221. bool Browser::SetBadgeCount(std::optional<int> count) {
  222. DockSetBadgeText(!count.has_value() || count.value() != 0
  223. ? badging::BadgeManager::GetBadgeString(count)
  224. : "");
  225. if (count.has_value()) {
  226. badge_count_ = count.value();
  227. } else {
  228. badge_count_ = 0;
  229. }
  230. return true;
  231. }
  232. void Browser::SetUserActivity(const std::string& type,
  233. base::Value::Dict user_info,
  234. gin::Arguments* args) {
  235. std::string url_string;
  236. args->GetNext(&url_string);
  237. [[AtomApplication sharedApplication]
  238. setCurrentActivity:base::SysUTF8ToNSString(type)
  239. withUserInfo:DictionaryValueToNSDictionary(std::move(user_info))
  240. withWebpageURL:net::NSURLWithGURL(GURL(url_string))];
  241. }
  242. std::string Browser::GetCurrentActivityType() {
  243. NSUserActivity* userActivity =
  244. [[AtomApplication sharedApplication] getCurrentActivity];
  245. return base::SysNSStringToUTF8(userActivity.activityType);
  246. }
  247. void Browser::InvalidateCurrentActivity() {
  248. [[AtomApplication sharedApplication] invalidateCurrentActivity];
  249. }
  250. void Browser::ResignCurrentActivity() {
  251. [[AtomApplication sharedApplication] resignCurrentActivity];
  252. }
  253. void Browser::UpdateCurrentActivity(const std::string& type,
  254. base::Value::Dict user_info) {
  255. [[AtomApplication sharedApplication]
  256. updateCurrentActivity:base::SysUTF8ToNSString(type)
  257. withUserInfo:DictionaryValueToNSDictionary(
  258. std::move(user_info))];
  259. }
  260. bool Browser::WillContinueUserActivity(const std::string& type) {
  261. bool prevent_default = false;
  262. for (BrowserObserver& observer : observers_)
  263. observer.OnWillContinueUserActivity(&prevent_default, type);
  264. return prevent_default;
  265. }
  266. void Browser::DidFailToContinueUserActivity(const std::string& type,
  267. const std::string& error) {
  268. for (BrowserObserver& observer : observers_)
  269. observer.OnDidFailToContinueUserActivity(type, error);
  270. }
  271. bool Browser::ContinueUserActivity(const std::string& type,
  272. base::Value::Dict user_info,
  273. base::Value::Dict details) {
  274. bool prevent_default = false;
  275. for (BrowserObserver& observer : observers_)
  276. observer.OnContinueUserActivity(&prevent_default, type, user_info.Clone(),
  277. details.Clone());
  278. return prevent_default;
  279. }
  280. void Browser::UserActivityWasContinued(const std::string& type,
  281. base::Value::Dict user_info) {
  282. for (BrowserObserver& observer : observers_)
  283. observer.OnUserActivityWasContinued(type, user_info.Clone());
  284. }
  285. bool Browser::UpdateUserActivityState(const std::string& type,
  286. base::Value::Dict user_info) {
  287. bool prevent_default = false;
  288. for (BrowserObserver& observer : observers_)
  289. observer.OnUpdateUserActivityState(&prevent_default, type,
  290. user_info.Clone());
  291. return prevent_default;
  292. }
  293. // Modified from chrome/browser/ui/cocoa/l10n_util.mm.
  294. void Browser::ApplyForcedRTL() {
  295. NSUserDefaults* defaults = NSUserDefaults.standardUserDefaults;
  296. auto dir = base::i18n::GetForcedTextDirection();
  297. // An Electron app should respect RTL behavior of application locale over
  298. // system locale.
  299. auto should_be_rtl = dir == base::i18n::RIGHT_TO_LEFT || IsAppRTL();
  300. auto should_be_ltr = dir == base::i18n::LEFT_TO_RIGHT || !IsAppRTL();
  301. // -registerDefaults: won't do the trick here because these defaults exist
  302. // (in the global domain) to reflect the system locale. They need to be set
  303. // in Chrome's domain to supersede the system value.
  304. if (should_be_rtl) {
  305. [defaults setBool:YES forKey:@"AppleTextDirection"];
  306. [defaults setBool:YES forKey:@"NSForceRightToLeftWritingDirection"];
  307. } else if (should_be_ltr) {
  308. [defaults setBool:YES forKey:@"AppleTextDirection"];
  309. [defaults setBool:NO forKey:@"NSForceRightToLeftWritingDirection"];
  310. } else {
  311. [defaults removeObjectForKey:@"AppleTextDirection"];
  312. [defaults removeObjectForKey:@"NSForceRightToLeftWritingDirection"];
  313. }
  314. }
  315. Browser::LoginItemSettings Browser::GetLoginItemSettings(
  316. const LoginItemSettings& options) {
  317. LoginItemSettings settings;
  318. if (options.type != "mainAppService" && options.service_name.empty()) {
  319. gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate())
  320. .ThrowTypeError("'name' is required when type is not mainAppService");
  321. return settings;
  322. }
  323. #if IS_MAS_BUILD()
  324. const std::string status =
  325. platform_util::GetLoginItemEnabled(options.type, options.service_name);
  326. settings.open_at_login =
  327. status == "enabled" || status == "enabled-deprecated";
  328. if (@available(macOS 13, *))
  329. settings.status = status;
  330. #else
  331. // If the app was previously set as a LoginItem with the deprecated API,
  332. // we should report its LoginItemSettings via the old API.
  333. LoginItemSettings settings_deprecated = GetLoginItemSettingsDeprecated();
  334. if (@available(macOS 13, *)) {
  335. const std::string status =
  336. platform_util::GetLoginItemEnabled(options.type, options.service_name);
  337. if (status == "enabled-deprecated") {
  338. settings = settings_deprecated;
  339. } else {
  340. settings.open_at_login = status == "enabled";
  341. settings.status = status;
  342. }
  343. } else {
  344. settings = settings_deprecated;
  345. }
  346. #endif
  347. return settings;
  348. }
  349. void Browser::SetLoginItemSettings(LoginItemSettings settings) {
  350. if (settings.type != "mainAppService" && settings.service_name.empty()) {
  351. gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate())
  352. .ThrowTypeError("'name' is required when type is not mainAppService");
  353. return;
  354. }
  355. #if IS_MAS_BUILD()
  356. platform_util::SetLoginItemEnabled(settings.type, settings.service_name,
  357. settings.open_at_login);
  358. #else
  359. const base::FilePath bundle_path = base::apple::MainBundlePath();
  360. if (@available(macOS 13, *)) {
  361. // If the app was previously set as a LoginItem with the old API, remove it
  362. // as a LoginItem via the old API before re-enabling with the new API.
  363. const std::string status =
  364. platform_util::GetLoginItemEnabled("mainAppService", "");
  365. if (status == "enabled-deprecated") {
  366. base::mac::RemoveFromLoginItems(bundle_path);
  367. if (settings.open_at_login) {
  368. platform_util::SetLoginItemEnabled(settings.type, settings.service_name,
  369. settings.open_at_login);
  370. }
  371. } else {
  372. platform_util::SetLoginItemEnabled(settings.type, settings.service_name,
  373. settings.open_at_login);
  374. }
  375. } else {
  376. if (settings.open_at_login) {
  377. base::mac::AddToLoginItems(bundle_path, settings.open_as_hidden);
  378. } else {
  379. base::mac::RemoveFromLoginItems(bundle_path);
  380. }
  381. }
  382. #endif
  383. }
  384. std::string Browser::GetExecutableFileVersion() const {
  385. return GetApplicationVersion();
  386. }
  387. std::string Browser::GetExecutableFileProductName() const {
  388. return GetApplicationName();
  389. }
  390. int Browser::DockBounce(BounceType type) {
  391. return [[AtomApplication sharedApplication]
  392. requestUserAttention:static_cast<NSRequestUserAttentionType>(type)];
  393. }
  394. void Browser::DockCancelBounce(int request_id) {
  395. [[AtomApplication sharedApplication] cancelUserAttentionRequest:request_id];
  396. }
  397. void Browser::DockSetBadgeText(const std::string& label) {
  398. NSDockTile* tile = [[AtomApplication sharedApplication] dockTile];
  399. [tile setBadgeLabel:base::SysUTF8ToNSString(label)];
  400. }
  401. void Browser::DockDownloadFinished(const std::string& filePath) {
  402. [[NSDistributedNotificationCenter defaultCenter]
  403. postNotificationName:@"com.apple.DownloadFileFinished"
  404. object:base::SysUTF8ToNSString(filePath)];
  405. }
  406. std::string Browser::DockGetBadgeText() {
  407. NSDockTile* tile = [[AtomApplication sharedApplication] dockTile];
  408. return base::SysNSStringToUTF8([tile badgeLabel]);
  409. }
  410. void Browser::DockHide() {
  411. // Transforming application state from UIElement to Foreground is an
  412. // asynchronous operation, and unfortunately there is currently no way to know
  413. // when it is finished.
  414. // So if we call DockHide => DockShow => DockHide => DockShow in a very short
  415. // time, we would trigger a bug of macOS that, there would be multiple dock
  416. // icons of the app left in system.
  417. // To work around this, we make sure DockHide does nothing if it is called
  418. // immediately after DockShow. After some experiments, 1 second seems to be
  419. // a proper interval.
  420. if (!last_dock_show_.is_null() &&
  421. base::Time::Now() - last_dock_show_ < base::Seconds(1)) {
  422. return;
  423. }
  424. for (auto* const& window : WindowList::GetWindows())
  425. [window->GetNativeWindow().GetNativeNSWindow() setCanHide:NO];
  426. ProcessSerialNumber psn = {0, kCurrentProcess};
  427. TransformProcessType(&psn, kProcessTransformToUIElementApplication);
  428. }
  429. bool Browser::DockIsVisible() {
  430. // Because DockShow has a slight delay this may not be true immediately
  431. // after that call.
  432. return ([[NSRunningApplication currentApplication] activationPolicy] ==
  433. NSApplicationActivationPolicyRegular);
  434. }
  435. v8::Local<v8::Promise> Browser::DockShow(v8::Isolate* isolate) {
  436. last_dock_show_ = base::Time::Now();
  437. gin_helper::Promise<void> promise(isolate);
  438. v8::Local<v8::Promise> handle = promise.GetHandle();
  439. BOOL active = [[NSRunningApplication currentApplication] isActive];
  440. ProcessSerialNumber psn = {0, kCurrentProcess};
  441. if (active) {
  442. // Workaround buggy behavior of TransformProcessType.
  443. // http://stackoverflow.com/questions/7596643/
  444. NSArray* runningApps = [NSRunningApplication
  445. runningApplicationsWithBundleIdentifier:@"com.apple.dock"];
  446. for (NSRunningApplication* app in runningApps) {
  447. [app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
  448. break;
  449. }
  450. __block gin_helper::Promise<void> p = std::move(promise);
  451. dispatch_time_t one_ms = dispatch_time(DISPATCH_TIME_NOW, USEC_PER_SEC);
  452. dispatch_after(one_ms, dispatch_get_main_queue(), ^{
  453. TransformProcessType(&psn, kProcessTransformToForegroundApplication);
  454. dispatch_time_t one_ms_2 = dispatch_time(DISPATCH_TIME_NOW, USEC_PER_SEC);
  455. dispatch_after(one_ms_2, dispatch_get_main_queue(), ^{
  456. [[NSRunningApplication currentApplication]
  457. activateWithOptions:NSApplicationActivateIgnoringOtherApps];
  458. p.Resolve();
  459. });
  460. });
  461. } else {
  462. TransformProcessType(&psn, kProcessTransformToForegroundApplication);
  463. promise.Resolve();
  464. }
  465. return handle;
  466. }
  467. void Browser::DockSetMenu(ElectronMenuModel* model) {
  468. ElectronApplicationDelegate* delegate =
  469. (ElectronApplicationDelegate*)[NSApp delegate];
  470. [delegate setApplicationDockMenu:model];
  471. }
  472. void Browser::DockSetIcon(v8::Isolate* isolate, v8::Local<v8::Value> icon) {
  473. gfx::Image image;
  474. if (!icon->IsNull()) {
  475. api::NativeImage* native_image = nullptr;
  476. if (!api::NativeImage::TryConvertNativeImage(isolate, icon, &native_image))
  477. return;
  478. image = native_image->image();
  479. }
  480. // This is needed to avoid a hard CHECK when this fn is called
  481. // before the browser process is ready, since supported scales
  482. // are normally set by ui::ResourceBundle::InitSharedInstance
  483. // during browser process startup.
  484. if (!is_ready())
  485. ui::SetSupportedResourceScaleFactors({ui::k100Percent});
  486. [[AtomApplication sharedApplication]
  487. setApplicationIconImage:image.AsNSImage()];
  488. }
  489. void Browser::ShowAboutPanel() {
  490. NSDictionary* options = DictionaryValueToNSDictionary(about_panel_options_);
  491. // Credits must be a NSAttributedString instead of NSString
  492. NSString* credits = (NSString*)options[@"Credits"];
  493. if (credits != nil) {
  494. NSMutableDictionary* mutable_options = [options mutableCopy];
  495. NSAttributedString* creditString = [[NSAttributedString alloc]
  496. initWithString:credits
  497. attributes:@{NSForegroundColorAttributeName : [NSColor textColor]}];
  498. [mutable_options setValue:creditString forKey:@"Credits"];
  499. options = [NSDictionary dictionaryWithDictionary:mutable_options];
  500. }
  501. [[AtomApplication sharedApplication]
  502. orderFrontStandardAboutPanelWithOptions:options];
  503. }
  504. void Browser::SetAboutPanelOptions(base::Value::Dict options) {
  505. about_panel_options_.clear();
  506. for (const auto pair : options) {
  507. std::string key = pair.first;
  508. if (!key.empty() && pair.second.is_string()) {
  509. key[0] = base::ToUpperASCII(key[0]);
  510. about_panel_options_.Set(key, pair.second.Clone());
  511. }
  512. }
  513. }
  514. void Browser::ShowEmojiPanel() {
  515. [[AtomApplication sharedApplication] orderFrontCharacterPalette:nil];
  516. }
  517. bool Browser::IsEmojiPanelSupported() {
  518. return true;
  519. }
  520. bool Browser::IsSecureKeyboardEntryEnabled() {
  521. return password_input_enabler_.get() != nullptr;
  522. }
  523. void Browser::SetSecureKeyboardEntryEnabled(bool enabled) {
  524. if (enabled) {
  525. password_input_enabler_ =
  526. std::make_unique<ui::ScopedPasswordInputEnabler>();
  527. } else {
  528. password_input_enabler_.reset();
  529. }
  530. }
  531. } // namespace electron