summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/pepper/pepper_extensions_common_message_filter.cc
blob: b96d125471e8d905556b6529ca391658206dcd2c (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
// Copyright (c) 2013 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.

#include "chrome/browser/renderer_host/pepper/pepper_extensions_common_message_filter.h"

#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_function_dispatcher.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/browser_ppapi_host.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_messages.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_message_macros.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/host/dispatch_host_message.h"
#include "ppapi/host/host_message_context.h"
#include "ppapi/proxy/ppapi_messages.h"

namespace chrome {

class PepperExtensionsCommonMessageFilter::DispatcherOwner
    : public content::WebContentsObserver,
      public ExtensionFunctionDispatcher::Delegate {
 public:
  DispatcherOwner(PepperExtensionsCommonMessageFilter* message_filter,
                  Profile* profile,
                  content::RenderFrameHost* frame_host,
                  content::WebContents* web_contents)
      : content::WebContentsObserver(web_contents),
        render_frame_host_(frame_host),
        message_filter_(message_filter),
        dispatcher_(profile, this) {
  }

  virtual ~DispatcherOwner() {
    message_filter_->DetachDispatcherOwner();
  }

  // content::WebContentsObserver implementation.
  virtual void RenderFrameDeleted(
      content::RenderFrameHost* render_frame_host) OVERRIDE {
    if (render_frame_host == render_frame_host_)
      delete this;
  }

  // ExtensionFunctionDispatcher::Delegate implementation.
  virtual extensions::WindowController* GetExtensionWindowController(
      ) const OVERRIDE {
    NOTREACHED();
    return NULL;
  }

  virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE {
    NOTREACHED();
    return NULL;
  }

  ExtensionFunctionDispatcher* dispatcher() { return &dispatcher_; }
  content::RenderFrameHost* render_frame_host() { return render_frame_host_; }

 private:
  content::RenderFrameHost* render_frame_host_;
  scoped_refptr<PepperExtensionsCommonMessageFilter> message_filter_;
  ExtensionFunctionDispatcher dispatcher_;

  DISALLOW_COPY_AND_ASSIGN(DispatcherOwner);
};

// static
PepperExtensionsCommonMessageFilter*
PepperExtensionsCommonMessageFilter::Create(content::BrowserPpapiHost* host,
                                            PP_Instance instance) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));

  int render_process_id = 0;
  int render_frame_id = 0;
  if (!host->GetRenderFrameIDsForInstance(instance, &render_process_id,
                                         &render_frame_id)) {
    return NULL;
  }

  base::FilePath profile_directory = host->GetProfileDataDirectory();
  GURL document_url = host->GetDocumentURLForInstance(instance);

  return new PepperExtensionsCommonMessageFilter(render_process_id,
                                                 render_frame_id,
                                                 profile_directory,
                                                 document_url);
}

PepperExtensionsCommonMessageFilter::PepperExtensionsCommonMessageFilter(
    int render_process_id,
    int render_frame_id,
    const base::FilePath& profile_directory,
    const GURL& document_url)
    : render_process_id_(render_process_id),
      render_frame_id_(render_frame_id),
      profile_directory_(profile_directory),
      document_url_(document_url),
      dispatcher_owner_(NULL),
      dispatcher_owner_initialized_(false) {
}

PepperExtensionsCommonMessageFilter::~PepperExtensionsCommonMessageFilter() {
}

scoped_refptr<base::TaskRunner>
PepperExtensionsCommonMessageFilter::OverrideTaskRunnerForMessage(
    const IPC::Message& msg) {
  return content::BrowserThread::GetMessageLoopProxyForThread(
      content::BrowserThread::UI);
}

int32_t PepperExtensionsCommonMessageFilter::OnResourceMessageReceived(
    const IPC::Message& msg,
    ppapi::host::HostMessageContext* context) {
  IPC_BEGIN_MESSAGE_MAP(PepperExtensionsCommonMessageFilter, msg)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Post,
                                      OnPost)
    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_ExtensionsCommon_Call,
                                      OnCall)
  IPC_END_MESSAGE_MAP()
  return PP_ERROR_FAILED;
}

