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, FROM_HERE, |
| 44 base::Bind(&InitOnIOThread, g_browser_process->io_thread()), |
| 45 base::Bind(&ChromeDataUseAscriberService::SetDataUseAscriber, |
| 46 base::Unretained(this))); |
| 47 } |
| 48 |
| 49 ChromeDataUseAscriberService::~ChromeDataUseAscriberService() { |
| 50 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 51 } |
| 52 |
| 53 void ChromeDataUseAscriberService::RenderFrameCreated( |
| 54 content::RenderFrameHost* render_frame_host) { |
| 55 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 56 |
| 57 if (!ascriber_) |
| 58 return; |
| 59 |
| 60 int parent_render_process_id = -1; |
| 61 int parent_render_frame_id = -1; |
| 62 if (render_frame_host->GetParent()) { |
| 63 parent_render_process_id = |
| 64 render_frame_host->GetParent()->GetProcess()->GetID(); |
| 65 parent_render_frame_id = render_frame_host->GetParent()->GetRoutingID(); |
| 66 } |
| 67 content::BrowserThread::PostTask( |
| 68 content::BrowserThread::IO, FROM_HERE, |
| 69 base::Bind(&ChromeDataUseAscriber::RenderFrameCreated, |
| 70 base::Unretained(ascriber_), |
| 71 render_frame_host->GetProcess()->GetID(), |
| 72 render_frame_host->GetRoutingID(), parent_render_process_id, |
| 73 parent_render_frame_id)); |
| 74 } |
| 75 |
| 76 void ChromeDataUseAscriberService::RenderFrameDeleted( |
| 77 content::RenderFrameHost* render_frame_host) { |
| 78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 79 |
| 80 if (!ascriber_) |
| 81 return; |
| 82 |
| 83 int parent_render_frame_id = -1; |
| 84 int parent_render_process_id = -1; |
| 85 |
| 86 if (render_frame_host->GetParent()) { |
| 87 parent_render_process_id = |
| 88 render_frame_host->GetParent()->GetProcess()->GetID(); |
| 89 parent_render_frame_id = render_frame_host->GetParent()->GetRoutingID(); |
| 90 } |
| 91 content::BrowserThread::PostTask( |
| 92 content::BrowserThread::IO, FROM_HERE, |
| 93 base::Bind(&ChromeDataUseAscriber::RenderFrameDeleted, |
| 94 base::Unretained(ascriber_), |
| 95 render_frame_host->GetProcess()->GetID(), |
| 96 render_frame_host->GetRoutingID(), parent_render_process_id, |
| 97 parent_render_frame_id)); |
| 98 } |
| 99 |
| 100 void ChromeDataUseAscriberService::DidStartNavigation( |
| 101 content::NavigationHandle* navigation_handle) { |
| 102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 103 |
| 104 if (!ascriber_ || !navigation_handle->IsInMainFrame()) |
| 105 return; |
| 106 |
| 107 content::WebContents* web_contents = navigation_handle->GetWebContents(); |
| 108 content::BrowserThread::PostTask( |
| 109 content::BrowserThread::IO, FROM_HERE, |
| 110 base::Bind(&ChromeDataUseAscriber::DidStartMainFrameNavigation, |
| 111 base::Unretained(ascriber_), navigation_handle->GetURL(), |
| 112 web_contents->GetRenderProcessHost()->GetID(), |
| 113 web_contents->GetMainFrame()->GetRoutingID(), |
| 114 navigation_handle)); |
| 115 } |
| 116 |
| 117 void ChromeDataUseAscriberService::DidFinishNavigation( |
| 118 content::NavigationHandle* navigation_handle) { |
| 119 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 120 |
| 121 if (!ascriber_ || !navigation_handle->IsInMainFrame()) |
| 122 return; |
| 123 |
| 124 content::WebContents* web_contents = navigation_handle->GetWebContents(); |
| 125 content::BrowserThread::PostTask( |
| 126 content::BrowserThread::IO, FROM_HERE, |
| 127 base::Bind( |
| 128 &ChromeDataUseAscriber::DidFinishMainFrameNavigation, |
| 129 base::Unretained(ascriber_), navigation_handle->GetURL(), |
| 130 web_contents->GetRenderProcessHost()->GetID(), |
| 131 web_contents->GetMainFrame()->GetRoutingID(), |
| 132 !navigation_handle->HasCommitted() || navigation_handle->IsSamePage(), |
| 133 navigation_handle)); |
| 134 } |
| 135 |
| 136 void ChromeDataUseAscriberService::DidRedirectNavigation( |
| 137 content::NavigationHandle* navigation_handle) { |
| 138 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 139 |
| 140 if (!ascriber_ || !navigation_handle->IsInMainFrame()) |
| 141 return; |
| 142 |
| 143 content::WebContents* web_contents = navigation_handle->GetWebContents(); |
| 144 content::BrowserThread::PostTask( |
| 145 content::BrowserThread::IO, FROM_HERE, |
| 146 base::Bind(&ChromeDataUseAscriber::DidRedirectMainFrameNavigation, |
| 147 base::Unretained(ascriber_), navigation_handle->GetURL(), |
| 148 web_contents->GetRenderProcessHost()->GetID(), |
| 149 web_contents->GetMainFrame()->GetRoutingID(), |
| 150 navigation_handle)); |
| 151 } |
| 152 |
| 153 void ChromeDataUseAscriberService::SetDataUseAscriber( |
| 154 ChromeDataUseAscriber* ascriber) { |
| 155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 156 |
| 157 ascriber_ = ascriber; |
| 158 } |
| 159 |
| 160 } // namespace data_use_measurement |
OLD | NEW |