Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 // Attempt to get the associated tab ID for a given render frame. Returns -1 if | |
|
bengr
2015/11/05 00:00:37
"Attempt" -> "Attempts"
sclittle
2015/11/05 01:22:48
Done.
| |
| 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(scoped_ptr<DataUse> data_use, | |
|
bengr
2015/11/05 00:00:37
Why not just have the caller call the callback? Wh
sclittle
2015/11/05 01:22:48
For simplicity and ease of testing, the caller (wh
| |
| 46 const base::Callback<void(scoped_ptr<DataUse>)>& callback, | |
|
bengr
2015/11/05 00:00:37
Consider using a typedef.
sclittle
2015/11/05 01:22:48
Done.
| |
| 47 int32_t tab_id) { | |
| 48 DCHECK(data_use); | |
| 49 data_use->tab_id = tab_id; | |
|
bengr
2015/11/05 00:00:38
I'd prefer having setters on DataUse.
sclittle
2015/11/05 01:22:48
That would be for a separate CL, although I disagr
| |
| 50 callback.Run(data_use.Pass()); | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 TabIdAnnotator::TabIdAnnotator() {} | |
| 56 TabIdAnnotator::~TabIdAnnotator() {} | |
| 57 | |
| 58 void TabIdAnnotator::Annotate( | |
| 59 net::URLRequest* request, | |
| 60 scoped_ptr<DataUse> data_use, | |
| 61 const base::Callback<void(scoped_ptr<DataUse>)>& callback) { | |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 63 DCHECK(request); | |
|
bengr
2015/11/05 00:00:38
I've heard and agree with the argument that there'
sclittle
2015/11/05 01:22:48
Sure. The argument for having these pointer DCHECK
| |
| 64 DCHECK(data_use); | |
| 65 | |
| 66 TabIdProvider* existing_tab_id_provider = reinterpret_cast<TabIdProvider*>( | |
| 67 request->GetUserData(TabIdProvider::kUserDataKey)); | |
| 68 if (existing_tab_id_provider) { | |
| 69 existing_tab_id_provider->ProvideTabId( | |
| 70 base::Bind(&AnnotateDataUse, base::Passed(&data_use), callback)); | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 int render_process_id = -1, render_frame_id = -1; | |
| 75 if (!content::ResourceRequestInfo::GetRenderFrameForRequest( | |
| 76 request, &render_process_id, &render_frame_id)) { | |
| 77 // Run the callback immediately with a tab ID of -1 if the request has no | |
| 78 // render frame. | |
| 79 AnnotateDataUse(data_use.Pass(), callback, -1 /* tab_id */); | |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner = | |
| 84 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | |
| 85 scoped_ptr<TabIdProvider> tab_id_provider(new TabIdProvider( | |
| 86 ui_thread_task_runner.get(), FROM_HERE, | |
| 87 base::Bind(&GetTabIdForRenderFrame, render_process_id, render_frame_id))); | |
| 88 tab_id_provider->ProvideTabId( | |
| 89 base::Bind(&AnnotateDataUse, base::Passed(&data_use), callback)); | |
| 90 | |
| 91 // |request| takes ownership of |tab_id_provider|. | |
| 92 request->SetUserData(TabIdProvider::kUserDataKey, tab_id_provider.release()); | |
| 93 } | |
| 94 | |
| 95 } // namespace chrome_browser_data_usage | |
| OLD | NEW |