OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/loader/resource_message_filter.h" | 5 #include "content/browser/loader/resource_message_filter.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "content/browser/appcache/chrome_appcache_service.h" | 8 #include "content/browser/appcache/chrome_appcache_service.h" |
9 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | 9 #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
10 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 10 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
11 #include "content/browser/loader/resource_requester_info.h" | 11 #include "content/browser/loader/resource_requester_info.h" |
12 #include "content/browser/loader/url_loader_factory_impl.h" | 12 #include "content/browser/loader/url_loader_factory_impl.h" |
13 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
14 #include "content/common/resource_messages.h" | 14 #include "content/common/resource_messages.h" |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "content/public/browser/resource_context.h" | 15 #include "content/public/browser/resource_context.h" |
17 #include "storage/browser/fileapi/file_system_context.h" | 16 #include "storage/browser/fileapi/file_system_context.h" |
18 | 17 |
19 namespace content { | 18 namespace content { |
20 | 19 |
21 ResourceMessageFilter::ResourceMessageFilter( | 20 ResourceMessageFilter::ResourceMessageFilter( |
22 int child_id, | 21 int child_id, |
23 ChromeAppCacheService* appcache_service, | 22 ChromeAppCacheService* appcache_service, |
24 ChromeBlobStorageContext* blob_storage_context, | 23 ChromeBlobStorageContext* blob_storage_context, |
25 storage::FileSystemContext* file_system_context, | 24 storage::FileSystemContext* file_system_context, |
26 ServiceWorkerContextWrapper* service_worker_context, | 25 ServiceWorkerContextWrapper* service_worker_context, |
27 const GetContextsCallback& get_contexts_callback) | 26 const GetContextsCallback& get_contexts_callback, |
| 27 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread_runner) |
28 : BrowserMessageFilter(ResourceMsgStart), | 28 : BrowserMessageFilter(ResourceMsgStart), |
29 BrowserAssociatedInterface<mojom::URLLoaderFactory>(this, this), | 29 BrowserAssociatedInterface<mojom::URLLoaderFactory>(this, this), |
30 is_channel_closed_(false), | 30 is_channel_closed_(false), |
31 requester_info_( | 31 requester_info_( |
32 ResourceRequesterInfo::CreateForRenderer(child_id, | 32 ResourceRequesterInfo::CreateForRenderer(child_id, |
33 appcache_service, | 33 appcache_service, |
34 blob_storage_context, | 34 blob_storage_context, |
35 file_system_context, | 35 file_system_context, |
36 service_worker_context, | 36 service_worker_context, |
37 get_contexts_callback)), | 37 get_contexts_callback)), |
38 weak_ptr_factory_(this) { | 38 io_thread_task_runner_(io_thread_runner), |
39 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 39 weak_ptr_factory_(this) {} |
40 } | |
41 | 40 |
42 ResourceMessageFilter::~ResourceMessageFilter() { | 41 ResourceMessageFilter::~ResourceMessageFilter() { |
43 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 42 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
44 DCHECK(is_channel_closed_); | 43 DCHECK(is_channel_closed_); |
45 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | 44 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); |
46 } | 45 } |
47 | 46 |
48 void ResourceMessageFilter::OnFilterAdded(IPC::Channel*) { | 47 void ResourceMessageFilter::OnFilterAdded(IPC::Channel*) { |
49 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 48 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
50 InitializeOnIOThread(); | 49 InitializeOnIOThread(); |
51 } | 50 } |
52 | 51 |
53 void ResourceMessageFilter::OnChannelClosing() { | 52 void ResourceMessageFilter::OnChannelClosing() { |
54 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 53 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
55 | 54 |
56 // Unhook us from all pending network requests so they don't get sent to a | 55 // Unhook us from all pending network requests so they don't get sent to a |
57 // deleted object. | 56 // deleted object. |
58 ResourceDispatcherHostImpl::Get()->CancelRequestsForProcess( | 57 ResourceDispatcherHostImpl::Get()->CancelRequestsForProcess( |
59 requester_info_->child_id()); | 58 requester_info_->child_id()); |
60 | 59 |
61 weak_ptr_factory_.InvalidateWeakPtrs(); | 60 weak_ptr_factory_.InvalidateWeakPtrs(); |
62 is_channel_closed_ = true; | 61 is_channel_closed_ = true; |
63 } | 62 } |
64 | 63 |
65 bool ResourceMessageFilter::OnMessageReceived(const IPC::Message& message) { | 64 bool ResourceMessageFilter::OnMessageReceived(const IPC::Message& message) { |
66 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 65 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
67 // Check if InitializeOnIOThread() has been called. | 66 // Check if InitializeOnIOThread() has been called. |
68 DCHECK_EQ(this, requester_info_->filter()); | 67 DCHECK_EQ(this, requester_info_->filter()); |
69 return ResourceDispatcherHostImpl::Get()->OnMessageReceived( | 68 return ResourceDispatcherHostImpl::Get()->OnMessageReceived( |
70 message, requester_info_.get()); | 69 message, requester_info_.get()); |
71 } | 70 } |
72 | 71 |
73 void ResourceMessageFilter::OnDestruct() const { | 72 void ResourceMessageFilter::OnDestruct() const { |
74 // Destroy the filter on the IO thread since that's where its weak pointers | 73 // Destroy the filter on the IO thread since that's where its weak pointers |
75 // are being used. | 74 // are being used. |
76 BrowserThread::DeleteOnIOThread::Destruct(this); | 75 if (io_thread_task_runner_->BelongsToCurrentThread()) { |
| 76 delete this; |
| 77 } else { |
| 78 io_thread_task_runner_->DeleteSoon(FROM_HERE, this); |
| 79 } |
77 } | 80 } |
78 | 81 |
79 base::WeakPtr<ResourceMessageFilter> ResourceMessageFilter::GetWeakPtr() { | 82 base::WeakPtr<ResourceMessageFilter> ResourceMessageFilter::GetWeakPtr() { |
80 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 83 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
81 return is_channel_closed_ ? nullptr : weak_ptr_factory_.GetWeakPtr(); | 84 return is_channel_closed_ ? nullptr : weak_ptr_factory_.GetWeakPtr(); |
82 } | 85 } |
83 | 86 |
84 void ResourceMessageFilter::CreateLoaderAndStart( | 87 void ResourceMessageFilter::CreateLoaderAndStart( |
85 mojom::URLLoaderAssociatedRequest request, | 88 mojom::URLLoaderAssociatedRequest request, |
86 int32_t routing_id, | 89 int32_t routing_id, |
87 int32_t request_id, | 90 int32_t request_id, |
88 const ResourceRequest& url_request, | 91 const ResourceRequest& url_request, |
89 mojom::URLLoaderClientPtr client) { | 92 mojom::URLLoaderClientPtr client) { |
90 URLLoaderFactoryImpl::CreateLoaderAndStart( | 93 URLLoaderFactoryImpl::CreateLoaderAndStart( |
(...skipping 11 matching lines...) Expand all Loading... |
102 | 105 |
103 int ResourceMessageFilter::child_id() const { | 106 int ResourceMessageFilter::child_id() const { |
104 return requester_info_->child_id(); | 107 return requester_info_->child_id(); |
105 } | 108 } |
106 | 109 |
107 void ResourceMessageFilter::InitializeForTest() { | 110 void ResourceMessageFilter::InitializeForTest() { |
108 InitializeOnIOThread(); | 111 InitializeOnIOThread(); |
109 } | 112 } |
110 | 113 |
111 void ResourceMessageFilter::InitializeOnIOThread() { | 114 void ResourceMessageFilter::InitializeOnIOThread() { |
112 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 115 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
113 // The WeakPtr of the filter must be created on the IO thread. So sets the | 116 // The WeakPtr of the filter must be created on the IO thread. So sets the |
114 // WeakPtr of |requester_info_| now. | 117 // WeakPtr of |requester_info_| now. |
115 requester_info_->set_filter(GetWeakPtr()); | 118 requester_info_->set_filter(GetWeakPtr()); |
116 } | 119 } |
117 | 120 |
118 } // namespace content | 121 } // namespace content |
OLD | NEW |