summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/extensions/renderer_extension_bindings.cc
blob: e3d820a52ef5a7a18725081ead6ea93abab7bb7e (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
// Copyright (c) 2009 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/renderer/extensions/renderer_extension_bindings.h"

#include <map>
#include <string>

#include "base/basictypes.h"
#include "base/singleton.h"
#include "base/values.h"
#include "chrome/common/extensions/extension_message_bundle.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "chrome/renderer/extensions/bindings_utils.h"
#include "chrome/renderer/extensions/event_bindings.h"
#include "chrome/renderer/render_thread.h"
#include "chrome/renderer/render_view.h"
#include "grit/renderer_resources.h"

using bindings_utils::GetStringResource;
using bindings_utils::ExtensionBase;

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

namespace {

struct ExtensionData {
  struct PortData {
    int ref_count;  // how many contexts have a handle to this port
    bool disconnected;  // true if this port was forcefully disconnected
    PortData() : ref_count(0), disconnected(false) {}
  };
  std::map<int, PortData> ports;  // port ID -> data
};

static bool HasPortData(int port_id) {
  return Singleton<ExtensionData>::get()->ports.find(port_id) !=
      Singleton<ExtensionData>::get()->ports.end();
}

static ExtensionData::PortData& GetPortData(int port_id) {
  return Singleton<ExtensionData>::get()->ports[port_id];
}

static void ClearPortData(int port_id) {
  Singleton<ExtensionData>::get()->ports.erase(port_id);
}

const char kPortClosedError[] = "Attempting to use a disconnected port object";
const char* kExtensionDeps[] = { EventBindings::kName };

class ExtensionImpl : public ExtensionBase {
 public:
  ExtensionImpl()
      : ExtensionBase(RendererExtensionBindings::kName,
                      GetStringResource<IDR_RENDERER_EXTENSION_BINDINGS_JS>(),
                      arraysize(kExtensionDeps), kExtensionDeps) {
  }
  ~ExtensionImpl() {}

  virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
      v8::Handle<v8::String> name) {
    if (name->Equals(v8::String::New("OpenChannelToExtension"))) {
      return v8::FunctionTemplate::New(OpenChannelToExtension);
    } else if (name->Equals(v8::String::New("PostMessage"))) {
      return v8::FunctionTemplate::New(PostMessage);
    } else if (name->Equals(v8::String::New("CloseChannel"))) {
      return v8::FunctionTemplate::New(CloseChannel);
    } else if (name->Equals(v8::String::New("PortAddRef"))) {
      return v8::FunctionTemplate::New(PortAddRef);
    } else if (name->Equals(v8::String::New("PortRelease"))) {
      return v8::FunctionTemplate::New(PortRelease);
    } else if (name->Equals(v8::String::New("GetL10nMessage"))) {
      return v8::FunctionTemplate::New(GetL10nMessage);
    }
    return ExtensionBase::GetNativeFunction(name);
  }

  // Creates a new messaging channel to the given extension.
  static v8::Handle<v8::Value> OpenChannelToExtension(
      const v8::Arguments& args) {
    // Get the current RenderView so that we can send a routed IPC message from
    // the correct source.
    RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext();
    if (!renderview)
      return v8::Undefined();

    if (args.Length() >= 3 && args[0]->IsString() && args[1]->IsString() &&
        args[2]->IsString()) {
      std::string source_id = *v8::String::Utf8Value(args[0]->ToString());
      std::string target_id = *v8::String::Utf8Value(args[1]->ToString());
      std::string channel_name = *v8::String::Utf8Value(args[2]->ToString());
      int port_id = -1;
      renderview->Send(new ViewHostMsg_OpenChannelToExtension(
          renderview->routing_id(), source_id, target_id,
          channel_name, &port_id));
      return v8::Integer::New(port_id);
    }
    return v8::Undefined();
  }

  // Sends a message along the given channel.
  static v8::Handle<v8::Value> PostMessage(const v8::Arguments& args) {
    RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext();
    if (!renderview)
      return v8::Undefined();

    if (args.Length() >= 2 && args[0]->IsInt32() && args[1]->IsString()) {
      int port_id = args[0]->Int32Value();
      if (!HasPortData(port_id)) {
        return v8::ThrowException(v8::Exception::Error(
          v8::String::New(kPortClosedError)));
      }
      std::string message = *v8::String::Utf8Value(args[1]->ToString());
      renderview->Send(new ViewHostMsg_ExtensionPostMessage(
          renderview->routing_id(), port_id, message));
    }
    return v8::Undefined();
  }

  // Forcefully disconnects a port.
  static v8::Handle<v8::Value> CloseChannel(const v8::Arguments& args) {
    if (args.Length() >= 1 && args[0]->IsInt32()) {
      int port_id = args[0]->Int32Value();
      if (!HasPortData(port_id)) {
        return v8::Undefined();
      }
      // Send via the RenderThread because the RenderView might be closing.
      EventBindings::GetRenderThread()->Send(
          new ViewHostMsg_ExtensionCloseChannel(port_id));
      ClearPortData(port_id);
    }
    return v8::Undefined();
  }

  // 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.
  static v8::Handle<v8::Value> PortAddRef(const v8::Arguments& args) {
    if (args.Length() >= 1 && args[0]->IsInt32()) {
      int port_id = args[0]->Int32Value();
      ++GetPortData(port_id).ref_count;
    }
    return v8::Undefined();
  }

  // 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.
  static v8::Handle<v8::Value> PortRelease(const v8::Arguments& args) {
    if (args.Length() >= 1 && args[0]->IsInt32()) {
      int port_id = args[0]->Int32Value();
      if (HasPortData(port_id) && --GetPortData(port_id).ref_count == 0) {
        // Send via the RenderThread because the RenderView might be closing.
        EventBindings::GetRenderThread()->Send(
            new ViewHostMsg_ExtensionCloseChannel(port_id));
        ClearPortData(port_id);
      }
    }
    return v8::Undefined();
  }

  static v8::Handle<v8::Value> GetL10nMessage(const v8::Arguments& args) {
    if (args.Length() != 3 || !args[0]->IsString()) {
      NOTREACHED() << "Bad arguments";
      return v8::Undefined();
    }

    std::string extension_id;
    if (args[2]->IsNull() || !args[2]->IsString()) {
      return v8::Undefined();
    } else {
      extension_id = *v8::String::Utf8Value(args[2]->ToString());
      if (extension_id.empty())
        return v8::Undefined();
    }

    L10nMessagesMap* l10n_messages = GetL10nMessagesMap(extension_id);
    if (!l10n_messages) {
      // Get the current RenderView so that we can send a routed IPC message
      // from the correct source.
      RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext();
      if (!renderview)
        return v8::Undefined();

      L10nMessagesMap messages;
      // A sync call to load message catalogs for current extension.
      renderview->Send(new ViewHostMsg_GetExtensionMessageBundle(
          extension_id, &messages));

      // Save messages we got.
      ExtensionToL10nMessagesMap& l10n_messages_map =
          *GetExtensionToL10nMessagesMap();
      l10n_messages_map[extension_id] = messages;

      l10n_messages = GetL10nMessagesMap(extension_id);
    }

    std::string message_name = *v8::String::AsciiValue(args[0]);
    std::string message =
        ExtensionMessageBundle::GetL10nMessage(message_name, *l10n_messages);

    std::vector<std::string> substitutions;
    if (args[1]->IsNull() || args[1]->IsUndefined()) {
      // chrome.i18n.getMessage("message_name");
      // chrome.i18n.getMessage("message_name", null);
      return v8::String::New(message.c_str());
    } else if (args[1]->IsString()) {
      // chrome.i18n.getMessage("message_name", "one param");
      std::string substitute = *v8::String::Utf8Value(args[1]->ToString());
      substitutions.push_back(substitute);
    } else if (args[1]->IsArray()) {
      // chrome.i18n.getMessage("message_name", ["more", "params"]);
      v8::Array* placeholders = static_cast<v8::Array*>(*args[1]);
      uint32_t count = placeholders->Length();
      if (count <= 0 || count > 9)
        return v8::Undefined();
      for (uint32_t i = 0; i < count; ++i) {
        std::string substitute =
            *v8::String::Utf8Value(
                placeholders->Get(v8::Integer::New(i))->ToString());
        substitutions.push_back(substitute);
      }
    } else {
      NOTREACHED() << "Couldn't parse second parameter.";
      return v8::Undefined();
    }

    return v8::String::New(ReplaceStringPlaceholders(
        message, substitutions, NULL).c_str());
  }
};

// Convert a ListValue to a vector of V8 values.
static std::vector< v8::Handle<v8::Value> > ListValueToV8(
    const ListValue& value) {
  std::vector< v8::Handle<v8::Value> > v8_values;

  for (size_t i = 0; i < value.GetSize(); ++i) {
    Value* elem = NULL;
    value.Get(i, &elem);
    switch (elem->GetType()) {
      case Value::TYPE_NULL:
        v8_values.push_back(v8::Null());
        break;
      case Value::TYPE_BOOLEAN: {
        bool val;
        elem->GetAsBoolean(&val);
        v8_values.push_back(v8::Boolean::New(val));
        break;
      }
      case Value::TYPE_INTEGER: {
        int val;
        elem->GetAsInteger(&val);
        v8_values.push_back(v8::Integer::New(val));
        break;
      }
      case Value::TYPE_REAL: {
        double val;
        elem->GetAsReal(&val);
        v8_values.push_back(v8::Number::New(val));
        break;
      }
      case Value::TYPE_STRING: {
        std::string val;
        elem->GetAsString(&val);
        v8_values.push_back(v8::String::New(val.c_str()));
        break;
      }
      default:
        NOTREACHED() << "Unsupported Value type.";
        break;
    }
  }

  return v8_values;
}

}  // namespace

const char* RendererExtensionBindings::kName =
    "chrome/RendererExtensionBindings";

v8::Extension* RendererExtensionBindings::Get() {
  static v8::Extension* extension = new ExtensionImpl();
  return extension;
}

void RendererExtensionBindings::Invoke(const std::string& function_name,
                                       const ListValue& args,
                                       RenderView* renderview,
                                       bool requires_incognito_access,
                                       const GURL& event_url) {
  v8::HandleScope handle_scope;
  std::vector< v8::Handle<v8::Value> > argv = ListValueToV8(args);
  EventBindings::CallFunction(function_name,
                              argv.size(),
                              &argv[0],
                              renderview,
                              requires_incognito_access,
                              event_url);
}