Chromium Code Reviews| Index: chrome/browser/metrics/tracking_synchronizer.h |
| =================================================================== |
| --- chrome/browser/metrics/tracking_synchronizer.h (revision 111617) |
| +++ chrome/browser/metrics/tracking_synchronizer.h (working copy) |
| @@ -11,18 +11,17 @@ |
| #include <vector> |
| #include "base/basictypes.h" |
| +#include "base/lazy_instance.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/memory/weak_ptr.h" |
| -#include "base/time.h" |
| #include "base/values.h" |
| #include "chrome/browser/ui/webui/profiler_ui.h" |
| -#include "content/common/child_process_info.h" |
| -#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/profiler_subscriber.h" |
| -// This class maintains state that is used to upload tracking data from the |
| +// This class maintains state that is used to upload profiler data from the |
| // various processes, into the browser process. Such transactions are usually |
| // instigated by the browser. In general, a process will respond by gathering |
| -// tracking data, and transmitting the pickled tracking data. We collect the |
| +// profiler data, and transmitting the pickled profiler data. We collect the |
| // data in asynchronous mode that doesn't block the UI thread. |
| // |
| // To assure that all the processes have responded, a counter is maintained |
| @@ -31,99 +30,58 @@ |
| // create RequestContext object which stores the sequence number, pending |
| // processes and the callback_object that needs to be notified when we receive |
| // an update from processes. When an update arrives we find the RequestContext |
| -// associated with sequence number and send the unpickled tracking data to the |
| +// associated with sequence number and send the unpickled profiler data to the |
| // |callback_object_|. |
| namespace chrome_browser_metrics { |
| -class TrackingSynchronizer : public |
| - base::RefCountedThreadSafe<TrackingSynchronizer> { |
| +class RequestContext; |
| + |
| +class TrackingSynchronizer |
| + : public ProfilerSubscriber, |
| + public base::RefCountedThreadSafe<TrackingSynchronizer> { |
| public: |
| - // The "RequestContext" structure describes an individual request received |
| - // from the UI. |
| - struct RequestContext { |
| - RequestContext(const base::WeakPtr<ProfilerUI>& callback_object, |
| - int sequence_number, |
| - int processes_pending, |
| - base::TimeTicks callback_start_time) |
| - : callback_object_(callback_object), |
| - sequence_number_(sequence_number), |
| - processes_pending_(processes_pending), |
| - request_start_time_(callback_start_time) { |
| - } |
| - |
| - ~RequestContext() {} |
| - |
| - // Requests are made to asynchronously send data to the |callback_object_|. |
| - base::WeakPtr<ProfilerUI> callback_object_; |
| - |
| - // The sequence number used by the most recent update request to contact all |
| - // processes. |
| - int sequence_number_; |
| - |
| - // The number of processes that have not yet responded to requests. |
| - int processes_pending_; |
| - |
| - // The time when we were told to start the fetching of data from processes. |
| - base::TimeTicks request_start_time_; |
| - }; |
| - |
| - // A map from sequence_number_ to the actual RequestContexts. |
| - typedef std::map<int, RequestContext*> RequestContextMap; |
| - |
| // Construction also sets up the global singleton instance. This instance is |
| // used to communicate between the IO and UI thread, and is destroyed only as |
| // the main thread (browser_main) terminates, which means the IO thread has |
| // already completed, and will not need this instance any further. |
| TrackingSynchronizer(); |
| - // Return pointer to the singleton instance, which is allocated and |
| - // deallocated on the main UI thread (during system startup and teardown). |
| - static TrackingSynchronizer* CurrentSynchronizer(); |
| - |
| // Contact all processes, and get them to upload to the browser any/all |
| - // changes to tracking data. It calls |callback_object|'s SetData method with |
| + // changes to profiler data. It calls |callback_object|'s SetData method with |
| // the data received from each sub-process. |
| // This method is accessible on UI thread. |
| - static void FetchTrackingDataAsynchronously( |
| + static void FetchProfilerDataAsynchronously( |
| const base::WeakPtr<ProfilerUI>& callback_object); |
| - // Contact all processes and set tracking status to |enable|. |
| - // This method is accessible on UI thread. |
| - static void SetTrackingStatus(bool enable); |
| + // ------------------------------------------------------ |
| + // ProfilerSubscriber methods for browser child processes |
| + // ------------------------------------------------------ |
| - // Respond to this message from the renderer by setting the tracking status |
| - // (SetTrackingStatusInProcess) in that renderer process. |
| - // |process_id| is used to find the renderer process. |
| + // Update the number of pending processes for the given |sequence_number|. |
| + // This is called on UI thread. |
| + virtual void OnPendingProcesses(int sequence_number, |
| + int pending_processes) OVERRIDE; |
| + |
| + // Post a task on UI thread to call OnProfilerDataCollectedOnUI which sends |
| + // profiler_data back to callback_object_. |
| // This method is accessible on IO thread. |
| - static void IsTrackingEnabled(int process_id); |
| - |
| - // Get the current tracking status from the browser process and set it in the |
| - // renderer process. |process_id| is used to find the renderer process. |
| - // This method is accessible on UI thread. |
| - static void SetTrackingStatusInProcess(int process_id); |
| - |
| - // Deserialize the tracking data and record that we have received tracking |
| - // data from a process. This method posts a task to call |
| - // DeserializeTrackingListOnUI on UI thread to send the |tracking_data| to |
| - // callback_object_. This method is accessible on IO thread. |
| - static void DeserializeTrackingList( |
| + virtual void OnProfilerDataCollected( |
| int sequence_number, |
| - const std::string& tracking_data, |
| - ChildProcessInfo::ProcessType process_type); |
| + const base::DictionaryValue& profiler_data) OVERRIDE; |
| - // Deserialize the tracking data and record that we have received tracking |
| - // data from a process. This method is accessible on UI thread. |
| - static void DeserializeTrackingListOnUI( |
| - int sequence_number, |
| - const std::string& tracking_data, |
| - ChildProcessInfo::ProcessType process_type); |
| - |
| private: |
| friend class base::RefCountedThreadSafe<TrackingSynchronizer>; |
| + friend class RequestContext; |
| virtual ~TrackingSynchronizer(); |
| + // Send profiler_data back to callback_object_. It records that we are waiting |
| + // for one less profiler data from renderer or browser child process for the |
| + // given sequence number. This method is accessible on UI thread. |
| + void OnProfilerDataCollectedOnUI(int sequence_number, |
| + base::DictionaryValue* profiler_data); |
| + |
| // Establish a new sequence_number_, and use it to notify all the processes of |
| // the need to supply, to the browser, their tracking data. It also registers |
| // |callback_object| in |outstanding_requests_| map. Return the |
| @@ -131,34 +89,24 @@ |
| int RegisterAndNotifyAllProcesses( |
| const base::WeakPtr<ProfilerUI>& callback_object); |
| - // It finds the |callback_object_| in |outstanding_requests_| map for the |
| - // given |sequence_number| and notifies the |callback_object_| about the |
| - // |value|. This is called whenever we receive tracked data from processes. It |
| - // also records that we are waiting for one less tracking data from a process |
| - // for the given sequence number. If we have received a response from all |
| - // renderers, then it deletes the entry for sequence_number from |
| - // |outstanding_requests_| map. This method is accessible on UI thread. |
| + // It finds the RequestContext for the given |sequence_number| and notifies |
| + // the RequestContext's |callback_object_| about the |value|. This is called |
| + // whenever we receive profiler data from processes. It also records that we |
| + // are waiting for one less profiler data from a process for the given |
| + // sequence number. If we have received a response from all renderers and |
| + // browser processes, then it calls RequestContext's DeleteIfAllDone to delete |
| + // the entry for sequence_number. This method is accessible on UI thread. |
| void DecrementPendingProcessesAndSendData(int sequence_number, |
| base::DictionaryValue* value); |
| - // Records that we are waiting for one less tracking data from a process for |
| - // the given sequence number. |
| - // This method is accessible on UI thread. |
| - void DecrementPendingProcesses(int sequence_number); |
| - |
| - // When all changes have been acquired, or when the wait time expires |
| - // (whichever is sooner), this method is called. This method deletes the entry |
| - // for the given sequence_number from |outstanding_requests_| map. |
| - // This method is accessible on UI thread. |
| - void ForceTrackingSynchronizationDoneCallback(int sequence_number); |
| - |
| // Get a new sequence number to be sent to processes from browser process. |
| // This method is accessible on UI thread. |
| int GetNextAvailableSequenceNumber(); |
| - // Map of all outstanding RequestContexts, from sequence_number_ to |
| - // RequestContext. |
| - RequestContextMap outstanding_requests_; |
| + // Return pointer to the singleton instance, which is allocated and |
| + // deallocated on the main UI thread (during system startup and teardown). |
| + // This method is accessible on UI thread. |
| + static TrackingSynchronizer* CurrentSynchronizer(); |
| // We don't track the actual processes that are contacted for an update, only |
| // the count of the number of processes, and we can sometimes time-out and |
| @@ -173,12 +121,85 @@ |
| // portion of main(). It initializes globals to provide support for all future |
| // calls. This object is created on the UI thread, and it is destroyed after |
| // all the other threads have gone away. As a result, it is ok to call it |
| - // from the UI thread, or for about:tracking. |
| + // from the UI thread, or for about:profiler. |
| static TrackingSynchronizer* tracking_synchronizer_; |
| DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); |
| }; |
| +// The "RequestContext" structure describes an individual request received |
| +// from the UI. All methods are accessible on UI thread. |
| +class RequestContext { |
|
jam
2011/11/28 15:17:34
is this class used by other files, or only trackin
ramant (doing other things)
2011/11/29 01:32:20
Done.
|
| + public: |
| + // A map from sequence_number_ to the actual RequestContexts. |
| + typedef std::map<int, RequestContext*> RequestContextMap; |
| + |
| + ~RequestContext(); |
| + |
| + private: |
| + friend class TrackingSynchronizer; |
| + |
| + RequestContext(const base::WeakPtr<ProfilerUI>& callback_object, |
| + int sequence_number); |
| + |
| + // Methods for book keeping of process_group_count_. |
| + void AddProcessGroupCount(int process_group_count); |
| + void DecrementProcessGroupCount(); |
| + |
| + // Methods for book keeping of processes_pending_. |
| + void IncrementProcessesPending(); |
| + void AddProcessesPending(int processes_pending); |
| + void DecrementProcessesPending(); |
| + |
| + // Records that we are waiting for one less tracking data from a process for |
| + // the given sequence number. If |process_group_count_| and |
| + // |processes_pending_| are zero, then delete the current object by calling |
| + // Unregister. |
| + void DeleteIfAllDone(); |
| + |
| + // Register |callback_object| in |outstanding_requests_| map for the given |
| + // |sequence_number|. |
| + static RequestContext* Register( |
| + int sequence_number, |
| + const base::WeakPtr<ProfilerUI>& callback_object); |
| + |
| + // Find the |RequestContext| in |outstanding_requests_| map for the given |
| + // |sequence_number|. |
| + static RequestContext* GetRequestContext(int sequence_number); |
| + |
| + // Delete the entry for the given sequence_number| from |
| + // |outstanding_requests_| map. This method is called when all changes have |
| + // been acquired, or when the wait time expires (whichever is sooner). |
| + static void Unregister(int sequence_number); |
| + |
| + // Delete all the entries in |outstanding_requests_| map. |
| + static void OnShutdown(); |
| + |
| + // Requests are made to asynchronously send data to the |callback_object_|. |
| + base::WeakPtr<ProfilerUI> callback_object_; |
| + |
| + // The sequence number used by the most recent update request to contact all |
| + // processes. |
| + int sequence_number_; |
| + |
| + // The number of times we need to hear from |content| about number of |
| + // pending processes. |content| code calls OnPendingProcesses once for |
| + // RenderProcessHosts and another time for BrowserChildProcessHosts after |
| + // sending GetChildProfilerData message to all respective child processes. |
| + // RegisterAndNotifyAllProcesses initializes it by the value returned by the |
| + // content::GetProfilerData and OnPendingProcesses decrements it when it gets |
| + // processes pending count. |
| + int process_group_count_; |
| + |
| + // The number of pending processes (browser, all renderer processes and |
| + // browser child processes) that have not yet responded to requests. |
| + int processes_pending_; |
| + |
| + // Map of all outstanding RequestContexts, from sequence_number_ to |
| + // RequestContext. |
| + static base::LazyInstance<RequestContextMap> outstanding_requests_; |
| +}; |
| + |
| } // namespace chrome_browser_metrics |
| #endif // CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |