12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef ANDROID_WEBVIEW_NATIVE_INPUT_STREAM_READER_H_
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define ANDROID_WEBVIEW_NATIVE_INPUT_STREAM_READER_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/ref_counted.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace net {
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class HttpByteRange;
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class IOBuffer;
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace android_webview {
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class InputStream;
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Class responsible for reading the InputStream.
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class InputStreamReader {
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The constructor is called on the IO thread, not on the worker thread.
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InputStreamReader(android_webview::InputStream* stream);
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~InputStreamReader();
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Perform a seek operation on the InputStream associated with this job.
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // On successful completion the InputStream would have skipped reading the
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // number of bytes equal to the lower range of |byte_range|.
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This method should be called on the |g_worker_thread| thread.
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |byte_range| is the range of bytes to be read from |stream|
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A negative return value will indicate an error code, a positive value
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // will indicate the expected size of the content.
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual int Seek(const net::HttpByteRange& byte_range);
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Read data from |stream_|.  This method should be called on the
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |g_worker_thread| thread.
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A negative return value will indicate an error code, a positive value
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // will indicate the expected size of the content.
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual int ReadRawData(net::IOBuffer* buffer, int buffer_size);
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Verify the requested range against the stream size.
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // net::OK is returned on success, the error code otherwise.
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int VerifyRequestedRange(net::HttpByteRange* byte_range,
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           int* content_size);
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Skip to the first byte of the requested read range.
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // net::OK is returned on success, the error code otherwise.
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int SkipToRequestedRange(const net::HttpByteRange& byte_range);
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  android_webview::InputStream* stream_;
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(InputStreamReader);
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // namespace android_webview
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif //  ANDROID_WEBVIEW_NATIVE_INPUT_STREAM_READER_H_
62