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 "webkit/child/resource_loader_bridge.h"
9 #include "webkit/common/resource_response_info.h"
10 #include "webkit/common/resource_type.h"
12 // The SecurityFilterPeer is a proxy to a
13 // webkit_glue::ResourceLoaderBridge::Peer 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 insure they delete themselves at the end of the
18 // OnReceiveComplete call.
19 class SecurityFilterPeer
: public webkit_glue::ResourceLoaderBridge::Peer
{
21 virtual ~SecurityFilterPeer();
23 static SecurityFilterPeer
* CreateSecurityFilterPeerForDeniedRequest(
24 ResourceType::Type resource_type
,
25 webkit_glue::ResourceLoaderBridge::Peer
* peer
,
28 static SecurityFilterPeer
* CreateSecurityFilterPeerForFrame(
29 webkit_glue::ResourceLoaderBridge::Peer
* peer
,
32 // ResourceLoaderBridge::Peer methods.
33 virtual void OnUploadProgress(uint64 position
, uint64 size
) OVERRIDE
;
34 virtual bool OnReceivedRedirect(
36 const webkit_glue::ResourceResponseInfo
& info
,
37 bool* has_new_first_party_for_cookies
,
38 GURL
* new_first_party_for_cookies
) OVERRIDE
;
39 virtual void OnReceivedResponse(
40 const webkit_glue::ResourceResponseInfo
& info
) OVERRIDE
;
41 virtual void OnDownloadedData(int len
, int encoded_data_length
) OVERRIDE
{}
42 virtual void OnReceivedData(const char* data
,
44 int encoded_data_length
) OVERRIDE
;
45 virtual void OnCompletedRequest(
47 bool was_ignored_by_handler
,
48 bool stale_copy_in_cache
,
49 const std::string
& security_info
,
50 const base::TimeTicks
& completion_time
,
51 int64 total_transfer_size
) OVERRIDE
;
54 SecurityFilterPeer(webkit_glue::ResourceLoaderBridge
* resource_loader_bridge
,
55 webkit_glue::ResourceLoaderBridge::Peer
* peer
);
57 webkit_glue::ResourceLoaderBridge::Peer
* original_peer_
;
58 webkit_glue::ResourceLoaderBridge
* resource_loader_bridge_
;
61 DISALLOW_COPY_AND_ASSIGN(SecurityFilterPeer
);
64 // The BufferedPeer reads all the data of the request into an internal buffer.
65 // Subclasses should implement DataReady() to process the data as necessary.
66 class BufferedPeer
: public SecurityFilterPeer
{
68 BufferedPeer(webkit_glue::ResourceLoaderBridge
* resource_loader_bridge
,
69 webkit_glue::ResourceLoaderBridge::Peer
* peer
,
70 const std::string
& mime_type
);
71 virtual ~BufferedPeer();
73 // ResourceLoaderBridge::Peer Implementation.
74 virtual void OnReceivedResponse(
75 const webkit_glue::ResourceResponseInfo
& info
) OVERRIDE
;
76 virtual void OnReceivedData(const char* data
,
78 int encoded_data_length
) OVERRIDE
;
79 virtual void OnCompletedRequest(
81 bool was_ignored_by_handler
,
82 bool stale_copy_in_cache
,
83 const std::string
& security_info
,
84 const base::TimeTicks
& completion_time
,
85 int64 total_transfer_size
) OVERRIDE
;
88 // Invoked when the entire request has been processed before the data is sent
89 // to the original peer, giving an opportunity to subclasses to process the
90 // data in data_. If this method returns true, the data is fed to the
91 // original peer, if it returns false, an error is sent instead.
92 virtual bool DataReady() = 0;
94 webkit_glue::ResourceResponseInfo response_info_
;
98 std::string mime_type_
;
100 DISALLOW_COPY_AND_ASSIGN(BufferedPeer
);
103 // The ReplaceContentPeer cancels the request and serves the provided data as
105 // TODO(jcampan): we do not as of now cancel the request, as we do not have
106 // access to the resource_loader_bridge in the SecurityFilterPeer factory
107 // method. For now the resource is still being fetched, but ignored, as once
108 // we have provided the replacement content, the associated pending request
109 // in ResourceDispatcher is removed and further OnReceived* notifications are
111 class ReplaceContentPeer
: public SecurityFilterPeer
{
113 ReplaceContentPeer(webkit_glue::ResourceLoaderBridge
* resource_loader_bridge
,
114 webkit_glue::ResourceLoaderBridge::Peer
* peer
,
115 const std::string
& mime_type
,
116 const std::string
& data
);
117 virtual ~ReplaceContentPeer();
119 // ResourceLoaderBridge::Peer Implementation.
120 virtual void OnReceivedResponse(
121 const webkit_glue::ResourceResponseInfo
& info
) OVERRIDE
;
122 virtual void OnReceivedData(const char* data
,
124 int encoded_data_length
) OVERRIDE
;
125 virtual void OnCompletedRequest(
127 bool was_ignored_by_handler
,
128 bool stale_copy_in_cache
,
129 const std::string
& security_info
,
130 const base::TimeTicks
& completion_time
,
131 int64 total_transfer_size
) OVERRIDE
;
134 webkit_glue::ResourceResponseInfo response_info_
;
135 std::string mime_type_
;
138 DISALLOW_COPY_AND_ASSIGN(ReplaceContentPeer
);
141 #endif // CHROME_RENDERER_SECURITY_FILTER_PEER_H_