Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: chrome/browser/extensions/api/feedback_private/log_source_access_manager.cc

Issue 2840103002: Add new API function: feedbackPrivate.readLogSource (Closed)
Patch Set: Rebased; updated histogram enum Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/api/feedback_private/log_source_access_manag er.h"
6
7 #include <algorithm>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/time/default_tick_clock.h"
12
13 namespace extensions {
14
15 namespace {
16
17 #if defined(OS_CHROMEOS)
18 // The minimum time between consecutive reads of a log source by a particular
19 // extension.
20 const int kDefaultRateLimitingTimeoutMs = 1000;
21
22 // If this is null, then |kDefaultRateLimitingTimeoutMs| is used as the timeout.
23 const base::TimeDelta* g_rate_limiting_timeout = nullptr;
24 #endif // defined(OS_CHROMEOS)
25
26 base::TimeDelta GetMinTimeBetweenReads() {
27 return g_rate_limiting_timeout
28 ? *g_rate_limiting_timeout
29 : base::TimeDelta::FromMilliseconds(kDefaultRateLimitingTimeoutMs);
30 }
31
32 } // namespace
33
34 LogSourceAccessManager::SourceAndExtension::SourceAndExtension(
35 api::feedback_private::LogSource source,
36 const std::string& extension_id)
37 : source(source), extension_id(extension_id) {}
38
39 LogSourceAccessManager::LogSourceAccessManager()
40 : tick_clock_(new base::DefaultTickClock), weak_factory_(this) {}
41
42 LogSourceAccessManager::~LogSourceAccessManager() {}
43
44 // static
45 void LogSourceAccessManager::SetRateLimitingTimeoutForTesting(
46 const base::TimeDelta* timeout) {
47 g_rate_limiting_timeout = timeout;
48 }
49
50 bool LogSourceAccessManager::AddExtension(const SourceAndExtension& key) {
51 // emplace() returns a pair where the second element is a flag indicating
52 // whether the entry was newly added.
53 return active_keys_.emplace(key).second;
54 }
55
56 bool LogSourceAccessManager::RemoveExtension(const SourceAndExtension& key) {
57 auto iter = active_keys_.find(key);
tbarzic 2017/06/01 19:23:46 I'd prefer something like: return active_keys_.er
Simon Que 2017/06/01 21:49:09 Done.
58 if (iter == active_keys_.end())
59 return false;
60
61 active_keys_.erase(iter);
62 return true;
63 }
64
65 size_t LogSourceAccessManager::GetNumActiveExtensionsForSource(
66 api::feedback_private::LogSource source) const {
67 size_t count = 0;
68 // The stored entries are sorted first by source type, then by extension ID.
69 // We can take advantage of this fact to avoid iterating over all elements.
70 // Instead start from the first element that matches |source|, and end at the
71 // first element that does not match |source| anymore.
72 for (auto iter = active_keys_.lower_bound(SourceAndExtension(source, ""));
73 iter != active_keys_.end() && iter->source == source; ++iter) {
74 ++count;
75 }
76 return count;
77 }
78
79 bool LogSourceAccessManager::AccessSourceFromExtension(
80 const SourceAndExtension& key) {
81 base::TimeTicks last = GetLastExtensionAccessTime(key);
82 base::TimeTicks now = tick_clock_->NowTicks();
83 if (last.is_null() || now >= last + GetMinTimeBetweenReads()) {
84 last_access_times_[key] = now;
85 return true;
86 }
87 return false;
tbarzic 2017/06/01 19:23:46 nit: I'd switch this as: if (!last.is_null() && no
Simon Que 2017/06/01 21:49:09 Done.
88 }
89
90 base::TimeTicks LogSourceAccessManager::GetLastExtensionAccessTime(
91 const SourceAndExtension& key) const {
92 auto iter = last_access_times_.find(key);
93 if (iter == last_access_times_.end())
94 return base::TimeTicks();
95
96 return iter->second;
97 }
98
99 base::Closure LogSourceAccessManager::GetUnregisterCallback(
100 const SourceAndExtension& key) {
101 return base::Bind(&LogSourceAccessManager::RemoveExtensionNoReturn,
102 weak_factory_.GetWeakPtr(), key);
103 }
104
105 void LogSourceAccessManager::RemoveExtensionNoReturn(
106 const SourceAndExtension& key) {
107 RemoveExtension(key);
108 }
109
110 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698