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