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