| OLD | NEW | 
|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #ifndef CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | 5 #ifndef CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | 
| 6 #define CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | 6 #define CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | 
| 7 | 7 | 
| 8 #include "base/memory/memory_coordinator_client_registry.h" | 8 #include "base/memory/memory_coordinator_client_registry.h" | 
| 9 #include "base/memory/singleton.h" |  | 
| 10 #include "base/process/process_handle.h" | 9 #include "base/process/process_handle.h" | 
| 11 #include "content/common/content_export.h" | 10 #include "content/common/content_export.h" | 
| 12 #include "content/common/memory_coordinator.mojom.h" | 11 #include "content/common/memory_coordinator.mojom.h" | 
| 13 #include "content/public/browser/memory_coordinator_delegate.h" | 12 #include "content/public/browser/memory_coordinator_delegate.h" | 
| 14 #include "mojo/public/cpp/bindings/binding.h" | 13 #include "mojo/public/cpp/bindings/binding.h" | 
| 15 | 14 | 
| 16 namespace content { | 15 namespace content { | 
| 17 | 16 | 
| 18 // NOTE: Memory coordinator is under development and not fully working. | 17 // NOTE: Memory coordinator is under development and not fully working. | 
| 19 // TODO(bashi): Add more explanations when we implement memory coordinator V0. | 18 // TODO(bashi): Add more explanations when we implement memory coordinator V0. | 
| 20 | 19 | 
| 21 class MemoryCoordinatorHandleImpl; | 20 class MemoryCoordinatorHandleImpl; | 
| 22 | 21 | 
| 23 // MemoryCoordinator is responsible for the whole memory management accross the | 22 // MemoryCoordinator is responsible for the whole memory management accross the | 
| 24 // browser and child proceeses. It dispatches memory events to its clients and | 23 // browser and child proceeses. It dispatches memory events to its clients and | 
| 25 // child processes based on its best knowledge of the memory usage. | 24 // child processes based on its best knowledge of the memory usage. | 
| 26 class CONTENT_EXPORT MemoryCoordinator { | 25 class CONTENT_EXPORT MemoryCoordinator { | 
| 27  public: | 26  public: | 
| 28   ~MemoryCoordinator(); | 27   virtual ~MemoryCoordinator(); | 
| 29 | 28 | 
| 30   // Singleton factory/accessor. | 29   // Singleton factory/accessor. | 
| 31   static MemoryCoordinator* GetInstance(); | 30   static MemoryCoordinator* GetInstance(); | 
| 32 | 31 | 
|  | 32   // Starts monitoring memory usage. After calling this method, memory | 
|  | 33   // coordinator will start dispatching state changes. | 
|  | 34   virtual void Start() {} | 
|  | 35 | 
| 33   // Creates a handle to the provided child process. | 36   // Creates a handle to the provided child process. | 
| 34   void CreateHandle(int render_process_id, | 37   void CreateHandle(int render_process_id, | 
| 35                     mojom::MemoryCoordinatorHandleRequest request); | 38                     mojom::MemoryCoordinatorHandleRequest request); | 
| 36 | 39 | 
| 37   // Returns number of children. Only used for testing. | 40   // Returns number of children. Only used for testing. | 
| 38   size_t NumChildrenForTesting(); | 41   size_t NumChildrenForTesting(); | 
| 39 | 42 | 
| 40   // Dispatches a memory state change to the provided process. Returns true if | 43   // Dispatches a memory state change to the provided process. Returns true if | 
| 41   // the process is tracked by this coordinator and successfully dispatches, | 44   // the process is tracked by this coordinator and successfully dispatches, | 
| 42   // returns false otherwise. | 45   // returns false otherwise. | 
| 43   bool SetMemoryState( | 46   bool SetMemoryState( | 
| 44       int render_process_id, mojom::MemoryState memory_state); | 47       int render_process_id, mojom::MemoryState memory_state); | 
| 45 | 48 | 
| 46   // Returns the memory state of the specified render process. Returns UNKNOWN | 49   // Returns the memory state of the specified render process. Returns UNKNOWN | 
| 47   // if the process is not tracked by this coordinator. | 50   // if the process is not tracked by this coordinator. | 
| 48   mojom::MemoryState GetMemoryState(int render_process_id) const; | 51   mojom::MemoryState GetMemoryState(int render_process_id) const; | 
| 49 | 52 | 
|  | 53   // Called when ChildMemoryCoordinator calls AddChild(). | 
|  | 54   virtual void OnChildAdded(int render_process_id) {} | 
|  | 55 | 
| 50  protected: | 56  protected: | 
| 51   // Constructor. Protected as this is a singleton, but accessible for | 57   // Constructor. Protected as this is a singleton, but accessible for | 
| 52   // unittests. | 58   // unittests. | 
| 53   MemoryCoordinator(); | 59   MemoryCoordinator(); | 
| 54 | 60 | 
| 55   // Adds the given ChildMemoryCoordinator as a child of this coordinator. | 61   // Adds the given ChildMemoryCoordinator as a child of this coordinator. | 
| 56   void AddChildForTesting(int dummy_render_process_id, | 62   void AddChildForTesting(int dummy_render_process_id, | 
| 57                           mojom::ChildMemoryCoordinatorPtr child); | 63                           mojom::ChildMemoryCoordinatorPtr child); | 
| 58 | 64 | 
| 59   // Callback invoked by mojo when the child connection goes down. Exposed | 65   // Callback invoked by mojo when the child connection goes down. Exposed | 
| 60   // for testing. | 66   // for testing. | 
| 61   void OnConnectionError(int render_process_id); | 67   void OnConnectionError(int render_process_id); | 
| 62 | 68 | 
|  | 69   // Returns true when a given renderer can be throttled. | 
|  | 70   bool CanThrottleRenderer(int render_process_id); | 
|  | 71 | 
| 63   // Returns true when a given renderer can be suspended. | 72   // Returns true when a given renderer can be suspended. | 
| 64   bool CanSuspendRenderer(int render_process_id); | 73   bool CanSuspendRenderer(int render_process_id); | 
| 65 | 74 | 
| 66  private: |  | 
| 67   friend struct base::DefaultSingletonTraits<MemoryCoordinator>; |  | 
| 68 |  | 
| 69   // Helper function of CreateHandle and AddChildForTesting. |  | 
| 70   void CreateChildInfoMapEntry( |  | 
| 71       int render_process_id, |  | 
| 72       std::unique_ptr<MemoryCoordinatorHandleImpl> handle); |  | 
| 73 |  | 
| 74   // Stores information about any known child processes. | 75   // Stores information about any known child processes. | 
| 75   struct ChildInfo { | 76   struct ChildInfo { | 
| 76     // This object must be compatible with STL containers. | 77     // This object must be compatible with STL containers. | 
| 77     ChildInfo(); | 78     ChildInfo(); | 
| 78     ChildInfo(const ChildInfo& rhs); | 79     ChildInfo(const ChildInfo& rhs); | 
| 79     ~ChildInfo(); | 80     ~ChildInfo(); | 
| 80 | 81 | 
| 81     mojom::MemoryState memory_state; | 82     mojom::MemoryState memory_state; | 
| 82     std::unique_ptr<MemoryCoordinatorHandleImpl> handle; | 83     std::unique_ptr<MemoryCoordinatorHandleImpl> handle; | 
| 83   }; | 84   }; | 
| 84 | 85 | 
| 85   // A map from process ID (RenderProcessHost::GetID()) to child process info. | 86   // A map from process ID (RenderProcessHost::GetID()) to child process info. | 
| 86   using ChildInfoMap = std::map<int, ChildInfo>; | 87   using ChildInfoMap = std::map<int, ChildInfo>; | 
| 87 | 88 | 
|  | 89   ChildInfoMap& children() { return children_; } | 
|  | 90 | 
|  | 91 private: | 
|  | 92   // Helper function of CreateHandle and AddChildForTesting. | 
|  | 93   void CreateChildInfoMapEntry( | 
|  | 94       int render_process_id, | 
|  | 95       std::unique_ptr<MemoryCoordinatorHandleImpl> handle); | 
|  | 96 | 
| 88   // Tracks child processes. An entry is added when a renderer connects to | 97   // Tracks child processes. An entry is added when a renderer connects to | 
| 89   // MemoryCoordinator and removed automatically when an underlying binding is | 98   // MemoryCoordinator and removed automatically when an underlying binding is | 
| 90   // disconnected. | 99   // disconnected. | 
| 91   ChildInfoMap children_; | 100   ChildInfoMap children_; | 
| 92 | 101 | 
| 93   std::unique_ptr<MemoryCoordinatorDelegate> delegate_; | 102   std::unique_ptr<MemoryCoordinatorDelegate> delegate_; | 
| 94 | 103 | 
| 95   DISALLOW_COPY_AND_ASSIGN(MemoryCoordinator); | 104   DISALLOW_COPY_AND_ASSIGN(MemoryCoordinator); | 
| 96 }; | 105 }; | 
| 97 | 106 | 
| 98 }  // namespace content | 107 }  // namespace content | 
| 99 | 108 | 
| 100 #endif  // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | 109 #endif  // CONTENT_BROWSER_MEMORY_MEMORY_COORDINATOR_H_ | 
| OLD | NEW | 
|---|