| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/instant/instant_io_context.h" | 5 #include "chrome/browser/instant/instant_io_context.h" |
| 6 | 6 |
| 7 #include "content/public/browser/browser_thread.h" | 7 #include "content/public/browser/browser_thread.h" |
| 8 #include "content/public/browser/resource_context.h" | 8 #include "content/public/browser/resource_context.h" |
| 9 #include "content/public/browser/resource_request_info.h" | 9 #include "content/public/browser/resource_request_info.h" |
| 10 #include "googleurl/src/gurl.h" |
| 10 #include "net/url_request/url_request.h" | 11 #include "net/url_request/url_request.h" |
| 11 | 12 |
| 12 using content::BrowserThread; | 13 using content::BrowserThread; |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // Retrieves the Instant data from the |context|'s named user-data. | 17 // Retrieves the Instant data from the |context|'s named user-data. |
| 17 InstantIOContext* GetDataForResourceContext( | 18 InstantIOContext* GetDataForResourceContext( |
| 18 content::ResourceContext* context) { | 19 content::ResourceContext* context) { |
| 19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 20 return base::UserDataAdapter<InstantIOContext>::Get( | 21 return base::UserDataAdapter<InstantIOContext>::Get( |
| 21 context, InstantIOContext::kInstantIOContextKeyName); | 22 context, InstantIOContext::kInstantIOContextKeyName); |
| 22 } | 23 } |
| 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 |
| 24 } // namespace | 34 } // namespace |
| 25 | 35 |
| 26 const char InstantIOContext::kInstantIOContextKeyName[] = "instant_io_context"; | 36 const char InstantIOContext::kInstantIOContextKeyName[] = "instant_io_context"; |
| 27 | 37 |
| 28 InstantIOContext::InstantIOContext() { | 38 InstantIOContext::InstantIOContext() { |
| 29 // The InstantIOContext is created on the UI thread but is accessed | 39 // The InstantIOContext is created on the UI thread but is accessed |
| 30 // on the IO thread. | 40 // on the IO thread. |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 32 } | 42 } |
| 33 | 43 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 58 } | 68 } |
| 59 | 69 |
| 60 // static | 70 // static |
| 61 void InstantIOContext::ClearInstantProcessesOnIO( | 71 void InstantIOContext::ClearInstantProcessesOnIO( |
| 62 scoped_refptr<InstantIOContext> instant_io_context) { | 72 scoped_refptr<InstantIOContext> instant_io_context) { |
| 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 64 instant_io_context->process_ids_.clear(); | 74 instant_io_context->process_ids_.clear(); |
| 65 } | 75 } |
| 66 | 76 |
| 67 // static | 77 // static |
| 78 void InstantIOContext::AddRestrictedIDOnIO( |
| 79 scoped_refptr<InstantIOContext> instant_io_context, |
| 80 uint64 restricted_id, const GURL& url) { |
| 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 82 instant_io_context->id_to_url_map_[restricted_id] = url; |
| 83 } |
| 84 |
| 85 // static |
| 68 bool InstantIOContext::ShouldServiceRequest(const net::URLRequest* request) { | 86 bool InstantIOContext::ShouldServiceRequest(const net::URLRequest* request) { |
| 69 const content::ResourceRequestInfo* info = | 87 const content::ResourceRequestInfo* info = |
| 70 content::ResourceRequestInfo::ForRequest(request); | 88 content::ResourceRequestInfo::ForRequest(request); |
| 71 if (!info) | 89 if (!info) |
| 72 return false; | 90 return false; |
| 73 | 91 |
| 74 InstantIOContext* instant_io_context = | 92 InstantIOContext* instant_io_context = GetDataForRequest(request); |
| 75 GetDataForResourceContext(info->GetContext()); | |
| 76 if (!instant_io_context) | 93 if (!instant_io_context) |
| 77 return false; | 94 return false; |
| 78 | 95 |
| 79 int process_id = -1; | 96 int process_id = -1; |
| 80 int render_view_id = -1; | 97 int render_view_id = -1; |
| 81 if (info->GetAssociatedRenderView(&process_id, &render_view_id) && | 98 if (info->GetAssociatedRenderView(&process_id, &render_view_id) && |
| 82 instant_io_context->IsInstantProcess(process_id)) | 99 instant_io_context->IsInstantProcess(process_id)) |
| 83 return true; | 100 return true; |
| 84 return false; | 101 return false; |
| 85 } | 102 } |
| 86 | 103 |
| 104 // static |
| 105 bool InstantIOContext::GetURLForRestrictedId(const net::URLRequest* request, |
| 106 uint64 restricted_id, GURL* url) { |
| 107 InstantIOContext* instant_io_context = GetDataForRequest(request); |
| 108 if (!instant_io_context) { |
| 109 *url = GURL(); |
| 110 return false; |
| 111 } |
| 112 |
| 113 return instant_io_context->GetURLForRestrictedId(restricted_id, url); |
| 114 } |
| 115 |
| 87 bool InstantIOContext::IsInstantProcess(int process_id) const { | 116 bool InstantIOContext::IsInstantProcess(int process_id) const { |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 89 return process_ids_.count(process_id) != 0; | 118 return process_ids_.count(process_id) != 0; |
| 90 } | 119 } |
| 120 |
| 121 bool InstantIOContext::GetURLForRestrictedId(uint64 restricted_id, GURL* url) { |
| 122 std::map<uint64, GURL>::iterator it = id_to_url_map_.find(restricted_id); |
| 123 if (it != id_to_url_map_.end()) { |
| 124 *url = it->second; |
| 125 return true; |
| 126 } |
| 127 *url = GURL(); |
| 128 return false; |
| 129 } |
| OLD | NEW |