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"
14 #include "net/disk_cache/disk_cache.h"
18 class URLRequestContext
;
20 class NET_EXPORT ViewCacheHelper
{
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
,
32 const CompletionCallback
& callback
);
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
,
42 const CompletionCallback
& callback
);
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
);
53 STATE_GET_BACKEND_COMPLETE
,
54 STATE_OPEN_NEXT_ENTRY
,
55 STATE_OPEN_NEXT_ENTRY_COMPLETE
,
57 STATE_OPEN_ENTRY_COMPLETE
,
59 STATE_READ_RESPONSE_COMPLETE
,
61 STATE_READ_DATA_COMPLETE
64 // Implements GetEntryInfoHTML and GetContentsHTML.
65 int GetInfoHTML(const std::string
& key
,
66 const URLRequestContext
* context
,
67 const std::string
& url_prefix
,
69 const CompletionCallback
& callback
);
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
);
75 // This will trigger the completion callback if appropriate.
76 void HandleResult(int rv
);
78 // Runs the state transition loop.
79 int DoLoop(int result
);
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.
85 int DoGetBackendComplete(int result
);
86 int DoOpenNextEntry();
87 int DoOpenNextEntryComplete(int result
);
89 int DoOpenEntryComplete(int result
);
91 int DoReadResponseComplete(int result
);
93 int DoReadDataComplete(int result
);
95 // Called to signal completion of asynchronous IO.
96 void OnIOComplete(int result
);
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_
;
107 std::string url_prefix_
;
109 CompletionCallback callback_
;
113 base::WeakPtrFactory
<ViewCacheHelper
> weak_factory_
;
115 DISALLOW_COPY_AND_ASSIGN(ViewCacheHelper
);
120 #endif // NET_URL_REQUEST_VIEW_CACHE_HELPER_H_