Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / components / domain_reliability / service.cc
blob4253f45a74988d0352da664c86aa1bc38529848d
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.
5 #include "components/domain_reliability/service.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/task_runner_util.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "components/domain_reliability/monitor.h"
14 #include "net/url_request/url_request_context_getter.h"
16 namespace domain_reliability {
18 namespace {
20 scoped_ptr<base::Value> GetWebUIDataOnNetworkTaskRunner(
21 base::WeakPtr<DomainReliabilityMonitor> monitor) {
22 if (!monitor) {
23 base::DictionaryValue* dict = new base::DictionaryValue();
24 dict->SetString("error", "no_monitor");
25 return scoped_ptr<base::Value>(dict);
28 return monitor->GetWebUIData();
31 } // namespace
33 class DomainReliabilityServiceImpl : public DomainReliabilityService {
34 public:
35 explicit DomainReliabilityServiceImpl(
36 const std::string& upload_reporter_string)
37 : upload_reporter_string_(upload_reporter_string) {}
39 ~DomainReliabilityServiceImpl() override {}
41 // DomainReliabilityService implementation:
43 scoped_ptr<DomainReliabilityMonitor> CreateMonitor(
44 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)
45 override {
46 DCHECK(!network_task_runner_.get());
48 scoped_ptr<DomainReliabilityMonitor> monitor(new DomainReliabilityMonitor(
49 upload_reporter_string_, base::ThreadTaskRunnerHandle::Get(),
50 network_task_runner));
52 monitor_ = monitor->MakeWeakPtr();
53 network_task_runner_ = network_task_runner;
55 return monitor.Pass();
58 void ClearBrowsingData(DomainReliabilityClearMode clear_mode,
59 const base::Closure& callback) override {
60 DCHECK(network_task_runner_.get());
62 network_task_runner_->PostTaskAndReply(
63 FROM_HERE,
64 base::Bind(&DomainReliabilityMonitor::ClearBrowsingData,
65 monitor_,
66 clear_mode),
67 callback);
70 void GetWebUIData(const base::Callback<void(scoped_ptr<base::Value>)>&
71 callback) const override {
72 DCHECK(network_task_runner_.get());
74 PostTaskAndReplyWithResult(
75 network_task_runner_.get(),
76 FROM_HERE,
77 base::Bind(&GetWebUIDataOnNetworkTaskRunner, monitor_),
78 callback);
81 private:
82 std::string upload_reporter_string_;
83 base::WeakPtr<DomainReliabilityMonitor> monitor_;
84 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
87 // static
88 DomainReliabilityService* DomainReliabilityService::Create(
89 const std::string& upload_reporter_string) {
90 return new DomainReliabilityServiceImpl(upload_reporter_string);
93 DomainReliabilityService::~DomainReliabilityService() {}
95 DomainReliabilityService::DomainReliabilityService() {}
97 } // namespace domain_reliability