Chromium Code Reviews| 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 LogSourceAccessManager::SourceAndExtension::SourceAndExtension( | |
| 15 api::feedback_private::LogSource source, | |
| 16 const std::string& extension_id) | |
| 17 : source(source), extension_id(extension_id) {} | |
| 18 | |
| 19 LogSourceAccessManager::LogSourceAccessManager( | |
| 20 const base::TimeDelta& min_time_between_reads) | |
| 21 : min_time_between_reads_(min_time_between_reads), weak_factory_(this) {} | |
| 22 | |
| 23 LogSourceAccessManager::~LogSourceAccessManager() {} | |
| 24 | |
| 25 bool LogSourceAccessManager::AddExtension(const SourceAndExtension& key) { | |
| 26 // emplace() returns a pair where the second element is a flag indicating | |
| 27 // whether the entry was newly added. | |
| 28 return active_keys_.emplace(key).second; | |
| 29 } | |
| 30 | |
| 31 bool LogSourceAccessManager::RemoveExtension(const SourceAndExtension& key) { | |
| 32 auto iter = active_keys_.find(key); | |
| 33 if (iter == active_keys_.end()) | |
| 34 return false; | |
| 35 | |
| 36 active_keys_.erase(iter); | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 size_t LogSourceAccessManager::GetNumActiveExtensionsForSource( | |
| 41 api::feedback_private::LogSource source) const { | |
| 42 size_t count = 0; | |
| 43 // The stored entries are sorted first by source type, then by extension ID. | |
| 44 // We can take advantage of this fact to avoid iterating over all elements. | |
| 45 // Instead start from the first element that matches |source|, and end at the | |
| 46 // first element that does not match |source| anymore. | |
| 47 for (auto iter = active_keys_.lower_bound(SourceAndExtension(source, "")); | |
| 48 iter != active_keys_.end() && iter->source == source; ++iter) { | |
| 49 ++count; | |
| 50 } | |
| 51 return count; | |
| 52 } | |
| 53 | |
| 54 bool LogSourceAccessManager::AccessSourceFromExtension( | |
| 55 const SourceAndExtension& key) { | |
| 56 base::Time last = GetLastExtensionAccessTime(key); | |
| 57 if (last.is_null() || base::Time::Now() > last + min_time_between_reads_) { | |
|
tbarzic
2017/05/25 22:46:45
you should use TimeTicks (as Time is not guarantee
Simon Que
2017/05/26 04:34:38
Done.
| |
| 58 last_access_times_[key] = base::Time::Now(); | |
| 59 return true; | |
| 60 } | |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 base::Time LogSourceAccessManager::GetLastExtensionAccessTime( | |
| 65 const SourceAndExtension& key) const { | |
| 66 auto iter = last_access_times_.find(key); | |
| 67 if (iter == last_access_times_.end()) | |
| 68 return base::Time(); | |
| 69 | |
| 70 return iter->second; | |
| 71 } | |
| 72 | |
| 73 base::Closure LogSourceAccessManager::GetUnregisterCallback( | |
| 74 const SourceAndExtension& key) { | |
| 75 return base::Bind(&LogSourceAccessManager::RemoveExtensionNoReturn, | |
| 76 weak_factory_.GetWeakPtr(), key); | |
| 77 } | |
| 78 | |
| 79 void LogSourceAccessManager::RemoveExtensionNoReturn( | |
| 80 const SourceAndExtension& key) { | |
| 81 RemoveExtension(key); | |
| 82 } | |
| 83 | |
| 84 } // namespace extensions | |
| OLD | NEW |