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