summaryrefslogtreecommitdiffstats
path: root/extensions/renderer/messaging_bindings.cc
blob: 5dfb37cff31725d89fb2cb5fee9b89d377558559 (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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright 2014 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 "extensions/renderer/messaging_bindings.h"

#include <map>
#include <string>

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/lazy_instance.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/values.h"
#include "components/guest_view/common/guest_view_constants.h"
#include "content/public/child/v8_value_converter.h"
#include "content/public/common/child_process_host.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "extensions/common/api/messaging/message.h"
#include "extensions/common/extension_messages.h"
#include "extensions/common/manifest_handlers/externally_connectable.h"
#include "extensions/renderer/dispatcher.h"
#include "extensions/renderer/event_bindings.h"
#include "extensions/renderer/object_backed_native_handler.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/script_context_set.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
#include "third_party/WebKit/public/web/WebScopedUserGesture.h"
#include "third_party/WebKit/public/web/WebScopedWindowFocusAllowedIndicator.h"
#include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
#include "v8/include/v8.h"

// Message passing API example (in a content script):
// var extension =
//    new chrome.Extension('00123456789abcdef0123456789abcdef0123456');
// var port = runtime.connect();
// port.postMessage('Can you hear me now?');
// port.onmessage.addListener(function(msg, port) {
//   alert('response=' + msg);
//   port.postMessage('I got your reponse');
// });

using content::RenderThread;
using content::V8ValueConverter;

namespace extensions {

namespace {

// Binds |callback| to run when |object| is garbage collected. So as to not
// re-entrantly call into v8, we execute this function asynchronously, at
// which point |context| may have been invalidated. If so, |callback| is not
// run, and |fallback| will be called instead.
//
// Deletes itself when the object args[0] is garbage collected or when the
// context is invalidated.
class GCCallback : public base::SupportsWeakPtr<GCCallback> {
 public:
  GCCallback(ScriptContext* context,
             const v8::Local<v8::Object>& object,
             const v8::Local<v8::Function>& callback,
             const base::Closure& fallback)
      : context_(context),
        object_(context->isolate(), object),
        callback_(context->isolate(), callback),
        fallback_(fallback) {
    object_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter);
    context->AddInvalidationObserver(
        base::Bind(&GCCallback::OnContextInvalidated, AsWeakPtr()));
  }

 private:
  static void FirstWeakCallback(const v8::WeakCallbackInfo<GCCallback>& data) {
    // v8 says we need to explicitly reset weak handles from their callbacks.
    // It's not implicit as one might expect.
    data.GetParameter()->object_.Reset();
    data.SetSecondPassCallback(SecondWeakCallback);
  }

  static void SecondWeakCallback(const v8::WeakCallbackInfo<GCCallback>& data) {
    base::MessageLoop::current()->PostTask(
        FROM_HERE,
        base::Bind(&GCCallback::RunCallback, data.GetParameter()->AsWeakPtr()));
  }

  void RunCallback() {
    CHECK(context_);
    v8::Isolate* isolate = context_->isolate();
    v8::HandleScope handle_scope(isolate);
    context_->CallFunction(v8::Local<v8::Function>::New(isolate, callback_));
    delete this;
  }

  void OnContextInvalidated() {
    fallback_.Run();
    context_ = NULL;
    delete this;
  }

  // ScriptContext which owns this GCCallback.
  ScriptContext* context_;

  // Holds a global handle to the object this GCCallback is bound to.
  v8::Global<v8::Object> object_;

  // Function to run when |object_| bound to this GCCallback is GC'd.
  v8::Global<v8::Function> callback_;

  // Function to run if context is invalidated before we have a chance
  // to execute |callback_|.
  base::Closure fallback_;

  DISALLOW_COPY_AND_ASSIGN(GCCallback);
};

// Tracks every reference between ScriptContexts and Ports, by ID.
class PortTracker {
 public:
  PortTracker() {}
  ~PortTracker() {}

  // Returns true if |context| references |port_id|.
  bool HasReference(ScriptContext* context, int port_id) const {
    auto ports = contexts_to_ports_.find(context);
    return ports != contexts_to_ports_.end() &&
           ports->second.count(port_id) > 0;
  }

  // Marks |context| and |port_id| as referencing each other.
  void AddReference(ScriptContext* context, int port_id) {
    contexts_to_ports_[context].insert(port_id);
  }

  // Removes the references between |context| and |port_id|.
  // Returns true if a reference was removed, false if the reference didn't
  // exist to be removed.
  bool RemoveReference(ScriptContext* context, int port_id) {
    auto ports = contexts_to_ports_.find(context);
    if (ports == contexts_to_ports_.end() ||
        ports->second.erase(port_id) == 0) {
      return false;
    }
    if (ports->second.empty())
      contexts_to_ports_.erase(context);
    return true;
  }

  // Returns true if this tracker has any reference to |port_id|.
  bool HasPort(int port_id) const {
    for (auto it : contexts_to_ports_) {
      if (it.second.count(port_id) > 0)
        return true;
    }
    return false;
  }

  // Deletes all references to |port_id|.
  void DeletePort(int port_id) {
    for (auto it = contexts_to_ports_.begin();
         it != contexts_to_ports_.end();) {
      if (it->second.erase(port_id) > 0 && it->second.empty())
        contexts_to_ports_.erase(it++);
      else
        ++it;
    }
  }

  // Gets every port ID that has a reference to |context|.
  std::set<int> GetPortsForContext(ScriptContext* context) const {
    auto ports = contexts_to_ports_.find(context);
    return ports == contexts_to_ports_.end() ? std::set<int>() : ports->second;
  }

 private:
  // Maps ScriptContexts to the port IDs that have a reference to it.
  std::map<ScriptContext*, std::set<int>> contexts_to_ports_;

  DISALLOW_COPY_AND_ASSIGN(PortTracker);
};

base::LazyInstance<PortTracker> g_port_tracker = LAZY_INSTANCE_INITIALIZER;

const char kPortClosedError[] = "Attempting to use a disconnected port object";
const char kReceivingEndDoesntExistError[] =
    "Could not establish connection. Receiving end does not exist.";

class ExtensionImpl : public ObjectBackedNativeHandler {
 public:
  ExtensionImpl(Dispatcher* dispatcher, ScriptContext* context)
      : ObjectBackedNativeHandler(context),
        dispatcher_(dispatcher),
        weak_ptr_factory_(this) {
    RouteFunction(
        "CloseChannel",
        base::Bind(&ExtensionImpl::CloseChannel, base::Unretained(this)));
    RouteFunction(
        "PortAddRef",
        base::Bind(&ExtensionImpl::PortAddRef, base::Unretained(this)));
    RouteFunction(
        "PortRelease",
        base::Bind(&ExtensionImpl::PortRelease, base::Unretained(this)));
    RouteFunction(
        "PostMessage",
        base::Bind(&ExtensionImpl::PostMessage, base::Unretained(this)));
    // TODO(fsamuel, kalman): Move BindToGC out of messaging natives.
    RouteFunction("BindToGC",
                  base::Bind(&ExtensionImpl::BindToGC, base::Unretained(this)));

    // Observe |context| so that port references to it can be cleared.
    context->AddInvalidationObserver(base::Bind(
        &ExtensionImpl::OnContextInvalidated, weak_ptr_factory_.GetWeakPtr()));
  }

  ~ExtensionImpl() override {}

 private:
  void OnContextInvalidated() {
    for (int port_id : g_port_tracker.Get().GetPortsForContext(context()))
      ReleasePort(port_id);
  }

  void ClearPortDataAndNotifyDispatcher(int port_id) {
    g_port_tracker.Get().DeletePort(port_id);
    dispatcher_->ClearPortData(port_id);
  }

  // Sends a message along the given channel.
  void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) {
    content::RenderFrame* renderframe = context()->GetRenderFrame();
    if (!renderframe)
      return;

    // Arguments are (int32 port_id, string message).
    CHECK(args.Length() == 2 && args[0]->IsInt32() && args[1]->IsString());

    int port_id = args[0]->Int32Value();
    if (!g_port_tracker.Get().HasPort(port_id)) {
      args.GetIsolate()->ThrowException(v8::Exception::Error(
          v8::String::NewFromUtf8(args.GetIsolate(), kPortClosedError)));
      return;
    }

    renderframe->Send(new ExtensionHostMsg_PostMessage(
        renderframe->GetRoutingID(), port_id,
        Message(*v8::String::Utf8Value(args[1]),
                blink::WebUserGestureIndicator::isProcessingUserGesture())));
  }

  // Forcefully disconnects a port.
  void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args) {
    // Arguments are (int32 port_id, boolean notify_browser).
    CHECK_EQ(2, args.Length());
    CHECK(args[0]->IsInt32());
    CHECK(args[1]->IsBoolean());

    int port_id = args[0]->Int32Value();
    if (!g_port_tracker.Get().HasPort(port_id))
      return;

    // Send via the RenderThread because the RenderFrame might be closing.
    bool notify_browser = args[1]->BooleanValue();
    if (notify_browser) {
      content::RenderThread::Get()->Send(
          new ExtensionHostMsg_CloseChannel(port_id, std::string()));
    }

    ClearPortDataAndNotifyDispatcher(port_id);
  }

  // A new port has been created for a context.  This occurs both when script
  // opens a connection, and when a connection is opened to this script.
  void PortAddRef(const v8::FunctionCallbackInfo<v8::Value>& args) {
    // Arguments are (int32 port_id).
    CHECK_EQ(1, args.Length());
    CHECK(args[0]->IsInt32());

    int port_id = args[0]->Int32Value();
    g_port_tracker.Get().AddReference(context(), port_id);
  }

  // The frame a port lived in has been destroyed.  When there are no more
  // frames with a reference to a given port, we will disconnect it and notify
  // the other end of the channel.
  void PortRelease(const v8::FunctionCallbackInfo<v8::Value>& args) {
    // Arguments are (int32 port_id).
    CHECK(args.Length() == 1 && args[0]->IsInt32());
    ReleasePort(args[0]->Int32Value());
  }

  // Releases the reference to |port_id| for this context, and clears all port
  // data if there are no more references.
  void ReleasePort(int port_id) {
    if (g_port_tracker.Get().RemoveReference(context(), port_id) &&
        !g_port_tracker.Get().HasPort(port_id)) {
      // Send via the RenderThread because the RenderFrame might be closing.
      content::RenderThread::Get()->Send(
          new ExtensionHostMsg_CloseChannel(port_id, std::string()));
      ClearPortDataAndNotifyDispatcher(port_id);
    }
  }

  // void BindToGC(object, callback, port_id)
  //
  // Binds |callback| to be invoked *sometime after* |object| is garbage
  // collected. We don't call the method re-entrantly so as to avoid executing
  // JS in some bizarro undefined mid-GC state, nor do we then call into the
  // script context if it's been invalidated.
  //
  // If the script context *is* invalidated in the meantime, as a slight hack,
  // release the port with ID |port_id| if it's >= 0.
  void BindToGC(const v8::FunctionCallbackInfo<v8::Value>& args) {
    CHECK(args.Length() == 3 && args[0]->IsObject() && args[1]->IsFunction() &&
          args[2]->IsInt32());
    int port_id = args[2]->Int32Value();
    base::Closure fallback = base::Bind(&base::DoNothing);
    if (port_id >= 0) {
      fallback = base::Bind(&ExtensionImpl::ReleasePort,
                            weak_ptr_factory_.GetWeakPtr(), port_id);
    }
    // Destroys itself when the object is GC'd or context is invalidated.
    new GCCallback(context(), args[0].As<v8::Object>(),
                   args[1].As<v8::Function>(), fallback);
  }

  // Dispatcher handle. Not owned.
  Dispatcher* dispatcher_;

  base::WeakPtrFactory<ExtensionImpl> weak_ptr_factory_;
};

