OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/common/child_process_host.h" | 5 #include "content/common/child_process_host.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
10 #include "base/path_service.h" | 11 #include "base/path_service.h" |
11 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 12 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
12 #include "content/common/child_process_info.h" | 13 #include "content/common/child_process_info.h" |
13 #include "content/common/child_process_messages.h" | 14 #include "content/common/child_process_messages.h" |
14 #include "content/common/content_paths.h" | 15 #include "content/common/content_paths.h" |
15 #include "content/common/content_switches.h" | 16 #include "content/common/content_switches.h" |
16 #include "ipc/ipc_logging.h" | 17 #include "ipc/ipc_logging.h" |
17 | 18 |
18 #if defined(OS_LINUX) | 19 #if defined(OS_LINUX) |
19 #include "base/linux_util.h" | 20 #include "base/linux_util.h" |
20 #endif // OS_LINUX | 21 #endif // OS_LINUX |
21 | 22 |
| 23 #if defined(OS_MACOSX) |
| 24 namespace { |
| 25 |
| 26 // Given |path| identifying a Mac-style child process executable path, adjusts |
| 27 // it to correspond to |feature|. For a child process path such as |
| 28 // ".../Chromium Helper.app/Contents/MacOS/Chromium Helper", the transformed |
| 29 // path for feature "NP" would be |
| 30 // ".../Chromium Helper NP.app/Contents/MacOS/Chromium Helper NP". The new |
| 31 // path is returned. |
| 32 FilePath TransformPathForFeature(const FilePath& path, |
| 33 const std::string& feature) { |
| 34 std::string basename = path.BaseName().value(); |
| 35 |
| 36 FilePath macos_path = path.DirName(); |
| 37 const char kMacOSName[] = "MacOS"; |
| 38 DCHECK_EQ(kMacOSName, macos_path.BaseName().value()); |
| 39 |
| 40 FilePath contents_path = macos_path.DirName(); |
| 41 const char kContentsName[] = "Contents"; |
| 42 DCHECK_EQ(kContentsName, contents_path.BaseName().value()); |
| 43 |
| 44 FilePath helper_app_path = contents_path.DirName(); |
| 45 const char kAppExtension[] = ".app"; |
| 46 std::string basename_app = basename; |
| 47 basename_app.append(kAppExtension); |
| 48 DCHECK_EQ(basename_app, helper_app_path.BaseName().value()); |
| 49 |
| 50 FilePath root_path = helper_app_path.DirName(); |
| 51 |
| 52 std::string new_basename = basename; |
| 53 new_basename.append(1, ' '); |
| 54 new_basename.append(feature); |
| 55 std::string new_basename_app = new_basename; |
| 56 new_basename_app.append(kAppExtension); |
| 57 |
| 58 FilePath new_path = root_path.Append(new_basename_app) |
| 59 .Append(kContentsName) |
| 60 .Append(kMacOSName) |
| 61 .Append(new_basename); |
| 62 |
| 63 return new_path; |
| 64 } |
| 65 |
| 66 } // namespace |
| 67 #endif // OS_MACOSX |
| 68 |
22 ChildProcessHost::ChildProcessHost() | 69 ChildProcessHost::ChildProcessHost() |
23 : ALLOW_THIS_IN_INITIALIZER_LIST(listener_(this)), | 70 : ALLOW_THIS_IN_INITIALIZER_LIST(listener_(this)), |
24 opening_channel_(false) { | 71 opening_channel_(false) { |
25 } | 72 } |
26 | 73 |
27 ChildProcessHost::~ChildProcessHost() { | 74 ChildProcessHost::~ChildProcessHost() { |
28 for (size_t i = 0; i < filters_.size(); ++i) { | 75 for (size_t i = 0; i < filters_.size(); ++i) { |
29 filters_[i]->OnChannelClosing(); | 76 filters_[i]->OnChannelClosing(); |
30 filters_[i]->OnFilterRemoved(); | 77 filters_[i]->OnFilterRemoved(); |
31 } | 78 } |
32 } | 79 } |
33 | 80 |
34 void ChildProcessHost::AddFilter(IPC::ChannelProxy::MessageFilter* filter) { | 81 void ChildProcessHost::AddFilter(IPC::ChannelProxy::MessageFilter* filter) { |
35 filters_.push_back(filter); | 82 filters_.push_back(filter); |
36 | 83 |
37 if (channel_.get()) | 84 if (channel_.get()) |
38 filter->OnFilterAdded(channel_.get()); | 85 filter->OnFilterAdded(channel_.get()); |
39 } | 86 } |
40 | 87 |
41 // static | 88 // static |
42 FilePath ChildProcessHost::GetChildPath(bool allow_self) { | 89 FilePath ChildProcessHost::GetChildPath(int flags) { |
43 FilePath child_path; | 90 FilePath child_path; |
44 | 91 |
45 child_path = CommandLine::ForCurrentProcess()->GetSwitchValuePath( | 92 child_path = CommandLine::ForCurrentProcess()->GetSwitchValuePath( |
46 switches::kBrowserSubprocessPath); | 93 switches::kBrowserSubprocessPath); |
47 if (!child_path.empty()) | 94 if (!child_path.empty()) |
48 return child_path; | 95 return child_path; |
49 | 96 |
50 #if defined(OS_LINUX) | 97 #if defined(OS_LINUX) |
51 // Use /proc/self/exe rather than our known binary path so updates | 98 // Use /proc/self/exe rather than our known binary path so updates |
52 // can't swap out the binary from underneath us. | 99 // can't swap out the binary from underneath us. |
53 // When running under Valgrind, forking /proc/self/exe ends up forking the | 100 // When running under Valgrind, forking /proc/self/exe ends up forking the |
54 // Valgrind executable, which then crashes. However, it's almost safe to | 101 // Valgrind executable, which then crashes. However, it's almost safe to |
55 // assume that the updates won't happen while testing with Valgrind tools. | 102 // assume that the updates won't happen while testing with Valgrind tools. |
56 if (allow_self && !RunningOnValgrind()) | 103 if (flags & CHILD_ALLOW_SELF && !RunningOnValgrind()) |
57 return FilePath("/proc/self/exe"); | 104 return FilePath("/proc/self/exe"); |
58 #endif | 105 #endif |
59 | 106 |
60 // On most platforms, the child executable is the same as the current | 107 // On most platforms, the child executable is the same as the current |
61 // executable. | 108 // executable. |
62 PathService::Get(content::CHILD_PROCESS_EXE, &child_path); | 109 PathService::Get(content::CHILD_PROCESS_EXE, &child_path); |
| 110 |
| 111 #if defined(OS_MACOSX) |
| 112 DCHECK(!(flags & CHILD_NO_PIE && flags & CHILD_ALLOW_HEAP_EXECUTION)); |
| 113 |
| 114 // If needed, choose an executable with special flags set that inform the |
| 115 // kernel to enable or disable specific optional process-wide features. |
| 116 if (flags & CHILD_NO_PIE) { |
| 117 // "NP" is "No PIE". This results in Chromium Helper NP.app or |
| 118 // Google Chrome Helper NP.app. |
| 119 child_path = TransformPathForFeature(child_path, "NP"); |
| 120 } else if (flags & CHILD_ALLOW_HEAP_EXECUTION) { |
| 121 // "EH" is "Executable Heap". A non-executable heap is only available to |
| 122 // 32-bit processes on Mac OS X 10.7. Most code can and should run with a |
| 123 // non-executable heap, but the "EH" feature is provided to allow code |
| 124 // intolerant of a non-executable heap to work properly on 10.7. This |
| 125 // results in Chromium Helper EH.app or Google Chrome Helper EH.app. |
| 126 child_path = TransformPathForFeature(child_path, "EH"); |
| 127 } |
| 128 #endif |
| 129 |
63 return child_path; | 130 return child_path; |
64 } | 131 } |
65 | 132 |
66 #if defined(OS_WIN) | 133 #if defined(OS_WIN) |
67 // static | 134 // static |
68 void ChildProcessHost::PreCacheFont(LOGFONT font) { | 135 void ChildProcessHost::PreCacheFont(LOGFONT font) { |
69 // If a child process is running in a sandbox, GetTextMetrics() | 136 // If a child process is running in a sandbox, GetTextMetrics() |
70 // can sometimes fail. If a font has not been loaded | 137 // can sometimes fail. If a font has not been loaded |
71 // previously, GetTextMetrics() will try to load the font | 138 // previously, GetTextMetrics() will try to load the font |
72 // from the font file. However, the sandboxed process does | 139 // from the font file. However, the sandboxed process does |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 for (size_t i = 0; i < host_->filters_.size(); ++i) | 289 for (size_t i = 0; i < host_->filters_.size(); ++i) |
223 host_->filters_[i]->OnChannelError(); | 290 host_->filters_[i]->OnChannelError(); |
224 | 291 |
225 // This will delete host_, which will also destroy this! | 292 // This will delete host_, which will also destroy this! |
226 host_->OnChildDied(); | 293 host_->OnChildDied(); |
227 } | 294 } |
228 | 295 |
229 void ChildProcessHost::ForceShutdown() { | 296 void ChildProcessHost::ForceShutdown() { |
230 Send(new ChildProcessMsg_Shutdown()); | 297 Send(new ChildProcessMsg_Shutdown()); |
231 } | 298 } |
OLD | NEW |