OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "net/reporting/reporting_service.h" | 5 #include "net/reporting/reporting_service.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/time/tick_clock.h" | 12 #include "base/time/tick_clock.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "net/reporting/reporting_cache.h" | 15 #include "net/reporting/reporting_cache.h" |
16 #include "net/reporting/reporting_context.h" | 16 #include "net/reporting/reporting_context.h" |
17 #include "net/reporting/reporting_delegate.h" | 17 #include "net/reporting/reporting_delegate.h" |
18 #include "net/reporting/reporting_header_parser.h" | 18 #include "net/reporting/reporting_header_parser.h" |
19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 class ReportingServiceImpl : public ReportingService { | 25 class ReportingServiceImpl : public ReportingService { |
26 public: | 26 public: |
27 ReportingServiceImpl(std::unique_ptr<ReportingContext> context) | 27 ReportingServiceImpl(std::unique_ptr<ReportingContext> context) |
28 : context_(std::move(context)) {} | 28 : context_(std::move(context)) { |
| 29 // TODO(juliatuttle): This can be slow, so it might be better to expose it |
| 30 // as a separate method and call it separately from constructing everything. |
| 31 context_->Initialize(); |
| 32 } |
29 | 33 |
30 ~ReportingServiceImpl() override {} | 34 ~ReportingServiceImpl() override {} |
31 | 35 |
32 void QueueReport(const GURL& url, | 36 void QueueReport(const GURL& url, |
33 const std::string& group, | 37 const std::string& group, |
34 const std::string& type, | 38 const std::string& type, |
35 std::unique_ptr<const base::Value> body) override { | 39 std::unique_ptr<const base::Value> body) override { |
| 40 DCHECK(context_->initialized()); |
36 context_->cache()->AddReport(url, group, type, std::move(body), | 41 context_->cache()->AddReport(url, group, type, std::move(body), |
37 context_->tick_clock()->NowTicks(), 0); | 42 context_->tick_clock()->NowTicks(), 0); |
38 } | 43 } |
39 | 44 |
40 void ProcessHeader(const GURL& url, | 45 void ProcessHeader(const GURL& url, |
41 const std::string& header_value) override { | 46 const std::string& header_value) override { |
| 47 DCHECK(context_->initialized()); |
42 ReportingHeaderParser::ParseHeader(context_.get(), url, header_value); | 48 ReportingHeaderParser::ParseHeader(context_.get(), url, header_value); |
43 } | 49 } |
44 | 50 |
45 private: | 51 private: |
46 std::unique_ptr<ReportingContext> context_; | 52 std::unique_ptr<ReportingContext> context_; |
47 | 53 |
48 DISALLOW_COPY_AND_ASSIGN(ReportingServiceImpl); | 54 DISALLOW_COPY_AND_ASSIGN(ReportingServiceImpl); |
49 }; | 55 }; |
50 | 56 |
51 } // namespace | 57 } // namespace |
52 | 58 |
53 ReportingService::~ReportingService() {} | 59 ReportingService::~ReportingService() {} |
54 | 60 |
55 // static | 61 // static |
56 std::unique_ptr<ReportingService> ReportingService::Create( | 62 std::unique_ptr<ReportingService> ReportingService::Create( |
57 const ReportingPolicy& policy, | 63 const ReportingPolicy& policy, |
58 URLRequestContext* request_context, | 64 URLRequestContext* request_context, |
59 std::unique_ptr<ReportingDelegate> delegate) { | 65 std::unique_ptr<ReportingDelegate> delegate) { |
60 return base::MakeUnique<ReportingServiceImpl>( | 66 return base::MakeUnique<ReportingServiceImpl>( |
61 ReportingContext::Create(policy, std::move(delegate), request_context)); | 67 ReportingContext::Create(policy, std::move(delegate), request_context)); |
62 } | 68 } |
63 | 69 |
64 // static | 70 // static |
65 std::unique_ptr<ReportingService> ReportingService::CreateForTesting( | 71 std::unique_ptr<ReportingService> ReportingService::CreateForTesting( |
66 std::unique_ptr<ReportingContext> reporting_context) { | 72 std::unique_ptr<ReportingContext> reporting_context) { |
67 return base::MakeUnique<ReportingServiceImpl>(std::move(reporting_context)); | 73 return base::MakeUnique<ReportingServiceImpl>(std::move(reporting_context)); |
68 } | 74 } |
69 | 75 |
70 } // namespace net | 76 } // namespace net |
OLD | NEW |