1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/domain_reliability/context_manager.h"
7 namespace domain_reliability
{
9 DomainReliabilityContextManager::DomainReliabilityContextManager(
10 DomainReliabilityContext::Factory
* context_factory
)
11 : context_factory_(context_factory
) {
14 DomainReliabilityContextManager::~DomainReliabilityContextManager() {
18 void DomainReliabilityContextManager::RouteBeacon(
20 const DomainReliabilityBeacon
& beacon
) {
21 DomainReliabilityContext
* context
= GetContextForHost(url
.host());
25 context
->OnBeacon(url
, beacon
);
28 void DomainReliabilityContextManager::ClearBeaconsInAllContexts() {
29 for (auto& context_entry
: contexts_
)
30 context_entry
.second
->ClearBeacons();
33 DomainReliabilityContext
* DomainReliabilityContextManager::AddContextForConfig(
34 scoped_ptr
<const DomainReliabilityConfig
> config
) {
35 std::string domain
= config
->domain
;
36 scoped_ptr
<DomainReliabilityContext
> context
=
37 context_factory_
->CreateContextForConfig(config
.Pass());
38 DomainReliabilityContext
** entry
= &contexts_
[domain
];
41 *entry
= context
.release();
45 void DomainReliabilityContextManager::RemoveAllContexts() {
46 STLDeleteContainerPairSecondPointers(
47 contexts_
.begin(), contexts_
.end());
51 scoped_ptr
<base::Value
> DomainReliabilityContextManager::GetWebUIData() const {
52 scoped_ptr
<base::ListValue
> contexts_value(new base::ListValue());
53 for (const auto& context_entry
: contexts_
)
54 contexts_value
->Append(context_entry
.second
->GetWebUIData().release());
55 return contexts_value
.Pass();
58 DomainReliabilityContext
* DomainReliabilityContextManager::GetContextForHost(
59 const std::string
& host
) {
60 ContextMap::const_iterator context_it
;
62 context_it
= contexts_
.find(host
);
63 if (context_it
!= contexts_
.end())
64 return context_it
->second
;
66 std::string host_with_asterisk
= "*." + host
;
67 context_it
= contexts_
.find(host_with_asterisk
);
68 if (context_it
!= contexts_
.end())
69 return context_it
->second
;
71 size_t dot_pos
= host
.find('.');
72 if (dot_pos
== std::string::npos
)
75 // TODO(ttuttle): Make sure parent is not in PSL before using.
77 std::string parent_with_asterisk
= "*." + host
.substr(dot_pos
+ 1);
78 context_it
= contexts_
.find(parent_with_asterisk
);
79 if (context_it
!= contexts_
.end())
80 return context_it
->second
;
85 } // namespace domain_reliability