chrome_extension_message_filter.h revision 116680a4aac90f2aa7413d9095a592090648e557
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 CHROME_BROWSER_RENDERER_HOST_CHROME_EXTENSION_MESSAGE_FILTER_H_
6#define CHROME_BROWSER_RENDERER_HOST_CHROME_EXTENSION_MESSAGE_FILTER_H_
7
8#include <string>
9
10#include "base/sequenced_task_runner_helpers.h"
11#include "content/public/browser/browser_message_filter.h"
12#include "content/public/browser/notification_observer.h"
13#include "content/public/browser/notification_registrar.h"
14
15class GURL;
16class Profile;
17struct ExtensionHostMsg_APIActionOrEvent_Params;
18struct ExtensionHostMsg_DOMAction_Params;
19struct ExtensionMsg_ExternalConnectionInfo;
20
21namespace base {
22class FilePath;
23}
24
25namespace extensions {
26class InfoMap;
27struct Message;
28}
29
30// This class filters out incoming Chrome-specific IPC messages from the
31// extension process on the IPC thread.
32class ChromeExtensionMessageFilter : public content::BrowserMessageFilter,
33                                     public content::NotificationObserver {
34 public:
35  ChromeExtensionMessageFilter(int render_process_id, Profile* profile);
36
37  // content::BrowserMessageFilter methods:
38  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
39  virtual void OverrideThreadForMessage(
40      const IPC::Message& message,
41      content::BrowserThread::ID* thread) OVERRIDE;
42  virtual void OnDestruct() const OVERRIDE;
43
44 private:
45  friend class content::BrowserThread;
46  friend class base::DeleteHelper<ChromeExtensionMessageFilter>;
47
48  virtual ~ChromeExtensionMessageFilter();
49
50  void OnCanTriggerClipboardRead(const GURL& origin, bool* allowed);
51  void OnCanTriggerClipboardWrite(const GURL& origin, bool* allowed);
52
53  // TODO(jamescook): Move these functions into the extensions module. Ideally
54  // this would be in extensions::ExtensionMessageFilter but that will require
55  // resolving the MessageService and ActivityLog dependencies on src/chrome.
56  // http://crbug.com/339637
57  void OnOpenChannelToExtension(int routing_id,
58                                const ExtensionMsg_ExternalConnectionInfo& info,
59                                const std::string& channel_name,
60                                bool include_tls_channel_id,
61                                int* port_id);
62  void OpenChannelToExtensionOnUIThread(
63      int source_process_id,
64      int source_routing_id,
65      int receiver_port_id,
66      const ExtensionMsg_ExternalConnectionInfo& info,
67      const std::string& channel_name,
68      bool include_tls_channel_id);
69  void OnOpenChannelToNativeApp(int routing_id,
70                                const std::string& source_extension_id,
71                                const std::string& native_app_name,
72                                int* port_id);
73  void OpenChannelToNativeAppOnUIThread(int source_routing_id,
74                                        int receiver_port_id,
75                                        const std::string& source_extension_id,
76                                        const std::string& native_app_name);
77  void OnOpenChannelToTab(int routing_id, int tab_id,
78                          const std::string& extension_id,
79                          const std::string& channel_name, int* port_id);
80  void OpenChannelToTabOnUIThread(int source_process_id, int source_routing_id,
81                                  int receiver_port_id,
82                                  int tab_id, const std::string& extension_id,
83                                  const std::string& channel_name);
84  void OnPostMessage(int port_id, const extensions::Message& message);
85  void OnGetExtMessageBundle(const std::string& extension_id,
86                             IPC::Message* reply_msg);
87  void OnGetExtMessageBundleOnBlockingPool(
88      const base::FilePath& extension_path,
89      const std::string& extension_id,
90      const std::string& default_locale,
91      IPC::Message* reply_msg);
92  void OnExtensionCloseChannel(int port_id, const std::string& error_message);
93  void OnAddAPIActionToExtensionActivityLog(
94      const std::string& extension_id,
95      const ExtensionHostMsg_APIActionOrEvent_Params& params);
96  void OnAddBlockedCallToExtensionActivityLog(
97      const std::string& extension_id,
98      const std::string& function_name);
99  void OnAddDOMActionToExtensionActivityLog(
100      const std::string& extension_id,
101      const ExtensionHostMsg_DOMAction_Params& params);
102  void OnAddEventToExtensionActivityLog(
103      const std::string& extension_id,
104      const ExtensionHostMsg_APIActionOrEvent_Params& params);
105
106  // content::NotificationObserver implementation.
107  virtual void Observe(int type,
108                       const content::NotificationSource& source,
109                       const content::NotificationDetails& details) OVERRIDE;
110
111  const int render_process_id_;
112
113  // The Profile associated with our renderer process.  This should only be
114  // accessed on the UI thread! Furthermore since this class is refcounted it
115  // may outlive |profile_|, so make sure to NULL check if in doubt; async
116  // calls and the like.
117  Profile* profile_;
118
119  scoped_refptr<extensions::InfoMap> extension_info_map_;
120
121  content::NotificationRegistrar notification_registrar_;
122
123  DISALLOW_COPY_AND_ASSIGN(ChromeExtensionMessageFilter);
124};
125
126#endif  // CHROME_BROWSER_RENDERER_HOST_CHROME_EXTENSION_MESSAGE_FILTER_H_
127