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