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