• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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_URL_REQUEST_VIEW_CACHE_HELPER_H_
6 #define NET_URL_REQUEST_VIEW_CACHE_HELPER_H_
7 #pragma once
8 
9 #include <string>
10 
11 #include "net/base/completion_callback.h"
12 #include "net/base/io_buffer.h"
13 
14 namespace disk_cache {
15 class Backend;
16 class Entry;
17 }  // namespace disk_cache
18 
19 namespace net {
20 
21 class URLRequestContext;
22 
23 class ViewCacheHelper {
24  public:
25   ViewCacheHelper();
26   ~ViewCacheHelper();
27 
28   // Formats the cache information for |key| as HTML. Returns a net error code.
29   // If this method returns ERR_IO_PENDING, |callback| will be notified when the
30   // operation completes. |out| must remain valid until this operation completes
31   // or the object is destroyed.
32   int GetEntryInfoHTML(const std::string& key,
33                        URLRequestContext* context,
34                        std::string* out,
35                        CompletionCallback* callback);
36 
37   // Formats the cache contents as HTML. Returns a net error code.
38   // If this method returns ERR_IO_PENDING, |callback| will be notified when the
39   // operation completes. |out| must remain valid until this operation completes
40   // or the object is destroyed. |url_prefix| will be prepended to each entry
41   // key as a link to the entry.
42   int GetContentsHTML(URLRequestContext* context,
43                       const std::string& url_prefix,
44                       std::string* out,
45                       CompletionCallback* callback);
46 
47  private:
48   enum State {
49     STATE_NONE,
50     STATE_GET_BACKEND,
51     STATE_GET_BACKEND_COMPLETE,
52     STATE_OPEN_NEXT_ENTRY,
53     STATE_OPEN_NEXT_ENTRY_COMPLETE,
54     STATE_OPEN_ENTRY,
55     STATE_OPEN_ENTRY_COMPLETE,
56     STATE_READ_RESPONSE,
57     STATE_READ_RESPONSE_COMPLETE,
58     STATE_READ_DATA,
59     STATE_READ_DATA_COMPLETE
60   };
61 
62   // Implements GetEntryInfoHTML and GetContentsHTML.
63   int GetInfoHTML(const std::string& key,
64                   URLRequestContext* context,
65                   const std::string& url_prefix,
66                   std::string* out,
67                   CompletionCallback* callback);
68 
69   // This is a helper function used to trigger a completion callback. It may
70   // only be called if callback_ is non-null.
71   void DoCallback(int rv);
72 
73   // This will trigger the completion callback if appropriate.
74   void HandleResult(int rv);
75 
76   // Runs the state transition loop.
77   int DoLoop(int result);
78 
79   // Each of these methods corresponds to a State value. If there is an
80   // argument, the value corresponds to the return of the previous state or
81   // corresponding callback.
82   int DoGetBackend();
83   int DoGetBackendComplete(int result);
84   int DoOpenNextEntry();
85   int DoOpenNextEntryComplete(int result);
86   int DoOpenEntry();
87   int DoOpenEntryComplete(int result);
88   int DoReadResponse();
89   int DoReadResponseComplete(int result);
90   int DoReadData();
91   int DoReadDataComplete(int result);
92 
93   // Called to signal completion of asynchronous IO.
94   void OnIOComplete(int result);
95 
96   scoped_refptr<URLRequestContext> context_;
97   disk_cache::Backend* disk_cache_;
98   disk_cache::Entry* entry_;
99   void* iter_;
100   scoped_refptr<IOBuffer> buf_;
101   int buf_len_;
102   int index_;
103 
104   std::string key_;
105   std::string url_prefix_;
106   std::string* data_;
107   CompletionCallback* callback_;
108 
109   State next_state_;
110 
111   CompletionCallbackImpl<ViewCacheHelper> cache_callback_;
112   scoped_refptr<CancelableCompletionCallback<ViewCacheHelper> > entry_callback_;
113 
114   DISALLOW_COPY_AND_ASSIGN(ViewCacheHelper);
115 };
116 
117 }  // namespace net.
118 
119 #endif  // NET_URL_REQUEST_VIEW_CACHE_HELPER_H_
120