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

Unified Diff: chrome/browser/extensions/api/feedback_private/log_source_access_manager.cc

Issue 2840103002: Add new API function: feedbackPrivate.readLogSource (Closed)
Patch Set: Addressed comments from Patch Set 4 Created 3 years, 7 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 side-by-side diff with in-line comments
Download patch
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..7bea4410ee9892a7d3915728374ed87a178fbaa6
--- /dev/null
+++ b/chrome/browser/extensions/api/feedback_private/log_source_access_manager.cc
@@ -0,0 +1,84 @@
+// 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::SourceAndExtension::SourceAndExtension(
+ api::feedback_private::LogSource source,
+ const std::string& extension_id)
+ : source(source), extension_id(extension_id) {}
+
+LogSourceAccessManager::LogSourceAccessManager(
+ const base::TimeDelta& min_time_between_reads)
+ : min_time_between_reads_(min_time_between_reads), 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(SourceAndExtension(source, ""));
+ iter != active_keys_.end() && iter->source == source; ++iter) {
+ ++count;
+ }
+ return count;
+}
+
+bool LogSourceAccessManager::AccessSourceFromExtension(
+ const SourceAndExtension& key) {
+ base::Time last = GetLastExtensionAccessTime(key);
+ 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.
+ last_access_times_[key] = base::Time::Now();
+ return true;
+ }
+ return false;
+}
+
+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

Powered by Google App Engine
This is Rietveld 408576698