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 = user_data_dir.Append(app_mode::kAppShimVersionSymlinkName); |
| 83 } |
75 BrowserThread::PostTask( | 84 BrowserThread::PostTask( |
76 BrowserThread::FILE, | 85 BrowserThread::FILE, |
77 FROM_HERE, | 86 FROM_HERE, |
78 base::Bind(&DeleteSocketFiles, directory_in_tmp_, symlink_path)); | 87 base::Bind( |
| 88 &DeleteSocketFiles, directory_in_tmp_, symlink_path, version_path)); |
79 } | 89 } |
80 | 90 |
81 void AppShimHostManager::InitOnFileThread() { | 91 void AppShimHostManager::InitOnFileThread() { |
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
83 base::FilePath user_data_dir; | 93 base::FilePath user_data_dir; |
84 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) | 94 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) |
85 return; | 95 return; |
86 | 96 |
87 // The socket path must be shorter than 104 chars (IPC::kMaxSocketNameLength). | 97 // 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 | 98 // 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)); | 121 acceptor_.reset(new apps::UnixDomainSocketAcceptor(socket_path, this)); |
112 | 122 |
113 // Create a symlink to the socket in the user data dir. This lets the shim | 123 // 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 | 124 // process started from Finder find the actual socket path by following the |
115 // symlink with ::readlink(). | 125 // symlink with ::readlink(). |
116 base::FilePath symlink_path = | 126 base::FilePath symlink_path = |
117 user_data_dir.Append(app_mode::kAppShimSocketSymlinkName); | 127 user_data_dir.Append(app_mode::kAppShimSocketSymlinkName); |
118 base::DeleteFile(symlink_path, false); | 128 base::DeleteFile(symlink_path, false); |
119 base::CreateSymbolicLink(socket_path, symlink_path); | 129 base::CreateSymbolicLink(socket_path, symlink_path); |
120 | 130 |
| 131 // Create a symlink containing the current version string. This allows the |
| 132 // shim to load the same framework version as the currently running Chrome |
| 133 // process. |
| 134 base::FilePath version_path = |
| 135 user_data_dir.Append(app_mode::kAppShimVersionSymlinkName); |
| 136 base::DeleteFile(version_path, false); |
| 137 base::CreateSymbolicLink(base::FilePath(chrome::VersionInfo().Version()), |
| 138 version_path); |
| 139 |
121 BrowserThread::PostTask( | 140 BrowserThread::PostTask( |
122 BrowserThread::IO, FROM_HERE, | 141 BrowserThread::IO, FROM_HERE, |
123 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); | 142 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); |
124 } | 143 } |
125 | 144 |
126 void AppShimHostManager::ListenOnIOThread() { | 145 void AppShimHostManager::ListenOnIOThread() { |
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
128 if (!acceptor_->Listen()) { | 147 if (!acceptor_->Listen()) { |
129 BrowserThread::PostTask( | 148 BrowserThread::PostTask( |
130 BrowserThread::UI, FROM_HERE, | 149 BrowserThread::UI, FROM_HERE, |
131 base::Bind(&AppShimHostManager::OnListenError, this)); | 150 base::Bind(&AppShimHostManager::OnListenError, this)); |
132 } | 151 } |
133 } | 152 } |
134 | 153 |
135 void AppShimHostManager::OnClientConnected( | 154 void AppShimHostManager::OnClientConnected( |
136 const IPC::ChannelHandle& handle) { | 155 const IPC::ChannelHandle& handle) { |
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
138 BrowserThread::PostTask( | 157 BrowserThread::PostTask( |
139 BrowserThread::UI, FROM_HERE, | 158 BrowserThread::UI, FROM_HERE, |
140 base::Bind(&CreateAppShimHost, handle)); | 159 base::Bind(&CreateAppShimHost, handle)); |
141 } | 160 } |
142 | 161 |
143 void AppShimHostManager::OnListenError() { | 162 void AppShimHostManager::OnListenError() { |
144 // TODO(tapted): Set a timeout and attempt to reconstruct the channel. Until | 163 // 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 | 164 // cases where the error could occur are better known, just reset the acceptor |
146 // to allow failure to be communicated via the test API. | 165 // to allow failure to be communicated via the test API. |
147 acceptor_.reset(); | 166 acceptor_.reset(); |
148 } | 167 } |
OLD | NEW |