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