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

Unified Diff: components/framelet/browser/resource_usage_reporter.h

Issue 1602663003: Framelet Prototype 2016 using Mojo IPC Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Disabled oilpan Created 4 years, 11 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: components/framelet/browser/resource_usage_reporter.h
diff --git a/components/framelet/browser/resource_usage_reporter.h b/components/framelet/browser/resource_usage_reporter.h
new file mode 100644
index 0000000000000000000000000000000000000000..94d4e8857a27734b4b5a830814033fb7ce9e5ead
--- /dev/null
+++ b/components/framelet/browser/resource_usage_reporter.h
@@ -0,0 +1,68 @@
+// Copyright 2015 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.
+
+#ifndef COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_REPORTER_H_
+#define COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_REPORTER_H_
+
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "components/framelet/browser/resource_usage_range.h"
+
+namespace framelet {
+
+template <class T>
+class ResourceUsageReporterClient;
+
+enum class ResourceUsageLevel : int {
+ LOW = 0,
+ MEDIUM,
+ HIGH,
+ CRITICAL,
+ MAX_VALUE
+};
+
+template <class T>
+class ResourceUsageReporter {
+ public:
+ explicit ResourceUsageReporter(ResourceUsageReporterClient<T>* client)
+ : client_(client), last_reported_usage_level_(ResourceUsageLevel::LOW) {
+ for (int usage_level = static_cast<int>(ResourceUsageLevel::LOW);
+ usage_level < static_cast<int>(ResourceUsageLevel::MAX_VALUE);
+ ++usage_level) {
+ usage_levels_[usage_level] = std::move(client_->GetRangeForUsageLevel(
+ static_cast<ResourceUsageLevel>(usage_level)));
+ }
+ }
+
+ ~ResourceUsageReporter() {}
+
+ void ReportUsage(T usage) {
+ int usage_level = static_cast<int>(last_reported_usage_level_);
+ // TODO(fsamuel): This could be an infinite loop if the ranges are not
+ // monotonically increasing in high value and low value.
+ while (!usage_levels_[usage_level]->Contains(usage)) {
+ if (usage_levels_[usage_level]->IsLowerThan(usage)) {
+ ++usage_level;
+ } else {
+ --usage_level;
+ }
+ }
+ if (static_cast<int>(last_reported_usage_level_) != usage_level) {
+ last_reported_usage_level_ = static_cast<ResourceUsageLevel>(usage_level);
+ client_->OnResourceUsageLevelChanged(last_reported_usage_level_);
+ }
+ }
+
+ private:
+ ResourceUsageReporterClient<T>* const client_;
+ scoped_ptr<ResourceUsageRange<T>>
+ usage_levels_[static_cast<int>(ResourceUsageLevel::MAX_VALUE)];
+ ResourceUsageLevel last_reported_usage_level_;
+
+ DISALLOW_COPY_AND_ASSIGN(ResourceUsageReporter);
+};
+
+} // namespace framelet
+
+#endif // COMPONENTS_FRAMELET_BROWSER_RESOURCE_USAGE_REPORTER_H_
« no previous file with comments | « components/framelet/browser/resource_usage_range.h ('k') | components/framelet/browser/resource_usage_reporter_base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698