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

Side by Side Diff: chrome/renderer/chrome_render_thread_observer.cc

Issue 2020413002: Refactor out a new class for syncing field trials in child processes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments. Created 4 years, 6 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/renderer/chrome_render_thread_observer.h" 5 #include "chrome/renderer/chrome_render_thread_observer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <limits> 9 #include <limits>
10 #include <memory> 10 #include <memory>
11 #include <set> 11 #include <set>
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/base_switches.h"
16 #include "base/bind.h" 15 #include "base/bind.h"
17 #include "base/command_line.h" 16 #include "base/command_line.h"
18 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
19 #include "base/location.h" 18 #include "base/location.h"
20 #include "base/macros.h" 19 #include "base/macros.h"
21 #include "base/memory/weak_ptr.h" 20 #include "base/memory/weak_ptr.h"
22 #include "base/metrics/field_trial.h" 21 #include "base/metrics/field_trial.h"
23 #include "base/metrics/histogram.h" 22 #include "base/metrics/histogram.h"
24 #include "base/metrics/statistics_recorder.h" 23 #include "base/metrics/statistics_recorder.h"
25 #include "base/path_service.h" 24 #include "base/path_service.h"
26 #include "base/single_thread_task_runner.h" 25 #include "base/single_thread_task_runner.h"
27 #include "base/strings/utf_string_conversions.h" 26 #include "base/strings/utf_string_conversions.h"
28 #include "base/threading/platform_thread.h" 27 #include "base/threading/platform_thread.h"
29 #include "base/threading/thread_task_runner_handle.h" 28 #include "base/threading/thread_task_runner_handle.h"
30 #include "build/build_config.h" 29 #include "build/build_config.h"
31 #include "chrome/common/child_process_logging.h" 30 #include "chrome/common/child_process_logging.h"
32 #include "chrome/common/chrome_paths.h" 31 #include "chrome/common/chrome_paths.h"
33 #include "chrome/common/media/media_resource_provider.h" 32 #include "chrome/common/media/media_resource_provider.h"
34 #include "chrome/common/net/net_resource_provider.h" 33 #include "chrome/common/net/net_resource_provider.h"
35 #include "chrome/common/render_messages.h" 34 #include "chrome/common/render_messages.h"
36 #include "chrome/common/resource_usage_reporter.mojom.h" 35 #include "chrome/common/resource_usage_reporter.mojom.h"
37 #include "chrome/common/resource_usage_reporter_type_converters.h" 36 #include "chrome/common/resource_usage_reporter_type_converters.h"
38 #include "chrome/common/url_constants.h" 37 #include "chrome/common/url_constants.h"
39 #include "chrome/renderer/content_settings_observer.h" 38 #include "chrome/renderer/content_settings_observer.h"
40 #include "chrome/renderer/security_filter_peer.h" 39 #include "chrome/renderer/security_filter_peer.h"
41 #include "components/variations/variations_util.h"
42 #include "content/public/child/resource_dispatcher_delegate.h" 40 #include "content/public/child/resource_dispatcher_delegate.h"
43 #include "content/public/common/service_registry.h" 41 #include "content/public/common/service_registry.h"
44 #include "content/public/renderer/render_thread.h" 42 #include "content/public/renderer/render_thread.h"
45 #include "content/public/renderer/render_view.h" 43 #include "content/public/renderer/render_view.h"
46 #include "content/public/renderer/render_view_visitor.h" 44 #include "content/public/renderer/render_view_visitor.h"
47 #include "media/base/media_resources.h" 45 #include "media/base/media_resources.h"
48 #include "mojo/public/cpp/bindings/strong_binding.h" 46 #include "mojo/public/cpp/bindings/strong_binding.h"
49 #include "net/base/net_errors.h" 47 #include "net/base/net_errors.h"
50 #include "net/base/net_module.h" 48 #include "net/base/net_module.h"
51 #include "third_party/WebKit/public/web/WebCache.h" 49 #include "third_party/WebKit/public/web/WebCache.h"
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 base::WeakPtr<ChromeRenderThreadObserver> observer, 229 base::WeakPtr<ChromeRenderThreadObserver> observer,
232 mojo::InterfaceRequest<mojom::ResourceUsageReporter> request) { 230 mojo::InterfaceRequest<mojom::ResourceUsageReporter> request) {
233 new ResourceUsageReporterImpl(observer, std::move(request)); 231 new ResourceUsageReporterImpl(observer, std::move(request));
234 } 232 }
235 233
236 } // namespace 234 } // namespace
237 235
238 bool ChromeRenderThreadObserver::is_incognito_process_ = false; 236 bool ChromeRenderThreadObserver::is_incognito_process_ = false;
239 237
240 ChromeRenderThreadObserver::ChromeRenderThreadObserver() 238 ChromeRenderThreadObserver::ChromeRenderThreadObserver()
241 : weak_factory_(this) { 239 : field_trial_syncer_(content::RenderThread::Get()), weak_factory_(this) {
242 const base::CommandLine& command_line = 240 const base::CommandLine& command_line =
243 *base::CommandLine::ForCurrentProcess(); 241 *base::CommandLine::ForCurrentProcess();
244 242
245 RenderThread* thread = RenderThread::Get(); 243 RenderThread* thread = RenderThread::Get();
246 resource_delegate_.reset(new RendererResourceDelegate()); 244 resource_delegate_.reset(new RendererResourceDelegate());
247 thread->SetResourceDispatcherDelegate(resource_delegate_.get()); 245 thread->SetResourceDispatcherDelegate(resource_delegate_.get());
248 246
249 thread->GetServiceRegistry()->AddService( 247 thread->GetServiceRegistry()->AddService(
250 base::Bind(CreateResourceUsageReporter, weak_factory_.GetWeakPtr())); 248 base::Bind(CreateResourceUsageReporter, weak_factory_.GetWeakPtr()));
251 249
252 // Configure modules that need access to resources. 250 // Configure modules that need access to resources.
253 net::NetModule::SetResourceProvider(chrome_common_net::NetResourceProvider); 251 net::NetModule::SetResourceProvider(chrome_common_net::NetResourceProvider);
254 media::SetLocalizedStringProvider( 252 media::SetLocalizedStringProvider(
255 chrome_common_media::LocalizedStringProvider); 253 chrome_common_media::LocalizedStringProvider);
256 254
257 InitFieldTrialObserving(command_line); 255 field_trial_syncer_.InitFieldTrialObserving(command_line);
258 256
259 // chrome-native: is a scheme used for placeholder navigations that allow 257 // chrome-native: is a scheme used for placeholder navigations that allow
260 // UIs to be drawn with platform native widgets instead of HTML. These pages 258 // UIs to be drawn with platform native widgets instead of HTML. These pages
261 // should not be accessible, and should also be treated as empty documents 259 // should not be accessible, and should also be treated as empty documents
262 // that can commit synchronously. No code should be runnable in these pages, 260 // that can commit synchronously. No code should be runnable in these pages,
263 // so it should not need to access anything nor should it allow javascript 261 // so it should not need to access anything nor should it allow javascript
264 // URLs since it should never be visible to the user. 262 // URLs since it should never be visible to the user.
265 WebString native_scheme(base::ASCIIToUTF16(chrome::kChromeNativeScheme)); 263 WebString native_scheme(base::ASCIIToUTF16(chrome::kChromeNativeScheme));
266 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(native_scheme); 264 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(native_scheme);
267 WebSecurityPolicy::registerURLSchemeAsEmptyDocument(native_scheme); 265 WebSecurityPolicy::registerURLSchemeAsEmptyDocument(native_scheme);
268 WebSecurityPolicy::registerURLSchemeAsNoAccess(native_scheme); 266 WebSecurityPolicy::registerURLSchemeAsNoAccess(native_scheme);
269 WebSecurityPolicy::registerURLSchemeAsNotAllowingJavascriptURLs( 267 WebSecurityPolicy::registerURLSchemeAsNotAllowingJavascriptURLs(
270 native_scheme); 268 native_scheme);
271 } 269 }
272 270
273 ChromeRenderThreadObserver::~ChromeRenderThreadObserver() {} 271 ChromeRenderThreadObserver::~ChromeRenderThreadObserver() {}
274 272
275 void ChromeRenderThreadObserver::InitFieldTrialObserving(
276 const base::CommandLine& command_line) {
277 // Set up initial set of crash dump data for field trials in this renderer.
278 variations::SetVariationListCrashKeys();
279
280 // Listen for field trial activations to report them to the browser.
281 base::FieldTrialList::AddObserver(this);
282
283 // Some field trials may have been activated before this point. Notify the
284 // browser of these activations now. To detect these, take the set difference
285 // of currently active trials with the initially active trials.
286 base::FieldTrial::ActiveGroups initially_active_trials;
287 base::FieldTrialList::GetActiveFieldTrialGroupsFromString(
288 command_line.GetSwitchValueASCII(switches::kForceFieldTrials),
289 &initially_active_trials);
290 std::set<std::string> initially_active_trials_set;
291 for (const auto& entry : initially_active_trials) {
292 initially_active_trials_set.insert(std::move(entry.trial_name));
293 }
294
295 base::FieldTrial::ActiveGroups current_active_trials;
296 base::FieldTrialList::GetActiveFieldTrialGroups(&current_active_trials);
297 for (const auto& trial : current_active_trials) {
298 if (!ContainsKey(initially_active_trials_set, trial.trial_name))
299 OnFieldTrialGroupFinalized(trial.trial_name, trial.group_name);
300 }
301 }
302
303 bool ChromeRenderThreadObserver::OnControlMessageReceived( 273 bool ChromeRenderThreadObserver::OnControlMessageReceived(
304 const IPC::Message& message) { 274 const IPC::Message& message) {
305 bool handled = true; 275 bool handled = true;
306 IPC_BEGIN_MESSAGE_MAP(ChromeRenderThreadObserver, message) 276 IPC_BEGIN_MESSAGE_MAP(ChromeRenderThreadObserver, message)
307 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetIsIncognitoProcess, 277 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetIsIncognitoProcess,
308 OnSetIsIncognitoProcess) 278 OnSetIsIncognitoProcess)
309 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetFieldTrialGroup, OnSetFieldTrialGroup) 279 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetFieldTrialGroup, OnSetFieldTrialGroup)
310 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetContentSettingRules, 280 IPC_MESSAGE_HANDLER(ChromeViewMsg_SetContentSettingRules,
311 OnSetContentSettingRules) 281 OnSetContentSettingRules)
312 IPC_MESSAGE_UNHANDLED(handled = false) 282 IPC_MESSAGE_UNHANDLED(handled = false)
313 IPC_END_MESSAGE_MAP() 283 IPC_END_MESSAGE_MAP()
314 return handled; 284 return handled;
315 } 285 }
316 286
317 void ChromeRenderThreadObserver::OnSetIsIncognitoProcess( 287 void ChromeRenderThreadObserver::OnSetIsIncognitoProcess(
318 bool is_incognito_process) { 288 bool is_incognito_process) {
319 is_incognito_process_ = is_incognito_process; 289 is_incognito_process_ = is_incognito_process;
320 } 290 }
321 291
322 void ChromeRenderThreadObserver::OnSetContentSettingRules( 292 void ChromeRenderThreadObserver::OnSetContentSettingRules(
323 const RendererContentSettingRules& rules) { 293 const RendererContentSettingRules& rules) {
324 content_setting_rules_ = rules; 294 content_setting_rules_ = rules;
325 } 295 }
326 296
327 void ChromeRenderThreadObserver::OnSetFieldTrialGroup( 297 void ChromeRenderThreadObserver::OnSetFieldTrialGroup(
328 const std::string& field_trial_name, 298 const std::string& trial_name,
329 const std::string& group_name) { 299 const std::string& group_name) {
330 base::FieldTrial* trial = 300 field_trial_syncer_.OnSetFieldTrialGroup(trial_name, group_name);
331 base::FieldTrialList::CreateFieldTrial(field_trial_name, group_name);
332 // Ensure the trial is marked as "used" by calling group() on it if it is
333 // marked as activated.
334 trial->group();
335 variations::SetVariationListCrashKeys();
336 } 301 }
337 302
338 const RendererContentSettingRules* 303 const RendererContentSettingRules*
339 ChromeRenderThreadObserver::content_setting_rules() const { 304 ChromeRenderThreadObserver::content_setting_rules() const {
340 return &content_setting_rules_; 305 return &content_setting_rules_;
341 } 306 }
342
343 void ChromeRenderThreadObserver::OnFieldTrialGroupFinalized(
344 const std::string& trial_name,
345 const std::string& group_name) {
346 content::RenderThread::Get()->Send(
347 new ChromeViewHostMsg_FieldTrialActivated(trial_name));
348 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698