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 data_use_measurement { | |
19 | |
20 ChromeDataUseAscriberService::ChromeDataUseAscriberService() | |
21 : ascriber_(nullptr) { | |
22 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
23 | |
24 // Skip IO thread initialization if there is no IO thread. This check is | |
25 // required because unit tests that do no set up an IO thread can cause this | |
26 // code to execute. | |
27 if (!g_browser_process->io_thread()) | |
28 return; | |
29 | |
30 content::BrowserThread::PostTask( | |
Lei Zhang
2016/09/08 22:04:43
You can also try PostTaskAndReplyWithResult(), the
Not at Google. Contact bengr
2016/09/09 18:37:16
Good idea. Done.
| |
31 content::BrowserThread::IO, FROM_HERE, | |
32 base::Bind(&ChromeDataUseAscriberService::InitOnIOThread, | |
33 base::Unretained(this), g_browser_process->io_thread())); | |
34 } | |
35 | |
36 ChromeDataUseAscriberService::~ChromeDataUseAscriberService() { | |
37 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
38 } | |
39 | |
40 void ChromeDataUseAscriberService::RenderFrameCreated( | |
41 content::RenderFrameHost* render_frame_host) { | |
42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
43 | |
44 if (!ascriber_) | |
45 return; | |
46 | |
47 int parent_render_process_id = -1; | |
48 int parent_render_frame_id = -1; | |
49 if (render_frame_host->GetParent()) { | |
50 parent_render_process_id = | |
51 render_frame_host->GetParent()->GetProcess()->GetID(); | |
52 parent_render_frame_id = render_frame_host->GetParent()->GetRoutingID(); | |
53 } | |
54 content::BrowserThread::PostTask( | |
55 content::BrowserThread::IO, FROM_HERE, | |
56 base::Bind(&ChromeDataUseAscriber::RenderFrameCreated, | |
57 base::Unretained(ascriber_), | |
58 render_frame_host->GetProcess()->GetID(), | |
59 render_frame_host->GetRoutingID(), parent_render_process_id, | |
60 parent_render_frame_id)); | |
61 } | |
62 | |
63 void ChromeDataUseAscriberService::RenderFrameDeleted( | |
64 content::RenderFrameHost* render_frame_host) { | |
65 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
66 | |
67 if (!ascriber_) | |
68 return; | |
69 | |
70 int parent_render_process_id, parent_render_frame_id = -1; | |
Lei Zhang
2016/09/08 22:04:43
- One decl per line.
- Did |parent_render_process_
Not at Google. Contact bengr
2016/09/09 18:37:16
Done. Nope, |parent_render_process_id| will not be
| |
71 | |
72 if (render_frame_host->GetParent()) { | |
73 parent_render_process_id = | |
74 render_frame_host->GetParent()->GetProcess()->GetID(); | |
75 parent_render_frame_id = render_frame_host->GetParent()->GetRoutingID(); | |
76 } | |
77 content::BrowserThread::PostTask( | |
78 content::BrowserThread::IO, FROM_HERE, | |
79 base::Bind(&ChromeDataUseAscriber::RenderFrameDeleted, | |
80 base::Unretained(ascriber_), | |
81 render_frame_host->GetProcess()->GetID(), | |
82 render_frame_host->GetRoutingID(), parent_render_process_id, | |
83 parent_render_frame_id)); | |
84 } | |
85 | |
86 void ChromeDataUseAscriberService::DidStartNavigation( | |
87 content::NavigationHandle* navigation_handle) { | |
88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
89 | |
90 if (!ascriber_) | |
91 return; | |
92 | |
93 if (navigation_handle->IsInMainFrame()) { | |
Lei Zhang
2016/09/08 22:04:43
I'd just do:
if (!navigation_handle->IsInMainFram
Not at Google. Contact bengr
2016/09/09 18:37:16
Done.
| |
94 content::BrowserThread::PostTask( | |
95 content::BrowserThread::IO, FROM_HERE, | |
96 base::Bind( | |
97 &ChromeDataUseAscriber::DidStartMainFrameNavigation, | |
98 base::Unretained(ascriber_), navigation_handle->GetURL(), | |
99 navigation_handle->GetWebContents() | |
Lei Zhang
2016/09/08 22:04:43
Create a local variable for navigation_handle->Get
Not at Google. Contact bengr
2016/09/09 18:37:16
Done.
| |
100 ->GetRenderProcessHost() | |
101 ->GetID(), | |
102 navigation_handle->GetWebContents()->GetMainFrame()->GetRoutingID(), | |
103 navigation_handle)); | |
104 } | |
105 } | |
106 | |
107 void ChromeDataUseAscriberService::DidFinishNavigation( | |
108 content::NavigationHandle* navigation_handle) { | |
109 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
110 | |
111 if (!ascriber_) | |
112 return; | |
113 | |
114 if (navigation_handle->IsInMainFrame()) { | |
115 content::BrowserThread::PostTask( | |
116 content::BrowserThread::IO, FROM_HERE, | |
117 base::Bind( | |
118 &ChromeDataUseAscriber::DidFinishMainFrameNavigation, | |
119 base::Unretained(ascriber_), navigation_handle->GetURL(), | |
120 navigation_handle->GetWebContents() | |
121 ->GetRenderProcessHost() | |
122 ->GetID(), | |
123 navigation_handle->GetWebContents()->GetMainFrame()->GetRoutingID(), | |
124 !navigation_handle->HasCommitted() || | |
125 navigation_handle->IsSamePage(), | |
126 navigation_handle)); | |
127 } | |
128 } | |
129 | |
130 void ChromeDataUseAscriberService::DidRedirectNavigation( | |
131 content::NavigationHandle* navigation_handle) { | |
132 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
133 | |
134 if (!ascriber_) | |
135 return; | |
136 | |
137 if (navigation_handle->IsInMainFrame()) { | |
138 content::BrowserThread::PostTask( | |
139 content::BrowserThread::IO, FROM_HERE, | |
140 base::Bind( | |
141 &ChromeDataUseAscriber::DidRedirectMainFrameNavigation, | |
142 base::Unretained(ascriber_), navigation_handle->GetURL(), | |
143 navigation_handle->GetWebContents() | |
144 ->GetRenderProcessHost() | |
145 ->GetID(), | |
146 navigation_handle->GetWebContents()->GetMainFrame()->GetRoutingID(), | |
147 navigation_handle)); | |
148 } | |
149 } | |
150 | |
151 void ChromeDataUseAscriberService::InitOnIOThread(IOThread* io_thread) { | |
152 DCHECK(io_thread); | |
153 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
154 | |
155 content::BrowserThread::PostTask( | |
156 content::BrowserThread::UI, FROM_HERE, | |
157 base::Bind(&ChromeDataUseAscriberService::SetDataUseAscriber, | |
158 base::Unretained(this), | |
159 io_thread->globals()->data_use_ascriber.get())); | |
160 } | |
161 | |
162 void ChromeDataUseAscriberService::SetDataUseAscriber( | |
163 ChromeDataUseAscriber* ascriber) { | |
164 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
165 | |
166 ascriber_ = ascriber; | |
167 } | |
168 | |
169 } // namespace data_use_measurement | |
OLD | NEW |