| OLD | NEW |
| (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 |
| 12 namespace extensions { |
| 13 |
| 14 namespace { |
| 15 |
| 16 #if defined(OS_CHROMEOS) |
| 17 // The minimum time between consecutive reads of a log source by a particular |
| 18 // extension. |
| 19 const int kDefaultRateLimitingTimeoutMs = 1000; |
| 20 |
| 21 // If this is null, then |kDefaultRateLimitingTimeoutMs| is used as the timeout. |
| 22 const base::TimeDelta* g_rate_limiting_timeout = nullptr; |
| 23 #endif // defined(OS_CHROMEOS) |
| 24 |
| 25 base::TimeDelta GetMinTimeBetweenReads() { |
| 26 return g_rate_limiting_timeout |
| 27 ? *g_rate_limiting_timeout |
| 28 : base::TimeDelta::FromMilliseconds(kDefaultRateLimitingTimeoutMs); |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 LogSourceAccessManager::SourceAndExtension::SourceAndExtension( |
| 34 api::feedback_private::LogSource source, |
| 35 const std::string& extension_id) |
| 36 : source(source), extension_id(extension_id) {} |
| 37 |
| 38 LogSourceAccessManager::LogSourceAccessManager() : weak_factory_(this) {} |
| 39 |
| 40 LogSourceAccessManager::~LogSourceAccessManager() {} |
| 41 |
| 42 // static |
| 43 void LogSourceAccessManager::SetRateLimitingTimeoutForTesting( |
| 44 const base::TimeDelta* timeout) { |
| 45 g_rate_limiting_timeout = timeout; |
| 46 } |
| 47 |
| 48 bool LogSourceAccessManager::AddExtension(const SourceAndExtension& key) { |
| 49 // emplace() returns a pair where the second element is a flag indicating |
| 50 // whether the entry was newly added. |
| 51 return active_keys_.emplace(key).second; |
| 52 } |
| 53 |
| 54 bool LogSourceAccessManager::RemoveExtension(const SourceAndExtension& key) { |
| 55 auto iter = active_keys_.find(key); |
| 56 if (iter == active_keys_.end()) |
| 57 return false; |
| 58 |
| 59 active_keys_.erase(iter); |
| 60 return true; |
| 61 } |
| 62 |
| 63 size_t LogSourceAccessManager::GetNumActiveExtensionsForSource( |
| 64 api::feedback_private::LogSource source) const { |
| 65 size_t count = 0; |
| 66 // The stored entries are sorted first by source type, then by extension ID. |
| 67 // We can take advantage of this fact to avoid iterating over all elements. |
| 68 // Instead start from the first element that matches |source|, and end at the |
| 69 // first element that does not match |source| anymore. |
| 70 for (auto iter = active_keys_.lower_bound(SourceAndExtension(source, "")); |
| 71 iter != active_keys_.end() && iter->source == source; ++iter) { |
| 72 ++count; |
| 73 } |
| 74 return count; |
| 75 } |
| 76 |
| 77 bool LogSourceAccessManager::AccessSourceFromExtension( |
| 78 const SourceAndExtension& key) { |
| 79 base::TimeTicks last = GetLastExtensionAccessTime(key); |
| 80 base::TimeTicks now = tick_clock_->NowTicks(); |
| 81 if (last.is_null() || now > last + GetMinTimeBetweenReads()) { |
| 82 last_access_times_[key] = now; |
| 83 return true; |
| 84 } |
| 85 return false; |
| 86 } |
| 87 |
| 88 base::TimeTicks LogSourceAccessManager::GetLastExtensionAccessTime( |
| 89 const SourceAndExtension& key) const { |
| 90 auto iter = last_access_times_.find(key); |
| 91 if (iter == last_access_times_.end()) |
| 92 return base::TimeTicks(); |
| 93 |
| 94 return iter->second; |
| 95 } |
| 96 |
| 97 base::Closure LogSourceAccessManager::GetUnregisterCallback( |
| 98 const SourceAndExtension& key) { |
| 99 return base::Bind(&LogSourceAccessManager::RemoveExtensionNoReturn, |
| 100 weak_factory_.GetWeakPtr(), key); |
| 101 } |
| 102 |
| 103 void LogSourceAccessManager::RemoveExtensionNoReturn( |
| 104 const SourceAndExtension& key) { |
| 105 RemoveExtension(key); |
| 106 } |
| 107 |
| 108 } // namespace extensions |
| OLD | NEW |