1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/subresource_filter/content/browser/content_ruleset_service.h"
#include <utility>
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "components/subresource_filter/content/common/subresource_filter_messages.h"
#include "components/subresource_filter/core/browser/ruleset_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h"
#include "ipc/ipc_platform_file.h"
namespace subresource_filter {
namespace {
void SendRulesetToRenderProcess(base::File* file,
content::RenderProcessHost* rph) {
DCHECK(rph);
DCHECK(file);
DCHECK(file->IsValid());
rph->Send(new SubresourceFilterMsg_SetRulesetForProcess(
IPC::TakePlatformFileForTransit(file->Duplicate())));
}
// The file handle is closed when the argument goes out of scope.
void CloseFile(base::File) {}
// Posts the |file| handle to the file thread so it can be closed.
void CloseFileOnFileThread(base::File* file) {
if (!file->IsValid())
return;
content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
base::Bind(&CloseFile, base::Passed(file)));
}
} // namespace
ContentRulesetService::ContentRulesetService(
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner)
: ruleset_dealer_(base::MakeUnique<VerifiedRulesetDealer::Handle>(
std::move(blocking_task_runner))) {
// Must rely on notifications as RenderProcessHostObserver::RenderProcessReady
// would only be called after queued IPC messages (potentially triggering a
// navigation) had already been sent to the new renderer.
notification_registrar_.Add(
this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
content::NotificationService::AllBrowserContextsAndSources());
}
ContentRulesetService::~ContentRulesetService() {
CloseFileOnFileThread(&ruleset_data_);
}
void ContentRulesetService::SetRulesetPublishedCallbackForTesting(
base::Closure callback) {
ruleset_published_callback_ = callback;
}
void ContentRulesetService::PostAfterStartupTask(base::Closure task) {
content::BrowserThread::PostAfterStartupTask(
FROM_HERE,
content::BrowserThread::GetTaskRunnerForThread(
content::BrowserThread::UI),
task);
}
void ContentRulesetService::PublishNewRulesetVersion(base::File ruleset_data) {
DCHECK(ruleset_data.IsValid());
CloseFileOnFileThread(&ruleset_data_);
// Will not perform verification until the ruleset is retrieved the first
// time.
ruleset_dealer_->SetRulesetFile(ruleset_data.Duplicate());
ruleset_data_ = std::move(ruleset_data);
for (auto it = content::RenderProcessHost::AllHostsIterator(); !it.IsAtEnd();
it.Advance()) {
SendRulesetToRenderProcess(&ruleset_data_, it.GetCurrentValue());
}
if (!ruleset_published_callback_.is_null())
ruleset_published_callback_.Run();
}
void ContentRulesetService::set_ruleset_service(
std::unique_ptr<RulesetService> ruleset_service) {
ruleset_service_ = std::move(ruleset_service);
}
void ContentRulesetService::IndexAndStoreAndPublishRulesetIfNeeded(
const UnindexedRulesetInfo& unindexed_ruleset_info) {
DCHECK(ruleset_service_);
ruleset_service_->IndexAndStoreAndPublishRulesetIfNeeded(
unindexed_ruleset_info);
}
void ContentRulesetService::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED);
if (!ruleset_data_.IsValid())
return;
SendRulesetToRenderProcess(
&ruleset_data_,
content::Source<content::RenderProcessHost>(source).ptr());
}
} // namespace subresource_filter
|