summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/resource_message_filter.h
blob: db7c60b36c79274271bf44d8e3b0c4ba6de14f5d (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2006-2008 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_BROWSER_RENDERER_HOST_RESOURCE_MSG_FILTER_H_
#define CHROME_BROWSER_RENDERER_HOST_RESOURCE_MSG_FILTER_H_

#include <string>
#include <vector>

#include "base/clipboard.h"
#include "base/file_path.h"
#include "base/gfx/rect.h"
#include "base/gfx/native_widget_types.h"
#include "base/ref_counted.h"
#include "base/shared_memory.h"
#include "base/string16.h"
#include "build/build_config.h"
#include "chrome/browser/net/resolve_proxy_msg_helper.h"
#include "chrome/browser/renderer_host/render_widget_helper.h"
#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
#include "chrome/common/ipc_channel_proxy.h"
#include "chrome/common/modal_dialog_event.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/transport_dib.h"
#include "third_party/WebKit/WebKit/chromium/public/WebCache.h"

#if defined(OS_WIN)
#include <windows.h>
#else
// TODO(port): port ResourceDispatcherHost.
#include "chrome/common/temp_scaffolding_stubs.h"
#endif

class AudioRendererHost;
class ClipboardService;
class Profile;
class RenderWidgetHelper;
class SpellChecker;
struct ViewHostMsg_Audio_CreateStream;
struct WebPluginInfo;

namespace printing {
class PrinterQuery;
class PrintJobManager;
}

namespace WebKit {
struct WebScreenInfo;
}

// This class filters out incoming IPC messages for network requests and
// processes them on the IPC thread.  As a result, network requests are not
// delayed by costly UI processing that may be occuring on the main thread of
// the browser.  It also means that any hangs in starting a network request
// will not interfere with browser UI.

class ResourceMessageFilter : public IPC::ChannelProxy::MessageFilter,
                              public ResourceDispatcherHost::Receiver,
                              public NotificationObserver,
                              public ResolveProxyMsgHelper::Delegate {
 public:
  // Create the filter.
  // Note:  because the lifecycle of the ResourceMessageFilter is not
  //        tied to the lifecycle of the object which created it, the
  //        ResourceMessageFilter is 'given' ownership of the spellchecker
  //        object and must clean it up on exit.
  ResourceMessageFilter(ResourceDispatcherHost* resource_dispatcher_host,
                        AudioRendererHost* audio_renderer_host,
                        PluginService* plugin_service,
                        printing::PrintJobManager* print_job_manager,
                        Profile* profile,
                        RenderWidgetHelper* render_widget_helper,
                        SpellChecker* spellchecker);
  virtual ~ResourceMessageFilter();

  void Init(int render_process_id);

  // IPC::ChannelProxy::MessageFilter methods:
  virtual void OnFilterAdded(IPC::Channel* channel);
  virtual void OnChannelConnected(int32 peer_pid);
  virtual void OnChannelClosing();
  virtual bool OnMessageReceived(const IPC::Message& message);

  // ResourceDispatcherHost::Receiver methods:
  virtual bool Send(IPC::Message* message);
  virtual URLRequestContext* GetRequestContext(
      uint32 request_id,
      const ViewHostMsg_Resource_Request& request_data);
  virtual int GetProcessId() const { return render_process_id_; }

  SpellChecker* spellchecker() { return spellchecker_.get(); }
  ResourceDispatcherHost* resource_dispatcher_host() {
    return resource_dispatcher_host_;
  }
  MessageLoop* ui_loop() { return render_widget_helper_->ui_loop(); }
  bool off_the_record() { return off_the_record_; }

  // NotificationObserver implementation.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

 private:
  void OnMsgCreateWindow(int opener_id, bool user_gesture, int* route_id,
                         ModalDialogEvent* modal_dialog_event);
  void OnMsgCreateWidget(int opener_id, bool activatable, int* route_id);
  void OnSetCookie(const GURL& url, const GURL& policy_url,
                   const std::string& cookie);
  void OnGetCookies(const GURL& url, const GURL& policy_url,
                    std::string* cookies);
  void OnGetDataDir(std::wstring* data_dir);
  void OnPluginMessage(const FilePath& plugin_path,
                       const std::vector<uint8>& message);
  void OnPluginSyncMessage(const FilePath& plugin_path,
                           const std::vector<uint8>& message,
                           std::vector<uint8> *retval);
  void OnPluginFileDialog(const IPC::Message& msg,
                          bool multiple_files,
                          const std::wstring& title,
                          const std::wstring& filter,
                          uint32 user_data);

#if defined(OS_WIN)  // This hack is Windows-specific.
  // Cache fonts for the renderer. See ResourceMessageFilter::OnLoadFont
  // implementation for more details
  void OnLoadFont(LOGFONT font);
#endif

  void OnGetScreenInfo(gfx::NativeViewId window,
                       WebKit::WebScreenInfo* results);
  void OnGetPlugins(bool refresh, std::vector<WebPluginInfo>* plugins);
  void OnGetPluginPath(const GURL& url,
                       const std::string& mime_type,
                       const std::string& clsid,
                       FilePath* filename,
                       std::string* actual_mime_type);
  void OnOpenChannelToPlugin(const GURL& url,
                             const std::string& mime_type,
                             const std::string& clsid,
                             const std::wstring& locale,
                             IPC::Message* reply_msg);
  void OnCreateDedicatedWorker(const GURL& url,
                               int render_view_route_id,
                               int* route_id);
  void OnForwardToWorker(const IPC::Message& msg);
  void OnDownloadUrl(const IPC::Message& message,
                     const GURL& url,
                     const GURL& referrer);
  void OnSpellCheck(const std::wstring& word,
                    IPC::Message* reply_msg);
  void OnDnsPrefetch(const std::vector<std::string>& hostnames);
  void OnRendererHistograms(const std::vector<std::string>& histogram_info);
  void OnReceiveContextMenuMsg(const IPC::Message& msg);
  // Clipboard messages
  void OnClipboardWriteObjects(const Clipboard::ObjectMap& objects);
  void OnClipboardIsFormatAvailable(Clipboard::FormatType format, bool* result);
  void OnClipboardReadText(string16* result);
  void OnClipboardReadAsciiText(std::string* result);
  void OnClipboardReadHTML(string16* markup, GURL* src_url);
  void OnGetWindowRect(gfx::NativeViewId window, gfx::Rect *rect);
  void OnGetRootWindowRect(gfx::NativeViewId window, gfx::Rect *rect);
  void OnGetMimeTypeFromExtension(const FilePath::StringType& ext,
                                  std::string* mime_type);
  void OnGetMimeTypeFromFile(const FilePath& file_path,
                             std::string* mime_type);
  void OnGetPreferredExtensionForMimeType(const std::string& mime_type,
                                          FilePath::StringType* ext);
  void OnGetCPBrowsingContext(uint32* context);
  void OnDuplicateSection(base::SharedMemoryHandle renderer_handle,
                          base::SharedMemoryHandle* browser_handle);
  void OnResourceTypeStats(const WebKit::WebCache::ResourceTypeStats& stats);

  void OnResolveProxy(const GURL& url, IPC::Message* reply_msg);

  // ResolveProxyMsgHelper::Delegate implementation:
  virtual void OnResolveProxyCompleted(IPC::Message* reply_msg,
                                       int result,
                                       const std::string& proxy_list);

  // A javascript code requested to print the current page. This is done in two
  // steps and this is the first step. Get the print setting right here
  // synchronously. It will hang the I/O completely.
  void OnGetDefaultPrintSettings(IPC::Message* reply_msg);
  void OnGetDefaultPrintSettingsReply(
      scoped_refptr<printing::PrinterQuery> printer_query,
      IPC::Message* reply_msg);
#if defined(OS_WIN)
  // A javascript code requested to print the current page. The renderer host
  // have to show to the user the print dialog and returns the selected print
  // settings.
  void OnScriptedPrint(gfx::NativeViewId host_window,
                       int cookie,
                       int expected_pages_count,
                       IPC::Message* reply_msg);
  void OnScriptedPrintReply(
      scoped_refptr<printing::PrinterQuery> printer_query,
      IPC::Message* reply_msg);
#endif

  // Audio related IPC message handlers.
  void OnCreateAudioStream(const IPC::Message& msg, int stream_id,
                           const ViewHostMsg_Audio_CreateStream& params);
  void OnNotifyAudioPacketReady(const IPC::Message& msg, int stream_id,
                                size_t packet_size);
  void OnStartAudioStream(const IPC::Message& msg, int stream_id);
  void OnCloseAudioStream(const IPC::Message& msg, int stream_id);
  void OnGetAudioVolume(const IPC::Message& msg, int stream_id);
  void OnSetAudioVolume(const IPC::Message& msg, int stream_id,
                        double left_channel, double right_channel);

  // Browser side transport DIB allocation
  void OnAllocTransportDIB(size_t size,
                           TransportDIB::Handle* result);
  void OnFreeTransportDIB(TransportDIB::Id dib_id);

  void OnOpenChannelToExtension(const std::string& extension_id, int* port_id);
  void OnExtensionPostMessage(int port_id, const std::string& message);

  // We have our own clipboard service because we want to access the clipboard
  // on the IO thread instead of forwarding (possibly synchronous) messages to
  // the UI thread.
  // This instance of the clipboard service should be accessed only on the IO
  // thread.
  static ClipboardService* GetClipboardService();

  // The channel associated with the renderer connection. This pointer is not
  // owned by this class.
  IPC::Channel* channel_;

  // Cached resource request dispatcher host and plugin service, guaranteed to
  // be non-null if Init succeeds. We do not own the objects, they are managed
  // by the BrowserProcess, which has a wider scope than we do.
  ResourceDispatcherHost* resource_dispatcher_host_;
  PluginService* plugin_service_;
  printing::PrintJobManager* print_job_manager_;

  // ID for the RenderProcessHost that corresponds to this channel.  This is
  // used by the ResourceDispatcherHost to look up the TabContents that
  // originated URLRequest.  Since the RenderProcessHost can be destroyed
  // before this object, we only hold an ID for lookup.
  int render_process_id_;

  // Our spellchecker object.
  scoped_refptr<SpellChecker> spellchecker_;

  // Helper class for handling PluginProcessHost_ResolveProxy messages (manages
  // the requests to the proxy service).
  ResolveProxyMsgHelper resolve_proxy_msg_helper_;

  // Contextual information to be used for requests created here.
  scoped_refptr<URLRequestContext> request_context_;

  // A request context specific for media resources.
  scoped_refptr<URLRequestContext> media_request_context_;

  // A pointer to the profile associated with this filter.
  //
  // DANGER! Do not dereference this pointer! This class lives on the I/O thread
  // and the profile may only be used on the UI thread. It is used only for
  // determining which notifications to watch for.
  //
  // This is void* to prevent people from accidentally dereferencing it.
  // When registering for observers, cast to Profile*.
  void* profile_;

  scoped_refptr<RenderWidgetHelper> render_widget_helper_;

  // Object that should take care of audio related resource requests.
  scoped_refptr<AudioRendererHost> audio_renderer_host_;

  // Whether this process is used for off the record tabs.
  bool off_the_record_;

  DISALLOW_COPY_AND_ASSIGN(ResourceMessageFilter);
};

#endif  // CHROME_BROWSER_RENDERER_HOST_RESOURCE_MSG_FILTER_H_