Return backed up TemplateURL on default search change
[chromium-blink-merge.git] / chrome / browser / memory_details.h
blob568f591328a4af632411c6d058119e1e8db94596
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_MEMORY_DETAILS_H_
6 #define CHROME_BROWSER_MEMORY_DETAILS_H_
7 #pragma once
9 #include <vector>
11 #include "base/memory/ref_counted.h"
12 #include "base/process_util.h"
13 #include "base/string16.h"
14 #include "content/public/common/process_type.h"
16 // We collect data about each browser process. A browser may
17 // have multiple processes (of course!). Even IE has multiple
18 // processes these days.
19 struct ProcessMemoryInformation {
20 // NOTE: Do not remove or reorder the elements in this enum, and only add new
21 // items at the end. We depend on these specific values in a histogram.
22 enum RendererProcessType {
23 RENDERER_UNKNOWN = 0,
24 RENDERER_NORMAL,
25 RENDERER_CHROME, // WebUI (chrome:// URL)
26 RENDERER_EXTENSION, // chrome-extension://
27 RENDERER_DEVTOOLS, // Web inspector
28 RENDERER_INTERSTITIAL, // malware/phishing interstitial
29 RENDERER_NOTIFICATION, // HTML notification bubble
30 RENDERER_BACKGROUND_APP // hosted app background page
33 static std::string GetRendererTypeNameInEnglish(RendererProcessType type);
34 static std::string GetFullTypeNameInEnglish(
35 content::ProcessType type,
36 RendererProcessType rtype);
38 ProcessMemoryInformation();
39 ~ProcessMemoryInformation();
41 // The process id.
42 base::ProcessId pid;
43 // The working set information.
44 base::WorkingSetKBytes working_set;
45 // The committed bytes.
46 base::CommittedKBytes committed;
47 // The process version
48 string16 version;
49 // The process product name.
50 string16 product_name;
51 // The number of processes which this memory represents.
52 int num_processes;
53 // A process is a diagnostics process if it is rendering about:memory.
54 // Mark this specially so that it can avoid counting it in its own
55 // results.
56 bool is_diagnostics;
57 // If this is a child process of Chrome, what type (i.e. plugin) it is.
58 content::ProcessType type;
59 // If this is a renderer process, what type it is.
60 RendererProcessType renderer_type;
61 // A collection of titles used, i.e. for a tab it'll show all the page titles.
62 std::vector<string16> titles;
65 typedef std::vector<ProcessMemoryInformation> ProcessMemoryInformationList;
67 // Browser Process Information.
68 struct ProcessData {
69 ProcessData();
70 ProcessData(const ProcessData& rhs);
71 ~ProcessData();
72 ProcessData& operator=(const ProcessData& rhs);
74 string16 name;
75 string16 process_name;
76 ProcessMemoryInformationList processes;
79 #if defined(OS_MACOSX)
80 class ProcessInfoSnapshot;
81 #endif
83 // MemoryDetails fetches memory details about current running browsers.
84 // Because this data can only be fetched asynchronously, callers use
85 // this class via a callback.
87 // Example usage:
89 // class MyMemoryDetailConsumer : public MemoryDetails {
91 // MyMemoryDetailConsumer() {
92 // // Anything but |StartFetch()|.
93 // }
95 // // (Or just call |StartFetch()| explicitly if there's nothing else to
96 // // do.)
97 // void StartDoingStuff() {
98 // StartFetch(); // Starts fetching details.
99 // // Etc.
100 // }
102 // // Your other class stuff here
104 // virtual void OnDetailsAvailable() {
105 // // do work with memory info here
106 // }
107 // }
108 class MemoryDetails : public base::RefCountedThreadSafe<MemoryDetails> {
109 public:
110 // Constructor.
111 MemoryDetails();
113 // Access to the process detail information. This data is only available
114 // after OnDetailsAvailable() has been called.
115 const std::vector<ProcessData>& processes() { return process_data_; }
117 // Initiate updating the current memory details. These are fetched
118 // asynchronously because data must be collected from multiple threads.
119 // OnDetailsAvailable will be called when this process is complete.
120 void StartFetch();
122 virtual void OnDetailsAvailable() {}
124 protected:
125 friend class base::RefCountedThreadSafe<MemoryDetails>;
127 virtual ~MemoryDetails();
129 private:
130 // Collect child process information on the IO thread. This is needed because
131 // information about some child process types (i.e. plugins) can only be taken
132 // on that thread. The data will be used by about:memory. When finished,
133 // invokes back to the file thread to run the rest of the about:memory
134 // functionality.
135 void CollectChildInfoOnIOThread();
137 // Collect current process information from the OS and store it
138 // for processing. If data has already been collected, clears old
139 // data and re-collects the data.
140 // Note - this function enumerates memory details from many processes
141 // and is fairly expensive to run, hence it's run on the file thread.
142 // The parameter holds information about processes from the IO thread.
143 void CollectProcessData(const std::vector<ProcessMemoryInformation>&);
145 #if defined(OS_MACOSX)
146 // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium
147 // process with PID |pid|. The collected data is added to the state of the
148 // object (in |process_data_|).
149 void CollectProcessDataChrome(
150 const std::vector<ProcessMemoryInformation>& child_info,
151 base::ProcessId pid,
152 const ProcessInfoSnapshot& process_info);
153 #endif
155 // Collect child process information on the UI thread. Information about
156 // renderer processes is only available there.
157 void CollectChildInfoOnUIThread();
159 // Each time we take a memory sample, we do a little work to update
160 // the global histograms for tracking memory usage.
161 void UpdateHistograms();
163 // Returns a pointer to the ProcessData structure for Chrome.
164 ProcessData* ChromeBrowser();
166 std::vector<ProcessData> process_data_;
168 DISALLOW_COPY_AND_ASSIGN(MemoryDetails);
171 #endif // CHROME_BROWSER_MEMORY_DETAILS_H_