OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/data_usage/tab_id_annotator.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
| 11 #include "base/location.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "chrome/browser/data_usage/tab_id_provider.h" |
| 15 #include "chrome/browser/sessions/session_tab_helper.h" |
| 16 #include "components/data_usage/core/data_use.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/render_frame_host.h" |
| 19 #include "content/public/browser/resource_request_info.h" |
| 20 #include "content/public/browser/web_contents.h" |
| 21 #include "net/url_request/url_request.h" |
| 22 |
| 23 using content::BrowserThread; |
| 24 using data_usage::DataUse; |
| 25 |
| 26 namespace chrome_browser_data_usage { |
| 27 |
| 28 namespace { |
| 29 |
| 30 // Attempts to get the associated tab ID for a given render frame. Returns -1 if |
| 31 // no associated tab was found. |
| 32 int32_t GetTabIdForRenderFrame(int render_process_id, int render_frame_id) { |
| 33 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 34 // TODO(sclittle): For prerendering tabs, investigate if it's possible to find |
| 35 // the original tab that initiated the prerender. |
| 36 return SessionTabHelper::IdForTab(content::WebContents::FromRenderFrameHost( |
| 37 content::RenderFrameHost::FromID(render_process_id, render_frame_id))); |
| 38 } |
| 39 |
| 40 // Annotates |data_use| with the given |tab_id|, then passes it to |callback|. |
| 41 // 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 // destroyed. This doesn't make much of a difference for production code, but |
| 44 // makes it easier to test the TabIdAnnotator. |
| 45 void AnnotateDataUse( |
| 46 scoped_ptr<DataUse> data_use, |
| 47 const data_usage::DataUseAnnotator::DataUseConsumerCallback& callback, |
| 48 int32_t tab_id) { |
| 49 DCHECK(data_use); |
| 50 data_use->tab_id = tab_id; |
| 51 callback.Run(data_use.Pass()); |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
| 56 TabIdAnnotator::TabIdAnnotator() {} |
| 57 TabIdAnnotator::~TabIdAnnotator() {} |
| 58 |
| 59 void TabIdAnnotator::Annotate(net::URLRequest* request, |
| 60 scoped_ptr<DataUse> data_use, |
| 61 const DataUseConsumerCallback& callback) { |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); |
| 63 DCHECK(data_use); |
| 64 |
| 65 TabIdProvider* existing_tab_id_provider = reinterpret_cast<TabIdProvider*>( |
| 66 request->GetUserData(TabIdProvider::kUserDataKey)); |
| 67 if (existing_tab_id_provider) { |
| 68 existing_tab_id_provider->ProvideTabId( |
| 69 base::Bind(&AnnotateDataUse, base::Passed(&data_use), callback)); |
| 70 return; |
| 71 } |
| 72 |
| 73 int render_process_id = -1, render_frame_id = -1; |
| 74 if (!content::ResourceRequestInfo::GetRenderFrameForRequest( |
| 75 request, &render_process_id, &render_frame_id)) { |
| 76 // Run the callback immediately with a tab ID of -1 if the request has no |
| 77 // render frame. |
| 78 AnnotateDataUse(data_use.Pass(), callback, -1 /* tab_id */); |
| 79 return; |
| 80 } |
| 81 |
| 82 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner = |
| 83 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
| 84 scoped_ptr<TabIdProvider> tab_id_provider(new TabIdProvider( |
| 85 ui_thread_task_runner.get(), FROM_HERE, |
| 86 base::Bind(&GetTabIdForRenderFrame, render_process_id, render_frame_id))); |
| 87 tab_id_provider->ProvideTabId( |
| 88 base::Bind(&AnnotateDataUse, base::Passed(&data_use), callback)); |
| 89 |
| 90 // |request| takes ownership of |tab_id_provider|. |
| 91 request->SetUserData(TabIdProvider::kUserDataKey, tab_id_provider.release()); |
| 92 } |
| 93 |
| 94 } // namespace chrome_browser_data_usage |
OLD | NEW |