Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(542)

Side by Side Diff: content/browser/child_process_launcher_helper_posix.cc

Issue 2684433003: Files required by a service now listed in manifest. (Closed)
Patch Set: Removed unused method in apk_assets Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>>>
dcheng 2017/02/15 08:05:27 Nit: no unique_ptr indirection, using RequiredFile
Jay Civelli 2017/02/15 19:53:47 Done.
32 g_required_files_by_service = LAZY_INSTANCE_INITIALIZER;
33
34 base::LazyInstance<OpenedFileMap> g_opened_files = LAZY_INSTANCE_INITIALIZER;
dcheng 2017/02/15 08:05:27 Nit: use function-local statics instead of LazyIns
Jay Civelli 2017/02/15 19:53:47 Done.
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;
dcheng 2017/02/15 08:05:27 Another alternative: static auto* service_name_re
Jay Civelli 2017/02/15 19:53:47 Nice! Done.
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::kGpuProcess] = "content_gpu";
89 (*service_name_resolver)[switches::kPpapiPluginProcess] = "content_plugin";
90 (*service_name_resolver)[switches::kRendererProcess] = "content_renderer";
91 (*service_name_resolver)[switches::kUtilityProcess] = "content_utility";
92 (*service_name_resolver)["ppapi-broker"] = "ppapi_broker";
93 (*service_name_resolver)["nacl-loader"] = "nacl_loader";
94 (*service_name_resolver)["nacl-loader-nonsfi"] = "nacl_loader_nonsfi";
95 }
96
97 // Also include the files specified in the services' manifests.
98 const auto& service_name_iter = service_name_resolver->find(process_type);
99 CHECK(service_name_iter != service_name_resolver->end())
dcheng 2017/02/15 08:05:27 DCHECK, unless the input can be controlled by arbi
Jay Civelli 2017/02/15 19:53:47 Done.
100 << "No service found for process type " << process_type;
101 std::string service_name = service_name_iter->second;
dcheng 2017/02/15 08:05:27 Nit: const std::string& to save a copy
Jay Civelli 2017/02/15 19:53:47 Done.
102 const auto& files_iter = g_required_files_by_service.Get().find(service_name);
dcheng 2017/02/15 08:05:27 But this can just be auto, since iterators are lig
Jay Civelli 2017/02/15 19:53:47 Done.
103 if (files_iter != g_required_files_by_service.Get().end()) {
104 catalog::RequiredFileMap* required_files_map = files_iter->second.get();
105 base::GlobalDescriptors::Key key = kContentDynamicDescriptorStart;
106 std::string switch_value;
107 for (const auto& key_path_iter : *required_files_map) {
108 base::MemoryMappedFile::Region region;
109 base::PlatformFile file =
110 OpenFileIfNecessary(base::FilePath(key_path_iter.second), &region);
dcheng 2017/02/15 08:05:27 Can the map just hold a FilePath?
Jay Civelli 2017/02/15 19:53:47 Good idea, done.
111 if (file == base::kInvalidPlatformFile) {
112 LOG(ERROR) << "Ignoring invalid file " << key_path_iter.second;
dcheng 2017/02/15 08:05:27 DLOG instead of LOG? This comment applies to many
Jay Civelli 2017/02/15 19:53:47 Went through all of them and changed them to DLOG.
113 continue;
114 }
115 if (!switch_value.empty()) {
116 switch_value += ",";
117 }
118 switch_value += key_path_iter.first;
119 switch_value += ":";
120 switch_value += base::IntToString(key);
121 files_to_register->ShareWithRegion(key, file, region);
122 key++;
123 DCHECK(key < kContentDynamicDescriptorMax);
124 }
125 command_line->AppendSwitchASCII(switches::kSharedFiles, switch_value);
126 }
127
39 return files_to_register; 128 return files_to_register;
40 } 129 }
41 130
131 void SetFilesToShareForServicePosix(
132 const std::string& service_name,
133 std::unique_ptr<catalog::RequiredFileMap> required_files) {
dcheng 2017/02/15 08:05:27 Consider just passing by value and moving below, r
Jay Civelli 2017/02/15 19:53:47 Done.
134 if (required_files->empty())
135 return;
136
137 if (!base::StartsWith(service_name, "content",
dcheng 2017/02/15 08:05:27 Maybe content_ to be safe?
Jay Civelli 2017/02/15 19:53:47 Done.
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698