1// Copyright (c) 2011 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_FRAME_HTTP_NEGOTIATE_H_
6#define CHROME_FRAME_HTTP_NEGOTIATE_H_
7
8#include <shdeprecated.h>
9#include <urlmon.h>
10
11#include <string>
12
13#include "base/basictypes.h"
14#include "base/win/scoped_comptr.h"
15
16// Typedefs for IHttpNegotiate methods.
17typedef HRESULT (STDMETHODCALLTYPE* IHttpNegotiate_BeginningTransaction_Fn)(
18    IHttpNegotiate* me, LPCWSTR url, LPCWSTR headers, DWORD reserved,
19    LPWSTR* additional_headers);
20typedef HRESULT (STDMETHODCALLTYPE* IHttpNegotiate_OnResponse_Fn)(
21    IHttpNegotiate* me, DWORD response_code, LPCWSTR response_header,
22    LPCWSTR request_header, LPWSTR* additional_request_headers);
23
24// Typedefs for IBindStatusCallback methods.
25typedef HRESULT (STDMETHODCALLTYPE* IBindStatusCallback_StartBinding_Fn)(
26    IBindStatusCallback* me, DWORD reserved, IBinding *binding);
27
28// Typedefs for IInternetProtocolSink methods.
29typedef HRESULT (STDMETHODCALLTYPE* IInternetProtocolSink_ReportProgress_Fn)(
30    IInternetProtocolSink* me, ULONG status_code, LPCWSTR status_text);
31
32// Patches methods of urlmon's IHttpNegotiate implementation for the purposes
33// of adding to the http user agent header.
34
35// Also patches one of the IBindStatusCallback implementations in urlmon to pick
36// up an IBinding during the StartBinding call. The IBinding implementor then
37// gets a patch applied to its IInternetProtocolSink's ReportProgress method.
38// The patched is there so that the reporting of the MIME type to the IBinding
39// implementor can be changed if an X-Chrome-Frame HTTP header is present
40// in the response headers. If anyone can suggest a more straightforward way of
41// doing this, I would be eternally grateful.
42class HttpNegotiatePatch {
43  // class is not to be instantiated atm.
44  HttpNegotiatePatch();
45  ~HttpNegotiatePatch();
46
47 public:
48  static bool Initialize();
49  static void Uninitialize();
50
51  // Enables and disables the User Agent header modification. It is enabled
52  // by default when the patch is installed.
53  static void set_modify_user_agent(bool value) { modify_user_agent_ = value; }
54
55  // IHttpNegotiate patch methods
56  static STDMETHODIMP BeginningTransaction(
57      IHttpNegotiate_BeginningTransaction_Fn original, IHttpNegotiate* me,
58      LPCWSTR url, LPCWSTR headers, DWORD reserved, LPWSTR* additional_headers);
59
60 protected:
61  static HRESULT PatchHttpNegotiate(IUnknown* to_patch);
62
63 private:
64  static bool modify_user_agent_;
65
66  DISALLOW_COPY_AND_ASSIGN(HttpNegotiatePatch);
67};
68
69// From the latest urlmon.h. Symbol name prepended with LOCAL_ to
70// avoid conflict (and therefore build errors) for those building with
71// a newer Windows SDK.
72// TODO(robertshield): Remove this once we update our SDK version.
73extern const int LOCAL_BINDSTATUS_SERVER_MIMETYPEAVAILABLE;
74
75// Scans |additional_headers| and |headers| for User-Agent header or grabs
76// the default User-Agent if none is found. Append "chromeframe" at the end
77// of the string. Returns the original content of |additional_headers| but
78// with the new User-Agent header. Somewhat unusual interface is dictated
79// because we use it in IHttpNegotiate::BeginningTransaction.
80// The function is a public since we want to use it from
81// UrlmonUrlRequest::BeginningTransaction for the unusual but yet possible case
82// when |headers| contains an User-Agent string.
83std::string AppendCFUserAgentString(LPCWSTR headers,
84                                    LPCWSTR additional_headers);
85
86// Adds or replaces the User-Agent header in a set of HTTP headers.
87// Arguments are the same as with AppendCFUserAgentString.
88std::string ReplaceOrAddUserAgent(LPCWSTR headers,
89                                  const std::string& user_agent_value);
90
91#endif  // CHROME_FRAME_HTTP_NEGOTIATE_H_
92