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 EXTENSIONS_BROWSER_API_WEB_REQUEST_FORM_DATA_PARSER_H_
6 #define EXTENSIONS_BROWSER_API_WEB_REQUEST_FORM_DATA_PARSER_H_
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"
19 namespace extensions
{
21 // Interface for the form data parsers.
22 class FormDataParser
{
24 // Result encapsulates name-value pairs returned by GetNextNameValue.
30 const std::string
& name() const { return name_
; }
31 const std::string
& value() const { return value_
; }
33 void set_name(base::StringPiece str
) { str
.CopyToString(&name_
); }
34 void set_value(base::StringPiece str
) { str
.CopyToString(&value_
); }
40 DISALLOW_COPY_AND_ASSIGN(Result
);
43 virtual ~FormDataParser();
45 // Creates a correct parser instance based on the |request|. Returns NULL
47 static scoped_ptr
<FormDataParser
> Create(const net::URLRequest
& request
);
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
);
55 // Returns true if there was some data, it was well formed and all was read.
56 virtual bool AllDataReadOK() = 0;
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;
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;
77 DISALLOW_COPY_AND_ASSIGN(FormDataParser
);
80 } // namespace extensions
82 #endif // EXTENSIONS_BROWSER_API_WEB_REQUEST_FORM_DATA_PARSER_H_