1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_RENDERER_SECURITY_FILTER_PEER_H_
6 #define CHROME_RENDERER_SECURITY_FILTER_PEER_H_
10 #include "content/public/child/request_peer.h"
11 #include "content/public/common/resource_response_info.h"
12 #include "content/public/common/resource_type.h"
14 // The SecurityFilterPeer is a proxy to a
15 // content::RequestPeer instance. It is used to pre-process
16 // unsafe resources (such as mixed-content resource).
17 // Call the factory method CreateSecurityFilterPeer() to obtain an instance of
18 // SecurityFilterPeer based on the original Peer.
19 // NOTE: subclasses should ensure they delete themselves at the end of the
20 // OnReceiveComplete call.
21 class SecurityFilterPeer
: public content::RequestPeer
{
23 ~SecurityFilterPeer() override
;
25 static SecurityFilterPeer
* CreateSecurityFilterPeerForDeniedRequest(
26 content::ResourceType resource_type
,
27 content::RequestPeer
* peer
,
30 static SecurityFilterPeer
* CreateSecurityFilterPeerForFrame(
31 content::RequestPeer
* peer
,
34 // content::RequestPeer methods.
35 void OnUploadProgress(uint64 position
, uint64 size
) override
;
36 bool OnReceivedRedirect(const net::RedirectInfo
& redirect_info
,
37 const content::ResourceResponseInfo
& info
) override
;
38 void OnReceivedResponse(const content::ResourceResponseInfo
& info
) override
;
39 void OnDownloadedData(int len
, int encoded_data_length
) override
{}
40 void OnReceivedData(scoped_ptr
<ReceivedData
> data
) override
;
41 void OnCompletedRequest(int error_code
,
42 bool was_ignored_by_handler
,
43 bool stale_copy_in_cache
,
44 const std::string
& security_info
,
45 const base::TimeTicks
& completion_time
,
46 int64 total_transfer_size
) override
;
49 explicit SecurityFilterPeer(content::RequestPeer
* peer
);
51 content::RequestPeer
* original_peer_
;
54 DISALLOW_COPY_AND_ASSIGN(SecurityFilterPeer
);
57 // The BufferedPeer reads all the data of the request into an internal buffer.
58 // Subclasses should implement DataReady() to process the data as necessary.
59 class BufferedPeer
: public SecurityFilterPeer
{
61 BufferedPeer(content::RequestPeer
* peer
, const std::string
& mime_type
);
62 ~BufferedPeer() override
;
64 // content::RequestPeer Implementation.
65 void OnReceivedResponse(const content::ResourceResponseInfo
& info
) override
;
66 void OnReceivedData(scoped_ptr
<ReceivedData
> data
) override
;
67 void OnCompletedRequest(int error_code
,
68 bool was_ignored_by_handler
,
69 bool stale_copy_in_cache
,
70 const std::string
& security_info
,
71 const base::TimeTicks
& completion_time
,
72 int64 total_transfer_size
) override
;
75 // Invoked when the entire request has been processed before the data is sent
76 // to the original peer, giving an opportunity to subclasses to process the
77 // data in data_. If this method returns true, the data is fed to the
78 // original peer, if it returns false, an error is sent instead.
79 virtual bool DataReady() = 0;
81 content::ResourceResponseInfo response_info_
;
85 std::string mime_type_
;
87 DISALLOW_COPY_AND_ASSIGN(BufferedPeer
);
90 // The ReplaceContentPeer cancels the request and serves the provided data as
92 // TODO(jcampan): For now the resource is still being fetched, but ignored, as
93 // once we have provided the replacement content, the associated pending request
94 // in ResourceDispatcher is removed and further OnReceived* notifications are
96 class ReplaceContentPeer
: public SecurityFilterPeer
{
98 ReplaceContentPeer(content::RequestPeer
* peer
,
99 const std::string
& mime_type
,
100 const std::string
& data
);
101 ~ReplaceContentPeer() override
;
103 // content::RequestPeer Implementation.
104 void OnReceivedResponse(const content::ResourceResponseInfo
& info
) override
;
105 void OnReceivedData(scoped_ptr
<ReceivedData
> data
) override
;
106 void OnCompletedRequest(int error_code
,
107 bool was_ignored_by_handler
,
108 bool stale_copy_in_cache
,
109 const std::string
& security_info
,
110 const base::TimeTicks
& completion_time
,
111 int64 total_transfer_size
) override
;
114 content::ResourceResponseInfo response_info_
;
115 std::string mime_type_
;
118 DISALLOW_COPY_AND_ASSIGN(ReplaceContentPeer
);
121 #endif // CHROME_RENDERER_SECURITY_FILTER_PEER_H_