OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/browser/child_process_launcher_helper_posix.h" | 5 #include "content/browser/child_process_launcher_helper_posix.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/lazy_instance.h" |
8 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" |
9 #include "content/browser/file_descriptor_info_impl.h" | 12 #include "content/browser/file_descriptor_info_impl.h" |
10 #include "content/public/browser/content_browser_client.h" | 13 #include "content/public/browser/content_browser_client.h" |
11 #include "content/public/common/content_client.h" | 14 #include "content/public/common/content_client.h" |
12 #include "content/public/common/content_descriptors.h" | 15 #include "content/public/common/content_descriptors.h" |
| 16 #include "content/public/common/content_switches.h" |
13 #include "mojo/edk/embedder/platform_handle.h" | 17 #include "mojo/edk/embedder/platform_handle.h" |
| 18 #include "services/catalog/public/cpp/manifest_parsing_util.h" |
14 | 19 |
15 namespace content { | 20 namespace content { |
16 namespace internal { | 21 namespace internal { |
17 | 22 |
| 23 namespace { |
| 24 |
| 25 using OpenedFileMap = |
| 26 std::map<base::FilePath, |
| 27 std::pair<base::PlatformFile, base::MemoryMappedFile::Region>>; |
| 28 |
| 29 base::LazyInstance< |
| 30 std::map<std::string, std::unique_ptr<catalog::RequiredFileMap>>> |
| 31 g_required_files_by_service = LAZY_INSTANCE_INITIALIZER; |
| 32 |
| 33 base::LazyInstance<OpenedFileMap> g_opened_files = LAZY_INSTANCE_INITIALIZER; |
| 34 |
| 35 base::PlatformFile OpenFileIfNecessary(const base::FilePath& path, |
| 36 base::MemoryMappedFile::Region* region) { |
| 37 const auto& iter = g_opened_files.Get().find(path); |
| 38 if (iter != g_opened_files.Get().end()) { |
| 39 *region = iter->second.second; |
| 40 return iter->second.first; |
| 41 } |
| 42 base::File file = OpenFileToShare(path, region); |
| 43 if (!file.IsValid()) { |
| 44 return base::kInvalidPlatformFile; |
| 45 } |
| 46 // g_opened_files becomes the owner of the file descriptor. |
| 47 base::PlatformFile fd = file.TakePlatformFile(); |
| 48 g_opened_files.Get()[path] = std::make_pair(fd, *region); |
| 49 return fd; |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
18 std::unique_ptr<FileDescriptorInfo> CreateDefaultPosixFilesToMap( | 54 std::unique_ptr<FileDescriptorInfo> CreateDefaultPosixFilesToMap( |
19 const base::CommandLine& command_line, | |
20 int child_process_id, | 55 int child_process_id, |
21 const mojo::edk::PlatformHandle& mojo_client_handle) { | 56 const mojo::edk::PlatformHandle& mojo_client_handle, |
| 57 bool include_service_required_files, |
| 58 const std::string& process_type, |
| 59 base::CommandLine* command_line) { |
22 std::unique_ptr<FileDescriptorInfo> files_to_register( | 60 std::unique_ptr<FileDescriptorInfo> files_to_register( |
23 FileDescriptorInfoImpl::Create()); | 61 FileDescriptorInfoImpl::Create()); |
24 | 62 |
25 int field_trial_handle = base::FieldTrialList::GetFieldTrialHandle(); | 63 int field_trial_handle = base::FieldTrialList::GetFieldTrialHandle(); |
26 if (field_trial_handle != base::kInvalidPlatformFile) | 64 if (field_trial_handle != base::kInvalidPlatformFile) |
27 files_to_register->Share(kFieldTrialDescriptor, field_trial_handle); | 65 files_to_register->Share(kFieldTrialDescriptor, field_trial_handle); |
28 | 66 |
29 DCHECK(mojo_client_handle.is_valid()); | 67 DCHECK(mojo_client_handle.is_valid()); |
30 files_to_register->Share(kMojoIPCChannel, mojo_client_handle.handle); | 68 files_to_register->Share(kMojoIPCChannel, mojo_client_handle.handle); |
31 | 69 |
32 // TODO(jcivelli): remove this "if defined" by making | 70 // TODO(jcivelli): remove this "if defined" by making |
33 // GetAdditionalMappedFilesForChildProcess a no op on Mac. | 71 // GetAdditionalMappedFilesForChildProcess a no op on Mac. |
34 #if !defined(OS_MACOSX) | 72 #if !defined(OS_MACOSX) |
35 GetContentClient()->browser()->GetAdditionalMappedFilesForChildProcess( | 73 GetContentClient()->browser()->GetAdditionalMappedFilesForChildProcess( |
36 command_line, child_process_id, files_to_register.get()); | 74 *command_line, child_process_id, files_to_register.get()); |
37 #endif | 75 #endif |
38 | 76 |
| 77 if (!include_service_required_files) |
| 78 return files_to_register; |
| 79 |
| 80 static std::map<std::string, std::string>* service_name_resolver = nullptr; |
| 81 if (!service_name_resolver) { |
| 82 service_name_resolver = new std::map<std::string, std::string>(); |
| 83 // The service names are defined in the JSON manifests, so we don't have a |
| 84 // constant accessible for them. |
| 85 // TODO(jcivelli): remove this map once the service name is accessible from |
| 86 // the command line crbug.com/687250 |
| 87 (*service_name_resolver)[switches::kRendererProcess] = "content_renderer"; |
| 88 (*service_name_resolver)[switches::kGpuProcess] = "content_gpu"; |
| 89 (*service_name_resolver)[switches::kPpapiPluginProcess] = "content_plugin"; |
| 90 (*service_name_resolver)[switches::kUtilityProcess] = "content_utility"; |
| 91 } |
| 92 |
| 93 // Also include the files specified in the services' manifests. |
| 94 const auto& service_name_iter = service_name_resolver->find(process_type); |
| 95 CHECK(service_name_iter != service_name_resolver->end()); |
| 96 std::string service_name = service_name_iter->second; |
| 97 const auto& files_iter = g_required_files_by_service.Get().find(service_name); |
| 98 if (files_iter != g_required_files_by_service.Get().end()) { |
| 99 catalog::RequiredFileMap* required_files_map = files_iter->second.get(); |
| 100 base::GlobalDescriptors::Key key = kContentDynamicDescriptorStart; |
| 101 std::string switch_value; |
| 102 for (const auto& key_path_iter : *required_files_map) { |
| 103 base::MemoryMappedFile::Region region; |
| 104 base::PlatformFile file = |
| 105 OpenFileIfNecessary(base::FilePath(key_path_iter.second), ®ion); |
| 106 if (file == base::kInvalidPlatformFile) { |
| 107 LOG(ERROR) << "Ignoring invalid file " << key_path_iter.second; |
| 108 continue; |
| 109 } |
| 110 if (!switch_value.empty()) { |
| 111 switch_value += ","; |
| 112 } |
| 113 switch_value += key_path_iter.first; |
| 114 switch_value += ":"; |
| 115 switch_value += base::IntToString(key); |
| 116 files_to_register->ShareWithRegion(key, file, region); |
| 117 key++; |
| 118 DCHECK(key < kContentDynamicDescriptorMax); |
| 119 } |
| 120 command_line->AppendSwitchASCII(switches::kSharedFiles, switch_value); |
| 121 } |
| 122 |
39 return files_to_register; | 123 return files_to_register; |
40 } | 124 } |
41 | 125 |
| 126 void SetFilesToShareForServicePosix( |
| 127 const std::string& service_name, |
| 128 std::unique_ptr<catalog::RequiredFileMap> required_files) { |
| 129 if (required_files->empty()) |
| 130 return; |
| 131 |
| 132 if (!base::StartsWith(service_name, "content", |
| 133 base::CompareCase::INSENSITIVE_ASCII)) { |
| 134 // Not a content child service, ignore. |
| 135 return; |
| 136 } |
| 137 |
| 138 DCHECK(g_required_files_by_service.Get().count(service_name) == 0); |
| 139 g_required_files_by_service.Get()[service_name] = std::move(required_files); |
| 140 } |
| 141 |
42 } // namespace internal | 142 } // namespace internal |
43 } // namespace content | 143 } // namespace content |
OLD | NEW |