12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file. 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "apps/app_shim/app_shim_host_mac.h" 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 7a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "apps/app_shim/app_shim_handler_mac.h" 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "apps/app_shim/app_shim_messages.h" 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h" 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h" 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h" 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "content/public/browser/browser_thread.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ipc/ipc_channel_proxy.h" 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 157dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochAppShimHost::AppShimHost() : initial_launch_finished_(false) {} 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)AppShimHost::~AppShimHost() { 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(CalledOnValidThread()); 19a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); 20a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) if (handler) 21a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) handler->OnShimClose(this); 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AppShimHost::ServeChannel(const IPC::ChannelHandle& handle) { 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(CalledOnValidThread()); 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!channel_.get()); 277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) channel_.reset(new IPC::ChannelProxy( 287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) handle, 297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) IPC::Channel::MODE_SERVER, 307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) this, 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) content::BrowserThread::GetMessageLoopProxyForThread( 327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) content::BrowserThread::IO).get())); 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbase::FilePath AppShimHost::GetProfilePath() const { 36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return profile_path_; 3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)std::string AppShimHost::GetAppId() const { 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return app_id_; 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AppShimHost::OnMessageReceived(const IPC::Message& message) { 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(CalledOnValidThread()); 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool handled = true; 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IPC_BEGIN_MESSAGE_MAP(AppShimHost, message) 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IPC_MESSAGE_HANDLER(AppShimHostMsg_LaunchApp, OnLaunchApp) 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IPC_MESSAGE_HANDLER(AppShimHostMsg_FocusApp, OnFocus) 49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch IPC_MESSAGE_HANDLER(AppShimHostMsg_SetAppHidden, OnSetHidden) 50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) IPC_MESSAGE_HANDLER(AppShimHostMsg_QuitApp, OnQuit) 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IPC_MESSAGE_UNHANDLED(handled = false) 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IPC_END_MESSAGE_MAP() 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return handled; 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 57a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AppShimHost::OnChannelError() { 58a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) Close(); 59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)} 60a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool AppShimHost::Send(IPC::Message* message) { 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(channel_.get()); 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return channel_->Send(message); 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void AppShimHost::OnLaunchApp(base::FilePath profile_dir, 67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) std::string app_id, 68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) apps::AppShimLaunchType launch_type) { 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(CalledOnValidThread()); 70eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch DCHECK(profile_path_.empty()); 717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // Only one app launch message per channel. 727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!profile_path_.empty()) 7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return; 7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 75eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch profile_path_ = profile_dir; 76a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) app_id_ = app_id; 77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 78a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); 797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (handler) 807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch handler->OnShimLaunch(this, launch_type); 817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // |handler| can only be NULL after AppShimHostManager is destroyed. Since 827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // this only happens at shutdown, do nothing here. 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 85eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AppShimHost::OnFocus(apps::AppShimFocusType focus_type) { 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(CalledOnValidThread()); 87a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); 8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (handler) 89eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch handler->OnShimFocus(this, focus_type); 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 92eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AppShimHost::OnSetHidden(bool hidden) { 93868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) DCHECK(CalledOnValidThread()); 94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); 95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (handler) 96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch handler->OnShimSetHidden(this, hidden); 97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 99eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid AppShimHost::OnQuit() { 100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch DCHECK(CalledOnValidThread()); 101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch apps::AppShimHandler* handler = apps::AppShimHandler::GetForAppMode(app_id_); 102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (handler) 103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch handler->OnShimQuit(this); 1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid AppShimHost::OnAppLaunchComplete(apps::AppShimLaunchResult result) { 1077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!initial_launch_finished_) { 1087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch Send(new AppShimMsg_LaunchApp_Done(result)); 1097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch initial_launch_finished_ = true; 1107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch } 1117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch} 1127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 113a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void AppShimHost::OnAppClosed() { 114a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) Close(); 115a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)} 116a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AppShimHost::Close() { 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(CalledOnValidThread()); 1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) delete this; 1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 121