summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/resource_message_filter.h
blob: 65f34c644fbd6472c57928773f4addd7a0ca5ec6 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright (c) 2010 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_MESSAGE_FILTER_H_
#define CHROME_BROWSER_RENDERER_HOST_RESOURCE_MESSAGE_FILTER_H_

#if defined(OS_WIN)
#include <windows.h>
#endif

#include <string>
#include <vector>

#include "app/clipboard/clipboard.h"
#include "app/gfx/native_widget_types.h"
#include "base/callback.h"
#include "base/file_path.h"
#include "base/gfx/rect.h"
#include "base/process.h"
#include "base/ref_counted.h"
#include "base/shared_memory.h"
#include "base/string16.h"
#include "base/task.h"
#include "build/build_config.h"
#include "chrome/browser/net/resolve_proxy_msg_helper.h"
#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
#include "chrome/browser/renderer_host/translation_service.h"
#include "chrome/common/nacl_types.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/transport_dib.h"
#include "ipc/ipc_channel_proxy.h"
#include "third_party/WebKit/WebKit/chromium/public/WebCache.h"

class AppCacheDispatcherHost;
class AudioRendererHost;
class ChromeURLRequestContext;
class DatabaseDispatcherHost;
class DOMStorageDispatcherHost;
class ExtensionMessageService;
class GeolocationDispatcherHost;
class HostZoomMap;
class NotificationsPrefsCache;
class Profile;
class RenderWidgetHelper;
class URLRequestContextGetter;
struct ViewHostMsg_CreateWorker_Params;
struct WebPluginInfo;

namespace printing {
class PrinterQuery;
class PrintJobManager;
}

namespace webkit_glue {
struct WebCookie;
}

namespace WebKit {
struct WebScreenInfo;
}

struct ViewHostMsg_ScriptedPrint_Params;
#if defined(OS_POSIX) && !defined(OS_MACOSX)
struct ViewHostMsg_DidPrintPage_Params;
#endif

// 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.
  ResourceMessageFilter(ResourceDispatcherHost* resource_dispatcher_host,
                        int child_id,
                        AudioRendererHost* audio_renderer_host,
                        PluginService* plugin_service,
                        printing::PrintJobManager* print_job_manager,
                        Profile* profile,
                        RenderWidgetHelper* render_widget_helper,
                        URLRequestContextGetter* request_context);

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

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

  ResourceDispatcherHost* resource_dispatcher_host() {
    return resource_dispatcher_host_;
  }
  bool off_the_record() { return off_the_record_; }
  CallbackWithReturnValue<int>::Type* next_route_id_callback() {
    return next_route_id_callback_.get();
  }

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

  // Returns either the extension URLRequestContext or regular URLRequestContext
  // depending on whether |url| is an extension URL.
  // Only call on the IO thread.
  ChromeURLRequestContext* GetRequestContextForURL(const GURL& url);

 private:
  friend class ChromeThread;
  friend class DeleteTask<ResourceMessageFilter>;

  virtual ~ResourceMessageFilter();

  void OnMsgCreateWindow(int opener_id, bool user_gesture,
                         int64 session_storage_namespace_id, int* route_id,
                         int64* cloned_session_storage_namespace_id);
  void OnMsgCreateWidget(int opener_id, bool activatable, int* route_id);
  void OnSetCookie(const IPC::Message& message,
                   const GURL& url,
                   const GURL& first_party_for_cookies,
                   const std::string& cookie);
  void OnGetCookies(const GURL& url,
                    const GURL& first_party_for_cookies,
                    IPC::Message* reply_msg);
  void OnGetRawCookies(const GURL& url,
                       const GURL& first_party_for_cookies,
                       IPC::Message* reply_msg);
  void OnDeleteCookie(const GURL& url,
                      const std::string& cookieName);
  void OnGetCookiesEnabled(const GURL& url,
                           const GURL& first_party_for_cookies,
                           bool* enabled);
  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

#if !defined(OS_MACOSX)
  // Not handled in the IO thread on Mac.
  void OnGetScreenInfo(gfx::NativeViewId window, IPC::Message* reply);
