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_manager_mac.h"
7 #include "apps/app_shim/app_shim_handler_mac.h"
8 #include "apps/app_shim/app_shim_host_mac.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/path_service.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "chrome/common/mac/app_mode_common.h"
18 using content::BrowserThread;
22 void CreateAppShimHost(const IPC::ChannelHandle& handle) {
23 // AppShimHost takes ownership of itself.
24 (new AppShimHost)->ServeChannel(handle);
29 AppShimHostManager::AppShimHostManager() {}
31 void AppShimHostManager::Init() {
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
33 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_);
34 BrowserThread::PostTask(
35 BrowserThread::FILE, FROM_HERE,
36 base::Bind(&AppShimHostManager::InitOnFileThread, this));
39 AppShimHostManager::~AppShimHostManager() {
40 apps::AppShimHandler::SetDefaultHandler(NULL);
43 void AppShimHostManager::InitOnFileThread() {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
45 base::FilePath user_data_dir;
46 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) {
47 LOG(ERROR) << "Couldn't get user data directory while creating App Shim "
51 base::FilePath socket_path =
52 user_data_dir.Append(app_mode::kAppShimSocketName);
53 factory_.reset(new IPC::ChannelFactory(socket_path, this));
54 BrowserThread::PostTask(
55 BrowserThread::IO, FROM_HERE,
56 base::Bind(&AppShimHostManager::ListenOnIOThread, this));
59 void AppShimHostManager::ListenOnIOThread() {
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
64 void AppShimHostManager::OnClientConnected(
65 const IPC::ChannelHandle& handle) {
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
67 BrowserThread::PostTask(
68 BrowserThread::UI, FROM_HERE,
69 base::Bind(&CreateAppShimHost, handle));
72 void AppShimHostManager::OnListenError() {
73 // TODO(jeremya): set a timeout and attempt to reconstruct the channel.