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.
5 #ifndef NET_URL_REQUEST_VIEW_CACHE_HELPER_H_
6 #define NET_URL_REQUEST_VIEW_CACHE_HELPER_H_
11 #include "base/memory/weak_ptr.h"
12 #include "net/base/completion_callback.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_export.h"
16 namespace disk_cache
{
19 } // namespace disk_cache
23 class URLRequestContext
;
25 class NET_EXPORT ViewCacheHelper
{
30 // Formats the cache information for |key| as HTML. Returns a net error code.
31 // If this method returns ERR_IO_PENDING, |callback| will be notified when the
32 // operation completes. |out| must remain valid until this operation completes
33 // or the object is destroyed.
34 int GetEntryInfoHTML(const std::string
& key
,
35 const URLRequestContext
* context
,
37 const CompletionCallback
& callback
);
39 // Formats the cache contents as HTML. Returns a net error code.
40 // If this method returns ERR_IO_PENDING, |callback| will be notified when the
41 // operation completes. |out| must remain valid until this operation completes
42 // or the object is destroyed. |url_prefix| will be prepended to each entry
43 // key as a link to the entry.
44 int GetContentsHTML(const URLRequestContext
* context
,
45 const std::string
& url_prefix
,
47 const CompletionCallback
& callback
);
49 // Lower-level helper to produce a textual representation of binary data.
50 // The results are appended to |result| and can be used in HTML pages
51 // provided the dump is contained within <pre></pre> tags.
52 static void HexDump(const char *buf
, size_t buf_len
, std::string
* result
);
58 STATE_GET_BACKEND_COMPLETE
,
59 STATE_OPEN_NEXT_ENTRY
,
60 STATE_OPEN_NEXT_ENTRY_COMPLETE
,
62 STATE_OPEN_ENTRY_COMPLETE
,
64 STATE_READ_RESPONSE_COMPLETE
,
66 STATE_READ_DATA_COMPLETE
69 // Implements GetEntryInfoHTML and GetContentsHTML.
70 int GetInfoHTML(const std::string
& key
,
71 const URLRequestContext
* context
,
72 const std::string
& url_prefix
,
74 const CompletionCallback
& callback
);
76 // This is a helper function used to trigger a completion callback. It may
77 // only be called if callback_ is non-null.
78 void DoCallback(int rv
);
80 // This will trigger the completion callback if appropriate.
81 void HandleResult(int rv
);
83 // Runs the state transition loop.
84 int DoLoop(int result
);
86 // Each of these methods corresponds to a State value. If there is an
87 // argument, the value corresponds to the return of the previous state or
88 // corresponding callback.
90 int DoGetBackendComplete(int result
);
91 int DoOpenNextEntry();
92 int DoOpenNextEntryComplete(int result
);
94 int DoOpenEntryComplete(int result
);
96 int DoReadResponseComplete(int result
);
98 int DoReadDataComplete(int result
);
100 // Called to signal completion of asynchronous IO.
101 void OnIOComplete(int result
);
103 scoped_refptr
<const URLRequestContext
> context_
;
104 disk_cache::Backend
* disk_cache_
;
105 disk_cache::Entry
* entry_
;
107 scoped_refptr
<IOBuffer
> buf_
;
112 std::string url_prefix_
;
114 CompletionCallback callback_
;
118 base::WeakPtrFactory
<ViewCacheHelper
> weak_factory_
;
120 DISALLOW_COPY_AND_ASSIGN(ViewCacheHelper
);
125 #endif // NET_URL_REQUEST_VIEW_CACHE_HELPER_H_