Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / url_request / url_request_simple_job.h
blob6c9d5e8b929f4daf8297036c4624db0d0aa4b643
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.
5 #ifndef NET_URL_REQUEST_URL_REQUEST_SIMPLE_JOB_H_
6 #define NET_URL_REQUEST_URL_REQUEST_SIMPLE_JOB_H_
8 #include <string>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/strings/string_piece.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/net_export.h"
15 #include "net/url_request/url_range_request_job.h"
17 namespace base {
18 class RefCountedMemory;
21 namespace net {
23 class URLRequest;
25 class NET_EXPORT URLRequestSimpleJob : public URLRangeRequestJob {
26 public:
27 URLRequestSimpleJob(URLRequest* request, NetworkDelegate* network_delegate);
29 void Start() override;
30 void Kill() override;
31 bool ReadRawData(IOBuffer* buf, int buf_size, int* bytes_read) override;
32 bool GetMimeType(std::string* mime_type) const override;
33 bool GetCharset(std::string* charset) override;
35 protected:
36 ~URLRequestSimpleJob() override;
38 // Subclasses must override either GetData or GetRefCountedData to define the
39 // way response data is determined.
40 // The return value should be:
41 // - OK if data is obtained;
42 // - ERR_IO_PENDING if async processing is needed to finish obtaining data.
43 // This is the only case when |callback| should be called after
44 // completion of the operation. In other situations |callback| should
45 // never be called;
46 // - any other ERR_* code to indicate an error. This code will be used
47 // as the error code in the URLRequestStatus when the URLRequest
48 // is finished.
49 virtual int GetData(std::string* mime_type,
50 std::string* charset,
51 std::string* data,
52 const CompletionCallback& callback) const;
54 // Similar to GetData(), except |*data| can share ownership of the bytes
55 // instead of copying them into a std::string.
56 virtual int GetRefCountedData(std::string* mime_type,
57 std::string* charset,
58 scoped_refptr<base::RefCountedMemory>* data,
59 const CompletionCallback& callback) const;
61 // Returns the task runner used by ReadRawData. This method is virtual so
62 // that it can be overridden in tests.
63 virtual base::TaskRunner* GetTaskRunner() const;
65 void StartAsync();
67 private:
68 void OnGetDataCompleted(int result);
69 void OnReadCompleted(int bytes_read);
71 HttpByteRange byte_range_;
72 std::string mime_type_;
73 std::string charset_;
74 scoped_refptr<base::RefCountedMemory> data_;
75 int64 next_data_offset_;
76 scoped_refptr<base::TaskRunner> task_runner_;
77 base::WeakPtrFactory<URLRequestSimpleJob> weak_factory_;
80 } // namespace net
82 #endif // NET_URL_REQUEST_URL_REQUEST_SIMPLE_JOB_H_