1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_RESOURCE_COORDINATOR_LIFECYCLE_UNIT_H_
#define CHROME_BROWSER_RESOURCE_COORDINATOR_LIFECYCLE_UNIT_H_
#include <stdint.h>
#include <string>
#include <vector>
#include "base/containers/flat_set.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "chrome/browser/resource_coordinator/decision_details.h"
#include "chrome/browser/resource_coordinator/lifecycle_unit_state.mojom-forward.h"
#include "content/public/browser/visibility.h"
namespace resource_coordinator {
using ::mojom::LifecycleUnitDiscardReason;
using ::mojom::LifecycleUnitLoadingState;
using ::mojom::LifecycleUnitState;
class DecisionDetails;
class LifecycleUnitObserver;
class LifecycleUnitSource;
class TabLifecycleUnitExternal;
// A LifecycleUnit represents a unit that can switch between the "loaded" and
// "discarded" states. When it is loaded, the unit uses system resources and
// provides functionality to the user. When it is discarded, the unit doesn't
// use any system resource.
class LifecycleUnit {
public:
// Used to sort LifecycleUnit by importance using the last focused time.
// The most important LifecycleUnit has the greatest SortKey.
struct SortKey {
SortKey();
// Creates a SortKey based on the LifecycleUnit's last focused time.
explicit SortKey(base::TimeTicks last_focused_time);
SortKey(const SortKey& other);
SortKey& operator=(const SortKey& other);
bool operator<(const SortKey& other) const;
bool operator>(const SortKey& other) const;
// Last time at which the LifecycleUnit was focused. base::TimeTicks::Max()
// if the LifecycleUnit is currently focused.
base::TimeTicks last_focused_time;
};
virtual ~LifecycleUnit();
// Returns the LifecycleUnitSource associated with this unit.
virtual LifecycleUnitSource* GetSource() const = 0;
// Returns the TabLifecycleUnitExternal associated with this LifecycleUnit, if
// any.
virtual TabLifecycleUnitExternal* AsTabLifecycleUnitExternal() = 0;
// Returns a unique id representing this LifecycleUnit.
virtual int32_t GetID() const = 0;
// Returns the last time ticks at which the LifecycleUnit was focused, or
// base::TimeTicks::Max() if the LifecycleUnit is currently focused.
virtual base::TimeTicks GetLastFocusedTimeTicks() const = 0;
// Returns the last time at which the LifecycleUnit was focused, or
// base::Time::Max() if the LifecycleUnit is currently focused.
virtual base::Time GetLastFocusedTime() const = 0;
// Returns the loading state associated with a LifecycleUnit.
virtual LifecycleUnitLoadingState GetLoadingState() const = 0;
// Returns a key that can be used to evaluate the relative importance of this
// LifecycleUnit. This key may not be trivial to calculate, so this should not
// be called repeatedly if the value will be reused, e.g. during a sort.
//
// TODO(fdoray): Figure out if GetSortKey() and CanDiscard() should be
// replaced with a method that returns a numeric value representing the
// expected user pain caused by a discard. A values above a given threshold
// would be equivalent to CanDiscard() returning false for a given
// mojom::LifecycleUnitDiscardReason. https://crbug.com/775644
virtual SortKey GetSortKey() const = 0;
// Returns the current state of this LifecycleUnit.
virtual LifecycleUnitState GetState() const = 0;
// Returns the last time at which the state of this LifecycleUnit changed.
virtual base::TimeTicks GetStateChangeTime() const = 0;
// Request that the LifecycleUnit be loaded, return true if the request is
// successful.
virtual bool Load() = 0;
// Returns true if this LifecycleUnit can be discarded. Full details regarding
// the policy decision are recorded in the |decision_details|, for logging.
// Returning false but with an empty |decision_details| means the transition
// is not possible for a trivial reason that doesn't need to be reported
// (ie, the page is already discarded).
virtual bool CanDiscard(LifecycleUnitDiscardReason reason,
DecisionDetails* decision_details) const = 0;
// Discards this LifecycleUnit.
//
// TODO(fdoray): Consider handling urgent discard with groups of
// LifecycleUnits. On urgent discard, we want to minimize memory accesses. It
// is easier to achieve that if we discard a group of LifecycleUnits that live
// in the same process(es) than if we discard individual LifecycleUnits.
// https://crbug.com/775644
virtual bool Discard(LifecycleUnitDiscardReason discard_reason,
uint64_t resident_set_size_estimate = 0) = 0;
// Returns the number of times this lifecycle unit has been discarded.
virtual size_t GetDiscardCount() const = 0;
// Returns the most recent discard reason that was applied to this lifecycle
// unit. This only makes sense if the lifecycle unit has ever been discarded.
virtual LifecycleUnitDiscardReason GetDiscardReason() const = 0;
// Adds/removes an observer to this LifecycleUnit.
virtual void AddObserver(LifecycleUnitObserver* observer) = 0;
virtual void RemoveObserver(LifecycleUnitObserver* observer) = 0;
};
using LifecycleUnitSet =
base::flat_set<raw_ptr<LifecycleUnit, CtnExperimental>>;
using LifecycleUnitVector =
std::vector<raw_ptr<LifecycleUnit, VectorExperimental>>;
} // namespace resource_coordinator
#endif // CHROME_BROWSER_RESOURCE_COORDINATOR_LIFECYCLE_UNIT_H_
|