| 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 #include "chrome/browser/metrics/tracking_synchronizer.h" | 5 #include "chrome/browser/metrics/tracking_synchronizer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "base/tracked_objects.h" | 13 #include "base/tracked_objects.h" |
| 14 #include "chrome/browser/ui/webui/tracing_ui.h" | 14 #include "chrome/browser/ui/webui/tracing_ui.h" |
| 15 #include "chrome/common/chrome_constants.h" | 15 #include "chrome/common/chrome_constants.h" |
| 16 #include "chrome/common/render_messages.h" | 16 #include "chrome/common/render_messages.h" |
| 17 #include "content/browser/renderer_host/render_process_host.h" | |
| 18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/render_process_host.h" |
| 19 | 19 |
| 20 using base::TimeTicks; | 20 using base::TimeTicks; |
| 21 using content::BrowserThread; | 21 using content::BrowserThread; |
| 22 | 22 |
| 23 namespace chrome_browser_metrics { | 23 namespace chrome_browser_metrics { |
| 24 | 24 |
| 25 // Negative numbers are never used as sequence numbers. We explicitly pick a | 25 // Negative numbers are never used as sequence numbers. We explicitly pick a |
| 26 // negative number that is "so negative" that even when we add one (as is done | 26 // negative number that is "so negative" that even when we add one (as is done |
| 27 // when we generated the next sequence number) that it will still be negative. | 27 // when we generated the next sequence number) that it will still be negative. |
| 28 // We have code that handles wrapping around on an overflow into negative | 28 // We have code that handles wrapping around on an overflow into negative |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 sequence_number), | 77 sequence_number), |
| 78 60000); | 78 60000); |
| 79 } | 79 } |
| 80 | 80 |
| 81 // static | 81 // static |
| 82 void TrackingSynchronizer::SetTrackingStatus(bool enable) { | 82 void TrackingSynchronizer::SetTrackingStatus(bool enable) { |
| 83 // To iterate over all processes, or to send messages to the hosts, we need | 83 // To iterate over all processes, or to send messages to the hosts, we need |
| 84 // to be on the UI thread. | 84 // to be on the UI thread. |
| 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 86 | 86 |
| 87 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); | 87 for (content::RenderProcessHost::iterator it( |
| 88 content::RenderProcessHost::AllHostsIterator()); |
| 88 !it.IsAtEnd(); it.Advance()) { | 89 !it.IsAtEnd(); it.Advance()) { |
| 89 RenderProcessHost* render_process_host = it.GetCurrentValue(); | 90 content::RenderProcessHost* render_process_host = it.GetCurrentValue(); |
| 90 DCHECK(render_process_host); | 91 DCHECK(render_process_host); |
| 91 // Ignore processes that don't have a connection, such as crashed tabs. | 92 // Ignore processes that don't have a connection, such as crashed tabs. |
| 92 if (!render_process_host->HasConnection()) | 93 if (!render_process_host->HasConnection()) |
| 93 continue; | 94 continue; |
| 94 | 95 |
| 95 render_process_host->Send(new ChromeViewMsg_SetTrackingStatus(enable)); | 96 render_process_host->Send(new ChromeViewMsg_SetTrackingStatus(enable)); |
| 96 } | 97 } |
| 97 } | 98 } |
| 98 | 99 |
| 99 // static | 100 // static |
| 100 void TrackingSynchronizer::IsTrackingEnabled(int process_id) { | 101 void TrackingSynchronizer::IsTrackingEnabled(int process_id) { |
| 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 102 // To find the process, or to send messages to the hosts, we need to be on the | 103 // To find the process, or to send messages to the hosts, we need to be on the |
| 103 // UI thread. | 104 // UI thread. |
| 104 BrowserThread::PostTask( | 105 BrowserThread::PostTask( |
| 105 BrowserThread::UI, FROM_HERE, | 106 BrowserThread::UI, FROM_HERE, |
| 106 base::Bind( | 107 base::Bind( |
| 107 &TrackingSynchronizer::SetTrackingStatusInProcess, process_id)); | 108 &TrackingSynchronizer::SetTrackingStatusInProcess, process_id)); |
| 108 } | 109 } |
| 109 | 110 |
| 110 // static | 111 // static |
| 111 void TrackingSynchronizer::SetTrackingStatusInProcess(int process_id) { | 112 void TrackingSynchronizer::SetTrackingStatusInProcess(int process_id) { |
| 112 // To find the process, or to send messages to the hosts, we need to be on the | 113 // To find the process, or to send messages to the hosts, we need to be on the |
| 113 // UI thread. | 114 // UI thread. |
| 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 115 | 116 |
| 116 bool enable = tracked_objects::ThreadData::tracking_status(); | 117 bool enable = tracked_objects::ThreadData::tracking_status(); |
| 117 | 118 |
| 118 RenderProcessHost* process = RenderProcessHost::FromID(process_id); | 119 content::RenderProcessHost* process = |
| 120 content::RenderProcessHost::FromID(process_id); |
| 119 // Ignore processes that don't have a connection, such as crashed tabs. | 121 // Ignore processes that don't have a connection, such as crashed tabs. |
| 120 if (!process || !process->HasConnection()) | 122 if (!process || !process->HasConnection()) |
| 121 return; | 123 return; |
| 122 process->Send(new ChromeViewMsg_SetTrackingStatus(enable)); | 124 process->Send(new ChromeViewMsg_SetTrackingStatus(enable)); |
| 123 } | 125 } |
| 124 | 126 |
| 125 // static | 127 // static |
| 126 void TrackingSynchronizer::DeserializeTrackingList( | 128 void TrackingSynchronizer::DeserializeTrackingList( |
| 127 int sequence_number, | 129 int sequence_number, |
| 128 const std::string& tracking_data, | 130 const std::string& tracking_data, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 168 |
| 167 int sequence_number = GetNextAvailableSequenceNumber(); | 169 int sequence_number = GetNextAvailableSequenceNumber(); |
| 168 | 170 |
| 169 // Initialize processes_pending with one because we are going to send | 171 // Initialize processes_pending with one because we are going to send |
| 170 // browser's ThreadData. | 172 // browser's ThreadData. |
| 171 RequestContext* request = new RequestContext( | 173 RequestContext* request = new RequestContext( |
| 172 callback_object, sequence_number, 1, TimeTicks::Now()); | 174 callback_object, sequence_number, 1, TimeTicks::Now()); |
| 173 outstanding_requests_[sequence_number] = request; | 175 outstanding_requests_[sequence_number] = request; |
| 174 | 176 |
| 175 DCHECK_GT(request->processes_pending_, 0); | 177 DCHECK_GT(request->processes_pending_, 0); |
| 176 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); | 178 for (content::RenderProcessHost::iterator it( |
| 179 content::RenderProcessHost::AllHostsIterator()); |
| 177 !it.IsAtEnd(); it.Advance()) { | 180 !it.IsAtEnd(); it.Advance()) { |
| 178 RenderProcessHost* render_process_host = it.GetCurrentValue(); | 181 content::RenderProcessHost* render_process_host = it.GetCurrentValue(); |
| 179 DCHECK(render_process_host); | 182 DCHECK(render_process_host); |
| 180 // Ignore processes that don't have a connection, such as crashed tabs. | 183 // Ignore processes that don't have a connection, such as crashed tabs. |
| 181 if (!render_process_host->HasConnection()) | 184 if (!render_process_host->HasConnection()) |
| 182 continue; | 185 continue; |
| 183 | 186 |
| 184 ++request->processes_pending_; | 187 ++request->processes_pending_; |
| 185 if (!render_process_host->Send( | 188 if (!render_process_host->Send( |
| 186 new ChromeViewMsg_GetRendererTrackedData(sequence_number))) { | 189 new ChromeViewMsg_GetRendererTrackedData(sequence_number))) { |
| 187 DecrementPendingProcesses(sequence_number); | 190 DecrementPendingProcesses(sequence_number); |
| 188 } | 191 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 // Watch out for wrapping to a negative number. | 265 // Watch out for wrapping to a negative number. |
| 263 if (last_used_sequence_number_ < 0) | 266 if (last_used_sequence_number_ < 0) |
| 264 last_used_sequence_number_ = 1; | 267 last_used_sequence_number_ = 1; |
| 265 return last_used_sequence_number_; | 268 return last_used_sequence_number_; |
| 266 } | 269 } |
| 267 | 270 |
| 268 // static | 271 // static |
| 269 TrackingSynchronizer* TrackingSynchronizer::tracking_synchronizer_ = NULL; | 272 TrackingSynchronizer* TrackingSynchronizer::tracking_synchronizer_ = NULL; |
| 270 | 273 |
| 271 } // namespace chrome_browser_metrics | 274 } // namespace chrome_browser_metrics |
| OLD | NEW |