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 "apps/app_shim/app_shim_host_manager_mac.h" 6 7 #include <unistd.h> 8 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" 24 25 using content::BrowserThread; 26 27 namespace { 28 29 void CreateAppShimHost(const IPC::ChannelHandle& handle) { 30 // AppShimHost takes ownership of itself. 31 (new AppShimHost)->ServeChannel(handle); 32 } 33 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"); 41 } 42 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); 49 } 50 51 } // namespace 52 53 AppShimHostManager::AppShimHostManager() 54 : did_init_(false) {} 55 56 void AppShimHostManager::Init() { 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 58 DCHECK(!did_init_); 59 did_init_ = true; 60 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_); 61 BrowserThread::PostTask( 62 BrowserThread::FILE, FROM_HERE, 63 base::Bind(&AppShimHostManager::InitOnFileThread, this)); 64 } 65 66 AppShimHostManager::~AppShimHostManager() { 67 factory_.reset(); 68 if (!did_init_) 69 return; 70 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( 76 BrowserThread::FILE, 77 FROM_HERE, 78 base::Bind(&DeleteSocketFiles, directory_in_tmp_, symlink_path)); 79 } 80 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)) 85 return; 86 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(); 92 93 // mkdtemp() replaces trailing X's randomly and creates the directory. 94 if (!mkdtemp(&directory_string[0])) { 95 PLOG(ERROR) << directory_string; 96 return; 97 } 98 99 directory_in_tmp_ = base::FilePath(directory_string); 100 // Check that the directory was created with the correct permissions. 101 int dir_mode = 0; 102 if (!base::GetPosixFilePermissions(directory_in_tmp_, &dir_mode) || 103 dir_mode != base::FILE_PERMISSION_USER_MASK) { 104 NOTREACHED(); 105 return; 106 } 107 108 // IPC::ChannelFactory creates the socket immediately. 109 base::FilePath socket_path = 110 directory_in_tmp_.Append(app_mode::kAppShimSocketShortName); 111 factory_.reset(new IPC::ChannelFactory(socket_path, this)); 112 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); 120 121 BrowserThread::PostTask( 122 BrowserThread::IO, FROM_HERE, 123 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); 124 } 125 126 void AppShimHostManager::ListenOnIOThread() { 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 128 if (!factory_->Listen()) { 129 BrowserThread::PostTask( 130 BrowserThread::UI, FROM_HERE, 131 base::Bind(&AppShimHostManager::OnListenError, this)); 132 } 133 } 134 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)); 141 } 142 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 factory 146 // to allow failure to be communicated via the test API. 147 factory_.reset(); 148 } 149