| OLD | NEW |
| (Empty) | |
| 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/instant/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 |
| 12 using content::BrowserThread; |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Retrieves the Instant data from the |context|'s named user-data. |
| 17 InstantIOContext* GetDataForResourceContext( |
| 18 content::ResourceContext* context) { |
| 19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 20 return base::UserDataAdapter<InstantIOContext>::Get( |
| 21 context, InstantIOContext::kInstantIOContextKeyName); |
| 22 } |
| 23 |
| 24 } // namespace |
| 25 |
| 26 const char InstantIOContext::kInstantIOContextKeyName[] = "instant_io_context"; |
| 27 |
| 28 InstantIOContext::InstantIOContext() { |
| 29 // The InstantIOContext is created on the UI thread but is accessed |
| 30 // on the IO thread. |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 32 } |
| 33 |
| 34 InstantIOContext::~InstantIOContext() { |
| 35 } |
| 36 |
| 37 // static |
| 38 void InstantIOContext::SetUserDataOnIO( |
| 39 content::ResourceContext* resource_context, |
| 40 scoped_refptr<InstantIOContext> instant_io_context) { |
| 41 resource_context->SetUserData( |
| 42 InstantIOContext::kInstantIOContextKeyName, |
| 43 new base::UserDataAdapter<InstantIOContext>(instant_io_context)); |
| 44 } |
| 45 |
| 46 // static |
| 47 void InstantIOContext::AddInstantProcessOnIO( |
| 48 scoped_refptr<InstantIOContext> instant_io_context, int process_id) { |
| 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 50 instant_io_context->process_ids_.insert(process_id); |
| 51 } |
| 52 |
| 53 // static |
| 54 void InstantIOContext::RemoveInstantProcessOnIO( |
| 55 scoped_refptr<InstantIOContext> instant_io_context, int process_id) { |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 57 instant_io_context->process_ids_.erase(process_id); |
| 58 } |
| 59 |
| 60 // static |
| 61 void InstantIOContext::ClearInstantProcessesOnIO( |
| 62 scoped_refptr<InstantIOContext> instant_io_context) { |
| 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 64 instant_io_context->process_ids_.clear(); |
| 65 } |
| 66 |
| 67 // static |
| 68 bool InstantIOContext::ShouldServiceRequest(const net::URLRequest* request) { |
| 69 const content::ResourceRequestInfo* info = |
| 70 content::ResourceRequestInfo::ForRequest(request); |
| 71 if (!info) |
| 72 return false; |
| 73 |
| 74 InstantIOContext* instant_io_context = |
| 75 GetDataForResourceContext(info->GetContext()); |
| 76 if (!instant_io_context) |
| 77 return false; |
| 78 |
| 79 int process_id = -1; |
| 80 int render_view_id = -1; |
| 81 if (info->GetAssociatedRenderView(&process_id, &render_view_id) && |
| 82 instant_io_context->IsInstantProcess(process_id)) |
| 83 return true; |
| 84 return false; |
| 85 } |
| 86 |
| 87 bool InstantIOContext::IsInstantProcess(int process_id) const { |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 89 return process_ids_.count(process_id) != 0; |
| 90 } |
| OLD | NEW |