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