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> |
| 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").Append("chrome-" + udd_hash + ".XXXXXX"); |
| 40 } |
| 41 |
| 42 void DeleteSocketFiles(const base::FilePath& directory_in_tmp, |
| 43 const base::FilePath& symlink_path) { |
| 44 if (!directory_in_tmp.empty()) |
| 45 base::DeleteFile(directory_in_tmp, true); |
| 46 if (!symlink_path.empty()) |
| 47 base::DeleteFile(symlink_path, false); |
| 48 } |
| 49 |
29 } // namespace | 50 } // namespace |
30 | 51 |
31 const base::FilePath* AppShimHostManager::g_override_user_data_dir_ = NULL; | |
32 | |
33 AppShimHostManager::AppShimHostManager() {} | 52 AppShimHostManager::AppShimHostManager() {} |
34 | 53 |
35 void AppShimHostManager::Init() { | 54 void AppShimHostManager::Init() { |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
37 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_); | 56 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_); |
38 BrowserThread::PostTask( | 57 BrowserThread::PostTask( |
39 BrowserThread::FILE, FROM_HERE, | 58 BrowserThread::FILE, FROM_HERE, |
40 base::Bind(&AppShimHostManager::InitOnFileThread, this)); | 59 base::Bind(&AppShimHostManager::InitOnFileThread, this)); |
41 } | 60 } |
42 | 61 |
43 AppShimHostManager::~AppShimHostManager() { | 62 AppShimHostManager::~AppShimHostManager() { |
44 apps::AppShimHandler::SetDefaultHandler(NULL); | 63 apps::AppShimHandler::SetDefaultHandler(NULL); |
| 64 factory_.reset(); |
| 65 base::FilePath symlink_path; |
| 66 if (PathService::Get(chrome::DIR_USER_DATA, &symlink_path)) |
| 67 symlink_path = symlink_path.Append(app_mode::kAppShimSocketName); |
| 68 BrowserThread::PostTask( |
| 69 BrowserThread::FILE, FROM_HERE, |
| 70 base::Bind(&DeleteSocketFiles, directory_in_tmp_, symlink_path)); |
45 } | 71 } |
46 | 72 |
47 void AppShimHostManager::InitOnFileThread() { | 73 void AppShimHostManager::InitOnFileThread() { |
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
49 base::FilePath user_data_dir; | 75 base::FilePath user_data_dir; |
50 if (g_override_user_data_dir_) { | 76 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) |
51 user_data_dir = *g_override_user_data_dir_; | 77 return; |
52 } else if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) { | 78 |
53 LOG(ERROR) << "Couldn't get user data directory while creating App Shim " | 79 // The socket path must be shorter than 104 chars (IPC::kMaxSocketNameLength). |
54 << "Host manager."; | 80 // To accommodate this, we use a short path in /tmp/ that is generated from a |
| 81 // hash of the user data dir. |
| 82 std::string directory_string = |
| 83 GetDirectoryInTmpTemplate(user_data_dir).value(); |
| 84 |
| 85 // mkdtemp() replaces trailing X's randomly and creates the directory. |
| 86 if (!mkdtemp(&directory_string[0])) { |
| 87 PLOG(ERROR) << directory_string; |
55 return; | 88 return; |
56 } | 89 } |
57 | 90 |
58 base::FilePath socket_path = | 91 directory_in_tmp_ = base::FilePath(directory_string); |
59 user_data_dir.Append(app_mode::kAppShimSocketName); | 92 // Check that the directory was created with the correct permissions. |
60 // This mirrors a check in unix_domain_socket_util.cc which will guarantee | 93 int dir_mode = 0; |
61 // failure and spam log files on bots because they have deeply nested paths to | 94 if (!base::GetPosixFilePermissions(directory_in_tmp_, &dir_mode) || |
62 // |user_data_dir| when swarming. See http://crbug.com/240554. Shim tests that | 95 dir_mode != base::FILE_PERMISSION_USER_MASK) { |
63 // run on the bots must override the path using AppShimHostManagerTestApi. | 96 NOTREACHED(); |
64 if (socket_path.value().length() >= IPC::kMaxSocketNameLength && | |
65 CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { | |
66 return; | 97 return; |
67 } | 98 } |
68 | 99 |
| 100 // IPC::ChannelFactory creates the socket immediately. |
| 101 base::FilePath socket_path = |
| 102 directory_in_tmp_.Append(app_mode::kAppShimSocketName); |
69 factory_.reset(new IPC::ChannelFactory(socket_path, this)); | 103 factory_.reset(new IPC::ChannelFactory(socket_path, this)); |
| 104 |
| 105 // Create a symlink to the socket in the user data dir. This lets the shim |
| 106 // process started from Finder find the actual socket path by following the |
| 107 // symlink with ::readlink(). |
| 108 base::FilePath symlink_path = |
| 109 user_data_dir.Append(app_mode::kAppShimSocketName); |
| 110 base::DeleteFile(symlink_path, false); |
| 111 base::CreateSymbolicLink(socket_path, symlink_path); |
| 112 |
70 BrowserThread::PostTask( | 113 BrowserThread::PostTask( |
71 BrowserThread::IO, FROM_HERE, | 114 BrowserThread::IO, FROM_HERE, |
72 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); | 115 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); |
73 } | 116 } |
74 | 117 |
75 void AppShimHostManager::ListenOnIOThread() { | 118 void AppShimHostManager::ListenOnIOThread() { |
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
77 if (!factory_->Listen()) { | 120 if (!factory_->Listen()) { |
78 BrowserThread::PostTask( | 121 BrowserThread::PostTask( |
79 BrowserThread::UI, FROM_HERE, | 122 BrowserThread::UI, FROM_HERE, |
80 base::Bind(&AppShimHostManager::OnListenError, this)); | 123 base::Bind(&AppShimHostManager::OnListenError, this)); |
81 } | 124 } |
82 } | 125 } |
83 | 126 |
84 void AppShimHostManager::OnClientConnected( | 127 void AppShimHostManager::OnClientConnected( |
85 const IPC::ChannelHandle& handle) { | 128 const IPC::ChannelHandle& handle) { |
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
87 BrowserThread::PostTask( | 130 BrowserThread::PostTask( |
88 BrowserThread::UI, FROM_HERE, | 131 BrowserThread::UI, FROM_HERE, |
89 base::Bind(&CreateAppShimHost, handle)); | 132 base::Bind(&CreateAppShimHost, handle)); |
90 } | 133 } |
91 | 134 |
92 void AppShimHostManager::OnListenError() { | 135 void AppShimHostManager::OnListenError() { |
93 // TODO(tapted): Set a timeout and attempt to reconstruct the channel. Until | 136 // 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 | 137 // cases where the error could occur are better known, just reset the factory |
95 // to allow failure to be communicated via the test API. | 138 // to allow failure to be communicated via the test API. |
96 factory_.reset(); | 139 factory_.reset(); |
97 } | 140 } |
OLD | NEW |