Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(357)

Side by Side Diff: content/browser/loader/intercepting_resource_handler.h

Issue 2327463002: Relax ad-hoc assumptions on InterceptingResourceHandler (Closed)
Patch Set: fix Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #ifndef CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ 5 #ifndef CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ 6 #define CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "content/browser/loader/layered_resource_handler.h" 14 #include "content/browser/loader/layered_resource_handler.h"
15 #include "content/browser/loader/resource_handler.h" 15 #include "content/browser/loader/resource_handler.h"
16 #include "content/common/content_export.h" 16 #include "content/common/content_export.h"
17 #include "content/public/browser/resource_controller.h"
17 #include "net/base/io_buffer.h" 18 #include "net/base/io_buffer.h"
19 #include "net/url_request/url_request_status.h"
18 20
19 namespace net { 21 namespace net {
20 class URLRequest; 22 class URLRequest;
21 } 23 }
22 24
23 namespace content { 25 namespace content {
24 26
25 // ResourceHandler that initiates special handling of the response if needed, 27 // ResourceHandler that initiates special handling of the response if needed,
26 // based on the response's MIME type (starts downloads, sends data to some 28 // based on the response's MIME type (starts downloads, sends data to some
27 // plugin types via a special channel). 29 // plugin types via a special channel).
30 //
31 // A ResourceHandler holds two handlers (next_handler and new_handler). It
Charlie Harrison 2016/09/29 22:32:58 A InterceptingResourceHandler?
yhirano 2016/10/03 11:50:22 Thanks, done.
32 // assumes the following:
33 // - OnResponseStarted on next_handler never sets |*defer|.
34 // - OnResponseCompleted on next_handler never sets |*defer|.
28 class CONTENT_EXPORT InterceptingResourceHandler 35 class CONTENT_EXPORT InterceptingResourceHandler
29 : public LayeredResourceHandler { 36 : public LayeredResourceHandler,
37 public ResourceController {
30 public: 38 public:
31 InterceptingResourceHandler(std::unique_ptr<ResourceHandler> next_handler, 39 InterceptingResourceHandler(std::unique_ptr<ResourceHandler> next_handler,
32 net::URLRequest* request); 40 net::URLRequest* request);
33 ~InterceptingResourceHandler() override; 41 ~InterceptingResourceHandler() override;
34 42
35 // ResourceHandler implementation: 43 // ResourceHandler implementation:
44 void SetController(ResourceController* controller) override;
36 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; 45 bool OnResponseStarted(ResourceResponse* response, bool* defer) override;
37 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, 46 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
38 int* buf_size, 47 int* buf_size,
39 int min_size) override; 48 int min_size) override;
40 bool OnReadCompleted(int bytes_read, bool* defer) override; 49 bool OnReadCompleted(int bytes_read, bool* defer) override;
50 void OnResponseCompleted(const net::URLRequestStatus& status,
51 bool* defer) override;
52
53 // ResourceController implementation:
54 void Cancel() override;
55 void CancelAndIgnore() override;
56 void CancelWithError(int error_code) override;
57 void Resume() override;
41 58
42 // Replaces the next handler with |new_handler|, sending 59 // Replaces the next handler with |new_handler|, sending
43 // |payload_for_old_handler| to the old handler. Must be called after 60 // |payload_for_old_handler| to the old handler. Must be called after
44 // OnWillStart and OnRequestRedirected and before OnResponseStarted. One 61 // OnWillStart and OnRequestRedirected and before OnResponseStarted. One
45 // OnWillRead call is permitted beforehand. |new_handler|'s OnWillStart and 62 // OnWillRead call is permitted beforehand. |new_handler|'s OnWillStart and
46 // OnRequestRedirected methods will not be called. 63 // OnRequestRedirected methods will not be called.
47 void UseNewHandler(std::unique_ptr<ResourceHandler> new_handler, 64 void UseNewHandler(std::unique_ptr<ResourceHandler> new_handler,
48 const std::string& payload_for_old_handler); 65 const std::string& payload_for_old_handler);
49 66
50 // Used in tests. 67 // Used in tests.
51 ResourceHandler* new_handler_for_testing() const { 68 ResourceHandler* new_handler_for_testing() const {
52 return new_handler_.get(); 69 return new_handler_.get();
53 } 70 }
54 71
55 private: 72 private:
56 enum class State { 73 enum class State {
Charlie Harrison 2016/09/29 22:32:58 Can you expand the comments here a bit? In particu
yhirano 2016/10/03 11:50:22 Added some explanation about "payload".
57 // In this state, the InterceptingResourceHandler is waiting for the mime 74 // In this state, the InterceptingResourceHandler is waiting for the mime
58 // type of the response to be identified, to check if the next handler 75 // type of the response to be identified, to check if the next handler
59 // should be replaced with an appropriate one. 76 // should be replaced with an appropriate one.
60 STARTING, 77 STARTING,
61 78
62 // In this state, the InterceptingResourceHandler is waiting to copy the 79 // In this state, the InterceptingResourcHandler is notifying
Charlie Harrison 2016/09/29 22:32:58 nit: remove "In this state" from these description
yhirano 2016/10/03 11:50:22 Done.
63 // read buffer to the next handler if it was replaced. This is needed 80 // OnResponseStarted to the new handler and waiting for its completion via
64 // because MimeTypeResourceHandler may call OnWillRead before calling 81 // Resume(). After the completion, the InterceptingResourcHandler will
65 // OnResponseStarted, that is before the InterceptingResourceHandler 82 // transit to STARTING on success.
66 // replaces the original ResourceHandler of the request. Therefore, the 83 NOTIFYING_ON_RESPONSE_STARTED_TO_NEW_HANDLER,
67 // data read at that time should be copied to the new ResourceHandler. 84
68 WAITING_FOR_BUFFER_COPY, 85 // In this state, the InterceptingResourceHandler is sending the old
86 // handler's contents to the new handler and waiting for its completion via
87 // Resume().
88 SENDING_BUFFER_TO_NEW_HANDLER,
89
90 // In this state, the InterceptingResourceHandler is sending the given
91 // payload to the old handler and waiting for its completion via
92 // Resume().
93 SENDING_PAYLOAD_TO_OLD_HANDLER,
69 94
70 // In this state, the InterceptingResourceHandler has replaced its next 95 // In this state, the InterceptingResourceHandler has replaced its next
71 // ResourceHandler if needed, and has ensured the buffered read data was 96 // ResourceHandler if needed, and has ensured the buffered read data was
72 // properly transmitted to the new ResourceHandler. The 97 // properly transmitted to the new ResourceHandler. The
73 // InterceptingResourceHandler now acts as a pass-through ResourceHandler. 98 // InterceptingResourceHandler now acts as a pass-through ResourceHandler.
99 PASS_THROUGH,
100
101 // In this state, the InterceptingResourceHandler is notifying
102 // OnResponseCompleted to the new handler and waiting for its completion via
103 // Resume(). After the completion, it will transit to DONE.
104 NOTIFYING_ON_RESPONSE_COMPLETED_TO_NEW_HANDLER,
105
106 // In this state, both handlers are torn down. No new messages are expected
107 // to arrive.
74 DONE, 108 DONE,
75 }; 109 };
76 110
77 void SendPayloadToOldHandler(); 111 bool NotifyOnResponseStartedToNewHandler(bool* defer);
112 bool SendFirstReadBufferToNewHandler(bool* defer);
113 bool SendPayloadToOldHandler(bool* defer);
114 bool SendCompletionToOldHandler(bool* defer);
115 void NotifyOnResponseCompletedToOldHandler(bool* defer);
116 void NotifyOnResponseCompletedToNewHandler(bool* defer);
78 117
79 State state_; 118 State state_ = State::STARTING;
80 119
81 std::unique_ptr<ResourceHandler> new_handler_; 120 std::unique_ptr<ResourceHandler> new_handler_;
82 std::string payload_for_old_handler_; 121 std::string payload_for_old_handler_;
122 size_t payload_bytes_written_ = 0;
83 123
84 // Result of the first read, that may have to be passed to an alternate 124 // Result of the first read, that may have to be passed to an alternate
85 // ResourceHandler instead of the original ResourceHandler. 125 // ResourceHandler instead of the original ResourceHandler.
86 scoped_refptr<net::IOBuffer> first_read_buffer_; 126 scoped_refptr<net::IOBuffer> first_read_buffer_;
87 size_t first_read_buffer_size_; 127 size_t first_read_buffer_size_ = 0;
128 size_t first_read_buffer_bytes_read_ = 0;
129 size_t first_read_buffer_bytes_written_ = 0;
88 scoped_refptr<net::IOBuffer> first_read_buffer_copy_; 130 scoped_refptr<net::IOBuffer> first_read_buffer_copy_;
131 net::URLRequestStatus completion_status_;
89 132
90 DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler); 133 DISALLOW_COPY_AND_ASSIGN(InterceptingResourceHandler);
91 }; 134 };
92 135
93 } // namespace content 136 } // namespace content
94 137
95 #endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_ 138 #endif // CONTENT_BROWSER_LOADER_INTERCEPTING_RESOURCE_HANDLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698