| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/data_usage/tab_id_annotator.h" | 5 #include "chrome/browser/data_usage/tab_id_annotator.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 void ExpectDataUseAndQuit(base::RunLoop* ui_run_loop, | 73 void ExpectDataUseAndQuit(base::RunLoop* ui_run_loop, |
| 74 std::unique_ptr<DataUse> expected, | 74 std::unique_ptr<DataUse> expected, |
| 75 std::unique_ptr<DataUse> actual) { | 75 std::unique_ptr<DataUse> actual) { |
| 76 DCHECK(ui_run_loop); | 76 DCHECK(ui_run_loop); |
| 77 ExpectDataUse(std::move(expected), std::move(actual)); | 77 ExpectDataUse(std::move(expected), std::move(actual)); |
| 78 | 78 |
| 79 // This can't use run_loop->QuitClosure here because that uses WeakPtrs, which | 79 // This can't use run_loop->QuitClosure here because that uses WeakPtrs, which |
| 80 // aren't thread safe. | 80 // aren't thread safe. |
| 81 BrowserThread::PostTask( | 81 BrowserThread::PostTask( |
| 82 BrowserThread::UI, FROM_HERE, | 82 BrowserThread::UI, FROM_HERE, |
| 83 base::Bind(&base::RunLoop::Quit, base::Unretained(ui_run_loop))); | 83 base::BindOnce(&base::RunLoop::Quit, base::Unretained(ui_run_loop))); |
| 84 } | 84 } |
| 85 | 85 |
| 86 // Tests that for a sample URLRequest, associated with the given | 86 // Tests that for a sample URLRequest, associated with the given |
| 87 // |render_process_id| and |render_frame_id|, repeatedly annotating DataUse for | 87 // |render_process_id| and |render_frame_id|, repeatedly annotating DataUse for |
| 88 // that URLRequest yields the |expected_tab_id|. |ui_run_loop| is the RunLoop on | 88 // that URLRequest yields the |expected_tab_id|. |ui_run_loop| is the RunLoop on |
| 89 // the UI thread that should be quit after all the annotations are done. | 89 // the UI thread that should be quit after all the annotations are done. |
| 90 // Passing in -1 for either or both of |render_process_id| or |render_frame_id| | 90 // Passing in -1 for either or both of |render_process_id| or |render_frame_id| |
| 91 // indicates that the URLRequest should have no associated ResourceRequestInfo. | 91 // indicates that the URLRequest should have no associated ResourceRequestInfo. |
| 92 void TestAnnotateOnIOThread(base::RunLoop* ui_run_loop, | 92 void TestAnnotateOnIOThread(base::RunLoop* ui_run_loop, |
| 93 int render_process_id, | 93 int render_process_id, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 CreateDataUse(expected_tab_id); | 130 CreateDataUse(expected_tab_id); |
| 131 annotator.Annotate(request.get(), CreateDataUse(kInvalidTabId), | 131 annotator.Annotate(request.get(), CreateDataUse(kInvalidTabId), |
| 132 base::Bind(&ExpectDataUseAndQuit, ui_run_loop, | 132 base::Bind(&ExpectDataUseAndQuit, ui_run_loop, |
| 133 base::Passed(&second_expected_data_use))); | 133 base::Passed(&second_expected_data_use))); |
| 134 } | 134 } |
| 135 | 135 |
| 136 TEST_F(TabIdAnnotatorTest, AnnotateWithNoRenderFrame) { | 136 TEST_F(TabIdAnnotatorTest, AnnotateWithNoRenderFrame) { |
| 137 base::RunLoop ui_run_loop; | 137 base::RunLoop ui_run_loop; |
| 138 BrowserThread::PostTask( | 138 BrowserThread::PostTask( |
| 139 BrowserThread::IO, FROM_HERE, | 139 BrowserThread::IO, FROM_HERE, |
| 140 base::Bind(&TestAnnotateOnIOThread, &ui_run_loop, | 140 base::BindOnce(&TestAnnotateOnIOThread, &ui_run_loop, |
| 141 -1 /* render_process_id */, -1 /* render_frame_id */, | 141 -1 /* render_process_id */, -1 /* render_frame_id */, |
| 142 -1 /* expected_tab_id */)); | 142 -1 /* expected_tab_id */)); |
| 143 ui_run_loop.Run(); | 143 ui_run_loop.Run(); |
| 144 } | 144 } |
| 145 | 145 |
| 146 TEST_F(TabIdAnnotatorTest, AnnotateWithRenderFrameAndNoTab) { | 146 TEST_F(TabIdAnnotatorTest, AnnotateWithRenderFrameAndNoTab) { |
| 147 base::RunLoop ui_run_loop; | 147 base::RunLoop ui_run_loop; |
| 148 // |web_contents()| isn't a tab, so it shouldn't have a tab ID. | 148 // |web_contents()| isn't a tab, so it shouldn't have a tab ID. |
| 149 EXPECT_EQ(-1, SessionTabHelper::IdForTab(web_contents())); | 149 EXPECT_EQ(-1, SessionTabHelper::IdForTab(web_contents())); |
| 150 | 150 |
| 151 BrowserThread::PostTask( | 151 BrowserThread::PostTask( |
| 152 BrowserThread::IO, FROM_HERE, | 152 BrowserThread::IO, FROM_HERE, |
| 153 base::Bind(&TestAnnotateOnIOThread, &ui_run_loop, | 153 base::BindOnce(&TestAnnotateOnIOThread, &ui_run_loop, |
| 154 web_contents()->GetMainFrame()->GetProcess()->GetID(), | 154 web_contents()->GetMainFrame()->GetProcess()->GetID(), |
| 155 web_contents()->GetMainFrame()->GetRoutingID(), | 155 web_contents()->GetMainFrame()->GetRoutingID(), |
| 156 -1 /* expected_tab_id */)); | 156 -1 /* expected_tab_id */)); |
| 157 ui_run_loop.Run(); | 157 ui_run_loop.Run(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 TEST_F(TabIdAnnotatorTest, AnnotateWithRenderFrameAndTab) { | 160 TEST_F(TabIdAnnotatorTest, AnnotateWithRenderFrameAndTab) { |
| 161 base::RunLoop ui_run_loop; | 161 base::RunLoop ui_run_loop; |
| 162 // Make |web_contents()| into a tab. | 162 // Make |web_contents()| into a tab. |
| 163 SessionTabHelper::CreateForWebContents(web_contents()); | 163 SessionTabHelper::CreateForWebContents(web_contents()); |
| 164 int32_t expected_tab_id = SessionTabHelper::IdForTab(web_contents()); | 164 int32_t expected_tab_id = SessionTabHelper::IdForTab(web_contents()); |
| 165 // |web_contents()| is a tab, so it should have a tab ID. | 165 // |web_contents()| is a tab, so it should have a tab ID. |
| 166 EXPECT_NE(-1, expected_tab_id); | 166 EXPECT_NE(-1, expected_tab_id); |
| 167 | 167 |
| 168 BrowserThread::PostTask( | 168 BrowserThread::PostTask( |
| 169 BrowserThread::IO, FROM_HERE, | 169 BrowserThread::IO, FROM_HERE, |
| 170 base::Bind(&TestAnnotateOnIOThread, &ui_run_loop, | 170 base::BindOnce(&TestAnnotateOnIOThread, &ui_run_loop, |
| 171 web_contents()->GetMainFrame()->GetProcess()->GetID(), | 171 web_contents()->GetMainFrame()->GetProcess()->GetID(), |
| 172 web_contents()->GetMainFrame()->GetRoutingID(), | 172 web_contents()->GetMainFrame()->GetRoutingID(), |
| 173 expected_tab_id)); | 173 expected_tab_id)); |
| 174 ui_run_loop.Run(); | 174 ui_run_loop.Run(); |
| 175 } | 175 } |
| 176 | 176 |
| 177 } // namespace | 177 } // namespace |
| 178 | 178 |
| 179 } // namespace chrome_browser_data_usage | 179 } // namespace chrome_browser_data_usage |
| OLD | NEW |