1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that 3 // can be found in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_BROWSER_STREAM_IMPL_H_ 6 #define CEF_LIBCEF_BROWSER_STREAM_IMPL_H_ 7 #pragma once 8 9 #include "include/cef_stream.h" 10 11 #include <stdio.h> 12 #include <string> 13 14 #include "base/synchronization/lock.h" 15 16 // Implementation of CefStreamReader for files. 17 class CefFileReader : public CefStreamReader { 18 public: 19 CefFileReader(FILE* file, bool close); 20 ~CefFileReader() override; 21 22 size_t Read(void* ptr, size_t size, size_t n) override; 23 int Seek(int64 offset, int whence) override; 24 int64 Tell() override; 25 int Eof() override; MayBlock()26 bool MayBlock() override { return true; } 27 28 protected: 29 bool close_; 30 FILE* file_; 31 32 base::Lock lock_; 33 34 IMPLEMENT_REFCOUNTING(CefFileReader); 35 }; 36 37 // Implementation of CefStreamWriter for files. 38 class CefFileWriter : public CefStreamWriter { 39 public: 40 CefFileWriter(FILE* file, bool close); 41 ~CefFileWriter() override; 42 43 size_t Write(const void* ptr, size_t size, size_t n) override; 44 int Seek(int64 offset, int whence) override; 45 int64 Tell() override; 46 int Flush() override; MayBlock()47 bool MayBlock() override { return true; } 48 49 protected: 50 FILE* file_; 51 bool close_; 52 53 base::Lock lock_; 54 55 IMPLEMENT_REFCOUNTING(CefFileWriter); 56 }; 57 58 // Implementation of CefStreamReader for byte buffers. 59 class CefBytesReader : public CefStreamReader { 60 public: 61 CefBytesReader(void* data, int64 datasize, bool copy); 62 ~CefBytesReader() override; 63 64 size_t Read(void* ptr, size_t size, size_t n) override; 65 int Seek(int64 offset, int whence) override; 66 int64 Tell() override; 67 int Eof() override; MayBlock()68 bool MayBlock() override { return false; } 69 70 void SetData(void* data, int64 datasize, bool copy); 71 GetData()72 void* GetData() { return data_; } GetDataSize()73 size_t GetDataSize() { return offset_; } 74 75 protected: 76 void* data_; 77 int64 datasize_; 78 bool copy_; 79 int64 offset_; 80 81 base::Lock lock_; 82 83 IMPLEMENT_REFCOUNTING(CefBytesReader); 84 }; 85 86 // Implementation of CefStreamWriter for byte buffers. 87 class CefBytesWriter : public CefStreamWriter { 88 public: 89 explicit CefBytesWriter(size_t grow); 90 ~CefBytesWriter() override; 91 92 size_t Write(const void* ptr, size_t size, size_t n) override; 93 int Seek(int64 offset, int whence) override; 94 int64 Tell() override; 95 int Flush() override; MayBlock()96 bool MayBlock() override { return false; } 97 GetData()98 void* GetData() { return data_; } GetDataSize()99 int64 GetDataSize() { return offset_; } 100 std::string GetDataString(); 101 102 protected: 103 size_t Grow(size_t size); 104 105 size_t grow_; 106 void* data_; 107 int64 datasize_; 108 int64 offset_; 109 110 base::Lock lock_; 111 112 IMPLEMENT_REFCOUNTING(CefBytesWriter); 113 }; 114 115 // Implementation of CefStreamReader for handlers. 116 class CefHandlerReader : public CefStreamReader { 117 public: CefHandlerReader(CefRefPtr<CefReadHandler> handler)118 explicit CefHandlerReader(CefRefPtr<CefReadHandler> handler) 119 : handler_(handler) {} 120 Read(void * ptr,size_t size,size_t n)121 size_t Read(void* ptr, size_t size, size_t n) override { 122 return handler_->Read(ptr, size, n); 123 } Seek(int64 offset,int whence)124 int Seek(int64 offset, int whence) override { 125 return handler_->Seek(offset, whence); 126 } Tell()127 int64 Tell() override { return handler_->Tell(); } Eof()128 int Eof() override { return handler_->Eof(); } MayBlock()129 bool MayBlock() override { return handler_->MayBlock(); } 130 131 protected: 132 CefRefPtr<CefReadHandler> handler_; 133 134 IMPLEMENT_REFCOUNTING(CefHandlerReader); 135 }; 136 137 // Implementation of CefStreamWriter for handlers. 138 class CefHandlerWriter : public CefStreamWriter { 139 public: CefHandlerWriter(CefRefPtr<CefWriteHandler> handler)140 explicit CefHandlerWriter(CefRefPtr<CefWriteHandler> handler) 141 : handler_(handler) {} 142 Write(const void * ptr,size_t size,size_t n)143 size_t Write(const void* ptr, size_t size, size_t n) override { 144 return handler_->Write(ptr, size, n); 145 } Seek(int64 offset,int whence)146 int Seek(int64 offset, int whence) override { 147 return handler_->Seek(offset, whence); 148 } Tell()149 int64 Tell() override { return handler_->Tell(); } Flush()150 int Flush() override { return handler_->Flush(); } MayBlock()151 bool MayBlock() override { return handler_->MayBlock(); } 152 153 protected: 154 CefRefPtr<CefWriteHandler> handler_; 155 156 IMPLEMENT_REFCOUNTING(CefHandlerWriter); 157 }; 158 159 #endif // CEF_LIBCEF_BROWSER_STREAM_IMPL_H_ 160