ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / search / instant_io_context.cc
blob777f16fa58170cbe44952ae1d72d89d75aaf5e9b
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"
11 #include "url/gurl.h"
13 using content::BrowserThread;
15 namespace {
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);
28 if (!info)
29 return NULL;
31 return GetDataForResourceContext(info->GetContext());
34 } // namespace
36 const char InstantIOContext::kInstantIOContextKeyName[] = "instant_io_context";
38 InstantIOContext::InstantIOContext() {
39 // The InstantIOContext is created on the UI thread but is accessed
40 // on the IO thread.
41 DCHECK_CURRENTLY_ON(BrowserThread::UI);
44 InstantIOContext::~InstantIOContext() {
47 // static
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()));
56 // static
57 void InstantIOContext::AddInstantProcessOnIO(
58 scoped_refptr<InstantIOContext> instant_io_context,
59 int process_id) {
60 DCHECK_CURRENTLY_ON(BrowserThread::IO);
61 instant_io_context->process_ids_.insert(process_id);
64 // static
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);
71 // static
72 void InstantIOContext::ClearInstantProcessesOnIO(
73 scoped_refptr<InstantIOContext> instant_io_context) {
74 DCHECK_CURRENTLY_ON(BrowserThread::IO);
75 instant_io_context->process_ids_.clear();
78 // static
79 bool InstantIOContext::ShouldServiceRequest(const net::URLRequest* request) {
80 const content::ResourceRequestInfo* info =
81 content::ResourceRequestInfo::ForRequest(request);
82 if (!info)
83 return false;
85 InstantIOContext* instant_io_context = GetDataForRequest(request);
86 if (!instant_io_context)
87 return false;
89 int process_id = -1;
90 int render_frame_id = -1;
91 if (info->GetAssociatedRenderFrame(&process_id, &render_frame_id) &&
92 instant_io_context->IsInstantProcess(process_id))
93 return true;
94 return false;
97 bool InstantIOContext::IsInstantProcess(int process_id) const {
98 DCHECK_CURRENTLY_ON(BrowserThread::IO);
99 return process_ids_.find(process_id) != process_ids_.end();