| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "apps/app_shim/app_shim_host_manager_mac.h" | 5 #include "apps/app_shim/app_shim_host_manager_mac.h" |
| 6 | 6 |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 | 8 |
| 9 #include "apps/app_shim/app_shim_handler_mac.h" | 9 #include "apps/app_shim/app_shim_handler_mac.h" |
| 10 #include "apps/app_shim/app_shim_host_mac.h" | 10 #include "apps/app_shim/app_shim_host_mac.h" |
| 11 #include "base/base64.h" | 11 #include "base/base64.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/sha1.h" | 18 #include "base/sha1.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
| 22 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 23 #include "chrome/common/chrome_version_info.h" |
| 23 #include "chrome/common/mac/app_mode_common.h" | 24 #include "chrome/common/mac/app_mode_common.h" |
| 24 | 25 |
| 25 using content::BrowserThread; | 26 using content::BrowserThread; |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 void CreateAppShimHost(const IPC::ChannelHandle& handle) { | 30 void CreateAppShimHost(const IPC::ChannelHandle& handle) { |
| 30 // AppShimHost takes ownership of itself. | 31 // AppShimHost takes ownership of itself. |
| 31 (new AppShimHost)->ServeChannel(handle); | 32 (new AppShimHost)->ServeChannel(handle); |
| 32 } | 33 } |
| 33 | 34 |
| 34 base::FilePath GetDirectoryInTmpTemplate(const base::FilePath& user_data_dir) { | 35 base::FilePath GetDirectoryInTmpTemplate(const base::FilePath& user_data_dir) { |
| 35 base::FilePath temp_dir; | 36 base::FilePath temp_dir; |
| 36 CHECK(PathService::Get(base::DIR_TEMP, &temp_dir)); | 37 CHECK(PathService::Get(base::DIR_TEMP, &temp_dir)); |
| 37 // Check that it's shorter than the IPC socket length (104) minus the | 38 // Check that it's shorter than the IPC socket length (104) minus the |
| 38 // intermediate folder ("/chrome-XXXXXX/") and kAppShimSocketShortName. | 39 // intermediate folder ("/chrome-XXXXXX/") and kAppShimSocketShortName. |
| 39 DCHECK_GT(83u, temp_dir.value().length()); | 40 DCHECK_GT(83u, temp_dir.value().length()); |
| 40 return temp_dir.Append("chrome-XXXXXX"); | 41 return temp_dir.Append("chrome-XXXXXX"); |
| 41 } | 42 } |
| 42 | 43 |
| 43 void DeleteSocketFiles(const base::FilePath& directory_in_tmp, | 44 void DeleteSocketFiles(const base::FilePath& directory_in_tmp, |
| 44 const base::FilePath& symlink_path) { | 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); |
| 45 if (!directory_in_tmp.empty()) | 52 if (!directory_in_tmp.empty()) |
| 46 base::DeleteFile(directory_in_tmp, true); | 53 base::DeleteFile(directory_in_tmp, true); |
| 47 if (!symlink_path.empty()) | |
| 48 base::DeleteFile(symlink_path, false); | |
| 49 } | 54 } |
| 50 | 55 |
| 51 } // namespace | 56 } // namespace |
| 52 | 57 |
| 53 AppShimHostManager::AppShimHostManager() | 58 AppShimHostManager::AppShimHostManager() |
| 54 : did_init_(false) {} | 59 : did_init_(false) {} |
| 55 | 60 |
| 56 void AppShimHostManager::Init() { | 61 void AppShimHostManager::Init() { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 58 DCHECK(!did_init_); | 63 DCHECK(!did_init_); |
| 59 did_init_ = true; | 64 did_init_ = true; |
| 60 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_); | 65 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_); |
| 61 BrowserThread::PostTask( | 66 BrowserThread::PostTask( |
| 62 BrowserThread::FILE, FROM_HERE, | 67 BrowserThread::FILE, FROM_HERE, |
| 63 base::Bind(&AppShimHostManager::InitOnFileThread, this)); | 68 base::Bind(&AppShimHostManager::InitOnFileThread, this)); |
| 64 } | 69 } |
| 65 | 70 |
| 66 AppShimHostManager::~AppShimHostManager() { | 71 AppShimHostManager::~AppShimHostManager() { |
| 67 acceptor_.reset(); | 72 acceptor_.reset(); |
| 68 if (!did_init_) | 73 if (!did_init_) |
| 69 return; | 74 return; |
| 70 | 75 |
| 71 apps::AppShimHandler::SetDefaultHandler(NULL); | 76 apps::AppShimHandler::SetDefaultHandler(NULL); |
| 77 base::FilePath user_data_dir; |
| 72 base::FilePath symlink_path; | 78 base::FilePath symlink_path; |
| 73 if (PathService::Get(chrome::DIR_USER_DATA, &symlink_path)) | 79 base::FilePath version_path; |
| 74 symlink_path = symlink_path.Append(app_mode::kAppShimSocketSymlinkName); | 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 } |
| 75 BrowserThread::PostTask( | 85 BrowserThread::PostTask( |
| 76 BrowserThread::FILE, | 86 BrowserThread::FILE, |
| 77 FROM_HERE, | 87 FROM_HERE, |
| 78 base::Bind(&DeleteSocketFiles, directory_in_tmp_, symlink_path)); | 88 base::Bind( |
| 89 &DeleteSocketFiles, directory_in_tmp_, symlink_path, version_path)); |
| 79 } | 90 } |
| 80 | 91 |
| 81 void AppShimHostManager::InitOnFileThread() { | 92 void AppShimHostManager::InitOnFileThread() { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 83 base::FilePath user_data_dir; | 94 base::FilePath user_data_dir; |
| 84 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) | 95 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) |
| 85 return; | 96 return; |
| 86 | 97 |
| 87 // The socket path must be shorter than 104 chars (IPC::kMaxSocketNameLength). | 98 // 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 | 99 // To accommodate this, we use a short path in /tmp/ that is generated from a |
| (...skipping 22 matching lines...) Expand all Loading... |
| 111 acceptor_.reset(new apps::UnixDomainSocketAcceptor(socket_path, this)); | 122 acceptor_.reset(new apps::UnixDomainSocketAcceptor(socket_path, this)); |
| 112 | 123 |
| 113 // Create a symlink to the socket in the user data dir. This lets the shim | 124 // 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 | 125 // process started from Finder find the actual socket path by following the |
| 115 // symlink with ::readlink(). | 126 // symlink with ::readlink(). |
| 116 base::FilePath symlink_path = | 127 base::FilePath symlink_path = |
| 117 user_data_dir.Append(app_mode::kAppShimSocketSymlinkName); | 128 user_data_dir.Append(app_mode::kAppShimSocketSymlinkName); |
| 118 base::DeleteFile(symlink_path, false); | 129 base::DeleteFile(symlink_path, false); |
| 119 base::CreateSymbolicLink(socket_path, symlink_path); | 130 base::CreateSymbolicLink(socket_path, symlink_path); |
| 120 | 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 |
| 121 BrowserThread::PostTask( | 141 BrowserThread::PostTask( |
| 122 BrowserThread::IO, FROM_HERE, | 142 BrowserThread::IO, FROM_HERE, |
| 123 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); | 143 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); |
| 124 } | 144 } |
| 125 | 145 |
| 126 void AppShimHostManager::ListenOnIOThread() { | 146 void AppShimHostManager::ListenOnIOThread() { |
| 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 128 if (!acceptor_->Listen()) { | 148 if (!acceptor_->Listen()) { |
| 129 BrowserThread::PostTask( | 149 BrowserThread::PostTask( |
| 130 BrowserThread::UI, FROM_HERE, | 150 BrowserThread::UI, FROM_HERE, |
| 131 base::Bind(&AppShimHostManager::OnListenError, this)); | 151 base::Bind(&AppShimHostManager::OnListenError, this)); |
| 132 } | 152 } |
| 133 } | 153 } |
| 134 | 154 |
| 135 void AppShimHostManager::OnClientConnected( | 155 void AppShimHostManager::OnClientConnected( |
| 136 const IPC::ChannelHandle& handle) { | 156 const IPC::ChannelHandle& handle) { |
| 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 138 BrowserThread::PostTask( | 158 BrowserThread::PostTask( |
| 139 BrowserThread::UI, FROM_HERE, | 159 BrowserThread::UI, FROM_HERE, |
| 140 base::Bind(&CreateAppShimHost, handle)); | 160 base::Bind(&CreateAppShimHost, handle)); |
| 141 } | 161 } |
| 142 | 162 |
| 143 void AppShimHostManager::OnListenError() { | 163 void AppShimHostManager::OnListenError() { |
| 144 // TODO(tapted): Set a timeout and attempt to reconstruct the channel. Until | 164 // 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 | 165 // cases where the error could occur are better known, just reset the acceptor |
| 146 // to allow failure to be communicated via the test API. | 166 // to allow failure to be communicated via the test API. |
| 147 acceptor_.reset(); | 167 acceptor_.reset(); |
| 148 } | 168 } |
| OLD | NEW |