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  // TODO(jamescook): Move these functions into the extensions module. Ideally
51  // this would be in extensions::ExtensionMessageFilter but that will require
52  // resolving the MessageService and ActivityLog dependencies on src/chrome.
53  // http://crbug.com/339637
54  void OnOpenChannelToExtension(int routing_id,
55                                const ExtensionMsg_ExternalConnectionInfo& info,
56                                const std::string& channel_name,
57                                bool include_tls_channel_id,
58                                int* port_id);
59  void OpenChannelToExtensionOnUIThread(
60      int source_process_id,
61      int source_routing_id,
62      int receiver_port_id,
63      const ExtensionMsg_ExternalConnectionInfo& info,
64      const std::string& channel_name,
65      bool include_tls_channel_id);
66  void OnOpenChannelToNativeApp(int routing_id,
67                                const std::string& source_extension_id,
68                                const std::string& native_app_name,
69                                int* port_id);
70  void OpenChannelToNativeAppOnUIThread(int source_routing_id,
71                                        int receiver_port_id,
72                                        const std::string& source_extension_id,
73                                        const std::string& native_app_name);
74  void OnOpenChannelToTab(int routing_id, int tab_id,
75                          const std::string& extension_id,
76                          const std::string& channel_name, int* port_id);
77  void OpenChannelToTabOnUIThread(int source_process_id, int source_routing_id,
78                                  int receiver_port_id,
79                                  int tab_id, const std::string& extension_id,
80                                  const std::string& channel_name);
81  void OnPostMessage(int port_id, const extensions::Message& message);
82  void OnGetExtMessageBundle(const std::string& extension_id,
83                             IPC::Message* reply_msg);
84  void OnGetExtMessageBundleOnBlockingPool(
85      const base::FilePath& extension_path,
86      const std::string& extension_id,
87      const std::string& default_locale,
88      IPC::Message* reply_msg);
89  void OnExtensionCloseChannel(int port_id, const std::string& error_message);
90  void OnAddAPIActionToExtensionActivityLog(
91      const std::string& extension_id,
92      const ExtensionHostMsg_APIActionOrEvent_Params& params);
93  void OnAddBlockedCallToExtensionActivityLog(
94      const std::string& extension_id,
95      const std::string& function_name);
96  void OnAddDOMActionToExtensionActivityLog(
97      const std::string& extension_id,
98      const ExtensionHostMsg_DOMAction_Params& params);
99  void OnAddEventToExtensionActivityLog(
100      const std::string& extension_id,
101      const ExtensionHostMsg_APIActionOrEvent_Params& params);
102
103  // content::NotificationObserver implementation.
104  virtual void Observe(int type,
105                       const content::NotificationSource& source,
106                       const content::NotificationDetails& details) OVERRIDE;
107
108  const int render_process_id_;
109
110  // The Profile associated with our renderer process.  This should only be
111  // accessed on the UI thread! Furthermore since this class is refcounted it
112  // may outlive |profile_|, so make sure to NULL check if in doubt; async
113  // calls and the like.
114  Profile* profile_;
115
116  scoped_refptr<extensions::InfoMap> extension_info_map_;
117
118  content::NotificationRegistrar notification_registrar_;
119
120  DISALLOW_COPY_AND_ASSIGN(ChromeExtensionMessageFilter);
121};
122
123#endif  // CHROME_BROWSER_RENDERER_HOST_CHROME_EXTENSION_MESSAGE_FILTER_H_
124