| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/subresource_filter/content/browser/content_ruleset_service_
delegate.h" | 5 #include "components/subresource_filter/content/browser/content_ruleset_service.
h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "components/subresource_filter/content/common/subresource_filter_messag
es.h" | 11 #include "components/subresource_filter/content/common/subresource_filter_messag
es.h" |
| 12 #include "components/subresource_filter/core/browser/ruleset_service.h" |
| 12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/notification_service.h" | 14 #include "content/public/browser/notification_service.h" |
| 14 #include "content/public/browser/notification_source.h" | 15 #include "content/public/browser/notification_source.h" |
| 15 #include "content/public/browser/notification_types.h" | 16 #include "content/public/browser/notification_types.h" |
| 16 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 17 #include "ipc/ipc_platform_file.h" | 18 #include "ipc/ipc_platform_file.h" |
| 18 | 19 |
| 19 namespace subresource_filter { | 20 namespace subresource_filter { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 35 // Posts the |file| handle to the file thread so it can be closed. | 36 // Posts the |file| handle to the file thread so it can be closed. |
| 36 void CloseFileOnFileThread(base::File* file) { | 37 void CloseFileOnFileThread(base::File* file) { |
| 37 if (!file->IsValid()) | 38 if (!file->IsValid()) |
| 38 return; | 39 return; |
| 39 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, | 40 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, |
| 40 base::Bind(&CloseFile, base::Passed(file))); | 41 base::Bind(&CloseFile, base::Passed(file))); |
| 41 } | 42 } |
| 42 | 43 |
| 43 } // namespace | 44 } // namespace |
| 44 | 45 |
| 45 ContentRulesetServiceDelegate::ContentRulesetServiceDelegate() { | 46 ContentRulesetService::ContentRulesetService() { |
| 46 // Must rely on notifications as RenderProcessHostObserver::RenderProcessReady | 47 // Must rely on notifications as RenderProcessHostObserver::RenderProcessReady |
| 47 // would only be called after queued IPC messages (potentially triggering a | 48 // would only be called after queued IPC messages (potentially triggering a |
| 48 // navigation) had already been sent to the new renderer. | 49 // navigation) had already been sent to the new renderer. |
| 49 notification_registrar_.Add( | 50 notification_registrar_.Add( |
| 50 this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 51 this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 51 content::NotificationService::AllBrowserContextsAndSources()); | 52 content::NotificationService::AllBrowserContextsAndSources()); |
| 52 } | 53 } |
| 53 | 54 |
| 54 ContentRulesetServiceDelegate::~ContentRulesetServiceDelegate() { | 55 ContentRulesetService::~ContentRulesetService() { |
| 55 CloseFileOnFileThread(&ruleset_data_); | 56 CloseFileOnFileThread(&ruleset_data_); |
| 56 } | 57 } |
| 57 | 58 |
| 58 void ContentRulesetServiceDelegate::SetRulesetPublishedCallbackForTesting( | 59 void ContentRulesetService::SetRulesetPublishedCallbackForTesting( |
| 59 base::Closure callback) { | 60 base::Closure callback) { |
| 60 ruleset_published_callback_ = callback; | 61 ruleset_published_callback_ = callback; |
| 61 } | 62 } |
| 62 | 63 |
| 63 void ContentRulesetServiceDelegate::PostAfterStartupTask(base::Closure task) { | 64 void ContentRulesetService::PostAfterStartupTask(base::Closure task) { |
| 64 content::BrowserThread::PostAfterStartupTask( | 65 content::BrowserThread::PostAfterStartupTask( |
| 65 FROM_HERE, content::BrowserThread::GetTaskRunnerForThread( | 66 FROM_HERE, |
| 66 content::BrowserThread::UI), | 67 content::BrowserThread::GetTaskRunnerForThread( |
| 68 content::BrowserThread::UI), |
| 67 task); | 69 task); |
| 68 } | 70 } |
| 69 | 71 |
| 70 void ContentRulesetServiceDelegate::PublishNewRulesetVersion( | 72 void ContentRulesetService::PublishNewRulesetVersion(base::File ruleset_data) { |
| 71 base::File ruleset_data) { | |
| 72 DCHECK(ruleset_data.IsValid()); | 73 DCHECK(ruleset_data.IsValid()); |
| 73 CloseFileOnFileThread(&ruleset_data_); | 74 CloseFileOnFileThread(&ruleset_data_); |
| 74 ruleset_data_ = std::move(ruleset_data); | 75 ruleset_data_ = std::move(ruleset_data); |
| 75 for (auto it = content::RenderProcessHost::AllHostsIterator(); !it.IsAtEnd(); | 76 for (auto it = content::RenderProcessHost::AllHostsIterator(); !it.IsAtEnd(); |
| 76 it.Advance()) { | 77 it.Advance()) { |
| 77 SendRulesetToRenderProcess(&ruleset_data_, it.GetCurrentValue()); | 78 SendRulesetToRenderProcess(&ruleset_data_, it.GetCurrentValue()); |
| 78 } | 79 } |
| 79 | 80 |
| 80 if (!ruleset_published_callback_.is_null()) | 81 if (!ruleset_published_callback_.is_null()) |
| 81 ruleset_published_callback_.Run(); | 82 ruleset_published_callback_.Run(); |
| 82 } | 83 } |
| 83 | 84 |
| 84 void ContentRulesetServiceDelegate::Observe( | 85 void ContentRulesetService::set_ruleset_service( |
| 86 std::unique_ptr<RulesetService> ruleset_service) { |
| 87 ruleset_service_ = std::move(ruleset_service); |
| 88 } |
| 89 |
| 90 void ContentRulesetService::IndexAndStoreAndPublishRulesetIfNeeded( |
| 91 const UnindexedRulesetInfo& unindexed_ruleset_info) { |
| 92 DCHECK(ruleset_service_); |
| 93 ruleset_service_->IndexAndStoreAndPublishRulesetIfNeeded( |
| 94 unindexed_ruleset_info); |
| 95 } |
| 96 |
| 97 void ContentRulesetService::Observe( |
| 85 int type, | 98 int type, |
| 86 const content::NotificationSource& source, | 99 const content::NotificationSource& source, |
| 87 const content::NotificationDetails& details) { | 100 const content::NotificationDetails& details) { |
| 88 DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED); | 101 DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED); |
| 89 if (!ruleset_data_.IsValid()) | 102 if (!ruleset_data_.IsValid()) |
| 90 return; | 103 return; |
| 91 SendRulesetToRenderProcess( | 104 SendRulesetToRenderProcess( |
| 92 &ruleset_data_, | 105 &ruleset_data_, |
| 93 content::Source<content::RenderProcessHost>(source).ptr()); | 106 content::Source<content::RenderProcessHost>(source).ptr()); |
| 94 } | 107 } |
| 95 | 108 |
| 96 } // namespace subresource_filter | 109 } // namespace subresource_filter |
| OLD | NEW |