Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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. | |
| 4 | |
| 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_FRAME_BUILDER_H_ | |
| 6 #define NET_WEBSOCKETS_WEBSOCKET_FRAME_BUILDER_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/scoped_vector.h" | |
| 14 #include "net/base/net_export.h" | |
| 15 #include "net/websockets/websocket_frame.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 // Builds byte-stream data from WebSocket frame chunks. | |
| 20 | |
| 21 class NET_EXPORT_PRIVATE WebSocketFrameBuilder { | |
| 22 public: | |
| 23 WebSocketFrameBuilder(); | |
| 24 ~WebSocketFrameBuilder(); | |
| 25 | |
| 26 // Encodes the given frame chunks into byte stream. | |
| 27 // | |
| 28 // If the builder finds an error in |frame_chunks|, Encode() returns false | |
| 29 // and the builder goes into "failed" state. Once the builder fails, it will | |
| 30 // no longer accept any new frame chunks and Encode() will always fail. | |
| 31 // | |
| 32 // Note that a logical error in |frame_chunks| (like payload length mismatch) | |
| 33 // causes a DCHECK failure, so users of this class need to make sure | |
| 34 // |frame_chunks| follow the restrictions documented in websocket_frame.h. | |
| 35 // | |
| 36 // For each new frame, Encode() will pick a random masking key in | |
| 37 // a cryptographically secure manner (unless PinMaskingKeyForTesting() was | |
| 38 // not called before). | |
| 39 bool Encode(ScopedVector<WebSocketFrameChunk> frame_chunks, | |
|
willchan no longer on Chromium
2012/05/16 03:12:54
I'd prefer not to use .Pass() stuff here. I'd like
| |
| 40 std::vector<char>* output); | |
|
mmenke
2012/05/15 18:36:31
I think it makes sense to basically redocument som
| |
| 41 | |
| 42 // Returns true if the builder has ever failed to encode frame data. | |
| 43 bool failed() const { return failed_; } | |
|
willchan no longer on Chromium
2012/05/16 03:12:54
Is this only for testing? If so, I don't really se
| |
| 44 | |
| 45 // Pins the masking key for future frames to a specific value. | |
| 46 // | |
| 47 // |masking_key| must be 4-byte data. As the name suggests, this function is | |
| 48 // exposed only for testing purposes. | |
| 49 void PinMaskingKeyForTesting(const char masking_key[]); | |
|
mmenke
2012/05/15 18:36:31
Call me paranoid, but I'd rather this be private,
| |
| 50 | |
| 51 private: | |
| 52 // Encodes the frame header and writes the result to |output|. | |
| 53 // | |
| 54 // This function returns true if it has encoded the header successfully. | |
| 55 bool EncodeFrameHeader(WebSocketFrameHeader* header, | |
| 56 std::vector<char>* output) const; | |
| 57 | |
| 58 // Generates a new masking key and sets it in masking_key_. If | |
| 59 // has_pinned_masking_key_for_testing_ is true, the function uses the pinned | |
| 60 // key instead of generating a random one. | |
| 61 void GenerateNewMaskingKey(); | |
| 62 | |
| 63 // Masks the payload with masking_key_. |payload| must be the payload data | |
| 64 // starting at |frame_offset_|. | |
| 65 void MaskPayload(std::vector<char>* payload); | |
| 66 | |
| 67 // Clears the information of the current frame and turns the |failed_| bit on. | |
| 68 void Fail(); | |
| 69 | |
| 70 scoped_ptr<WebSocketFrameHeader> current_frame_header_; | |
| 71 char masking_key_[WebSocketFrameHeader::kMaskingKeyLength]; | |
| 72 | |
| 73 uint64 frame_offset_; | |
| 74 | |
| 75 char pinned_masking_key_for_testing_[WebSocketFrameHeader::kMaskingKeyLength]; | |
| 76 bool has_pinned_masking_key_for_testing_; | |
| 77 | |
| 78 bool failed_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(WebSocketFrameBuilder); | |
| 81 }; | |
| 82 | |
| 83 } // namespace net | |
| 84 | |
| 85 #endif // NET_WEBSOCKETS_WEBSOCKET_FRAME_BUILDER_H_ | |
| OLD | NEW |