Chromium Code Reviews| 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> | |
|
tapted
2014/01/03 13:06:54
(remind me..) what's unistd for? I think mkdtemp i
jackhou1
2014/01/06 05:29:52
The man page says to include unistd.h (unless I'm
tapted
2014/01/06 07:31:28
right you are - must be a BSD thing :/
| |
| 8 | |
| 7 #include "apps/app_shim/app_shim_handler_mac.h" | 9 #include "apps/app_shim/app_shim_handler_mac.h" |
| 8 #include "apps/app_shim/app_shim_host_mac.h" | 10 #include "apps/app_shim/app_shim_host_mac.h" |
| 11 #include "base/base64.h" | |
| 9 #include "base/bind.h" | 12 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/file_util.h" | |
| 11 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 12 #include "base/logging.h" | 16 #include "base/logging.h" |
| 13 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/sha1.h" | |
| 19 #include "base/strings/string_util.h" | |
| 14 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/mac/app_mode_common.h" | 23 #include "chrome/common/mac/app_mode_common.h" |
| 18 #include "ipc/unix_domain_socket_util.h" | |
| 19 | 24 |
| 20 using content::BrowserThread; | 25 using content::BrowserThread; |
| 21 | 26 |
| 22 namespace { | 27 namespace { |
| 23 | 28 |
| 24 void CreateAppShimHost(const IPC::ChannelHandle& handle) { | 29 void CreateAppShimHost(const IPC::ChannelHandle& handle) { |
| 25 // AppShimHost takes ownership of itself. | 30 // AppShimHost takes ownership of itself. |
| 26 (new AppShimHost)->ServeChannel(handle); | 31 (new AppShimHost)->ServeChannel(handle); |
| 27 } | 32 } |
| 28 | 33 |
| 34 base::FilePath GetDirectoryInTmpTemplate(const base::FilePath& user_data_dir) { | |
| 35 std::string udd_hash; | |
| 36 base::Base64Encode(base::SHA1HashString(user_data_dir.value()), &udd_hash); | |
| 37 base::ReplaceChars(udd_hash, "/", "_", &udd_hash); | |
| 38 DCHECK_EQ(28u, udd_hash.length()); | |
| 39 return base::FilePath("/tmp") | |
| 40 .Append("chrome-" + udd_hash + ".XXXXXX"); | |
|
tapted
2014/01/03 13:06:54
nit: pretty sure this will fit on the line above
jackhou1
2014/01/06 05:29:52
Done.
| |
| 41 } | |
| 42 | |
| 43 void DeleteFile(base::FilePath path) { | |
|
tapted
2014/01/03 13:06:54
nit: const reference for |path|?
jackhou1
2014/01/06 05:29:52
Done.
| |
| 44 if (!path.empty()) | |
| 45 base::DeleteFile(path, true); | |
| 46 } | |
| 47 | |
| 29 } // namespace | 48 } // namespace |
| 30 | 49 |
| 31 const base::FilePath* AppShimHostManager::g_override_user_data_dir_ = NULL; | |
| 32 | |
| 33 AppShimHostManager::AppShimHostManager() {} | 50 AppShimHostManager::AppShimHostManager() {} |
| 34 | 51 |
| 35 void AppShimHostManager::Init() { | 52 void AppShimHostManager::Init() { |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 37 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_); | 54 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_); |
| 38 BrowserThread::PostTask( | 55 BrowserThread::PostTask( |
| 39 BrowserThread::FILE, FROM_HERE, | 56 BrowserThread::FILE, FROM_HERE, |
| 40 base::Bind(&AppShimHostManager::InitOnFileThread, this)); | 57 base::Bind(&AppShimHostManager::InitOnFileThread, this)); |
| 41 } | 58 } |
| 42 | 59 |
| 43 AppShimHostManager::~AppShimHostManager() { | 60 AppShimHostManager::~AppShimHostManager() { |
| 44 apps::AppShimHandler::SetDefaultHandler(NULL); | 61 apps::AppShimHandler::SetDefaultHandler(NULL); |
| 62 factory_.reset(); | |
| 63 BrowserThread::PostTask( | |
| 64 BrowserThread::FILE, FROM_HERE, | |
| 65 base::Bind(&DeleteFile, directory_in_tmp_)); | |
|
tapted
2014/01/03 13:06:54
We should delete the symlink in the user data dir
jackhou1
2014/01/06 05:29:52
Done.
| |
| 45 } | 66 } |
| 46 | 67 |
| 47 void AppShimHostManager::InitOnFileThread() { | 68 void AppShimHostManager::InitOnFileThread() { |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 49 base::FilePath user_data_dir; | 70 base::FilePath user_data_dir; |
| 50 if (g_override_user_data_dir_) { | 71 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) |
| 51 user_data_dir = *g_override_user_data_dir_; | 72 return; |
| 52 } else if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) { | 73 |
| 53 LOG(ERROR) << "Couldn't get user data directory while creating App Shim " | 74 // The socket path must be shorter than 104 chars (IPC::kMaxSocketNameLength). |
| 54 << "Host manager."; | 75 // To accommodate this, we use a short path in /tmp/ that is generated from a |
| 76 // hash of the user data dir. | |
| 77 std::string directory_string = | |
| 78 GetDirectoryInTmpTemplate(user_data_dir).value(); | |
| 79 | |
| 80 // |mkdtemp| replaces trailing X's randomly and creates the directory. | |
|
tapted
2014/01/03 13:06:54
nit: |mkdtemp| -> mkdtemp().
jackhou1
2014/01/06 05:29:52
Done.
| |
| 81 if (!mkdtemp(const_cast<char*>(directory_string.c_str()))) | |
|
tapted
2014/01/03 13:06:54
nit: if you use directory_string.data() (or &direc
jackhou1
2014/01/06 05:29:52
Using &directory_string[0] as .data() also returns
| |
| 82 return; | |
|
tapted
2014/01/03 13:06:54
Maybe `PLOG(ERROR) << directory_string` before thi
jackhou1
2014/01/06 05:29:52
Done.
| |
| 83 | |
| 84 directory_in_tmp_ = base::FilePath(directory_string); | |
| 85 // Check that the directory was created with the correct permissions. | |
| 86 int dir_mode = 0; | |
| 87 if (!base::GetPosixFilePermissions(directory_in_tmp_, &dir_mode) || | |
| 88 base::FILE_PERMISSION_USER_MASK != dir_mode) { | |
|
tapted
2014/01/03 13:06:54
nit: I'd maybe swap dir_mode and FILE_PERMISSION_U
jackhou1
2014/01/06 05:29:52
Done.
| |
| 55 return; | 89 return; |
|
tapted
2014/01/03 13:06:54
This could perhaps have a NOTREACHED() before it -
jackhou1
2014/01/06 05:29:52
Done.
| |
| 56 } | 90 } |
| 57 | 91 |
| 92 // IPC::ChannelFactory creates the socket immediately. | |
| 58 base::FilePath socket_path = | 93 base::FilePath socket_path = |
| 94 directory_in_tmp_.Append(app_mode::kAppShimSocketName); | |
| 95 factory_.reset(new IPC::ChannelFactory(socket_path, this)); | |
| 96 | |
| 97 // Create a symlink to the socket in the UDD. | |
|
tapted
2014/01/03 13:06:54
This is a bit subtle - maybe a wordier comment, li
jackhou1
2014/01/06 05:29:52
Done.
| |
| 98 base::FilePath symlink_path = | |
| 59 user_data_dir.Append(app_mode::kAppShimSocketName); | 99 user_data_dir.Append(app_mode::kAppShimSocketName); |
| 60 // This mirrors a check in unix_domain_socket_util.cc which will guarantee | 100 base::DeleteFile(symlink_path, false); |
| 61 // failure and spam log files on bots because they have deeply nested paths to | 101 base::CreateSymbolicLink(socket_path, symlink_path); |
| 62 // |user_data_dir| when swarming. See http://crbug.com/240554. Shim tests that | |
| 63 // run on the bots must override the path using AppShimHostManagerTestApi. | |
| 64 if (socket_path.value().length() >= IPC::kMaxSocketNameLength && | |
| 65 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { | |
| 66 return; | |
| 67 } | |
| 68 | 102 |
| 69 factory_.reset(new IPC::ChannelFactory(socket_path, this)); | |
| 70 BrowserThread::PostTask( | 103 BrowserThread::PostTask( |
| 71 BrowserThread::IO, FROM_HERE, | 104 BrowserThread::IO, FROM_HERE, |
| 72 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); | 105 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); |
| 73 } | 106 } |
| 74 | 107 |
| 75 void AppShimHostManager::ListenOnIOThread() { | 108 void AppShimHostManager::ListenOnIOThread() { |
| 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 77 if (!factory_->Listen()) { | 110 if (!factory_->Listen()) { |
| 78 BrowserThread::PostTask( | 111 BrowserThread::PostTask( |
| 79 BrowserThread::UI, FROM_HERE, | 112 BrowserThread::UI, FROM_HERE, |
| 80 base::Bind(&AppShimHostManager::OnListenError, this)); | 113 base::Bind(&AppShimHostManager::OnListenError, this)); |
| 81 } | 114 } |
| 82 } | 115 } |
| 83 | 116 |
| 84 void AppShimHostManager::OnClientConnected( | 117 void AppShimHostManager::OnClientConnected( |
| 85 const IPC::ChannelHandle& handle) { | 118 const IPC::ChannelHandle& handle) { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 87 BrowserThread::PostTask( | 120 BrowserThread::PostTask( |
| 88 BrowserThread::UI, FROM_HERE, | 121 BrowserThread::UI, FROM_HERE, |
| 89 base::Bind(&CreateAppShimHost, handle)); | 122 base::Bind(&CreateAppShimHost, handle)); |
| 90 } | 123 } |
| 91 | 124 |
| 92 void AppShimHostManager::OnListenError() { | 125 void AppShimHostManager::OnListenError() { |
| 93 // TODO(tapted): Set a timeout and attempt to reconstruct the channel. Until | 126 // TODO(tapted): Set a timeout and attempt to reconstruct the channel. Until |
| 94 // cases where the error could occur are better known, just reset the factory | 127 // cases where the error could occur are better known, just reset the factory |
| 95 // to allow failure to be communicated via the test API. | 128 // to allow failure to be communicated via the test API. |
| 96 factory_.reset(); | 129 factory_.reset(); |
| 97 } | 130 } |
| OLD | NEW |