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. 4 5 #include "chrome/browser/search/instant_io_context.h" 6 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" 12 13 using content::BrowserThread; 14 15 namespace { 16 17 // Retrieves the Instant data from the |context|'s named user-data. 18 InstantIOContext* GetDataForResourceContext( 19 content::ResourceContext* context) { 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 21 return base::UserDataAdapter<InstantIOContext>::Get( 22 context, InstantIOContext::kInstantIOContextKeyName); 23 } 24 25 InstantIOContext* GetDataForRequest(const net::URLRequest* request) { 26 const content::ResourceRequestInfo* info = 27 content::ResourceRequestInfo::ForRequest(request); 28 if (!info) 29 return NULL; 30 31 return GetDataForResourceContext(info->GetContext()); 32 } 33 34 } // namespace 35 36 const char InstantIOContext::kInstantIOContextKeyName[] = "instant_io_context"; 37 38 InstantIOContext::InstantIOContext() { 39 // The InstantIOContext is created on the UI thread but is accessed 40 // on the IO thread. 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 42 } 43 44 InstantIOContext::~InstantIOContext() { 45 } 46 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())); 54 } 55 56 // static 57 void InstantIOContext::AddInstantProcessOnIO( 58 scoped_refptr<InstantIOContext> instant_io_context, 59 int process_id) { 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 61 instant_io_context->process_ids_.insert(process_id); 62 } 63 64 // static 65 void InstantIOContext::RemoveInstantProcessOnIO( 66 scoped_refptr<InstantIOContext> instant_io_context, int process_id) { 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 68 instant_io_context->process_ids_.erase(process_id); 69 } 70 71 // static 72 void InstantIOContext::ClearInstantProcessesOnIO( 73 scoped_refptr<InstantIOContext> instant_io_context) { 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 75 instant_io_context->process_ids_.clear(); 76 } 77 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; 84 85 InstantIOContext* instant_io_context = GetDataForRequest(request); 86 if (!instant_io_context) 87 return false; 88 89 int process_id = -1; 90 int render_view_id = -1; 91 if (info->GetAssociatedRenderView(&process_id, &render_view_id) && 92 instant_io_context->IsInstantProcess(process_id)) 93 return true; 94 return false; 95 } 96 97 bool InstantIOContext::IsInstantProcess(int process_id) const { 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 99 return process_ids_.find(process_id) != process_ids_.end(); 100 } 101