Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 #include "chrome/browser/data_use_measurement/chrome_data_use_ascriber_service.h " | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "chrome/browser/browser_process.h" | |
| 10 #include "chrome/browser/data_use_measurement/chrome_data_use_ascriber.h" | |
| 11 #include "chrome/browser/io_thread.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "content/public/browser/navigation_handle.h" | |
| 14 #include "content/public/browser/render_frame_host.h" | |
| 15 #include "content/public/browser/render_process_host.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 data_use_measurement::ChromeDataUseAscriber* InitOnIOThread( | |
| 21 IOThread* io_thread) { | |
| 22 DCHECK(io_thread); | |
| 23 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 24 | |
| 25 return io_thread->globals()->data_use_ascriber.get(); | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 namespace data_use_measurement { | |
| 31 | |
| 32 ChromeDataUseAscriberService::ChromeDataUseAscriberService() | |
| 33 : ascriber_(nullptr) { | |
| 34 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 35 | |
| 36 // Skip IO thread initialization if there is no IO thread. This check is | |
| 37 // required because unit tests that do no set up an IO thread can cause this | |
| 38 // code to execute. | |
| 39 if (!g_browser_process->io_thread()) | |
| 40 return; | |
| 41 | |
| 42 content::BrowserThread::PostTaskAndReplyWithResult( | |
| 43 content::BrowserThread::IO, | |
| 44 FROM_HERE, | |
| 45 base::Bind(&InitOnIOThread, g_browser_process->io_thread()), | |
| 46 base::Bind(&ChromeDataUseAscriberService::SetDataUseAscriber, | |
| 47 base::Unretained(this))); | |
| 48 } | |
| 49 | |
| 50 ChromeDataUseAscriberService::~ChromeDataUseAscriberService() { | |
| 51 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 52 } | |
| 53 | |
| 54 void ChromeDataUseAscriberService::RenderFrameCreated( | |
| 55 content::RenderFrameHost* render_frame_host) { | |
| 56 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 57 | |
| 58 if (!ascriber_) | |
| 59 return; | |
| 60 | |
| 61 int parent_render_process_id = -1; | |
| 62 int parent_render_frame_id = -1; | |
| 63 if (render_frame_host->GetParent()) { | |
| 64 parent_render_process_id = | |
| 65 render_frame_host->GetParent()->GetProcess()->GetID(); | |
| 66 parent_render_frame_id = render_frame_host->GetParent()->GetRoutingID(); | |
| 67 } | |
| 68 content::BrowserThread::PostTask( | |
| 69 content::BrowserThread::IO, FROM_HERE, | |
| 70 base::Bind(&ChromeDataUseAscriber::RenderFrameCreated, | |
| 71 base::Unretained(ascriber_), | |
| 72 render_frame_host->GetProcess()->GetID(), | |
| 73 render_frame_host->GetRoutingID(), parent_render_process_id, | |
| 74 parent_render_frame_id)); | |
| 75 } | |
| 76 | |
| 77 void ChromeDataUseAscriberService::RenderFrameDeleted( | |
| 78 content::RenderFrameHost* render_frame_host) { | |
| 79 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 80 | |
| 81 if (!ascriber_) | |
| 82 return; | |
| 83 | |
| 84 int parent_render_frame_id = -1; | |
| 85 int parent_render_process_id = -1; | |
| 86 | |
| 87 if (render_frame_host->GetParent()) { | |
| 88 parent_render_process_id = | |
| 89 render_frame_host->GetParent()->GetProcess()->GetID(); | |
| 90 parent_render_frame_id = render_frame_host->GetParent()->GetRoutingID(); | |
| 91 } | |
| 92 content::BrowserThread::PostTask( | |
| 93 content::BrowserThread::IO, FROM_HERE, | |
| 94 base::Bind(&ChromeDataUseAscriber::RenderFrameDeleted, | |
| 95 base::Unretained(ascriber_), | |
| 96 render_frame_host->GetProcess()->GetID(), | |
| 97 render_frame_host->GetRoutingID(), parent_render_process_id, | |
| 98 parent_render_frame_id)); | |
| 99 } | |
| 100 | |
| 101 void ChromeDataUseAscriberService::DidStartNavigation( | |
| 102 content::NavigationHandle* navigation_handle) { | |
| 103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 104 | |
| 105 if (!ascriber_ || !navigation_handle->IsInMainFrame()) | |
| 106 return; | |
| 107 | |
| 108 content::WebContents* web_contents = navigation_handle->GetWebContents(); | |
| 109 content::BrowserThread::PostTask( | |
| 110 content::BrowserThread::IO, FROM_HERE, | |
| 111 base::Bind( | |
| 112 &ChromeDataUseAscriber::DidStartMainFrameNavigation, | |
| 113 base::Unretained(ascriber_), navigation_handle->GetURL(), | |
| 114 web_contents->GetRenderProcessHost()->GetID(), | |
| 115 navigation_handle->GetWebContents()->GetMainFrame()->GetRoutingID(), | |
|
Lei Zhang
2016/09/09 20:58:42
Use |web_contents| here too.
Not at Google. Contact bengr
2016/09/09 21:36:37
Done.
| |
| 116 navigation_handle)); | |
| 117 } | |
| 118 | |
| 119 void ChromeDataUseAscriberService::DidFinishNavigation( | |
| 120 content::NavigationHandle* navigation_handle) { | |
| 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 122 | |
| 123 if (!ascriber_ || !navigation_handle->IsInMainFrame()) | |
| 124 return; | |
| 125 | |
| 126 content::BrowserThread::PostTask( | |
| 127 content::BrowserThread::IO, FROM_HERE, | |
| 128 base::Bind( | |
| 129 &ChromeDataUseAscriber::DidFinishMainFrameNavigation, | |
| 130 base::Unretained(ascriber_), navigation_handle->GetURL(), | |
| 131 navigation_handle->GetWebContents() | |
|
Lei Zhang
2016/09/09 20:58:42
Same pattern as above.
Not at Google. Contact bengr
2016/09/09 21:36:37
Done.
| |
| 132 ->GetRenderProcessHost() | |
| 133 ->GetID(), | |
| 134 navigation_handle->GetWebContents()->GetMainFrame()->GetRoutingID(), | |
| 135 !navigation_handle->HasCommitted() || | |
| 136 navigation_handle->IsSamePage(), | |
| 137 navigation_handle)); | |
| 138 } | |
| 139 | |
| 140 void ChromeDataUseAscriberService::DidRedirectNavigation( | |
| 141 content::NavigationHandle* navigation_handle) { | |
| 142 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 143 | |
| 144 if (!ascriber_ || !navigation_handle->IsInMainFrame()) | |
| 145 return; | |
| 146 | |
| 147 content::BrowserThread::PostTask( | |
| 148 content::BrowserThread::IO, FROM_HERE, | |
| 149 base::Bind( | |
| 150 &ChromeDataUseAscriber::DidRedirectMainFrameNavigation, | |
| 151 base::Unretained(ascriber_), navigation_handle->GetURL(), | |
| 152 navigation_handle->GetWebContents() | |
|
Lei Zhang
2016/09/09 20:58:42
One more...
Not at Google. Contact bengr
2016/09/09 21:36:37
Done.
| |
| 153 ->GetRenderProcessHost() | |
| 154 ->GetID(), | |
| 155 navigation_handle->GetWebContents()->GetMainFrame()->GetRoutingID(), | |
| 156 navigation_handle)); | |
| 157 } | |
| 158 | |
| 159 void ChromeDataUseAscriberService::SetDataUseAscriber( | |
| 160 ChromeDataUseAscriber* ascriber) { | |
| 161 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 162 | |
| 163 ascriber_ = ascriber; | |
| 164 } | |
| 165 | |
| 166 } // namespace data_use_measurement | |
| OLD | NEW |