Home | History | Annotate | Download | only in web_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 CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_FORM_DATA_PARSER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_FORM_DATA_PARSER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/scoped_ptr.h"
     12 // Cannot forward declare StringPiece because it is a typedef.
     13 #include "base/strings/string_piece.h"
     14 
     15 namespace net {
     16 class URLRequest;
     17 }
     18 
     19 namespace extensions {
     20 
     21 // Interface for the form data parsers.
     22 class FormDataParser {
     23  public:
     24   // Result encapsulates name-value pairs returned by GetNextNameValue.
     25   class Result {
     26    public:
     27     Result();
     28     ~Result();
     29 
     30     const std::string& name() const { return name_; }
     31     const std::string& value() const { return value_; }
     32 
     33     void set_name(base::StringPiece str) { str.CopyToString(&name_); }
     34     void set_value(base::StringPiece str) { str.CopyToString(&value_); }
     35 
     36    private:
     37     std::string name_;
     38     std::string value_;
     39 
     40     DISALLOW_COPY_AND_ASSIGN(Result);
     41   };
     42 
     43   virtual ~FormDataParser();
     44 
     45   // Creates a correct parser instance based on the |request|. Returns NULL
     46   // on failure.
     47   static scoped_ptr<FormDataParser> Create(const net::URLRequest& request);
     48 
     49   // Creates a correct parser instance based on |content_type_header|, the
     50   // "Content-Type" request header value. If |content_type_header| is NULL, it
     51   // defaults to "application/x-www-form-urlencoded". Returns NULL on failure.
     52   static scoped_ptr<FormDataParser> CreateFromContentTypeHeader(
     53       const std::string* content_type_header);
     54 
     55   // Returns true if there was some data, it was well formed and all was read.
     56   virtual bool AllDataReadOK() = 0;
     57 
     58   // Gets the next name-value pair from the source data and stores it in
     59   // |result|. Returns true if a pair was found. Callers must have previously
     60   // succesfully called the SetSource method.
     61   virtual bool GetNextNameValue(Result* result) = 0;
     62 
     63   // Sets the |source| of the data to be parsed. One form data parser is only
     64   // expected to be associated with one source, so generally, SetSource should
     65   // be only called once. However, for technical reasons, the source might only
     66   // be available in chunks for multipart encoded forms, in which case it is OK
     67   // to call SetSource multiple times to add all chunks of a single source. The
     68   // ownership of |source| is left with the caller and the source should live
     69   // until |this| dies or |this->SetSource()| is called again, whichever comes
     70   // sooner. Returns true on success.
     71   virtual bool SetSource(base::StringPiece source) = 0;
     72 
     73  protected:
     74   FormDataParser();
     75 
     76  private:
     77   DISALLOW_COPY_AND_ASSIGN(FormDataParser);
     78 };
     79 
     80 }  // namespace extensions
     81 
     82 #endif  // CHROME_BROWSER_EXTENSIONS_API_WEB_REQUEST_FORM_DATA_PARSER_H_
     83