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_persister.h" | 5 #include "net/reporting/reporting_persister.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/time/clock.h" | 11 #include "base/time/clock.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/timer/timer.h" | 14 #include "base/timer/timer.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "net/reporting/reporting_cache.h" | 16 #include "net/reporting/reporting_cache.h" |
17 #include "net/reporting/reporting_client.h" | 17 #include "net/reporting/reporting_client.h" |
18 #include "net/reporting/reporting_context.h" | 18 #include "net/reporting/reporting_context.h" |
19 #include "net/reporting/reporting_delegate.h" | |
20 #include "net/reporting/reporting_observer.h" | 19 #include "net/reporting/reporting_observer.h" |
21 #include "net/reporting/reporting_policy.h" | 20 #include "net/reporting/reporting_policy.h" |
22 #include "net/reporting/reporting_report.h" | 21 #include "net/reporting/reporting_report.h" |
23 | 22 |
24 namespace net { | 23 namespace net { |
25 namespace { | 24 namespace { |
26 | 25 |
27 std::unique_ptr<base::Value> SerializeOrigin(const url::Origin& origin) { | 26 std::unique_ptr<base::Value> SerializeOrigin(const url::Origin& origin) { |
28 auto serialized = base::MakeUnique<base::DictionaryValue>(); | 27 auto serialized = base::MakeUnique<base::DictionaryValue>(); |
29 | 28 |
(...skipping 24 matching lines...) Expand all Loading... |
54 | 53 |
55 std::string suborigin; | 54 std::string suborigin; |
56 if (!serialized.GetString("suborigin", &suborigin)) | 55 if (!serialized.GetString("suborigin", &suborigin)) |
57 return false; | 56 return false; |
58 | 57 |
59 *origin_out = url::Origin::CreateFromNormalizedTupleWithSuborigin( | 58 *origin_out = url::Origin::CreateFromNormalizedTupleWithSuborigin( |
60 scheme, host, port, suborigin); | 59 scheme, host, port, suborigin); |
61 return true; | 60 return true; |
62 } | 61 } |
63 | 62 |
64 class ReportingPersisterImpl : public ReportingPersister, | 63 class ReportingPersisterImpl : public ReportingPersister { |
65 public ReportingObserver { | |
66 public: | 64 public: |
67 ReportingPersisterImpl(ReportingContext* context) | 65 ReportingPersisterImpl(ReportingContext* context) : context_(context) {} |
68 : context_(context), timer_(base::MakeUnique<base::OneShotTimer>()) {} | |
69 | 66 |
70 // ReportingPersister implementation: | 67 // ReportingPersister implementation: |
71 | 68 |
72 ~ReportingPersisterImpl() override { | 69 ~ReportingPersisterImpl() override {} |
73 DCHECK(context_->initialized()); | |
74 context_->RemoveObserver(this); | |
75 } | |
76 | |
77 void Initialize() override { | |
78 std::unique_ptr<const base::Value> persisted_data = | |
79 context_->delegate()->GetPersistedData(); | |
80 if (persisted_data) | |
81 Deserialize(*persisted_data); | |
82 context_->AddObserver(this); | |
83 } | |
84 | |
85 void SetTimerForTesting(std::unique_ptr<base::Timer> timer) override { | |
86 DCHECK(!context_->initialized()); | |
87 timer_ = std::move(timer); | |
88 } | |
89 | |
90 // ReportingObserver implementation: | |
91 | |
92 void OnCacheUpdated() override { | |
93 DCHECK(context_->initialized()); | |
94 if (!timer_->IsRunning()) | |
95 StartTimer(); | |
96 } | |
97 | 70 |
98 private: | 71 private: |
99 void StartTimer() { | |
100 timer_->Start( | |
101 FROM_HERE, context_->policy().persistence_interval, | |
102 base::Bind(&ReportingPersisterImpl::Persist, base::Unretained(this))); | |
103 } | |
104 | |
105 void Persist() { delegate()->PersistData(Serialize()); } | |
106 | |
107 std::string SerializeTicks(base::TimeTicks time_ticks) { | 72 std::string SerializeTicks(base::TimeTicks time_ticks) { |
108 base::Time time = time_ticks - tick_clock()->NowTicks() + clock()->Now(); | 73 base::Time time = time_ticks - tick_clock()->NowTicks() + clock()->Now(); |
109 return base::Int64ToString(time.ToInternalValue()); | 74 return base::Int64ToString(time.ToInternalValue()); |
110 } | 75 } |
111 | 76 |
112 bool DeserializeTicks(const std::string& serialized, | 77 bool DeserializeTicks(const std::string& serialized, |
113 base::TimeTicks* time_ticks_out) { | 78 base::TimeTicks* time_ticks_out) { |
114 int64_t internal; | 79 int64_t internal; |
115 if (!base::StringToInt64(serialized, &internal)) | 80 if (!base::StringToInt64(serialized, &internal)) |
116 return false; | 81 return false; |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 if (!serialized->GetList("clients", &clients)) | 294 if (!serialized->GetList("clients", &clients)) |
330 return false; | 295 return false; |
331 if (!DeserializeClients(*clients)) | 296 if (!DeserializeClients(*clients)) |
332 return false; | 297 return false; |
333 } | 298 } |
334 | 299 |
335 return true; | 300 return true; |
336 } | 301 } |
337 | 302 |
338 const ReportingPolicy& policy() { return context_->policy(); } | 303 const ReportingPolicy& policy() { return context_->policy(); } |
339 ReportingDelegate* delegate() { return context_->delegate(); } | |
340 base::Clock* clock() { return context_->clock(); } | 304 base::Clock* clock() { return context_->clock(); } |
341 base::TickClock* tick_clock() { return context_->tick_clock(); } | 305 base::TickClock* tick_clock() { return context_->tick_clock(); } |
342 ReportingCache* cache() { return context_->cache(); } | 306 ReportingCache* cache() { return context_->cache(); } |
343 | 307 |
344 ReportingContext* context_; | 308 ReportingContext* context_; |
345 std::unique_ptr<base::Timer> timer_; | |
346 }; | 309 }; |
347 | 310 |
348 } // namespace | 311 } // namespace |
349 | 312 |
350 // static | 313 // static |
351 std::unique_ptr<ReportingPersister> ReportingPersister::Create( | 314 std::unique_ptr<ReportingPersister> ReportingPersister::Create( |
352 ReportingContext* context) { | 315 ReportingContext* context) { |
353 return base::MakeUnique<ReportingPersisterImpl>(context); | 316 return base::MakeUnique<ReportingPersisterImpl>(context); |
354 } | 317 } |
355 | 318 |
356 ReportingPersister::~ReportingPersister() {} | 319 ReportingPersister::~ReportingPersister() {} |
357 | 320 |
358 } // namespace net | 321 } // namespace net |
OLD | NEW |