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_
= IPC::ChannelProxy::Create(
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(const base::FilePath
& profile_dir
,
67 const std::string
& app_id
,
68 apps::AppShimLaunchType launch_type
,
69 const std::vector
<base::FilePath
>& files
) {
70 DCHECK(CalledOnValidThread());
71 DCHECK(profile_path_
.empty());
72 // Only one app launch message per channel.
73 if (!profile_path_
.empty())
76 profile_path_
= profile_dir
;
79 apps::AppShimHandler
* handler
= apps::AppShimHandler::GetForAppMode(app_id_
);
81 handler
->OnShimLaunch(this, launch_type
, files
);
82 // |handler| can only be NULL after AppShimHostManager is destroyed. Since
83 // this only happens at shutdown, do nothing here.
86 void AppShimHost::OnFocus(apps::AppShimFocusType focus_type
,
87 const std::vector
<base::FilePath
>& files
) {
88 DCHECK(CalledOnValidThread());
89 apps::AppShimHandler
* handler
= apps::AppShimHandler::GetForAppMode(app_id_
);
91 handler
->OnShimFocus(this, focus_type
, files
);
94 void AppShimHost::OnSetHidden(bool hidden
) {
95 DCHECK(CalledOnValidThread());
96 apps::AppShimHandler
* handler
= apps::AppShimHandler::GetForAppMode(app_id_
);
98 handler
->OnShimSetHidden(this, hidden
);
101 void AppShimHost::OnQuit() {
102 DCHECK(CalledOnValidThread());
103 apps::AppShimHandler
* handler
= apps::AppShimHandler::GetForAppMode(app_id_
);
105 handler
->OnShimQuit(this);
108 void AppShimHost::OnAppLaunchComplete(apps::AppShimLaunchResult result
) {
109 if (!initial_launch_finished_
) {
110 Send(new AppShimMsg_LaunchApp_Done(result
));
111 initial_launch_finished_
= true;
115 void AppShimHost::OnAppClosed() {
119 void AppShimHost::OnAppHide() {
120 Send(new AppShimMsg_Hide
);
123 void AppShimHost::OnAppRequestUserAttention(apps::AppShimAttentionType type
) {
124 Send(new AppShimMsg_SetUserAttention(type
));
127 void AppShimHost::Close() {
128 DCHECK(CalledOnValidThread());