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 "chrome/browser/apps/app_shim/app_shim_host_manager_mac.h" 6 7#include <unistd.h> 8 9#include "base/base64.h" 10#include "base/bind.h" 11#include "base/command_line.h" 12#include "base/files/file_path.h" 13#include "base/files/file_util.h" 14#include "base/logging.h" 15#include "base/path_service.h" 16#include "base/sha1.h" 17#include "base/strings/string_util.h" 18#include "chrome/browser/apps/app_shim/app_shim_handler_mac.h" 19#include "chrome/browser/apps/app_shim/app_shim_host_mac.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/chrome_version_info.h" 24#include "chrome/common/mac/app_mode_common.h" 25 26using content::BrowserThread; 27 28namespace { 29 30void CreateAppShimHost(const IPC::ChannelHandle& handle) { 31 // AppShimHost takes ownership of itself. 32 (new AppShimHost)->ServeChannel(handle); 33} 34 35base::FilePath GetDirectoryInTmpTemplate(const base::FilePath& user_data_dir) { 36 base::FilePath temp_dir; 37 CHECK(PathService::Get(base::DIR_TEMP, &temp_dir)); 38 // Check that it's shorter than the IPC socket length (104) minus the 39 // intermediate folder ("/chrome-XXXXXX/") and kAppShimSocketShortName. 40 DCHECK_GT(83u, temp_dir.value().length()); 41 return temp_dir.Append("chrome-XXXXXX"); 42} 43 44void DeleteSocketFiles(const base::FilePath& directory_in_tmp, 45 const base::FilePath& symlink_path, 46 const base::FilePath& version_path) { 47 // Delete in reverse order of creation. 48 if (!version_path.empty()) 49 base::DeleteFile(version_path, false); 50 if (!symlink_path.empty()) 51 base::DeleteFile(symlink_path, false); 52 if (!directory_in_tmp.empty()) 53 base::DeleteFile(directory_in_tmp, true); 54} 55 56} // namespace 57 58AppShimHostManager::AppShimHostManager() 59 : did_init_(false) {} 60 61void AppShimHostManager::Init() { 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 63 DCHECK(!did_init_); 64 did_init_ = true; 65 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_); 66 BrowserThread::PostTask( 67 BrowserThread::FILE, FROM_HERE, 68 base::Bind(&AppShimHostManager::InitOnFileThread, this)); 69} 70 71AppShimHostManager::~AppShimHostManager() { 72 acceptor_.reset(); 73 if (!did_init_) 74 return; 75 76 apps::AppShimHandler::SetDefaultHandler(NULL); 77 base::FilePath user_data_dir; 78 base::FilePath symlink_path; 79 base::FilePath version_path; 80 if (PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) { 81 symlink_path = user_data_dir.Append(app_mode::kAppShimSocketSymlinkName); 82 version_path = 83 user_data_dir.Append(app_mode::kRunningChromeVersionSymlinkName); 84 } 85 BrowserThread::PostTask( 86 BrowserThread::FILE, 87 FROM_HERE, 88 base::Bind( 89 &DeleteSocketFiles, directory_in_tmp_, symlink_path, version_path)); 90} 91 92void AppShimHostManager::InitOnFileThread() { 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 94 base::FilePath user_data_dir; 95 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) 96 return; 97 98 // The socket path must be shorter than 104 chars (IPC::kMaxSocketNameLength). 99 // To accommodate this, we use a short path in /tmp/ that is generated from a 100 // hash of the user data dir. 101 std::string directory_string = 102 GetDirectoryInTmpTemplate(user_data_dir).value(); 103 104 // mkdtemp() replaces trailing X's randomly and creates the directory. 105 if (!mkdtemp(&directory_string[0])) { 106 PLOG(ERROR) << directory_string; 107 return; 108 } 109 110 directory_in_tmp_ = base::FilePath(directory_string); 111 // Check that the directory was created with the correct permissions. 112 int dir_mode = 0; 113 if (!base::GetPosixFilePermissions(directory_in_tmp_, &dir_mode) || 114 dir_mode != base::FILE_PERMISSION_USER_MASK) { 115 NOTREACHED(); 116 return; 117 } 118 119 // UnixDomainSocketAcceptor creates the socket immediately. 120 base::FilePath socket_path = 121 directory_in_tmp_.Append(app_mode::kAppShimSocketShortName); 122 acceptor_.reset(new apps::UnixDomainSocketAcceptor(socket_path, this)); 123 124 // Create a symlink to the socket in the user data dir. This lets the shim 125 // process started from Finder find the actual socket path by following the 126 // symlink with ::readlink(). 127 base::FilePath symlink_path = 128 user_data_dir.Append(app_mode::kAppShimSocketSymlinkName); 129 base::DeleteFile(symlink_path, false); 130 base::CreateSymbolicLink(socket_path, symlink_path); 131 132 // Create a symlink containing the current version string. This allows the 133 // shim to load the same framework version as the currently running Chrome 134 // process. 135 base::FilePath version_path = 136 user_data_dir.Append(app_mode::kRunningChromeVersionSymlinkName); 137 base::DeleteFile(version_path, false); 138 base::CreateSymbolicLink(base::FilePath(chrome::VersionInfo().Version()), 139 version_path); 140 141 BrowserThread::PostTask( 142 BrowserThread::IO, FROM_HERE, 143 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); 144} 145 146void AppShimHostManager::ListenOnIOThread() { 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 148 if (!acceptor_->Listen()) { 149 BrowserThread::PostTask( 150 BrowserThread::UI, FROM_HERE, 151 base::Bind(&AppShimHostManager::OnListenError, this)); 152 } 153} 154 155void AppShimHostManager::OnClientConnected( 156 const IPC::ChannelHandle& handle) { 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 158 BrowserThread::PostTask( 159 BrowserThread::UI, FROM_HERE, 160 base::Bind(&CreateAppShimHost, handle)); 161} 162 163void AppShimHostManager::OnListenError() { 164 // TODO(tapted): Set a timeout and attempt to reconstruct the channel. Until 165 // cases where the error could occur are better known, just reset the acceptor 166 // to allow failure to be communicated via the test API. 167 acceptor_.reset(); 168} 169