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"
9 #include "apps/app_shim/app_shim_handler_mac.h"
10 #include "apps/app_shim/app_shim_host_mac.h"
11 #include "base/base64.h"
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "base/file_util.h"
15 #include "base/files/file_path.h"
16 #include "base/logging.h"
17 #include "base/path_service.h"
18 #include "base/sha1.h"
19 #include "base/strings/string_util.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/mac/app_mode_common.h"
25 using content::BrowserThread;
29 void CreateAppShimHost(const IPC::ChannelHandle& handle) {
30 // AppShimHost takes ownership of itself.
31 (new AppShimHost)->ServeChannel(handle);
34 base::FilePath GetDirectoryInTmpTemplate(const base::FilePath& user_data_dir) {
35 base::FilePath temp_dir;
36 CHECK(PathService::Get(base::DIR_TEMP, &temp_dir));
37 // Check that it's shorter than the IPC socket length (104) minus the
38 // intermediate folder ("/chrome-XXXXXX/") and kAppShimSocketShortName.
39 DCHECK_GT(83u, temp_dir.value().length());
40 return temp_dir.Append("chrome-XXXXXX");
43 void DeleteSocketFiles(const base::FilePath& directory_in_tmp,
44 const base::FilePath& symlink_path) {
45 if (!directory_in_tmp.empty())
46 base::DeleteFile(directory_in_tmp, true);
47 if (!symlink_path.empty())
48 base::DeleteFile(symlink_path, false);
53 AppShimHostManager::AppShimHostManager()
56 void AppShimHostManager::Init() {
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
60 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_);
61 BrowserThread::PostTask(
62 BrowserThread::FILE, FROM_HERE,
63 base::Bind(&AppShimHostManager::InitOnFileThread, this));
66 AppShimHostManager::~AppShimHostManager() {
71 apps::AppShimHandler::SetDefaultHandler(NULL);
72 base::FilePath symlink_path;
73 if (PathService::Get(chrome::DIR_USER_DATA, &symlink_path))
74 symlink_path = symlink_path.Append(app_mode::kAppShimSocketSymlinkName);
75 BrowserThread::PostTask(
78 base::Bind(&DeleteSocketFiles, directory_in_tmp_, symlink_path));
81 void AppShimHostManager::InitOnFileThread() {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
83 base::FilePath user_data_dir;
84 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
87 // The socket path must be shorter than 104 chars (IPC::kMaxSocketNameLength).
88 // To accommodate this, we use a short path in /tmp/ that is generated from a
89 // hash of the user data dir.
90 std::string directory_string =
91 GetDirectoryInTmpTemplate(user_data_dir).value();
93 // mkdtemp() replaces trailing X's randomly and creates the directory.
94 if (!mkdtemp(&directory_string[0])) {
95 PLOG(ERROR) << directory_string;
99 directory_in_tmp_ = base::FilePath(directory_string);
100 // Check that the directory was created with the correct permissions.
102 if (!base::GetPosixFilePermissions(directory_in_tmp_, &dir_mode) ||
103 dir_mode != base::FILE_PERMISSION_USER_MASK) {
108 // UnixDomainSocketAcceptor creates the socket immediately.
109 base::FilePath socket_path =
110 directory_in_tmp_.Append(app_mode::kAppShimSocketShortName);
111 acceptor_.reset(new apps::UnixDomainSocketAcceptor(socket_path, this));
113 // Create a symlink to the socket in the user data dir. This lets the shim
114 // process started from Finder find the actual socket path by following the
115 // symlink with ::readlink().
116 base::FilePath symlink_path =
117 user_data_dir.Append(app_mode::kAppShimSocketSymlinkName);
118 base::DeleteFile(symlink_path, false);
119 base::CreateSymbolicLink(socket_path, symlink_path);
121 BrowserThread::PostTask(
122 BrowserThread::IO, FROM_HERE,
123 base::Bind(&AppShimHostManager::ListenOnIOThread, this));
126 void AppShimHostManager::ListenOnIOThread() {
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
128 if (!acceptor_->Listen()) {
129 BrowserThread::PostTask(
130 BrowserThread::UI, FROM_HERE,
131 base::Bind(&AppShimHostManager::OnListenError, this));
135 void AppShimHostManager::OnClientConnected(
136 const IPC::ChannelHandle& handle) {
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
138 BrowserThread::PostTask(
139 BrowserThread::UI, FROM_HERE,
140 base::Bind(&CreateAppShimHost, handle));
143 void AppShimHostManager::OnListenError() {
144 // TODO(tapted): Set a timeout and attempt to reconstruct the channel. Until
145 // cases where the error could occur are better known, just reset the acceptor
146 // to allow failure to be communicated via the test API.