OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
| 6 #define CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <map> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/basictypes.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/time.h" |
| 17 #include "base/values.h" |
| 18 #include "chrome/browser/ui/webui/tracking_ui.h" |
| 19 #include "content/common/child_process_info.h" |
| 20 #include "content/public/browser/browser_thread.h" |
| 21 |
| 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 |
| 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 |
| 26 // data in asynchronous mode that doesn't block the UI thread. |
| 27 // |
| 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 |
| 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 |
| 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 |
| 34 // associated with sequence number and send the unpickled tracking data to the |
| 35 // |callback_object_|. |
| 36 |
| 37 namespace chrome_browser_metrics { |
| 38 |
| 39 class TrackingSynchronizer : public |
| 40 base::RefCountedThreadSafe<TrackingSynchronizer> { |
| 41 public: |
| 42 // The "RequestContext" structure describes an individual request received |
| 43 // from the UI. |
| 44 struct RequestContext { |
| 45 RequestContext(const base::WeakPtr<TrackingUI>& callback_object, |
| 46 int sequence_number, |
| 47 int processes_pending, |
| 48 base::TimeTicks callback_start_time) |
| 49 : callback_object_(callback_object), |
| 50 sequence_number_(sequence_number), |
| 51 processes_pending_(processes_pending), |
| 52 request_start_time_(callback_start_time) { |
| 53 } |
| 54 |
| 55 ~RequestContext() {} |
| 56 |
| 57 // Requests are made to asynchronously send data to the |callback_object_|. |
| 58 base::WeakPtr<TrackingUI> callback_object_; |
| 59 |
| 60 // The sequence number used by the most recent update request to contact all |
| 61 // processes. |
| 62 int sequence_number_; |
| 63 |
| 64 // The number of processes that have not yet responded to requests. |
| 65 int processes_pending_; |
| 66 |
| 67 // The time when we were told to start the fetching of data from processes. |
| 68 base::TimeTicks request_start_time_; |
| 69 }; |
| 70 |
| 71 // A map from sequence_number_ to the actual RequestContexts. |
| 72 typedef std::map<int, RequestContext*> RequestContextMap; |
| 73 |
| 74 // Construction also sets up the global singleton instance. This instance is |
| 75 // used to communicate between the IO and UI thread, and is destroyed only as |
| 76 // the main thread (browser_main) terminates, which means the IO thread has |
| 77 // already completed, and will not need this instance any further. |
| 78 TrackingSynchronizer(); |
| 79 |
| 80 // Return pointer to the singleton instance, which is allocated and |
| 81 // deallocated on the main UI thread (during system startup and teardown). |
| 82 static TrackingSynchronizer* CurrentSynchronizer(); |
| 83 |
| 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 |
| 86 // the data received from each sub-process. |
| 87 // This method is accessible on UI thread. |
| 88 static void FetchTrackingDataAsynchronously( |
| 89 const base::WeakPtr<TrackingUI>& callback_object); |
| 90 |
| 91 // Contact all processes and set tracking status to |enable|. |
| 92 // This method is accessible on UI thread. |
| 93 static void SetTrackingStatus(bool enable); |
| 94 |
| 95 // Respond to this message from the renderer by setting the tracking status |
| 96 // (SetTrackingStatusInProcess) in that renderer process. |
| 97 // |process_id| is used to find the renderer process. |
| 98 // This method is accessible on IO thread. |
| 99 static void IsTrackingEnabled(int process_id); |
| 100 |
| 101 // Get the current tracking status from the browser process and set it in the |
| 102 // renderer process. |process_id| is used to find the renderer process. |
| 103 // This method is accessible on UI thread. |
| 104 static void SetTrackingStatusInProcess(int process_id); |
| 105 |
| 106 // Deserialize the tracking data and record that we have received tracking |
| 107 // data from a process. This method posts a task to call |
| 108 // DeserializeTrackingListOnUI on UI thread to send the |tracking_data| to |
| 109 // callback_object_. This method is accessible on IO thread. |
| 110 static void DeserializeTrackingList( |
| 111 int sequence_number, |
| 112 const std::string& tracking_data, |
| 113 ChildProcessInfo::ProcessType process_type); |
| 114 |
| 115 // Deserialize the tracking data and record that we have received tracking |
| 116 // data from a process. This method is accessible on UI thread. |
| 117 static void DeserializeTrackingListOnUI( |
| 118 int sequence_number, |
| 119 const std::string& tracking_data, |
| 120 ChildProcessInfo::ProcessType process_type); |
| 121 |
| 122 private: |
| 123 friend class base::RefCountedThreadSafe<TrackingSynchronizer>; |
| 124 |
| 125 virtual ~TrackingSynchronizer(); |
| 126 |
| 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 |
| 129 // |callback_object| in |outstanding_requests_| map. Return the |
| 130 // sequence_number_ that was used. This method is accessible on UI thread. |
| 131 int RegisterAndNotifyAllProcesses( |
| 132 const base::WeakPtr<TrackingUI>& callback_object); |
| 133 |
| 134 // It finds the |callback_object_| in |outstanding_requests_| map for 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 |
| 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 |
| 139 // renderers, then it deletes the entry for sequence_number from |
| 140 // |outstanding_requests_| map. This method is accessible on UI thread. |
| 141 void DecrementPendingProcessesAndSendData(int sequence_number, |
| 142 base::DictionaryValue* value); |
| 143 |
| 144 // Records that we are waiting for one less tracking data from a process for |
| 145 // the given sequence number. |
| 146 // This method is accessible on UI thread. |
| 147 void DecrementPendingProcesses(int sequence_number); |
| 148 |
| 149 // When all changes have been acquired, or when the wait time expires |
| 150 // (whichever is sooner), this method is called. This method deletes the entry |
| 151 // for the given sequence_number from |outstanding_requests_| map. |
| 152 // This method is accessible on UI thread. |
| 153 void ForceTrackingSynchronizationDoneCallback(int sequence_number); |
| 154 |
| 155 // Get a new sequence number to be sent to processes from browser process. |
| 156 // This method is accessible on UI thread. |
| 157 int GetNextAvailableSequenceNumber(); |
| 158 |
| 159 // Map of all outstanding RequestContexts, from sequence_number_ to |
| 160 // RequestContext. |
| 161 RequestContextMap outstanding_requests_; |
| 162 |
| 163 // We don't track the actual processes that are contacted for an update, only |
| 164 // the count of the number of processes, and we can sometimes time-out and |
| 165 // give up on a "slow to respond" process. We use a sequence_number to be |
| 166 // sure a response from a process is associated with the current round of |
| 167 // requests. All sequence numbers used are non-negative. |
| 168 // last_used_sequence_number_ is the most recently used number (used to avoid |
| 169 // reuse for a long time). |
| 170 int last_used_sequence_number_; |
| 171 |
| 172 // This singleton instance should be started during the single threaded |
| 173 // portion of main(). It initializes globals to provide support for all future |
| 174 // calls. This object is created on the UI thread, and it is destroyed after |
| 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. |
| 177 static TrackingSynchronizer* tracking_synchronizer_; |
| 178 |
| 179 DISALLOW_COPY_AND_ASSIGN(TrackingSynchronizer); |
| 180 }; |
| 181 |
| 182 } // namespace chrome_browser_metrics |
| 183 |
| 184 #endif // CHROME_BROWSER_METRICS_TRACKING_SYNCHRONIZER_H_ |
OLD | NEW |