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>& main_thread_runner, | |
jam
2017/04/03 14:47:03
no need to pass this one in, we can remove the dch
ananta
2017/04/03 22:58:19
Done.
| |
28 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread_runner) | |
28 : BrowserMessageFilter(ResourceMsgStart), | 29 : BrowserMessageFilter(ResourceMsgStart), |
29 BrowserAssociatedInterface<mojom::URLLoaderFactory>(this, this), | 30 BrowserAssociatedInterface<mojom::URLLoaderFactory>(this, this), |
30 is_channel_closed_(false), | 31 is_channel_closed_(false), |
31 requester_info_( | 32 requester_info_( |
32 ResourceRequesterInfo::CreateForRenderer(child_id, | 33 ResourceRequesterInfo::CreateForRenderer(child_id, |
33 appcache_service, | 34 appcache_service, |
34 blob_storage_context, | 35 blob_storage_context, |
35 file_system_context, | 36 file_system_context, |
36 service_worker_context, | 37 service_worker_context, |
37 get_contexts_callback)), | 38 get_contexts_callback)), |
39 main_thread_task_runner_(main_thread_runner), | |
40 io_thread_task_runner_(io_thread_runner), | |
38 weak_ptr_factory_(this) { | 41 weak_ptr_factory_(this) { |
39 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 42 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
40 } | 43 } |
41 | 44 |
42 ResourceMessageFilter::~ResourceMessageFilter() { | 45 ResourceMessageFilter::~ResourceMessageFilter() { |
43 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 46 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
44 DCHECK(is_channel_closed_); | 47 DCHECK(is_channel_closed_); |
45 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | 48 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); |
46 } | 49 } |
47 | 50 |
48 void ResourceMessageFilter::OnFilterAdded(IPC::Channel*) { | 51 void ResourceMessageFilter::OnFilterAdded(IPC::Channel*) { |
49 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 52 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
50 InitializeOnIOThread(); | 53 InitializeOnIOThread(); |
51 } | 54 } |
52 | 55 |
53 void ResourceMessageFilter::OnChannelClosing() { | 56 void ResourceMessageFilter::OnChannelClosing() { |
54 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 57 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
55 | 58 |
56 // Unhook us from all pending network requests so they don't get sent to a | 59 // Unhook us from all pending network requests so they don't get sent to a |
57 // deleted object. | 60 // deleted object. |
58 ResourceDispatcherHostImpl::Get()->CancelRequestsForProcess( | 61 ResourceDispatcherHostImpl::Get()->CancelRequestsForProcess( |
59 requester_info_->child_id()); | 62 requester_info_->child_id()); |
60 | 63 |
61 weak_ptr_factory_.InvalidateWeakPtrs(); | 64 weak_ptr_factory_.InvalidateWeakPtrs(); |
62 is_channel_closed_ = true; | 65 is_channel_closed_ = true; |
63 } | 66 } |
64 | 67 |
65 bool ResourceMessageFilter::OnMessageReceived(const IPC::Message& message) { | 68 bool ResourceMessageFilter::OnMessageReceived(const IPC::Message& message) { |
66 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 69 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
67 // Check if InitializeOnIOThread() has been called. | 70 // Check if InitializeOnIOThread() has been called. |
68 DCHECK_EQ(this, requester_info_->filter()); | 71 DCHECK_EQ(this, requester_info_->filter()); |
69 return ResourceDispatcherHostImpl::Get()->OnMessageReceived( | 72 return ResourceDispatcherHostImpl::Get()->OnMessageReceived( |
70 message, requester_info_.get()); | 73 message, requester_info_.get()); |
71 } | 74 } |
72 | 75 |
73 void ResourceMessageFilter::OnDestruct() const { | 76 void ResourceMessageFilter::OnDestruct() const { |
74 // Destroy the filter on the IO thread since that's where its weak pointers | 77 // Destroy the filter on the IO thread since that's where its weak pointers |
75 // are being used. | 78 // are being used. |
76 BrowserThread::DeleteOnIOThread::Destruct(this); | 79 if (io_thread_task_runner_->BelongsToCurrentThread()) { |
80 delete this; | |
81 } else { | |
82 io_thread_task_runner_->DeleteSoon(FROM_HERE, this); | |
83 } | |
77 } | 84 } |
78 | 85 |
79 base::WeakPtr<ResourceMessageFilter> ResourceMessageFilter::GetWeakPtr() { | 86 base::WeakPtr<ResourceMessageFilter> ResourceMessageFilter::GetWeakPtr() { |
80 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 87 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
81 return is_channel_closed_ ? nullptr : weak_ptr_factory_.GetWeakPtr(); | 88 return is_channel_closed_ ? nullptr : weak_ptr_factory_.GetWeakPtr(); |
82 } | 89 } |
83 | 90 |
84 void ResourceMessageFilter::CreateLoaderAndStart( | 91 void ResourceMessageFilter::CreateLoaderAndStart( |
85 mojom::URLLoaderAssociatedRequest request, | 92 mojom::URLLoaderAssociatedRequest request, |
86 int32_t routing_id, | 93 int32_t routing_id, |
87 int32_t request_id, | 94 int32_t request_id, |
88 const ResourceRequest& url_request, | 95 const ResourceRequest& url_request, |
89 mojom::URLLoaderClientPtr client) { | 96 mojom::URLLoaderClientPtr client) { |
90 URLLoaderFactoryImpl::CreateLoaderAndStart( | 97 URLLoaderFactoryImpl::CreateLoaderAndStart( |
(...skipping 11 matching lines...) Expand all Loading... | |
102 | 109 |
103 int ResourceMessageFilter::child_id() const { | 110 int ResourceMessageFilter::child_id() const { |
104 return requester_info_->child_id(); | 111 return requester_info_->child_id(); |
105 } | 112 } |
106 | 113 |
107 void ResourceMessageFilter::InitializeForTest() { | 114 void ResourceMessageFilter::InitializeForTest() { |
108 InitializeOnIOThread(); | 115 InitializeOnIOThread(); |
109 } | 116 } |
110 | 117 |
111 void ResourceMessageFilter::InitializeOnIOThread() { | 118 void ResourceMessageFilter::InitializeOnIOThread() { |
112 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 119 DCHECK(io_thread_task_runner_->BelongsToCurrentThread()); |
113 // The WeakPtr of the filter must be created on the IO thread. So sets the | 120 // The WeakPtr of the filter must be created on the IO thread. So sets the |
114 // WeakPtr of |requester_info_| now. | 121 // WeakPtr of |requester_info_| now. |
115 requester_info_->set_filter(GetWeakPtr()); | 122 requester_info_->set_filter(GetWeakPtr()); |
116 } | 123 } |
117 | 124 |
118 } // namespace content | 125 } // namespace content |
OLD | NEW |