1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_APP_CONTROLLER_MAC_H_
6 #define CHROME_BROWSER_APP_CONTROLLER_MAC_H_
10 #import <Cocoa/Cocoa.h>
15 #include "base/files/file_path.h"
16 #include "base/mac/scoped_nsobject.h"
17 #include "base/time/time.h"
18 #include "components/prefs/pref_change_registrar.h"
20 class AppControllerProfileObserver;
21 @class AppShimMenuController;
22 class BookmarkMenuBridge;
25 class HandoffActiveURLObserverBridge;
26 @class HandoffManager;
27 class HistoryMenuBridge;
29 @class ProfileMenuController;
30 class QuitWithAppsController;
31 class ScopedKeepAlive;
32 @class ShareMenuController;
35 // The application controller object, created by loading the MainMenu nib.
36 // This handles things like responding to menus when there are no windows
37 // open, etc and acts as the NSApplication delegate.
38 @interface AppController : NSObject<NSUserInterfaceValidations,
40 NSApplicationDelegate> {
42 // Manages the state of the command menu items.
43 std::unique_ptr<CommandUpdater> _menuState;
45 // The profile last used by a Browser. It is this profile that was used to
46 // build the user-data specific main menu items.
47 Profile* _lastProfile;
49 // The ProfileObserver observes the ProfileAttrbutesStorage and gets notified
50 // when a profile has been deleted.
51 std::unique_ptr<AppControllerProfileObserver>
52 _profileAttributesStorageObserver;
54 // Management of the bookmark menu which spans across all windows
55 // (and Browser*s). |profileBookmarkMenuBridgeMap_| is a cache that owns one
56 // pointer to a BookmarkMenuBridge for each profile. |bookmarkMenuBridge_| is
57 // a weak pointer that is updated to match the corresponding cache entry
58 // during a profile switch.
59 BookmarkMenuBridge* _bookmarkMenuBridge;
60 std::map<base::FilePath, std::unique_ptr<BookmarkMenuBridge>>
61 _profileBookmarkMenuBridgeMap;
63 std::unique_ptr<HistoryMenuBridge> _historyMenuBridge;
65 // Controller that manages main menu items for packaged apps.
66 base::scoped_nsobject<AppShimMenuController> _appShimMenuController;
68 // The profile menu, which appears right before the Help menu. It is only
69 // available when multiple profiles is enabled.
70 base::scoped_nsobject<ProfileMenuController> _profileMenuController;
72 // Controller for the macOS system share menu.
73 base::scoped_nsobject<ShareMenuController> _shareMenuController;
75 std::unique_ptr<TabMenuBridge> _tabMenuBridge;
77 // If we're told to open URLs (in particular, via |-application:openFiles:| by
78 // Launch Services) before we've launched the browser, we queue them up in
79 // |startupUrls_| so that they can go in the first browser window/tab.
80 std::vector<GURL> _startupUrls;
81 BOOL _startupComplete;
83 // Outlets for the close tab/window menu items so that we can adjust the
84 // commmand-key equivalent depending on the kind of window and how many
86 NSMenuItem* _closeTabMenuItem;
87 NSMenuItem* _closeWindowMenuItem;
89 // If we are expecting a workspace change in response to a reopen
90 // event, the time we got the event. A null time otherwise.
91 base::TimeTicks _reopenTime;
93 std::unique_ptr<PrefChangeRegistrar> _profilePrefRegistrar;
94 PrefChangeRegistrar _localPrefRegistrar;
96 // Displays a notification when quitting while apps are running.
97 scoped_refptr<QuitWithAppsController> _quitWithAppsController;
99 // Responsible for maintaining all state related to the Handoff feature.
100 base::scoped_nsobject<HandoffManager> _handoffManager;
102 // Observes changes to the active URL.
103 std::unique_ptr<HandoffActiveURLObserverBridge>
104 _handoff_active_url_observer_bridge;
106 // This will be true after receiving a NSWorkspaceWillPowerOffNotification.
109 // Request to keep the browser alive during that object's lifetime.
110 std::unique_ptr<ScopedKeepAlive> _keep_alive;
113 @property(readonly, nonatomic) BOOL startupComplete;
114 @property(readonly, nonatomic) Profile* lastProfile;
116 // This method is called very early in application startup after the main menu
118 - (void)mainMenuCreated;
120 - (void)didEndMainMessageLoop;
122 // Try to close all browser windows, and if that succeeds then quit.
123 - (BOOL)tryToTerminateApplication:(NSApplication*)app;
125 // Stop trying to terminate the application. That is, prevent the final browser
126 // window closure from causing the application to quit.
127 - (void)stopTryingToTerminateApplication:(NSApplication*)app;
129 // Run the quit confirmation panel and return whether or not to continue
131 - (BOOL)runConfirmQuitPanel;
133 // Indicate that the system is powering off or logging out.
134 - (void)willPowerOff:(NSNotification*)inNotification;
136 // Returns true if there is a modal window (either window- or application-
137 // modal) blocking the active browser. Note that tab modal dialogs (HTTP auth
138 // sheets) will not count as blocking the browser. But things like open/save
139 // dialogs that are window modal will block the browser.
140 - (BOOL)keyWindowIsModal;
142 // Called when the user picks a menu item when there are no key windows, or when
143 // there is no foreground browser window. Calls through to the browser object to
144 // execute the command. This assumes that the command is supported and doesn't
145 // check, otherwise it should have been disabled in the UI in
146 // |-validateUserInterfaceItem:|.
147 - (void)commandDispatch:(id)sender;
149 // Show the preferences window, or bring it to the front if it's already
151 - (IBAction)showPreferences:(id)sender;
153 // Redirect in the menu item from the expected target of "File's
154 // Owner" (NSApplication) for a Branded About Box
155 - (IBAction)orderFrontStandardAboutPanel:(id)sender;
157 // Toggles the "Confirm to Quit" preference.
158 - (IBAction)toggleConfirmToQuit:(id)sender;
160 // Delegate method to return the dock menu.
161 - (NSMenu*)applicationDockMenu:(NSApplication*)sender;
163 // Get the URLs that Launch Services expects the browser to open at startup.
164 - (const std::vector<GURL>&)startupUrls;
166 - (BookmarkMenuBridge*)bookmarkMenuBridge;
167 - (HistoryMenuBridge*)historyMenuBridge;
168 - (TabMenuBridge*)tabMenuBridge;
170 // Initializes the AppShimMenuController. This enables changing the menu bar for
172 - (void)initAppShimMenuController;
174 // Called when the user has changed browser windows, meaning the backing profile
175 // may have changed. This can cause a rebuild of the user-data menus. This is a
176 // no-op if the new profile is the same as the current one. This will always be
177 // the original profile and never incognito.
178 - (void)windowChangedToProfile:(Profile*)profile;
180 // Certain NSMenuItems [Close Tab and Close Window] have different
181 // keyEquivalents depending on context. This must be invoked in two locations:
182 // * In menuNeedsUpdate:, which is called prior to showing the NSMenu.
183 // * In CommandDispatcher, which independently searches for a matching
185 - (void)updateMenuItemKeyEquivalents;
191 // Functions that may be accessed from non-Objective-C C/C++ code.
193 namespace app_controller_mac {
195 // True if we are currently handling an IDC_NEW_{TAB,WINDOW} command. Used in
196 // SessionService::Observe() to get around windows/linux and mac having
197 // different models of application lifetime.
198 bool IsOpeningNewWindow();
200 // Create a guest profile if one is needed. Afterwards, even if the profile
201 // already existed, notify the AppController of the profile in use.
202 void CreateGuestProfileIfNeeded();
204 // Called when Enterprise startup dialog is close and repost
205 // applicationDidFinished notification.
206 void EnterpriseStartupDialogClosed();
208 } // namespace app_controller_mac
210 #endif // CHROME_BROWSER_APP_CONTROLLER_MAC_H_