| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ | 5 #ifndef CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
| 6 #define CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ | 6 #define CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/time.h" | 16 #include "base/time.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "chrome/browser/ui/webui/tracking_ui.h" | 18 #include "chrome/browser/ui/webui/profiler_ui.h" |
| 19 #include "content/common/child_process_info.h" | 19 #include "content/common/child_process_info.h" |
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 | 21 |
| 22 // This class maintains state that is used to upload tracking data from the | 22 // This class maintains state that is used to upload tracking data from the |
| 23 // various processes, into the browser process. Such transactions are usually | 23 // various processes, into the browser process. Such transactions are usually |
| 24 // instigated by the browser. In general, a process will respond by gathering | 24 // instigated by the browser. In general, a process will respond by gathering |
| 25 // tracking data, and transmitting the pickled tracking data. We collect the | 25 // tracking data, and transmitting the pickled tracking data. We collect the |
| 26 // data in asynchronous mode that doesn't block the UI thread. | 26 // data in asynchronous mode that doesn't block the UI thread. |
| 27 // | 27 // |
| 28 // To assure that all the processes have responded, a counter is maintained | 28 // To assure that all the processes have responded, a counter is maintained |
| 29 // to indicate the number of pending (not yet responsive) processes. We tag | 29 // to indicate the number of pending (not yet responsive) processes. We tag |
| 30 // each group of requests with a sequence number. For each group of requests, we | 30 // each group of requests with a sequence number. For each group of requests, we |
| 31 // create RequestContext object which stores the sequence number, pending | 31 // create RequestContext object which stores the sequence number, pending |
| 32 // processes and the callback_object that needs to be notified when we receive | 32 // processes and the callback_object that needs to be notified when we receive |
| 33 // an update from processes. When an update arrives we find the RequestContext | 33 // an update from processes. When an update arrives we find the RequestContext |
| 34 // associated with sequence number and send the unpickled tracking data to the | 34 // associated with sequence number and send the unpickled tracking data to the |
| 35 // |callback_object_|. | 35 // |callback_object_|. |
| 36 | 36 |
| 37 namespace chrome_browser_metrics { | 37 namespace chrome_browser_metrics { |
| 38 | 38 |
| 39 class TrackingSynchronizer : public | 39 class TrackingSynchronizer : public |
| 40 base::RefCountedThreadSafe<TrackingSynchronizer> { | 40 base::RefCountedThreadSafe<TrackingSynchronizer> { |
| 41 public: | 41 public: |
| 42 // The "RequestContext" structure describes an individual request received | 42 // The "RequestContext" structure describes an individual request received |
| 43 // from the UI. | 43 // from the UI. |
| 44 struct RequestContext { | 44 struct RequestContext { |
| 45 RequestContext(const base::WeakPtr<TrackingUI>& callback_object, | 45 RequestContext(const base::WeakPtr<ProfilerUI>& callback_object, |
| 46 int sequence_number, | 46 int sequence_number, |
| 47 int processes_pending, | 47 int processes_pending, |
| 48 base::TimeTicks callback_start_time) | 48 base::TimeTicks callback_start_time) |
| 49 : callback_object_(callback_object), | 49 : callback_object_(callback_object), |
| 50 sequence_number_(sequence_number), | 50 sequence_number_(sequence_number), |
| 51 processes_pending_(processes_pending), | 51 processes_pending_(processes_pending), |
| 52 request_start_time_(callback_start_time) { | 52 request_start_time_(callback_start_time) { |
| 53 } | 53 } |
| 54 | 54 |
| 55 ~RequestContext() {} | 55 ~RequestContext() {} |
| 56 | 56 |
| 57 // Requests are made to asynchronously send data to the |callback_object_|. | 57 // Requests are made to asynchronously send data to the |callback_object_|. |
| 58 base::WeakPtr<TrackingUI> callback_object_; | 58 base::WeakPtr<ProfilerUI> callback_object_; |
| 59 | 59 |
| 60 // The sequence number used by the most recent update request to contact all | 60 // The sequence number used by the most recent update request to contact all |
| 61 // processes. | 61 // processes. |
| 62 int sequence_number_; | 62 int sequence_number_; |
| 63 | 63 |
| 64 // The number of processes that have not yet responded to requests. | 64 // The number of processes that have not yet responded to requests. |
| 65 int processes_pending_; | 65 int processes_pending_; |
| 66 | 66 |
| 67 // The time when we were told to start the fetching of data from processes. | 67 // The time when we were told to start the fetching of data from processes. |
| 68 base::TimeTicks request_start_time_; | 68 base::TimeTicks request_start_time_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 79 | 79 |
| 80 // Return pointer to the singleton instance, which is allocated and | 80 // Return pointer to the singleton instance, which is allocated and |
| 81 // deallocated on the main UI thread (during system startup and teardown). | 81 // deallocated on the main UI thread (during system startup and teardown). |
| 82 static TrackingSynchronizer* CurrentSynchronizer(); | 82 static TrackingSynchronizer* CurrentSynchronizer(); |
| 83 | 83 |
| 84 // Contact all processes, and get them to upload to the browser any/all | 84 // Contact all processes, and get them to upload to the browser any/all |
| 85 // changes to tracking data. It calls |callback_object|'s SetData method with | 85 // changes to tracking data. It calls |callback_object|'s SetData method with |
| 86 // the data received from each sub-process. | 86 // the data received from each sub-process. |
| 87 // This method is accessible on UI thread. | 87 // This method is accessible on UI thread. |
| 88 static void FetchTrackingDataAsynchronously( | 88 static void FetchTrackingDataAsynchronously( |
| 89 const base::WeakPtr<TrackingUI>& callback_object); | 89 const base::WeakPtr<ProfilerUI>& callback_object); |
| 90 | 90 |
| 91 // Contact all processes and set tracking status to |enable|. | 91 // Contact all processes and set tracking status to |enable|. |
| 92 // This method is accessible on UI thread. | 92 // This method is accessible on UI thread. |
| 93 static void SetTrackingStatus(bool enable); | 93 static void SetTrackingStatus(bool enable); |
| 94 | 94 |
| 95 // Respond to this message from the renderer by setting the tracking status | 95 // Respond to this message from the renderer by setting the tracking status |
| 96 // (SetTrackingStatusInProcess) in that renderer process. | 96 // (SetTrackingStatusInProcess) in that renderer process. |
| 97 // |process_id| is used to find the renderer process. | 97 // |process_id| is used to find the renderer process. |
| 98 // This method is accessible on IO thread. | 98 // This method is accessible on IO thread. |
| 99 static void IsTrackingEnabled(int process_id); | 99 static void IsTrackingEnabled(int process_id); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 122 private: | 122 private: |
| 123 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; | 123 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; |
| 124 | 124 |
| 125 virtual ~TrackingSynchronizer(); | 125 virtual ~TrackingSynchronizer(); |
| 126 | 126 |
| 127 // Establish a new sequence_number_, and use it to notify all the processes of | 127 // Establish a new sequence_number_, and use it to notify all the processes of |
| 128 // the need to supply, to the browser, their tracking data. It also registers | 128 // the need to supply, to the browser, their tracking data. It also registers |
| 129 // |callback_object| in |outstanding_requests_| map. Return the | 129 // |callback_object| in |outstanding_requests_| map. Return the |
| 130 // sequence_number_ that was used. This method is accessible on UI thread. | 130 // sequence_number_ that was used. This method is accessible on UI thread. |
| 131 int RegisterAndNotifyAllProcesses( | 131 int RegisterAndNotifyAllProcesses( |
| 132 const base::WeakPtr<TrackingUI>& callback_object); | 132 const base::WeakPtr<ProfilerUI>& callback_object); |
| 133 | 133 |
| 134 // It finds the |callback_object_| in |outstanding_requests_| map for the | 134 // It finds the |callback_object_| in |outstanding_requests_| map for the |
| 135 // given |sequence_number| and notifies the |callback_object_| about the | 135 // given |sequence_number| and notifies the |callback_object_| about the |
| 136 // |value|. This is called whenever we receive tracked data from processes. It | 136 // |value|. This is called whenever we receive tracked data from processes. It |
| 137 // also records that we are waiting for one less tracking data from a process | 137 // also records that we are waiting for one less tracking data from a process |
| 138 // for the given sequence number. If we have received a response from all | 138 // for the given sequence number. If we have received a response from all |
| 139 // renderers, then it deletes the entry for sequence_number from | 139 // renderers, then it deletes the entry for sequence_number from |
| 140 // |outstanding_requests_| map. This method is accessible on UI thread. | 140 // |outstanding_requests_| map. This method is accessible on UI thread. |
| 141 void DecrementPendingProcessesAndSendData(int sequence_number, | 141 void DecrementPendingProcessesAndSendData(int sequence_number, |
| 142 base::DictionaryValue* value); | 142 base::DictionaryValue* value); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // all the other threads have gone away. As a result, it is ok to call it | 175 // all the other threads have gone away. As a result, it is ok to call it |
| 176 // from the UI thread, or for about:tracking. | 176 // from the UI thread, or for about:tracking. |
| 177 static TrackingSynchronizer* tracking_synchronizer_; | 177 static TrackingSynchronizer* tracking_synchronizer_; |
| 178 | 178 |
| 179 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); | 179 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); |
| 180 }; | 180 }; |
| 181 | 181 |
| 182 } // namespace chrome_browser_metrics | 182 } // namespace chrome_browser_metrics |
| 183 | 183 |
| 184 #endif // CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ | 184 #endif // CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
| OLD | NEW |