void DispatchOnConnectToScriptContext(
    int target_port_id,
    const std::string& channel_name,
    const ExtensionMsg_TabConnectionInfo* source,
    const ExtensionMsg_ExternalConnectionInfo& info,
    const std::string& tls_channel_id,
    bool* port_created,
    ScriptContext* script_context) {
  // Only dispatch the events if this is the requested target frame (0 = main
  // frame; positive = child frame).
  content::RenderFrame* renderframe = script_context->GetRenderFrame();
  if (info.target_frame_id == 0 && renderframe->GetWebFrame()->parent() != NULL)
    return;
  if (info.target_frame_id > 0 &&
      renderframe->GetRoutingID() != info.target_frame_id)
    return;
  v8::Isolate* isolate = script_context->isolate();
  v8::HandleScope handle_scope(isolate);

  scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());

  const std::string& source_url_spec = info.source_url.spec();
  std::string target_extension_id = script_context->GetExtensionID();
  const Extension* extension = script_context->extension();

  v8::Local<v8::Value> tab = v8::Null(isolate);
  v8::Local<v8::Value> tls_channel_id_value = v8::Undefined(isolate);
  v8::Local<v8::Value> guest_process_id = v8::Undefined(isolate);

  if (extension) {
    if (!source->tab.empty() && !extension->is_platform_app())
      tab = converter->ToV8Value(&source->tab, script_context->v8_context());

    ExternallyConnectableInfo* externally_connectable =
        ExternallyConnectableInfo::Get(extension);
    if (externally_connectable &&
        externally_connectable->accepts_tls_channel_id) {
      tls_channel_id_value = v8::String::NewFromUtf8(isolate,
                                                     tls_channel_id.c_str(),
                                                     v8::String::kNormalString,
                                                     tls_channel_id.size());
    }

    if (info.guest_process_id != content::ChildProcessHost::kInvalidUniqueID)
      guest_process_id = v8::Integer::New(isolate, info.guest_process_id);
  }

  v8::Local<v8::Value> arguments[] = {
      // portId
      v8::Integer::New(isolate, target_port_id),
      // channelName
      v8::String::NewFromUtf8(isolate, channel_name.c_str(),
                              v8::String::kNormalString, channel_name.size()),
      // sourceTab
      tab,
      // source_frame_id
      v8::Integer::New(isolate, source->frame_id),
      // guestProcessId
      guest_process_id,
      // sourceExtensionId
      v8::String::NewFromUtf8(isolate, info.source_id.c_str(),
                              v8::String::kNormalString, info.source_id.size()),
      // targetExtensionId
      v8::String::NewFromUtf8(isolate, target_extension_id.c_str(),
                              v8::String::kNormalString,
                              target_extension_id.size()),
      // sourceUrl
      v8::String::NewFromUtf8(isolate, source_url_spec.c_str(),
                              v8::String::kNormalString,
                              source_url_spec.size()),
      // tlsChannelId
      tls_channel_id_value,
  };

  v8::Local<v8::Value> retval =
      script_context->module_system()->CallModuleMethod(
          "messaging", "dispatchOnConnect", arraysize(arguments), arguments);

  if (!retval.IsEmpty()) {
    CHECK(retval->IsBoolean());
    *port_created |= retval->BooleanValue();
  } else {
    LOG(ERROR) << "Empty return value from dispatchOnConnect.";
  }
}