#endif
  void OnGetPlugins(bool refresh, IPC::Message* reply_msg);
  void OnGetPluginsOnFileThread(bool refresh, IPC::Message* reply_msg);
  void OnGetPluginPath(const GURL& url,
                       const GURL& policy_url,
                       const std::string& mime_type,
                       FilePath* filename,
                       std::string* actual_mime_type);
  void OnOpenChannelToPlugin(const GURL& url,
                             const std::string& mime_type,
                             const std::wstring& locale,
                             IPC::Message* reply_msg);
  void OnLaunchNaCl(const std::wstring& url,
                    int channel_descriptor,
                    IPC::Message* reply_msg);
  void OnCreateWorker(const ViewHostMsg_CreateWorker_Params& params,
                      int* route_id);
  void OnLookupSharedWorker(const ViewHostMsg_CreateWorker_Params& params,
                            bool* exists,
                            int* route_id,
                            bool* url_error);
  void OnDocumentDetached(unsigned long long document_id);
  void OnCancelCreateDedicatedWorker(int route_id);
  void OnForwardToWorker(const IPC::Message& msg);
  void OnDownloadUrl(const IPC::Message& message,
                     const GURL& url,
                     const GURL& referrer);
  void OnPlatformCheckSpelling(const string16& word, int tag, bool* correct);
  void OnPlatformFillSuggestionList(const string16& word,
                                    std::vector<string16>* suggestions);
  void OnGetDocumentTag(IPC::Message* reply_msg);
  void OnDocumentWithTagClosed(int tag);
  void OnShowSpellingPanel(bool show);
  void OnUpdateSpellingPanelWithMisspelledWord(const string16& word);
  void OnDnsPrefetch(const std::vector<std::string>& hostnames);
  void OnRendererHistograms(int sequence_number,
                            const std::vector<std::string>& histogram_info);
#if defined(USE_TCMALLOC)
  void OnRendererTcmalloc(base::ProcessId pid, const std::string& output);
#endif
  void OnReceiveContextMenuMsg(const IPC::Message& msg);
  // Clipboard messages
  void OnClipboardWriteObjectsAsync(const Clipboard::ObjectMap& objects);
  void OnClipboardWriteObjectsSync(const Clipboard::ObjectMap& objects,
                                   base::SharedMemoryHandle bitmap_handle);

  void OnClipboardIsFormatAvailable(Clipboard::FormatType format,
                                    Clipboard::Buffer buffer,
                                    IPC::Message* reply);
  void OnClipboardReadText(Clipboard::Buffer buffer, IPC::Message* reply);
  void OnClipboardReadAsciiText(Clipboard::Buffer buffer, IPC::Message* reply);
  void OnClipboardReadHTML(Clipboard::Buffer buffer, IPC::Message* reply);
#if defined(OS_MACOSX)
  void OnClipboardFindPboardWriteString(const string16& text);
#endif

  void OnCheckNotificationPermission(const GURL& source_url,
                                     const std::string& application_id,
                                     int* permission_level);

#if !defined(OS_MACOSX)
  // Not handled in the IO thread on Mac.
  void OnGetWindowRect(gfx::NativeViewId window, IPC::Message* reply);
  void OnGetRootWindowRect(gfx::NativeViewId window, IPC::Message* reply);
#endif
  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);

#if defined(OS_WIN)
  // Used to pass resulting EMF from renderer to browser in printing.
  void OnDuplicateSection(base::SharedMemoryHandle renderer_handle,
                          base::SharedMemoryHandle* browser_handle);
#endif

#if defined(OS_POSIX) && !defined(OS_MACOSX)
  // Used to ask the browser allocate a temporary file for the renderer
  // to fill in resulting PDF in renderer.
  void OnAllocateTempFileForPrinting(IPC::Message* reply_msg);
  void OnTempFileForPrintingWritten(int fd_in_browser);
#endif
#if defined(OS_MACOSX)
  // Used to ask the browser to allocate a block of shared memory for the
  // renderer to send back data in, since shared memory can't be created
  // in the renderer on OS X due to the sandbox.
  void OnAllocateSharedMemoryBuffer(uint32 buffer_size,
                                    base::SharedMemoryHandle* handle);
#endif

  void OnResourceTypeStats(const WebKit::WebCache::ResourceTypeStats& stats);
  static void OnResourceTypeStatsOnUIThread(WebKit::WebCache::ResourceTypeStats,
                                            base::ProcessId renderer_id);

  void OnV8HeapStats(int v8_memory_allocated, int v8_memory_used);
  static void OnV8HeapStatsOnUIThread(int v8_memory_allocated,
                                      int v8_memory_used,
                                      base::ProcessId renderer_id);

  void OnDidZoomHost(const std::string& host, int zoom_level);
  void UpdateHostZoomLevelsOnUIThread(const std::string& host, int zoom_level);

  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) || defined(OS_MACOSX)
  // 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(const ViewHostMsg_ScriptedPrint_Params& params,
                       IPC::Message* reply_msg);
  void OnScriptedPrintReply(
      scoped_refptr<printing::PrinterQuery> printer_query,
      int routing_id,
      IPC::Message* reply_msg);
