1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_APP_SHIM_APP_SHIM_CONTROLLER_H_
#define CHROME_APP_SHIM_APP_SHIM_CONTROLLER_H_
#include <vector>
#import <AppKit/AppKit.h>
#include "base/files/file_path.h"
#include "chrome/common/mac/app_shim.mojom.h"
#include "chrome/services/mac_notifications/public/mojom/mac_notifications.mojom.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/platform/named_platform_channel.h"
#include "mojo/public/cpp/system/isolated_connection.h"
#include "url/gurl.h"
namespace apps {
class MachBootstrapAcceptorTest;
}
namespace display {
class ScopedNativeScreen;
}
@class AppShimDelegate;
@class ProfileMenuTarget;
@class ApplicationDockMenuTarget;
@protocol RenderWidgetHostViewMacDelegate;
// The AppShimController is responsible for launching and maintaining the
// connection with the main Chrome process, and generally controls the lifetime
// of the app shim process.
class AppShimController
: public chrome::mojom::AppShim,
public mac_notifications::mojom::MacNotificationProvider {
public:
struct Params {
Params();
Params(const Params& other);
~Params();
// The full path of the user data dir.
base::FilePath user_data_dir;
// The relative path of the profile.
base::FilePath profile_dir;
std::string app_id;
std::u16string app_name;
GURL app_url;
};
explicit AppShimController(const Params& params);
AppShimController(const AppShimController&) = delete;
AppShimController& operator=(const AppShimController&) = delete;
~AppShimController() override;
chrome::mojom::AppShimHost* host() const { return host_.get(); }
// Called by AppShimDelegate in response to receiving the notification
// -[NSApplicationDelegate applicationDidFinishLaunching:]. This kicks off
// the initialization process (connecting to Chrome, etc).
void OnAppFinishedLaunching();
// Called by AppShimDelegate in response a file being opened. If this occurs
// before OnDidFinishLaunching, then the argument is the files that triggered
// the launch of the app.
void OpenFiles(const std::vector<base::FilePath>& files);
// Called when a profile is selected from the profiles NSMenu.
void ProfileMenuItemSelected(uint32_t index);
// Called when a item is selected from the application dock menu.
void CommandFromDock(uint32_t index);
// Called when an item is selected from the application menu while no windows
// are shown.
void CommandDispatch(int command_id);
// Called by AppShimDelegate in response to an URL being opened. If this
// occurs before OnDidFinishLaunching, then the argument is the files that
// triggered the launch of the app.
void OpenUrls(const std::vector<GURL>& urls);
NSMenu* GetApplicationDockMenu();
// Called when the app is about to terminate.
void ApplicationWillTerminate();
private:
friend class TestShimClient;
friend class apps::MachBootstrapAcceptorTest;
// The state of initialization.
enum class InitState {
// Waiting for OnAppFinishedLaunching to be called.
kWaitingForAppToFinishLaunch,
// Waiting for PollForChromeReady to connect to the browser process.
kWaitingForChromeReady,
// Has sent OnShimConnected to the browser process, waiting for the
// response.
kHasSentOnShimConnected,
// Has received the OnShimConnected response from the browser,
// initialization is now complete.
kHasReceivedOnShimConnectedResponse,
};
// Init step 1 after OnAppFinishedLaunching. Find a running instance of Chrome
// to connect to, or launch Chrome if none is found. Returns true if a
// running instance was found and polling for readiness is possible.
bool FindOrLaunchChrome();
// Init step 2: Poll for the mach server exposed by Chrome's AppShimListener
// to be initialized. Once it has, proceed to SendBootstrapOnShimConnected.
// If |time_until_timeout| runs out, quit the app shim.
void PollForChromeReady(const base::TimeDelta& time_until_timeout);
// Init step 3: Reinterpret |endpoint| as a mojom::AppShimHostBootstrap, and
// send the OnShimConnected message.
void SendBootstrapOnShimConnected(mojo::PlatformChannelEndpoint endpoint);
// Init step 4 (the last): The browser response to OnShimConnected. On
// success, this will initialize |shim_receiver_|, through which |this| will
// be controlled by the browser. On failure, the app will quit.
void OnShimConnectedResponse(
chrome::mojom::AppShimLaunchResult result,
mojo::PendingReceiver<chrome::mojom::AppShim> app_shim_receiver);
// Builds main menu bar items.
void SetUpMenu();
void ChannelError(uint32_t custom_reason, const std::string& description);
void BootstrapChannelError(uint32_t custom_reason,
const std::string& description);
// chrome::mojom::AppShim implementation.
void CreateRemoteCocoaApplication(
mojo::PendingAssociatedReceiver<remote_cocoa::mojom::Application>
receiver) override;
void CreateCommandDispatcherForWidget(uint64_t widget_id) override;
void SetBadgeLabel(const std::string& badge_label) override;
void SetUserAttention(
chrome::mojom::AppShimAttentionType attention_type) override;
void UpdateProfileMenu(std::vector<chrome::mojom::ProfileMenuItemPtr>
profile_menu_items) override;
void UpdateApplicationDockMenu(
std::vector<chrome::mojom::ApplicationDockMenuItemPtr> dock_menu_items)
override;
void BindNotificationProvider(
mojo::PendingReceiver<mac_notifications::mojom::MacNotificationProvider>
provider) override;
// mac_notifications::mojom::MacNotificationProvider implementation.
void BindNotificationService(
mojo::PendingReceiver<mac_notifications::mojom::MacNotificationService>
service,
mojo::PendingRemote<
mac_notifications::mojom::MacNotificationActionHandler> handler)
override;
// Helper function to set up a connection to the AppShimListener at the given
// Mach endpoint name.
static mojo::PlatformChannelEndpoint ConnectToBrowser(
const mojo::NamedPlatformChannel::ServerName& server_name);
// Helper function to search for the Chrome instance holding
// chrome::kSingletonLockFilename in the specified |user_data_dir|.
static NSRunningApplication* FindChromeFromSingletonLock(
const base::FilePath& user_data_dir);
static void CreateRenderWidgetHostNSView(
uint64_t view_id,
mojo::ScopedInterfaceEndpointHandle host_handle,
mojo::ScopedInterfaceEndpointHandle view_request_handle);
static NSObject<RenderWidgetHostViewMacDelegate>* GetDelegateForHost(
uint64_t view_id);
const Params params_;
// Populated by OpenFiles if it was called before OnAppFinishedLaunching
// was called.
std::vector<base::FilePath> launch_files_;
// Populated by OpenUrls if it was called before nAppFinishedLaunching
// was called.
std::vector<GURL> launch_urls_;
// This is the Chrome process that this app is committed to connecting to.
// The app will quit if this process is terminated before the mojo connection
// is established.
// This process is determined by either:
// - The pid specified to the app's command line (if it exists).
// - The pid specified in the chrome::kSingletonLockFilename file.
NSRunningApplication* __strong chrome_to_connect_to_;
// The Chrome process that was launched by this app in FindOrLaunchChrome.
// Note that the app is not compelled to connect to this process (consider the
// case where multiple apps launch at the same time, and all launch their own
// Chrome -- only one will grab the chrome::kSingletonLockFilename, and all
// apps should connect to that).
NSRunningApplication* __strong chrome_launched_by_app_;
mojo::IsolatedConnection bootstrap_mojo_connection_;
mojo::Remote<chrome::mojom::AppShimHostBootstrap> host_bootstrap_;
mojo::Receiver<chrome::mojom::AppShim> shim_receiver_{this};
mojo::Remote<chrome::mojom::AppShimHost> host_;
mojo::PendingReceiver<chrome::mojom::AppShimHost> host_receiver_;
mojo::Receiver<mac_notifications::mojom::MacNotificationProvider>
notifications_receiver_{this};
AppShimDelegate* __strong delegate_;
InitState init_state_ = InitState::kWaitingForAppToFinishLaunch;
// The target for NSMenuItems in the profile menu.
ProfileMenuTarget* __strong profile_menu_target_;
// The target for NSMenuItems in the application dock menu.
ApplicationDockMenuTarget* __strong application_dock_menu_target_;
// The screen object used in the app sim.
std::unique_ptr<display::ScopedNativeScreen> screen_;
// The items in the profile menu.
std::vector<chrome::mojom::ProfileMenuItemPtr> profile_menu_items_;
// The items in the application dock menu.
std::vector<chrome::mojom::ApplicationDockMenuItemPtr> dock_menu_items_;
// MacNotificationService implementation used by Chrome to display
// notifications in this app shim process.
std::unique_ptr<mac_notifications::mojom::MacNotificationService>
notification_service_;
NSInteger attention_request_id_ = 0;
};
#endif // CHROME_APP_SHIM_APP_SHIM_CONTROLLER_H_
|