1 // Copyright 2013 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.
5 #include "chrome/browser/search/instant_io_context.h"
7 #include "content/public/browser/browser_thread.h"
8 #include "content/public/browser/resource_context.h"
9 #include "content/public/browser/resource_request_info.h"
10 #include "net/url_request/url_request.h"
13 using content::BrowserThread
;
17 // Retrieves the Instant data from the |context|'s named user-data.
18 InstantIOContext
* GetDataForResourceContext(
19 content::ResourceContext
* context
) {
20 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
21 return base::UserDataAdapter
<InstantIOContext
>::Get(
22 context
, InstantIOContext::kInstantIOContextKeyName
);
25 InstantIOContext
* GetDataForRequest(const net::URLRequest
* request
) {
26 const content::ResourceRequestInfo
* info
=
27 content::ResourceRequestInfo::ForRequest(request
);
31 return GetDataForResourceContext(info
->GetContext());
36 const char InstantIOContext::kInstantIOContextKeyName
[] = "instant_io_context";
38 InstantIOContext::InstantIOContext() {
39 // The InstantIOContext is created on the UI thread but is accessed
41 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
44 InstantIOContext::~InstantIOContext() {
48 void InstantIOContext::SetUserDataOnIO(
49 content::ResourceContext
* resource_context
,
50 scoped_refptr
<InstantIOContext
> instant_io_context
) {
51 resource_context
->SetUserData(
52 InstantIOContext::kInstantIOContextKeyName
,
53 new base::UserDataAdapter
<InstantIOContext
>(instant_io_context
.get()));
57 void InstantIOContext::AddInstantProcessOnIO(
58 scoped_refptr
<InstantIOContext
> instant_io_context
,
60 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
61 instant_io_context
->process_ids_
.insert(process_id
);
65 void InstantIOContext::RemoveInstantProcessOnIO(
66 scoped_refptr
<InstantIOContext
> instant_io_context
, int process_id
) {
67 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
68 instant_io_context
->process_ids_
.erase(process_id
);
72 void InstantIOContext::ClearInstantProcessesOnIO(
73 scoped_refptr
<InstantIOContext
> instant_io_context
) {
74 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
75 instant_io_context
->process_ids_
.clear();
79 bool InstantIOContext::ShouldServiceRequest(const net::URLRequest
* request
) {
80 const content::ResourceRequestInfo
* info
=
81 content::ResourceRequestInfo::ForRequest(request
);
85 InstantIOContext
* instant_io_context
= GetDataForRequest(request
);
86 if (!instant_io_context
)
90 int render_frame_id
= -1;
91 if (info
->GetAssociatedRenderFrame(&process_id
, &render_frame_id
) &&
92 instant_io_context
->IsInstantProcess(process_id
))
97 bool InstantIOContext::IsInstantProcess(int process_id
) const {
98 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
99 return process_ids_
.find(process_id
) != process_ids_
.end();