void DeliverMessageToScriptContext(const Message& message,
                                   int target_port_id,
                                   ScriptContext* script_context) {
  v8::Isolate* isolate = script_context->isolate();
  v8::HandleScope handle_scope(isolate);

  // Check to see whether the context has this port before bothering to create
  // the message.
  v8::Local<v8::Value> port_id_handle =
      v8::Integer::New(isolate, target_port_id);
  v8::Local<v8::Value> has_port =
      script_context->module_system()->CallModuleMethod("messaging", "hasPort",
                                                        1, &port_id_handle);

  CHECK(!has_port.IsEmpty());
  if (!has_port->BooleanValue())
    return;

  std::vector<v8::Local<v8::Value>> arguments;
  arguments.push_back(v8::String::NewFromUtf8(isolate,
                                              message.data.c_str(),
                                              v8::String::kNormalString,
                                              message.data.size()));
  arguments.push_back(port_id_handle);

  scoped_ptr<blink::WebScopedUserGesture> web_user_gesture;
  scoped_ptr<blink::WebScopedWindowFocusAllowedIndicator> allow_window_focus;
  if (message.user_gesture) {
    web_user_gesture.reset(new blink::WebScopedUserGesture);

    if (script_context->web_frame()) {
      blink::WebDocument document = script_context->web_frame()->document();
      allow_window_focus.reset(new blink::WebScopedWindowFocusAllowedIndicator(
          &document));
    }
  }

  script_context->module_system()->CallModuleMethod(
      "messaging", "dispatchOnMessage", &arguments);
}