#endif
  // Browser side transport DIB allocation
  void OnAllocTransportDIB(size_t size,
                           TransportDIB::Handle* result);
  void OnFreeTransportDIB(TransportDIB::Id dib_id);

  void OnOpenChannelToExtension(int routing_id,
                                const std::string& source_extension_id,
                                const std::string& target_extension_id,
                                const std::string& channel_name, int* port_id);
  void OnOpenChannelToTab(int routing_id, int tab_id,
                          const std::string& extension_id,
                          const std::string& channel_name, int* port_id);

  void OnCloseCurrentConnections();
  void OnSetCacheMode(bool enabled);

  void OnGetFileSize(const FilePath& path, IPC::Message* reply_msg);
  void OnGetFileSizeOnFileThread(const FilePath& path, IPC::Message* reply_msg);
  void OnKeygen(uint32 key_size_index, const std::string& challenge_string,
                const GURL& url, std::string* signed_public_key);
  void OnGetExtensionMessageBundle(const std::string& extension_id,
                                   IPC::Message* reply_msg);
  void OnGetExtensionMessageBundleOnFileThread(
      const FilePath& extension_path,
      const std::string& extension_id,
      const std::string& default_locale,
      IPC::Message* reply_msg);

  void OnTranslateText(ViewHostMsg_TranslateTextParam param);

#if defined(USE_X11)
  void SendDelayedReply(IPC::Message* reply_msg);
  void DoOnGetScreenInfo(gfx::NativeViewId view, IPC::Message* reply_msg);
  void DoOnGetWindowRect(gfx::NativeViewId view, IPC::Message* reply_msg);
  void DoOnGetRootWindowRect(gfx::NativeViewId view, IPC::Message* reply_msg);
  void DoOnClipboardIsFormatAvailable(Clipboard::FormatType format,
                                      Clipboard::Buffer buffer,
                                      IPC::Message* reply_msg);
  void DoOnClipboardReadText(Clipboard::Buffer buffer, IPC::Message* reply_msg);
  void DoOnClipboardReadAsciiText(Clipboard::Buffer buffer,
                                  IPC::Message* reply_msg);
  void DoOnClipboardReadHTML(Clipboard::Buffer buffer, IPC::Message* reply_msg);
  void DoOnAllocateTempFileForPrinting(IPC::Message* reply_msg);
#endif

  bool CheckBenchmarkingEnabled();

  // We have our own clipboard 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 should be accessed only on the IO
  // thread.
  static Clipboard* GetClipboard();

  NotificationRegistrar registrar_;

  // 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_;

  // The Profile associated with our renderer process.  This should only be
  // accessed on the UI thread!
  Profile* profile_;

  // 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<URLRequestContextGetter> request_context_;

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

  // A request context that holds a cookie store for chrome-extension URLs.
  scoped_refptr<URLRequestContextGetter> extensions_request_context_;

  // Used for routing extension messages.
  scoped_refptr<ExtensionMessageService> extensions_message_service_;

  scoped_refptr<RenderWidgetHelper> render_widget_helper_;

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

  // Handles AppCache related messages.
  scoped_ptr<AppCacheDispatcherHost> appcache_dispatcher_host_;

  // Handles DOM Storage related messages.
  scoped_refptr<DOMStorageDispatcherHost> dom_storage_dispatcher_host_;

  // Handles HTML5 DB related messages
  scoped_refptr<DatabaseDispatcherHost> db_dispatcher_host_;

  // A cache of notifications preferences which is used to handle
  // Desktop Notifications permission messages.
  scoped_refptr<NotificationsPrefsCache> notification_prefs_;

  // Handles zoom-related messages.
  scoped_refptr<HostZoomMap> host_zoom_map_;

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

  // A callback to create a routing id for the associated renderer process.
  scoped_ptr<CallbackWithReturnValue<int>::Type> next_route_id_callback_;

  // Used to translate page contents from one language to another.
  TranslationService translation_service_;

  // Used to handle geolocation-related messages.
  scoped_refptr<GeolocationDispatcherHost> geolocation_dispatcher_host_;

  DISALLOW_COPY_AND_ASSIGN(ResourceMessageFilter);
};

#endif  // CHROME_BROWSER_RENDERER_HOST_RESOURCE_MESSAGE_FILTER_H_