| 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 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/location.h" | 12 #include "base/location.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 14 #include "chrome/browser/data_usage/tab_id_provider.h" | 15 #include "chrome/browser/data_usage/tab_id_provider.h" |
| 15 #include "chrome/browser/sessions/session_tab_helper.h" | 16 #include "chrome/browser/sessions/session_tab_helper.h" |
| 16 #include "components/data_usage/core/data_use.h" | 17 #include "components/data_usage/core/data_use.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 41 // This is done in a separate function instead of as a method on TabIdAnnotator | 42 // This is done in a separate function instead of as a method on TabIdAnnotator |
| 42 // so that an in-progress annotation can complete even if the TabIdAnnotator is | 43 // so that an in-progress annotation can complete even if the TabIdAnnotator is |
| 43 // destroyed. This doesn't make much of a difference for production code, but | 44 // destroyed. This doesn't make much of a difference for production code, but |
| 44 // makes it easier to test the TabIdAnnotator. | 45 // makes it easier to test the TabIdAnnotator. |
| 45 void AnnotateDataUse( | 46 void AnnotateDataUse( |
| 46 scoped_ptr<DataUse> data_use, | 47 scoped_ptr<DataUse> data_use, |
| 47 const data_usage::DataUseAnnotator::DataUseConsumerCallback& callback, | 48 const data_usage::DataUseAnnotator::DataUseConsumerCallback& callback, |
| 48 int32_t tab_id) { | 49 int32_t tab_id) { |
| 49 DCHECK(data_use); | 50 DCHECK(data_use); |
| 50 data_use->tab_id = tab_id; | 51 data_use->tab_id = tab_id; |
| 51 callback.Run(data_use.Pass()); | 52 callback.Run(std::move(data_use)); |
| 52 } | 53 } |
| 53 | 54 |
| 54 } // namespace | 55 } // namespace |
| 55 | 56 |
| 56 TabIdAnnotator::TabIdAnnotator() {} | 57 TabIdAnnotator::TabIdAnnotator() {} |
| 57 TabIdAnnotator::~TabIdAnnotator() {} | 58 TabIdAnnotator::~TabIdAnnotator() {} |
| 58 | 59 |
| 59 void TabIdAnnotator::Annotate(net::URLRequest* request, | 60 void TabIdAnnotator::Annotate(net::URLRequest* request, |
| 60 scoped_ptr<DataUse> data_use, | 61 scoped_ptr<DataUse> data_use, |
| 61 const DataUseConsumerCallback& callback) { | 62 const DataUseConsumerCallback& callback) { |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); | 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 63 DCHECK(data_use); | 64 DCHECK(data_use); |
| 64 | 65 |
| 65 TabIdProvider* existing_tab_id_provider = reinterpret_cast<TabIdProvider*>( | 66 TabIdProvider* existing_tab_id_provider = reinterpret_cast<TabIdProvider*>( |
| 66 request->GetUserData(TabIdProvider::kUserDataKey)); | 67 request->GetUserData(TabIdProvider::kUserDataKey)); |
| 67 if (existing_tab_id_provider) { | 68 if (existing_tab_id_provider) { |
| 68 existing_tab_id_provider->ProvideTabId( | 69 existing_tab_id_provider->ProvideTabId( |
| 69 base::Bind(&AnnotateDataUse, base::Passed(&data_use), callback)); | 70 base::Bind(&AnnotateDataUse, base::Passed(&data_use), callback)); |
| 70 return; | 71 return; |
| 71 } | 72 } |
| 72 | 73 |
| 73 int render_process_id = -1, render_frame_id = -1; | 74 int render_process_id = -1, render_frame_id = -1; |
| 74 if (!content::ResourceRequestInfo::GetRenderFrameForRequest( | 75 if (!content::ResourceRequestInfo::GetRenderFrameForRequest( |
| 75 request, &render_process_id, &render_frame_id)) { | 76 request, &render_process_id, &render_frame_id)) { |
| 76 // Run the callback immediately with a tab ID of -1 if the request has no | 77 // Run the callback immediately with a tab ID of -1 if the request has no |
| 77 // render frame. | 78 // render frame. |
| 78 AnnotateDataUse(data_use.Pass(), callback, -1 /* tab_id */); | 79 AnnotateDataUse(std::move(data_use), callback, -1 /* tab_id */); |
| 79 return; | 80 return; |
| 80 } | 81 } |
| 81 | 82 |
| 82 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner = | 83 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner = |
| 83 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | 84 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
| 84 scoped_ptr<TabIdProvider> tab_id_provider(new TabIdProvider( | 85 scoped_ptr<TabIdProvider> tab_id_provider(new TabIdProvider( |
| 85 ui_thread_task_runner.get(), FROM_HERE, | 86 ui_thread_task_runner.get(), FROM_HERE, |
| 86 base::Bind(&GetTabIdForRenderFrame, render_process_id, render_frame_id))); | 87 base::Bind(&GetTabIdForRenderFrame, render_process_id, render_frame_id))); |
| 87 tab_id_provider->ProvideTabId( | 88 tab_id_provider->ProvideTabId( |
| 88 base::Bind(&AnnotateDataUse, base::Passed(&data_use), callback)); | 89 base::Bind(&AnnotateDataUse, base::Passed(&data_use), callback)); |
| 89 | 90 |
| 90 // |request| takes ownership of |tab_id_provider|. | 91 // |request| takes ownership of |tab_id_provider|. |
| 91 request->SetUserData(TabIdProvider::kUserDataKey, tab_id_provider.release()); | 92 request->SetUserData(TabIdProvider::kUserDataKey, tab_id_provider.release()); |
| 92 } | 93 } |
| 93 | 94 |
| 94 } // namespace chrome_browser_data_usage | 95 } // namespace chrome_browser_data_usage |
| OLD | NEW |