void DispatchOnDisconnectToScriptContext(int port_id,
                                         const std::string& error_message,
                                         ScriptContext* script_context) {
  v8::Isolate* isolate = script_context->isolate();
  v8::HandleScope handle_scope(isolate);

  std::vector<v8::Local<v8::Value>> arguments;
  arguments.push_back(v8::Integer::New(isolate, port_id));
  if (!error_message.empty()) {
    arguments.push_back(
        v8::String::NewFromUtf8(isolate, error_message.c_str()));
  } else {
    arguments.push_back(v8::Null(isolate));
  }

  script_context->module_system()->CallModuleMethod(
      "messaging", "dispatchOnDisconnect", &arguments);
}

}  // namespace

ObjectBackedNativeHandler* MessagingBindings::Get(Dispatcher* dispatcher,
                                                  ScriptContext* context) {
  return new ExtensionImpl(dispatcher, context);
}

// static
void MessagingBindings::DispatchOnConnect(
    const ScriptContextSet& context_set,
    int target_port_id,
    const std::string& channel_name,
    const ExtensionMsg_TabConnectionInfo& source,
    const ExtensionMsg_ExternalConnectionInfo& info,
    const std::string& tls_channel_id,
    content::RenderFrame* restrict_to_render_frame) {
  // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*.
  content::RenderView* restrict_to_render_view =
      restrict_to_render_frame ? restrict_to_render_frame->GetRenderView()
                               : NULL;
  bool port_created = false;
  context_set.ForEach(
      info.target_id, restrict_to_render_view,
      base::Bind(&DispatchOnConnectToScriptContext, target_port_id,
                 channel_name, &source, info, tls_channel_id, &port_created));

  // If we didn't create a port, notify the other end of the channel (treat it
  // as a disconnect).
  if (!port_created) {
    content::RenderThread::Get()->Send(new ExtensionHostMsg_CloseChannel(
        target_port_id, kReceivingEndDoesntExistError));
  }
}

// static
void MessagingBindings::DeliverMessage(
    const ScriptContextSet& context_set,
    int target_port_id,
    const Message& message,
    content::RenderFrame* restrict_to_render_frame) {
  // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*.
  content::RenderView* restrict_to_render_view =
      restrict_to_render_frame ? restrict_to_render_frame->GetRenderView()
                               : NULL;
  context_set.ForEach(
      restrict_to_render_view,
      base::Bind(&DeliverMessageToScriptContext, message, target_port_id));
}

// static
void MessagingBindings::DispatchOnDisconnect(
    const ScriptContextSet& context_set,
    int port_id,
    const std::string& error_message,
    content::RenderFrame* restrict_to_render_frame) {
  // TODO(robwu): ScriptContextSet.ForEach should accept RenderFrame*.
  content::RenderView* restrict_to_render_view =
      restrict_to_render_frame ? restrict_to_render_frame->GetRenderView()
                               : NULL;
  context_set.ForEach(
      restrict_to_render_view,
      base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message));
}

}  // namespace extensions