OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/appcache/appcache_subresource_url_factory.h" | 5 #include "content/browser/appcache/appcache_subresource_url_factory.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "content/browser/appcache/appcache_host.h" |
9 #include "content/browser/appcache/appcache_request_handler.h" | 10 #include "content/browser/appcache/appcache_request_handler.h" |
10 #include "content/browser/appcache/appcache_url_loader_job.h" | 11 #include "content/browser/appcache/appcache_url_loader_job.h" |
11 #include "content/browser/appcache/appcache_url_loader_request.h" | 12 #include "content/browser/appcache/appcache_url_loader_request.h" |
12 #include "content/browser/url_loader_factory_getter.h" | 13 #include "content/browser/url_loader_factory_getter.h" |
13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/common/resource_request.h" | 15 #include "content/public/common/resource_request.h" |
15 #include "content/public/common/url_loader_factory.mojom.h" | 16 #include "content/public/common/url_loader_factory.mojom.h" |
16 #include "mojo/public/cpp/bindings/binding.h" | 17 #include "mojo/public/cpp/bindings/binding.h" |
17 #include "mojo/public/cpp/bindings/binding_set.h" | 18 #include "mojo/public/cpp/bindings/binding_set.h" |
18 #include "mojo/public/cpp/bindings/interface_ptr.h" | 19 #include "mojo/public/cpp/bindings/interface_ptr.h" |
19 #include "net/traffic_annotation/network_traffic_annotation.h" | 20 #include "net/traffic_annotation/network_traffic_annotation.h" |
20 | 21 |
21 namespace content { | 22 namespace content { |
22 | 23 |
23 // Implements the URLLoaderFactory mojom for AppCache requests. | 24 // Implements the URLLoaderFactory mojom for AppCache requests. |
24 AppCacheSubresourceURLFactory::AppCacheSubresourceURLFactory( | 25 AppCacheSubresourceURLFactory::AppCacheSubresourceURLFactory( |
25 mojom::URLLoaderFactoryRequest request, | 26 mojom::URLLoaderFactoryRequest request, |
26 URLLoaderFactoryGetter* default_url_loader_factory_getter) | 27 URLLoaderFactoryGetter* default_url_loader_factory_getter, |
| 28 base::WeakPtr<AppCacheHost> host) |
27 : binding_(this, std::move(request)), | 29 : binding_(this, std::move(request)), |
28 default_url_loader_factory_getter_(default_url_loader_factory_getter) { | 30 default_url_loader_factory_getter_(default_url_loader_factory_getter), |
| 31 appcache_host_(host) { |
29 binding_.set_connection_error_handler( | 32 binding_.set_connection_error_handler( |
30 base::Bind(&AppCacheSubresourceURLFactory::OnConnectionError, | 33 base::Bind(&AppCacheSubresourceURLFactory::OnConnectionError, |
31 base::Unretained(this))); | 34 base::Unretained(this))); |
32 } | 35 } |
33 | 36 |
34 AppCacheSubresourceURLFactory::~AppCacheSubresourceURLFactory() {} | 37 AppCacheSubresourceURLFactory::~AppCacheSubresourceURLFactory() {} |
35 | 38 |
36 // static | 39 // static |
37 mojom::URLLoaderFactoryPtr | 40 AppCacheSubresourceURLFactory* |
38 AppCacheSubresourceURLFactory::CreateURLLoaderFactory( | 41 AppCacheSubresourceURLFactory::CreateURLLoaderFactory( |
39 URLLoaderFactoryGetter* default_url_loader_factory_getter) { | 42 URLLoaderFactoryGetter* default_url_loader_factory_getter, |
40 mojom::URLLoaderFactoryPtr loader_factory; | 43 base::WeakPtr<AppCacheHost> host, |
41 mojom::URLLoaderFactoryRequest request = mojo::MakeRequest(&loader_factory); | 44 mojom::URLLoaderFactoryPtr* loader_factory) { |
| 45 mojom::URLLoaderFactoryRequest request = mojo::MakeRequest(loader_factory); |
42 | 46 |
43 // This instance will get deleted when the client drops the connection. | 47 // This instance will get deleted when the client drops the connection. |
44 // Please see OnConnectionError() for details. | 48 // Please see OnConnectionError() for details. |
45 new AppCacheSubresourceURLFactory(std::move(request), | 49 return new AppCacheSubresourceURLFactory( |
46 default_url_loader_factory_getter); | 50 std::move(request), default_url_loader_factory_getter, host); |
47 return loader_factory; | |
48 } | 51 } |
49 | 52 |
50 void AppCacheSubresourceURLFactory::CreateLoaderAndStart( | 53 void AppCacheSubresourceURLFactory::CreateLoaderAndStart( |
51 mojom::URLLoaderAssociatedRequest url_loader_request, | 54 mojom::URLLoaderAssociatedRequest url_loader_request, |
52 int32_t routing_id, | 55 int32_t routing_id, |
53 int32_t request_id, | 56 int32_t request_id, |
54 uint32_t options, | 57 uint32_t options, |
55 const ResourceRequest& request, | 58 const ResourceRequest& request, |
56 mojom::URLLoaderClientPtr client, | 59 mojom::URLLoaderClientPtr client, |
57 const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) { | 60 const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) { |
58 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 61 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
59 DLOG(WARNING) << "Received request for loading : " << request.url.spec(); | 62 DLOG(WARNING) << "Received request for loading : " << request.url.spec(); |
60 default_url_loader_factory_getter_->GetNetworkFactory() | 63 |
61 ->get() | 64 // If the host is invalid, it means that the renderer has probably died. |
62 ->CreateLoaderAndStart(mojom::URLLoaderAssociatedRequest(), routing_id, | 65 // (Frame has navigated elsewhere?) |
63 request_id, options, request, std::move(client), | 66 if (!appcache_host_.get()) |
64 traffic_annotation); | 67 return; |
| 68 |
| 69 std::unique_ptr<AppCacheRequestHandler> handler = |
| 70 appcache_host_->CreateRequestHandler( |
| 71 AppCacheURLLoaderRequest::Create(request), request.resource_type, |
| 72 request.should_reset_appcache); |
| 73 if (!handler) { |
| 74 ResourceRequestCompletionStatus request_result; |
| 75 request_result.error_code = net::ERR_FAILED; |
| 76 client->OnComplete(request_result); |
| 77 return; |
| 78 } |
| 79 |
| 80 handler->set_network_url_loader_factory_getter( |
| 81 default_url_loader_factory_getter_.get()); |
| 82 |
| 83 std::unique_ptr<SubresourceLoadInfo> load_info(new SubresourceLoadInfo()); |
| 84 load_info->url_loader_request = std::move(url_loader_request); |
| 85 load_info->routing_id = routing_id; |
| 86 load_info->request_id = request_id; |
| 87 load_info->options = options; |
| 88 load_info->request = request; |
| 89 load_info->client = std::move(client); |
| 90 load_info->traffic_annotation = traffic_annotation; |
| 91 |
| 92 handler->SetSubresourceRequestLoadInfo(std::move(load_info)); |
| 93 |
| 94 AppCacheJob* job = handler->MaybeLoadResource(nullptr); |
| 95 if (job) { |
| 96 // The handler is owned by the job. |
| 97 job->AsURLLoaderJob()->set_request_handler(std::move(handler)); |
| 98 } |
65 } | 99 } |
66 | 100 |
67 void AppCacheSubresourceURLFactory::SyncLoad(int32_t routing_id, | 101 void AppCacheSubresourceURLFactory::SyncLoad(int32_t routing_id, |
68 int32_t request_id, | 102 int32_t request_id, |
69 const ResourceRequest& request, | 103 const ResourceRequest& request, |
70 SyncLoadCallback callback) { | 104 SyncLoadCallback callback) { |
71 NOTREACHED(); | 105 NOTREACHED(); |
72 } | 106 } |
73 | 107 |
74 void AppCacheSubresourceURLFactory::OnConnectionError() { | 108 void AppCacheSubresourceURLFactory::OnConnectionError() { |
75 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | 109 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
76 } | 110 } |
77 | 111 |
78 } // namespace content | 112 } // namespace content |
OLD | NEW |