| Index: chrome/browser/extensions/api/feedback_private/log_source_access_manager.cc
|
| diff --git a/chrome/browser/extensions/api/feedback_private/log_source_access_manager.cc b/chrome/browser/extensions/api/feedback_private/log_source_access_manager.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..65a85ef01f1cf943513756180ade3761afde23ee
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/feedback_private/log_source_access_manager.cc
|
| @@ -0,0 +1,72 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/extensions/api/feedback_private/log_source_access_manager.h"
|
| +
|
| +#include <algorithm>
|
| +#include <utility>
|
| +
|
| +#include "base/bind.h"
|
| +
|
| +namespace extensions {
|
| +
|
| +LogSourceAccessManager::LogSourceAccessManager() : weak_factory_(this) {}
|
| +
|
| +LogSourceAccessManager::~LogSourceAccessManager() {}
|
| +
|
| +bool LogSourceAccessManager::AddExtension(const SourceAndExtension& key) {
|
| + // emplace() returns a pair where the second element is a flag indicating
|
| + // whether the entry was newly added.
|
| + return active_keys_.emplace(key).second;
|
| +}
|
| +
|
| +bool LogSourceAccessManager::RemoveExtension(const SourceAndExtension& key) {
|
| + auto iter = active_keys_.find(key);
|
| + if (iter == active_keys_.end())
|
| + return false;
|
| +
|
| + active_keys_.erase(iter);
|
| + return true;
|
| +}
|
| +
|
| +size_t LogSourceAccessManager::GetNumActiveExtensionsForSource(
|
| + api::feedback_private::LogSource source) const {
|
| + size_t count = 0;
|
| + // The stored entries are sorted first by source type, then by extension ID.
|
| + // We can take advantage of this fact to avoid iterating over all elements.
|
| + // Instead start from the first element that matches |source|, and end at the
|
| + // first element that does not match |source| anymore.
|
| + for (auto iter = active_keys_.lower_bound(std::make_pair(source, ""));
|
| + iter != active_keys_.end() && iter->first == source; ++iter) {
|
| + ++count;
|
| + }
|
| + return count;
|
| +}
|
| +
|
| +void LogSourceAccessManager::AccessSourceFromExtension(
|
| + const SourceAndExtension& key) {
|
| + last_access_times_[key] = base::Time::Now();
|
| +}
|
| +
|
| +base::Time LogSourceAccessManager::GetLastExtensionAccessTime(
|
| + const SourceAndExtension& key) const {
|
| + auto iter = last_access_times_.find(key);
|
| + if (iter == last_access_times_.end())
|
| + return base::Time();
|
| +
|
| + return iter->second;
|
| +}
|
| +
|
| +base::Closure LogSourceAccessManager::GetUnregisterCallback(
|
| + const SourceAndExtension& key) {
|
| + return base::Bind(&LogSourceAccessManager::RemoveExtensionNoReturn,
|
| + weak_factory_.GetWeakPtr(), key);
|
| +}
|
| +
|
| +void LogSourceAccessManager::RemoveExtensionNoReturn(
|
| + const SourceAndExtension& key) {
|
| + RemoveExtension(key);
|
| +}
|
| +
|
| +} // namespace extensions
|
|
|