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