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. 4 5 #include "apps/app_shim/app_shim_host_mac.h" 6 7 #include "apps/app_shim/app_shim_handler_mac.h" 8 #include "apps/app_shim/app_shim_messages.h" 9 #include "base/bind.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" 14 15 AppShimHost::AppShimHost() : initial_launch_finished_(false) {} 16 17 AppShimHost::~AppShimHost() { 18 DCHECK(CalledOnValidThread()); 19 apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); 20 if (handler) 21 handler->OnShimClose(this); 22 } 23 24 void AppShimHost::ServeChannel(const IPC::ChannelHandle& handle) { 25 DCHECK(CalledOnValidThread()); 26 DCHECK(!channel_.get()); 27 channel_.reset(new IPC::ChannelProxy( 28 handle, 29 IPC::Channel::MODE_SERVER, 30 this, 31 content::BrowserThread::GetMessageLoopProxyForThread( 32 content::BrowserThread::IO).get())); 33 } 34 35 base::FilePath AppShimHost::GetProfilePath() const { 36 return profile_path_; 37 } 38 39 std::string AppShimHost::GetAppId() const { 40 return app_id_; 41 } 42 43 bool AppShimHost::OnMessageReceived(const IPC::Message& message) { 44 DCHECK(CalledOnValidThread()); 45 bool handled = true; 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) 52 IPC_END_MESSAGE_MAP() 53 54 return handled; 55 } 56 57 void AppShimHost::OnChannelError() { 58 Close(); 59 } 60 61 bool AppShimHost::Send(IPC::Message* message) { 62 DCHECK(channel_.get()); 63 return channel_->Send(message); 64 } 65 66 void AppShimHost::OnLaunchApp(base::FilePath profile_dir, 67 std::string app_id, 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()) 73 return; 74 75 profile_path_ = profile_dir; 76 app_id_ = app_id; 77 78 apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); 79 if (handler) 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. 83 } 84 85 void AppShimHost::OnFocus(apps::AppShimFocusType focus_type) { 86 DCHECK(CalledOnValidThread()); 87 apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); 88 if (handler) 89 handler->OnShimFocus(this, focus_type); 90 } 91 92 void AppShimHost::OnSetHidden(bool hidden) { 93 DCHECK(CalledOnValidThread()); 94 apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); 95 if (handler) 96 handler->OnShimSetHidden(this, hidden); 97 } 98 99 void AppShimHost::OnQuit() { 100 DCHECK(CalledOnValidThread()); 101 apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); 102 if (handler) 103 handler->OnShimQuit(this); 104 } 105 106 void AppShimHost::OnAppLaunchComplete(apps::AppShimLaunchResult result) { 107 if (!initial_launch_finished_) { 108 Send(new AppShimMsg_LaunchApp_Done(result)); 109 initial_launch_finished_ = true; 110 } 111 } 112 113 void AppShimHost::OnAppClosed() { 114 Close(); 115 } 116 117 void AppShimHost::Close() { 118 DCHECK(CalledOnValidThread()); 119 delete this; 120 } 121