1// Copyright 2014 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 EXTENSIONS_BROWSER_EXTENSION_MESSAGE_FILTER_H_
6#define EXTENSIONS_BROWSER_EXTENSION_MESSAGE_FILTER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/compiler_specific.h"
12#include "base/macros.h"
13#include "base/memory/weak_ptr.h"
14#include "content/public/browser/browser_message_filter.h"
15#include "url/gurl.h"
16
17struct ExtensionHostMsg_Request_Params;
18
19namespace base {
20class DictionaryValue;
21}
22
23namespace content {
24class BrowserContext;
25class WebContents;
26}
27
28namespace extensions {
29
30class InfoMap;
31
32// This class filters out incoming extension-specific IPC messages from the
33// renderer process. It is created on the UI thread. Messages may be handled on
34// the IO thread or the UI thread.
35class ExtensionMessageFilter : public content::BrowserMessageFilter {
36 public:
37  ExtensionMessageFilter(int render_process_id,
38                         content::BrowserContext* context);
39
40  int render_process_id() { return render_process_id_; }
41
42 private:
43  friend class content::BrowserThread;
44  friend class base::DeleteHelper<ExtensionMessageFilter>;
45
46  virtual ~ExtensionMessageFilter();
47
48  // content::BrowserMessageFilter implementation.
49  virtual void OverrideThreadForMessage(
50      const IPC::Message& message,
51      content::BrowserThread::ID* thread) OVERRIDE;
52  virtual void OnDestruct() const OVERRIDE;
53  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
54
55  // Message handlers on the UI thread.
56  void OnExtensionAddListener(const std::string& extension_id,
57                              const GURL& listener_url,
58                              const std::string& event_name);
59  void OnExtensionRemoveListener(const std::string& extension_id,
60                                 const GURL& listener_url,
61                                 const std::string& event_name);
62  void OnExtensionAddLazyListener(const std::string& extension_id,
63                                  const std::string& event_name);
64  void OnExtensionAttachGuest(int routing_id,
65                              int element_instance_id,
66                              int guest_instance_id,
67                              const base::DictionaryValue& attach_params);
68  void OnExtensionCreateMimeHandlerViewGuest(int render_frame_id,
69                                             const std::string& url,
70                                             const std::string& mime_type,
71                                             int element_instance_id);
72  void OnExtensionRemoveLazyListener(const std::string& extension_id,
73                                     const std::string& event_name);
74  void OnExtensionAddFilteredListener(const std::string& extension_id,
75                                      const std::string& event_name,
76                                      const base::DictionaryValue& filter,
77                                      bool lazy);
78  void OnExtensionRemoveFilteredListener(const std::string& extension_id,
79                                         const std::string& event_name,
80                                         const base::DictionaryValue& filter,
81                                         bool lazy);
82  void OnExtensionShouldSuspendAck(const std::string& extension_id,
83                                   int sequence_id);
84  void OnExtensionSuspendAck(const std::string& extension_id);
85  void OnExtensionTransferBlobsAck(const std::vector<std::string>& blob_uuids);
86
87  // Message handlers on the IO thread.
88  void OnExtensionGenerateUniqueID(int* unique_id);
89  void OnExtensionResumeRequests(int route_id);
90  void OnExtensionRequestForIOThread(
91      int routing_id,
92      const ExtensionHostMsg_Request_Params& params);
93
94  // Runs on UI thread.
95  void MimeHandlerViewGuestCreatedCallback(int element_instance_id,
96                                           int embedder_render_process_id,
97                                           int embedder_render_frame_id,
98                                           const std::string& src,
99                                           content::WebContents* web_contents);
100
101  const int render_process_id_;
102
103  // Should only be accessed on the UI thread.
104  content::BrowserContext* browser_context_;
105
106  scoped_refptr<extensions::InfoMap> extension_info_map_;
107
108  // Weak pointers produced by this factory are bound to the IO thread.
109  base::WeakPtrFactory<ExtensionMessageFilter> weak_ptr_factory_;
110
111  DISALLOW_COPY_AND_ASSIGN(ExtensionMessageFilter);
112};
113
114}  // namespace extensions
115
116#endif  // EXTENSIONS_BROWSER_EXTENSION_MESSAGE_FILTER_H_
117