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