1 // Copyright 2014 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.
4
5 #include "components/domain_reliability/service.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/task_runner_util.h"
12 #include "components/domain_reliability/monitor.h"
13 #include "net/url_request/url_request_context_getter.h"
14
15 namespace domain_reliability {
16
17 namespace {
18
GetWebUIDataOnNetworkTaskRunner(base::WeakPtr<DomainReliabilityMonitor> monitor)19 scoped_ptr<base::Value> GetWebUIDataOnNetworkTaskRunner(
20 base::WeakPtr<DomainReliabilityMonitor> monitor) {
21 if (!monitor) {
22 base::DictionaryValue* dict = new base::DictionaryValue();
23 dict->SetString("error", "no_monitor");
24 return scoped_ptr<base::Value>(dict);
25 }
26
27 return monitor->GetWebUIData();
28 }
29
30 } // namespace
31
32 class DomainReliabilityServiceImpl : public DomainReliabilityService {
33 public:
DomainReliabilityServiceImpl(const std::string & upload_reporter_string)34 explicit DomainReliabilityServiceImpl(
35 const std::string& upload_reporter_string)
36 : upload_reporter_string_(upload_reporter_string) {}
37
~DomainReliabilityServiceImpl()38 virtual ~DomainReliabilityServiceImpl() {}
39
40 // DomainReliabilityService implementation:
41
CreateMonitor(scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)42 virtual scoped_ptr<DomainReliabilityMonitor> CreateMonitor(
43 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)
44 OVERRIDE {
45 DCHECK(!network_task_runner_.get());
46
47 scoped_ptr<DomainReliabilityMonitor> monitor(
48 new DomainReliabilityMonitor(upload_reporter_string_,
49 base::MessageLoopProxy::current(),
50 network_task_runner));
51
52 monitor_ = monitor->MakeWeakPtr();
53 network_task_runner_ = network_task_runner;
54
55 return monitor.Pass();
56 }
57
ClearBrowsingData(DomainReliabilityClearMode clear_mode,const base::Closure & callback)58 virtual void ClearBrowsingData(DomainReliabilityClearMode clear_mode,
59 const base::Closure& callback) OVERRIDE {
60 DCHECK(network_task_runner_.get());
61
62 network_task_runner_->PostTaskAndReply(
63 FROM_HERE,
64 base::Bind(&DomainReliabilityMonitor::ClearBrowsingData,
65 monitor_,
66 clear_mode),
67 callback);
68 }
69
GetWebUIData(const base::Callback<void (scoped_ptr<base::Value>)> & callback) const70 virtual void GetWebUIData(
71 const base::Callback<void(scoped_ptr<base::Value>)>& callback)
72 const OVERRIDE {
73 DCHECK(network_task_runner_.get());
74
75 PostTaskAndReplyWithResult(
76 network_task_runner_.get(),
77 FROM_HERE,
78 base::Bind(&GetWebUIDataOnNetworkTaskRunner, monitor_),
79 callback);
80 }
81
82 private:
83 std::string upload_reporter_string_;
84 base::WeakPtr<DomainReliabilityMonitor> monitor_;
85 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
86 };
87
88 // static
Create(const std::string & upload_reporter_string)89 DomainReliabilityService* DomainReliabilityService::Create(
90 const std::string& upload_reporter_string) {
91 return new DomainReliabilityServiceImpl(upload_reporter_string);
92 }
93
~DomainReliabilityService()94 DomainReliabilityService::~DomainReliabilityService() {}
95
DomainReliabilityService()96 DomainReliabilityService::DomainReliabilityService() {}
97
98 } // namespace domain_reliability
99