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 "apps/app_shim/app_shim_handler_mac.h" | 7 #include "apps/app_shim/app_shim_handler_mac.h" |
8 #include "apps/app_shim/app_shim_host_mac.h" | 8 #include "apps/app_shim/app_shim_host_mac.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/file_util.h" | |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/path_service.h" | 14 #include "base/path_service.h" |
14 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
15 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/common/mac/app_mode_common.h" | 18 #include "chrome/common/mac/app_mode_common.h" |
18 #include "ipc/unix_domain_socket_util.h" | |
19 | 19 |
20 using content::BrowserThread; | 20 using content::BrowserThread; |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 void CreateAppShimHost(const IPC::ChannelHandle& handle) { | 24 void CreateAppShimHost(const IPC::ChannelHandle& handle) { |
25 // AppShimHost takes ownership of itself. | 25 // AppShimHost takes ownership of itself. |
26 (new AppShimHost)->ServeChannel(handle); | 26 (new AppShimHost)->ServeChannel(handle); |
27 } | 27 } |
28 | 28 |
29 void DeleteFile(base::FilePath path) { | |
30 if (!path.empty()) | |
31 base::DeleteFile(path, true); | |
palmer
2013/12/10 20:04:23
Might this follow symlinks?
jackhou1
2014/01/06 05:29:52
The comments in file_util.h says this does not fol
| |
32 } | |
33 | |
29 } // namespace | 34 } // namespace |
30 | 35 |
31 const base::FilePath* AppShimHostManager::g_override_user_data_dir_ = NULL; | |
32 | |
33 AppShimHostManager::AppShimHostManager() {} | 36 AppShimHostManager::AppShimHostManager() {} |
34 | 37 |
35 void AppShimHostManager::Init() { | 38 void AppShimHostManager::Init() { |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
37 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_); | 40 apps::AppShimHandler::SetDefaultHandler(&extension_app_shim_handler_); |
38 BrowserThread::PostTask( | 41 BrowserThread::PostTask( |
39 BrowserThread::FILE, FROM_HERE, | 42 BrowserThread::FILE, FROM_HERE, |
40 base::Bind(&AppShimHostManager::InitOnFileThread, this)); | 43 base::Bind(&AppShimHostManager::InitOnFileThread, this)); |
41 } | 44 } |
42 | 45 |
43 AppShimHostManager::~AppShimHostManager() { | 46 AppShimHostManager::~AppShimHostManager() { |
44 apps::AppShimHandler::SetDefaultHandler(NULL); | 47 apps::AppShimHandler::SetDefaultHandler(NULL); |
48 factory_.reset(); | |
49 BrowserThread::PostTask( | |
50 BrowserThread::FILE, FROM_HERE, | |
51 base::Bind(&DeleteFile, directory_in_tmp_)); | |
45 } | 52 } |
46 | 53 |
47 void AppShimHostManager::InitOnFileThread() { | 54 void AppShimHostManager::InitOnFileThread() { |
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
49 base::FilePath user_data_dir; | 56 base::FilePath user_data_dir; |
50 if (g_override_user_data_dir_) { | 57 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) |
51 user_data_dir = *g_override_user_data_dir_; | 58 return; |
52 } else if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) { | 59 |
53 LOG(ERROR) << "Couldn't get user data directory while creating App Shim " | 60 // The socket path must be shorter than 128 chars (IPC::kMaxSocketNameLength). |
54 << "Host manager."; | 61 // To accommodate this, we use a short path in /tmp/ that is generated from a |
62 // hash of the user data dir. | |
63 base::FilePath socket_path = app_mode::GetShortSocketPath(user_data_dir); | |
64 CHECK(!socket_path.empty()); | |
65 directory_in_tmp_ = socket_path.DirName(); | |
66 base::CreateDirectory(directory_in_tmp_); | |
67 if (!base::PathIsWritable(directory_in_tmp_)) { | |
68 directory_in_tmp_.clear(); | |
55 return; | 69 return; |
56 } | 70 } |
57 | 71 |
58 base::FilePath socket_path = | |
59 user_data_dir.Append(app_mode::kAppShimSocketName); | |
60 // This mirrors a check in unix_domain_socket_util.cc which will guarantee | |
61 // failure and spam log files on bots because they have deeply nested paths to | |
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 | |
69 factory_.reset(new IPC::ChannelFactory(socket_path, this)); | 72 factory_.reset(new IPC::ChannelFactory(socket_path, this)); |
70 BrowserThread::PostTask( | 73 BrowserThread::PostTask( |
71 BrowserThread::IO, FROM_HERE, | 74 BrowserThread::IO, FROM_HERE, |
72 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); | 75 base::Bind(&AppShimHostManager::ListenOnIOThread, this)); |
73 } | 76 } |
74 | 77 |
75 void AppShimHostManager::ListenOnIOThread() { | 78 void AppShimHostManager::ListenOnIOThread() { |
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
77 if (!factory_->Listen()) { | 80 if (!factory_->Listen()) { |
78 BrowserThread::PostTask( | 81 BrowserThread::PostTask( |
79 BrowserThread::UI, FROM_HERE, | 82 BrowserThread::UI, FROM_HERE, |
80 base::Bind(&AppShimHostManager::OnListenError, this)); | 83 base::Bind(&AppShimHostManager::OnListenError, this)); |
81 } | 84 } |
82 } | 85 } |
83 | 86 |
84 void AppShimHostManager::OnClientConnected( | 87 void AppShimHostManager::OnClientConnected( |
85 const IPC::ChannelHandle& handle) { | 88 const IPC::ChannelHandle& handle) { |
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
87 BrowserThread::PostTask( | 90 BrowserThread::PostTask( |
88 BrowserThread::UI, FROM_HERE, | 91 BrowserThread::UI, FROM_HERE, |
89 base::Bind(&CreateAppShimHost, handle)); | 92 base::Bind(&CreateAppShimHost, handle)); |
90 } | 93 } |
91 | 94 |
92 void AppShimHostManager::OnListenError() { | 95 void AppShimHostManager::OnListenError() { |
93 // TODO(tapted): Set a timeout and attempt to reconstruct the channel. Until | 96 // 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 | 97 // cases where the error could occur are better known, just reset the factory |
95 // to allow failure to be communicated via the test API. | 98 // to allow failure to be communicated via the test API. |
96 factory_.reset(); | 99 factory_.reset(); |
97 } | 100 } |
OLD | NEW |