Home | History | Annotate | Download | only in url_request
      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 
     15 namespace disk_cache {
     16 class Backend;
     17 class Entry;
     18 }  // namespace disk_cache
     19 
     20 namespace net {
     21 
     22 class URLRequestContext;
     23 
     24 class NET_EXPORT ViewCacheHelper {
     25  public:
     26   ViewCacheHelper();
     27   ~ViewCacheHelper();
     28 
     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,
     35                        std::string* out,
     36                        const CompletionCallback& callback);
     37 
     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,
     45                       std::string* out,
     46                       const CompletionCallback& callback);
     47 
     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);
     52 
     53  private:
     54   enum State {
     55     STATE_NONE,
     56     STATE_GET_BACKEND,
     57     STATE_GET_BACKEND_COMPLETE,
     58     STATE_OPEN_NEXT_ENTRY,
     59     STATE_OPEN_NEXT_ENTRY_COMPLETE,
     60     STATE_OPEN_ENTRY,
     61     STATE_OPEN_ENTRY_COMPLETE,
     62     STATE_READ_RESPONSE,
     63     STATE_READ_RESPONSE_COMPLETE,
     64     STATE_READ_DATA,
     65     STATE_READ_DATA_COMPLETE
     66   };
     67 
     68   // Implements GetEntryInfoHTML and GetContentsHTML.
     69   int GetInfoHTML(const std::string& key,
     70                   const URLRequestContext* context,
     71                   const std::string& url_prefix,
     72                   std::string* out,
     73                   const CompletionCallback& callback);
     74 
     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);
     78 
     79   // This will trigger the completion callback if appropriate.
     80   void HandleResult(int rv);
     81 
     82   // Runs the state transition loop.
     83   int DoLoop(int result);
     84 
     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.
     88   int DoGetBackend();
     89   int DoGetBackendComplete(int result);
     90   int DoOpenNextEntry();
     91   int DoOpenNextEntryComplete(int result);
     92   int DoOpenEntry();
     93   int DoOpenEntryComplete(int result);
     94   int DoReadResponse();
     95   int DoReadResponseComplete(int result);
     96   int DoReadData();
     97   int DoReadDataComplete(int result);
     98 
     99   // Called to signal completion of asynchronous IO.
    100   void OnIOComplete(int result);
    101 
    102   const URLRequestContext* context_;
    103   disk_cache::Backend* disk_cache_;
    104   disk_cache::Entry* entry_;
    105   void* iter_;
    106   scoped_refptr<IOBuffer> buf_;
    107   int buf_len_;
    108   int index_;
    109 
    110   std::string key_;
    111   std::string url_prefix_;
    112   std::string* data_;
    113   CompletionCallback callback_;
    114 
    115   State next_state_;
    116 
    117   base::WeakPtrFactory<ViewCacheHelper> weak_factory_;
    118 
    119   DISALLOW_COPY_AND_ASSIGN(ViewCacheHelper);
    120 };
    121 
    122 }  // namespace net.
    123 
    124 #endif  // NET_URL_REQUEST_VIEW_CACHE_HELPER_H_
    125