[M120][Tizen][Onscreen] Fix build errors for TV profile
[platform/framework/web/chromium-efl.git] / chrome / browser / memory_details.h
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_MEMORY_DETAILS_H_
6 #define CHROME_BROWSER_MEMORY_DETAILS_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/memory/ref_counted.h"
13 #include "base/process/process_handle.h"
14 #include "base/process/process_metrics.h"
15 #include "build/build_config.h"
16 #include "build/chromeos_buildflags.h"
17 #include "content/public/common/process_type.h"
18
19 namespace memory_instrumentation {
20 class GlobalMemoryDump;
21 }  // namespace memory_instrumentation
22
23 // We collect data about each browser process.  A browser may
24 // have multiple processes (of course!).  Even IE has multiple
25 // processes these days.
26 struct ProcessMemoryInformation {
27   // NOTE: Do not remove or reorder the elements in this enum, and only add new
28   // items at the end. We depend on these specific values in a histogram.
29   enum RendererProcessType {
30     RENDERER_UNKNOWN = 0,
31     RENDERER_NORMAL,
32     RENDERER_CHROME,        // WebUI (chrome:// URL)
33     RENDERER_EXTENSION,     // chrome-extension://
34     RENDERER_DEVTOOLS,      // Web inspector
35     RENDERER_INTERSTITIAL,  // malware/phishing interstitial
36     RENDERER_BACKGROUND_APP // hosted app background page
37   };
38
39   static std::string GetRendererTypeNameInEnglish(RendererProcessType type);
40   static std::string GetFullTypeNameInEnglish(
41       int process_type,
42       RendererProcessType rtype);
43
44   ProcessMemoryInformation();
45   ProcessMemoryInformation(const ProcessMemoryInformation& other);
46   ~ProcessMemoryInformation();
47
48   // Default ordering is by private memory consumption.
49   bool operator<(const ProcessMemoryInformation& rhs) const;
50
51   // The process id.
52   base::ProcessId pid;
53   // The process version
54   std::u16string version;
55   // The process product name.
56   std::u16string product_name;
57   // The number of processes which this memory represents.
58   int num_processes;
59   // If this is a child process of Chrome, what type (i.e. plugin) it is.
60   int process_type;
61   // Number of open file descriptors in this process.
62   int num_open_fds;
63   // Maximum number of file descriptors that can be opened in this process.
64   int open_fds_soft_limit;
65   // If this is a renderer process, what type it is.
66   RendererProcessType renderer_type;
67   // A collection of titles used, i.e. for a tab it'll show all the page titles.
68   std::vector<std::u16string> titles;
69   // Consistent memory metric for all platforms.
70   size_t private_memory_footprint_kb;
71 };
72
73 typedef std::vector<ProcessMemoryInformation> ProcessMemoryInformationList;
74
75 // Browser Process Information.
76 struct ProcessData {
77   ProcessData();
78   ProcessData(const ProcessData& rhs);
79   ~ProcessData();
80   ProcessData& operator=(const ProcessData& rhs);
81
82   std::u16string name;
83   std::u16string process_name;
84   ProcessMemoryInformationList processes;
85 };
86
87 // MemoryDetails fetches memory details about current running browsers.
88 // Because this data can only be fetched asynchronously, callers use
89 // this class via a callback.
90 //
91 // Example usage:
92 //
93 //    class MyMemoryDetailConsumer : public MemoryDetails {
94 //
95 //      MyMemoryDetailConsumer() {
96 //        // Anything but |StartFetch()|.
97 //      }
98 //
99 //      // (Or just call |StartFetch()| explicitly if there's nothing else to
100 //      // do.)
101 //      void StartDoingStuff() {
102 //        StartFetch();  // Starts fetching details.
103 //        // Etc.
104 //      }
105 //
106 //      // Your other class stuff here
107 //
108 //      virtual void OnDetailsAvailable() {
109 //        // do work with memory info here
110 //      }
111 //    }
112 class MemoryDetails : public base::RefCountedThreadSafe<MemoryDetails> {
113  public:
114   // Constructor.
115   MemoryDetails();
116
117   MemoryDetails(const MemoryDetails&) = delete;
118   MemoryDetails& operator=(const MemoryDetails&) = delete;
119
120   // Initiate updating the current memory details.  These are fetched
121   // asynchronously because data must be collected from multiple threads.
122   // OnDetailsAvailable will be called when this process is complete.
123   void StartFetch();
124
125   virtual void OnDetailsAvailable() = 0;
126
127   // Returns a string summarizing memory usage of the Chrome browser process
128   // and all sub-processes, suitable for logging.  Tab title may contain PII,
129   // set |include_tab_title| to false to exclude tab titles when there are
130   // privacy concerns.
131   std::string ToLogString(bool include_tab_title);
132
133  protected:
134   friend class base::RefCountedThreadSafe<MemoryDetails>;
135
136   virtual ~MemoryDetails();
137
138   // Access to the process detail information.  This data is only available
139   // after OnDetailsAvailable() has been called.
140   const std::vector<ProcessData>& processes() { return process_data_; }
141
142   // Returns a pointer to the ProcessData structure for Chrome.
143   ProcessData* ChromeBrowser();
144
145 #if BUILDFLAG(IS_CHROMEOS_ASH)
146   const base::SwapInfo& swap_info() const { return swap_info_; }
147 #endif
148
149  private:
150   // Collect current process information from the OS and store it
151   // for processing.  If data has already been collected, clears old
152   // data and re-collects the data.
153   // Note - this function enumerates memory details from many processes
154   // and is fairly expensive to run, hence it's run on the blocking pool.
155   // The parameter holds information about processes from the IO thread.
156   void CollectProcessData(
157       const std::vector<ProcessMemoryInformation>& child_info);
158
159   // Collect child process information on the UI thread.  Information about
160   // renderer processes is only available there.
161   void CollectChildInfoOnUIThread();
162
163   void DidReceiveMemoryDump(
164       bool success,
165       std::unique_ptr<memory_instrumentation::GlobalMemoryDump> dump);
166
167   std::vector<ProcessData> process_data_;
168
169 #if BUILDFLAG(IS_CHROMEOS_ASH)
170   base::SwapInfo swap_info_;
171 #endif
172 };
173
174 #endif  // CHROME_BROWSER_MEMORY_DETAILS_H_