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_
8 #include "content/public/child/request_peer.h"
9 #include "content/public/common/resource_response_info.h"
10 #include "content/public/common/resource_type.h"
12 // The SecurityFilterPeer is a proxy to a
13 // content::RequestPeer instance. It is used to pre-process
14 // unsafe resources (such as mixed-content resource).
15 // Call the factory method CreateSecurityFilterPeer() to obtain an instance of
16 // SecurityFilterPeer based on the original Peer.
17 // NOTE: subclasses should ensure they delete themselves at the end of the
18 // OnReceiveComplete call.
19 class SecurityFilterPeer
: public content::RequestPeer
{
21 virtual ~SecurityFilterPeer();
23 static SecurityFilterPeer
* CreateSecurityFilterPeerForDeniedRequest(
24 content::ResourceType resource_type
,
25 content::RequestPeer
* peer
,
28 static SecurityFilterPeer
* CreateSecurityFilterPeerForFrame(
29 content::RequestPeer
* peer
,
32 // content::RequestPeer methods.
33 virtual void OnUploadProgress(uint64 position
, uint64 size
) OVERRIDE
;
34 virtual bool OnReceivedRedirect(
35 const net::RedirectInfo
& redirect_info
,
36 const content::ResourceResponseInfo
& info
) OVERRIDE
;
37 virtual void OnReceivedResponse(
38 const content::ResourceResponseInfo
& info
) OVERRIDE
;
39 virtual void OnDownloadedData(int len
, int encoded_data_length
) OVERRIDE
{}
40 virtual void OnReceivedData(const char* data
,
42 int encoded_data_length
) OVERRIDE
;
43 virtual void OnCompletedRequest(int error_code
,
44 bool was_ignored_by_handler
,
45 bool stale_copy_in_cache
,
46 const std::string
& security_info
,
47 const base::TimeTicks
& completion_time
,
48 int64 total_transfer_size
) OVERRIDE
;
51 explicit SecurityFilterPeer(content::RequestPeer
* peer
);
53 content::RequestPeer
* original_peer_
;
56 DISALLOW_COPY_AND_ASSIGN(SecurityFilterPeer
);
59 // The BufferedPeer reads all the data of the request into an internal buffer.
60 // Subclasses should implement DataReady() to process the data as necessary.
61 class BufferedPeer
: public SecurityFilterPeer
{
63 BufferedPeer(content::RequestPeer
* peer
, const std::string
& mime_type
);
64 virtual ~BufferedPeer();
66 // content::RequestPeer Implementation.
67 virtual void OnReceivedResponse(
68 const content::ResourceResponseInfo
& info
) OVERRIDE
;
69 virtual void OnReceivedData(const char* data
,
71 int encoded_data_length
) OVERRIDE
;
72 virtual void OnCompletedRequest(
74 bool was_ignored_by_handler
,
75 bool stale_copy_in_cache
,
76 const std::string
& security_info
,
77 const base::TimeTicks
& completion_time
,
78 int64 total_transfer_size
) OVERRIDE
;
81 // Invoked when the entire request has been processed before the data is sent
82 // to the original peer, giving an opportunity to subclasses to process the
83 // data in data_. If this method returns true, the data is fed to the
84 // original peer, if it returns false, an error is sent instead.
85 virtual bool DataReady() = 0;
87 content::ResourceResponseInfo response_info_
;
91 std::string mime_type_
;
93 DISALLOW_COPY_AND_ASSIGN(BufferedPeer
);
96 // The ReplaceContentPeer cancels the request and serves the provided data as
98 // TODO(jcampan): For now the resource is still being fetched, but ignored, as
99 // once we have provided the replacement content, the associated pending request
100 // in ResourceDispatcher is removed and further OnReceived* notifications are
102 class ReplaceContentPeer
: public SecurityFilterPeer
{
104 ReplaceContentPeer(content::RequestPeer
* peer
,
105 const std::string
& mime_type
,
106 const std::string
& data
);
107 virtual ~ReplaceContentPeer();
109 // content::RequestPeer Implementation.
110 virtual void OnReceivedResponse(
111 const content::ResourceResponseInfo
& info
) OVERRIDE
;
112 virtual void OnReceivedData(const char* data
,
114 int encoded_data_length
) OVERRIDE
;
115 virtual void OnCompletedRequest(
117 bool was_ignored_by_handler
,
118 bool stale_copy_in_cache
,
119 const std::string
& security_info
,
120 const base::TimeTicks
& completion_time
,
121 int64 total_transfer_size
) OVERRIDE
;
124 content::ResourceResponseInfo response_info_
;
125 std::string mime_type_
;
128 DISALLOW_COPY_AND_ASSIGN(ReplaceContentPeer
);
131 #endif // CHROME_RENDERER_SECURITY_FILTER_PEER_H_