1 // Copyright 2013 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 #include "apps/app_shim/app_shim_host_mac.h"
7 #include "apps/app_shim/app_shim_handler_mac.h"
8 #include "apps/app_shim/app_shim_messages.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "ipc/ipc_channel_proxy.h"
15 AppShimHost::AppShimHost() : initial_launch_finished_(false) {}
17 AppShimHost::~AppShimHost() {
18 DCHECK(CalledOnValidThread());
19 apps::AppShimHandler
* handler
= apps::AppShimHandler::GetForAppMode(app_id_
);
21 handler
->OnShimClose(this);
24 void AppShimHost::ServeChannel(const IPC::ChannelHandle
& handle
) {
25 DCHECK(CalledOnValidThread());
26 DCHECK(!channel_
.get());
27 channel_
.reset(new IPC::ChannelProxy(
29 IPC::Channel::MODE_SERVER
,
31 content::BrowserThread::GetMessageLoopProxyForThread(
32 content::BrowserThread::IO
).get()));
35 base::FilePath
AppShimHost::GetProfilePath() const {
39 std::string
AppShimHost::GetAppId() const {
43 bool AppShimHost::OnMessageReceived(const IPC::Message
& message
) {
44 DCHECK(CalledOnValidThread());
46 IPC_BEGIN_MESSAGE_MAP(AppShimHost
, message
)
47 IPC_MESSAGE_HANDLER(AppShimHostMsg_LaunchApp
, OnLaunchApp
)
48 IPC_MESSAGE_HANDLER(AppShimHostMsg_FocusApp
, OnFocus
)
49 IPC_MESSAGE_HANDLER(AppShimHostMsg_SetAppHidden
, OnSetHidden
)
50 IPC_MESSAGE_HANDLER(AppShimHostMsg_QuitApp
, OnQuit
)
51 IPC_MESSAGE_UNHANDLED(handled
= false)
57 void AppShimHost::OnChannelError() {
61 bool AppShimHost::Send(IPC::Message
* message
) {
62 DCHECK(channel_
.get());
63 return channel_
->Send(message
);
66 void AppShimHost::OnLaunchApp(base::FilePath profile_dir
,
68 apps::AppShimLaunchType launch_type
) {
69 DCHECK(CalledOnValidThread());
70 DCHECK(profile_path_
.empty());
71 // Only one app launch message per channel.
72 if (!profile_path_
.empty())
75 profile_path_
= profile_dir
;
78 apps::AppShimHandler
* handler
= apps::AppShimHandler::GetForAppMode(app_id_
);
80 handler
->OnShimLaunch(this, launch_type
);
81 // |handler| can only be NULL after AppShimHostManager is destroyed. Since
82 // this only happens at shutdown, do nothing here.
85 void AppShimHost::OnFocus(apps::AppShimFocusType focus_type
) {
86 DCHECK(CalledOnValidThread());
87 apps::AppShimHandler
* handler
= apps::AppShimHandler::GetForAppMode(app_id_
);
89 handler
->OnShimFocus(this, focus_type
);
92 void AppShimHost::OnSetHidden(bool hidden
) {
93 DCHECK(CalledOnValidThread());
94 apps::AppShimHandler
* handler
= apps::AppShimHandler::GetForAppMode(app_id_
);
96 handler
->OnShimSetHidden(this, hidden
);
99 void AppShimHost::OnQuit() {
100 DCHECK(CalledOnValidThread());
101 apps::AppShimHandler
* handler
= apps::AppShimHandler::GetForAppMode(app_id_
);
103 handler
->OnShimQuit(this);
106 void AppShimHost::OnAppLaunchComplete(apps::AppShimLaunchResult result
) {
107 if (!initial_launch_finished_
) {
108 Send(new AppShimMsg_LaunchApp_Done(result
));
109 initial_launch_finished_
= true;
113 void AppShimHost::OnAppClosed() {
117 void AppShimHost::Close() {
118 DCHECK(CalledOnValidThread());