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_VIEW_CACHE_HELPER_H_
6 #define NET_URL_REQUEST_VIEW_CACHE_HELPER_H_
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"
15 namespace disk_cache
{
18 } // namespace disk_cache
22 class URLRequestContext
;
24 class NET_EXPORT ViewCacheHelper
{
29 // Formats the cache information for |key| as HTML. Returns a net error code.
30 // If this method returns ERR_IO_PENDING, |callback| will be notified when the
31 // operation completes. |out| must remain valid until this operation completes
32 // or the object is destroyed.
33 int GetEntryInfoHTML(const std::string
& key
,
34 const URLRequestContext
* context
,
36 const CompletionCallback
& callback
);
38 // Formats the cache contents as HTML. Returns a net error code.
39 // If this method returns ERR_IO_PENDING, |callback| will be notified when the
40 // operation completes. |out| must remain valid until this operation completes
41 // or the object is destroyed. |url_prefix| will be prepended to each entry
42 // key as a link to the entry.
43 int GetContentsHTML(const URLRequestContext
* context
,
44 const std::string
& url_prefix
,
46 const CompletionCallback
& callback
);
48 // Lower-level helper to produce a textual representation of binary data.
49 // The results are appended to |result| and can be used in HTML pages
50 // provided the dump is contained within <pre></pre> tags.
51 static void HexDump(const char *buf
, size_t buf_len
, std::string
* result
);
57 STATE_GET_BACKEND_COMPLETE
,
58 STATE_OPEN_NEXT_ENTRY
,
59 STATE_OPEN_NEXT_ENTRY_COMPLETE
,
61 STATE_OPEN_ENTRY_COMPLETE
,
63 STATE_READ_RESPONSE_COMPLETE
,
65 STATE_READ_DATA_COMPLETE
68 // Implements GetEntryInfoHTML and GetContentsHTML.
69 int GetInfoHTML(const std::string
& key
,
70 const URLRequestContext
* context
,
71 const std::string
& url_prefix
,
73 const CompletionCallback
& callback
);
75 // This is a helper function used to trigger a completion callback. It may
76 // only be called if callback_ is non-null.
77 void DoCallback(int rv
);
79 // This will trigger the completion callback if appropriate.
80 void HandleResult(int rv
);
82 // Runs the state transition loop.
83 int DoLoop(int result
);
85 // Each of these methods corresponds to a State value. If there is an
86 // argument, the value corresponds to the return of the previous state or
87 // corresponding callback.
89 int DoGetBackendComplete(int result
);
90 int DoOpenNextEntry();
91 int DoOpenNextEntryComplete(int result
);
93 int DoOpenEntryComplete(int result
);
95 int DoReadResponseComplete(int result
);
97 int DoReadDataComplete(int result
);
99 // Called to signal completion of asynchronous IO.
100 void OnIOComplete(int result
);
102 const URLRequestContext
* context_
;
103 disk_cache::Backend
* disk_cache_
;
104 disk_cache::Entry
* entry_
;
106 scoped_refptr
<IOBuffer
> buf_
;
111 std::string url_prefix_
;
113 CompletionCallback callback_
;
117 base::WeakPtrFactory
<ViewCacheHelper
> weak_factory_
;
119 DISALLOW_COPY_AND_ASSIGN(ViewCacheHelper
);
124 #endif // NET_URL_REQUEST_VIEW_CACHE_HELPER_H_