(Patch by Teodora Novkovic <teodora.petrovic@gmail.com>, originally reviewed at ...
[chromium-blink-merge.git] / apps / app_shim / app_shim_host_manager_mac.mm
blob6133b9be38e253ddc0897d12a193e816e3452000
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_host_mac.h"
8 #include "base/bind.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/path_service.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "chrome/common/mac/app_mode_common.h"
17 using content::BrowserThread;
19 namespace {
21 void CreateAppShimHost(const IPC::ChannelHandle& handle) {
22   // AppShimHost takes ownership of itself.
23   (new AppShimHost)->ServeChannel(handle);
26 }  // namespace
28 AppShimHostManager::AppShimHostManager() {
29   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
30   BrowserThread::PostTask(
31       BrowserThread::FILE, FROM_HERE,
32       base::Bind(&AppShimHostManager::InitOnFileThread,
33           base::Unretained(this)));
36 AppShimHostManager::~AppShimHostManager() {
39 void AppShimHostManager::InitOnFileThread() {
40   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
41   base::FilePath user_data_dir;
42   if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) {
43     LOG(ERROR) << "Couldn't get user data directory while creating App Shim "
44                << "Host manager.";
45     return;
46   }
47   base::FilePath socket_path =
48       user_data_dir.Append(app_mode::kAppShimSocketName);
49   factory_.reset(new IPC::ChannelFactory(socket_path, this));
50   BrowserThread::PostTask(
51       BrowserThread::IO, FROM_HERE,
52       base::Bind(&AppShimHostManager::ListenOnIOThread,
53           base::Unretained(this)));
56 void AppShimHostManager::ListenOnIOThread() {
57   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
58   factory_->Listen();
61 void AppShimHostManager::OnClientConnected(
62     const IPC::ChannelHandle& handle) {
63   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
64   BrowserThread::PostTask(
65       BrowserThread::UI, FROM_HERE,
66       base::Bind(&CreateAppShimHost, handle));
69 void AppShimHostManager::OnListenError() {
70   // TODO(jeremya): set a timeout and attempt to reconstruct the channel.