summaryrefslogtreecommitdiffstats
path: root/chrome_frame/urlmon_url_request.h
blob: ebb9f5678a356e0628dd52a57eacc9fb626074e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_FRAME_URLMON_URL_REQUEST_H_
#define CHROME_FRAME_URLMON_URL_REQUEST_H_

#include <urlmon.h>
#include <atlbase.h>
#include <atlcom.h>
#include <string>

#include "base/lock.h"
#include "base/scoped_comptr_win.h"
#include "base/thread.h"
#include "base/waitable_event.h"
#include "chrome_frame/plugin_url_request.h"
#include "chrome_frame/utils.h"

class UrlmonUrlRequest;

class UrlmonUrlRequestManager :
    public PluginUrlRequestManager,
    public PluginUrlRequestDelegate {
 public:
  UrlmonUrlRequestManager();
  ~UrlmonUrlRequestManager();

  // Use specific moniker and bind context when Chrome request this url.
  // Used from ChromeActiveDocument's implementation of IPersistMoniker::Load().
  void UseMonikerForUrl(IMoniker* moniker, IBindCtx* bind_ctx,
                        const std::wstring& url);
  void StealMonikerFromRequest(int request_id, IMoniker** moniker);
  void SetErrorDialogsParentWindow(HWND window);
 private:
   struct MonikerForUrl {
     MonikerForUrl() {
       memset(&bind_opts, 0, sizeof(bind_opts));
     }
     ScopedComPtr<IMoniker> moniker;
     BIND_OPTS bind_opts;
     std::wstring url;
   };

  friend class MessageLoop;
  friend struct RunnableMethodTraits<UrlmonUrlRequestManager>;
  static bool ImplementsThreadSafeReferenceCounting() { return true; }
  void AddRef() {}
  void Release() {}

  // PluginUrlRequestManager implementation.
  virtual bool IsThreadSafe();
  virtual void StartRequest(int request_id,
    const IPC::AutomationURLRequest& request_info);
  virtual void ReadRequest(int request_id, int bytes_to_read);
  virtual void EndRequest(int request_id);
  virtual void StopAll();

  // PluginUrlRequestDelegate implementation
  virtual void OnResponseStarted(int request_id, const char* mime_type,
    const char* headers, int size, base::Time last_modified,
    const std::string& peristent_cookies, const std::string& redirect_url,
    int redirect_status);
  virtual void OnReadComplete(int request_id, const void* buffer, int len);
  virtual void OnResponseEnd(int request_id, const URLRequestStatus& status);

  bool ExecuteInWorkerThread(const tracked_objects::Location& from_here,
                             Task* task);
  // Methods executed in worker thread.
  void StartRequestWorker(int request_id,
                          const IPC::AutomationURLRequest& request_info,
                          MonikerForUrl* moniker_for_url);
  void ReadRequestWorker(int request_id, int bytes_to_read);
  void EndRequestWorker(int request_id);
  void StopAllWorker();
  void StealMonikerFromRequestWorker(int request_id, IMoniker** moniker,
                                     base::WaitableEvent* done);

  // Map for (request_id <-> UrlmonUrlRequest)
  typedef std::map<int, scoped_refptr<UrlmonUrlRequest> > RequestMap;
  RequestMap request_map_;
  scoped_refptr<UrlmonUrlRequest> LookupRequest(int request_id);

  scoped_ptr<MonikerForUrl> moniker_for_url_;
  STAThread worker_thread_;
  base::WaitableEvent map_empty_;
  bool stopping_;
  Lock worker_thread_access_;
  HWND err_dialog_parent_wnd_;
};

#endif  // CHROME_FRAME_URLMON_URL_REQUEST_H_