int32_t PepperExtensionsCommonMessageFilter::OnPost(
    ppapi::host::HostMessageContext* context,
    const std::string& request_name,
    base::ListValue& args) {
  if (HandleRequest(context, request_name, &args, false))
    return PP_OK;
  else
    return PP_ERROR_FAILED;
}

int32_t PepperExtensionsCommonMessageFilter::OnCall(
    ppapi::host::HostMessageContext* context,
    const std::string& request_name,
    base::ListValue& args) {
  if (HandleRequest(context, request_name, &args, true))
    return PP_OK_COMPLETIONPENDING;
  else
    return PP_ERROR_FAILED;
}

void PepperExtensionsCommonMessageFilter::EnsureDispatcherOwnerInitialized() {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  if (dispatcher_owner_initialized_)
    return;
  dispatcher_owner_initialized_ = true;

  DCHECK(!dispatcher_owner_);
  content::RenderFrameHost* frame_host = content::RenderFrameHost::FromID(
      render_process_id_, render_frame_id_);
  content::WebContents* web_contents =
      content::WebContents::FromRenderFrameHost(frame_host);

  if (!document_url_.SchemeIs(extensions::kExtensionScheme))
    return;

  ProfileManager* profile_manager = g_browser_process->profile_manager();
  if (!profile_manager)
    return;
  Profile* profile = profile_manager->GetProfile(profile_directory_);

  // It will be automatically destroyed when |view_host| goes away.
  dispatcher_owner_ = new DispatcherOwner(
      this, profile, frame_host, web_contents);
}

void PepperExtensionsCommonMessageFilter::DetachDispatcherOwner() {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  dispatcher_owner_ = NULL;
}

void PepperExtensionsCommonMessageFilter::PopulateParams(
    const std::string& request_name,
    base::ListValue* args,
    bool has_callback,
    ExtensionHostMsg_Request_Params* params) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  params->name = request_name;
  params->arguments.Swap(args);

  params->extension_id = document_url_.host();
  params->source_url = document_url_;

  // We don't need an ID to map a response to the corresponding request.
  params->request_id = 0;
  params->has_callback = has_callback;
  params->user_gesture = false;
}

void PepperExtensionsCommonMessageFilter::OnExtensionFunctionCompleted(
    scoped_ptr<ppapi::host::ReplyMessageContext> reply_context,
    ExtensionFunction::ResponseType type,
    const base::ListValue& results,
    const std::string& /* error */) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  // Ignore responses resulted from calls to OnPost().
  if (!reply_context) {
    DCHECK_EQ(0u, results.GetSize());
    return;
  }

  if (type == ExtensionFunction::BAD_MESSAGE) {
    // The input arguments were not validated at the plugin side, so don't kill
    // the plugin process when we see a message with invalid arguments.
    // TODO(yzshen): It is nicer to also do the validation at the plugin side.
    type = ExtensionFunction::FAILED;
  }

  reply_context->params.set_result(
      type == ExtensionFunction::SUCCEEDED ? PP_OK : PP_ERROR_FAILED);
  SendReply(*reply_context, PpapiPluginMsg_ExtensionsCommon_CallReply(results));
}

bool PepperExtensionsCommonMessageFilter::HandleRequest(
    ppapi::host::HostMessageContext* context,
    const std::string& request_name,
    base::ListValue* args,
    bool has_callback) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  EnsureDispatcherOwnerInitialized();
  if (!dispatcher_owner_)
    return false;

  ExtensionHostMsg_Request_Params params;
  PopulateParams(request_name, args, has_callback, &params);

  scoped_ptr<ppapi::host::ReplyMessageContext> reply_context;
  if (has_callback) {
    reply_context.reset(new ppapi::host::ReplyMessageContext(
        context->MakeReplyMessageContext()));
  }

  dispatcher_owner_->dispatcher()->DispatchWithCallback(
      params, dispatcher_owner_->render_frame_host(),
      base::Bind(
          &PepperExtensionsCommonMessageFilter::OnExtensionFunctionCompleted,
          this,
          base::Passed(&reply_context)));
  return true;
}

}  // namespace chrome