summaryrefslogtreecommitdiffstats
path: root/webkit/glue/plugins/plugin_instance.cc
blob: c64ff1fae16e95ff1789bdefd8915681742e5149 (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
// Copyright (c) 2006-2008 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 "webkit/glue/plugins/plugin_instance.h"

#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/thread_local_storage.h"
#include "webkit/glue/glue_util.h"
#include "webkit/glue/webplugin.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/glue/plugins/plugin_data_stream.h"
#include "webkit/glue/plugins/plugin_host.h"
#include "webkit/glue/plugins/plugin_lib.h"
#include "webkit/glue/plugins/plugin_stream_url.h"
#include "webkit/glue/plugins/plugin_string_stream.h"
#include "webkit/glue/plugins/mozilla_extensions.h"
#include "net/base/escape.h"

namespace NPAPI
{

// TODO(evanm): don't rely on static initialization.
ThreadLocalStorage::Slot PluginInstance::plugin_instance_tls_index_;

PluginInstance::PluginInstance(PluginLib *plugin, const std::string &mime_type)
    : plugin_(plugin),
      npp_(0),
      host_(PluginHost::Singleton()),
      npp_functions_(plugin->functions()),
      hwnd_(0),
      windowless_(false),
      transparent_(true),
      mime_type_(mime_type),
      webplugin_(0),
      use_mozilla_user_agent_(false),
      message_loop_(MessageLoop::current()),
      load_manually_(false),
      throttle_invalidate_(false),
      get_notify_data_(NULL),
      in_close_streams_(false) {
  npp_ = new NPP_t();
  npp_->ndata = 0;
  npp_->pdata = 0;

  memset(&zero_padding_, 0, sizeof(zero_padding_));
  DCHECK(message_loop_);
}

PluginInstance::~PluginInstance() {
  CloseStreams();

  if (npp_ != 0) {
    delete npp_;
    npp_ = 0;
  }

  if (plugin_)
    plugin_->CloseInstance();
}

PluginStreamUrl *PluginInstance::CreateStream(int resource_id,
                                              const std::string &url,
                                              const std::string &mime_type,
                                              bool notify_needed,
                                              void *notify_data) {
  PluginStreamUrl *stream = new PluginStreamUrl(
      resource_id, GURL(url), this, notify_needed, notify_data);

  AddStream(stream);
  return stream;
}

void PluginInstance::SendStream(const std::string &url,
                                bool notify_needed,
                                void *notify_data) {
  if (notify_needed) {
    host_->host_functions()->geturlnotify(npp(), url.c_str(), NULL,
                                          notify_data);
  } else {
    host_->host_functions()->geturl(npp(), url.c_str(), NULL);
  }
}

void PluginInstance::AddStream(PluginStream* stream) {
  open_streams_.push_back(stream);
}

void PluginInstance::RemoveStream(PluginStream* stream) {
  if (in_close_streams_)
    return;

  std::vector<scoped_refptr<PluginStream> >::iterator stream_index;
  for (stream_index = open_streams_.begin();
          stream_index != open_streams_.end(); ++stream_index) {
    if (*stream_index == stream) {
      open_streams_.erase(stream_index);
      break;
    }
  }
}

bool PluginInstance::IsValidStream(const NPStream* stream) {
  std::vector<scoped_refptr<PluginStream> >::iterator stream_index;
  for (stream_index = open_streams_.begin();
          stream_index != open_streams_.end(); ++stream_index) {
    if ((*stream_index)->stream() == stream)
      return true;
  }

  return false;
}

void PluginInstance::CloseStreams() {
  in_close_streams_ = true;
  for (unsigned int index = 0; index < open_streams_.size(); ++index) {
    // Close all streams on the way down.
    open_streams_[index]->Close(NPRES_USER_BREAK);
  }
  open_streams_.clear();
  in_close_streams_ = false;
}

bool PluginInstance::HandleEvent(UINT message, WPARAM wParam, LPARAM lParam) {
  if (!windowless_)
    return false;

  NPEvent windowEvent;
  windowEvent.event = message;
  windowEvent.lParam = static_cast<uint32>(lParam);
  windowEvent.wParam = static_cast<uint32>(wParam);
  return NPP_HandleEvent(&windowEvent) != 0;
}

bool PluginInstance::Start(const GURL& url,
                           char** const param_names,
                           char** const param_values,
                           int param_count,
                           bool load_manually) {
  load_manually_ = load_manually;
  instance_url_ = url;
  unsigned short mode = load_manually_ ? NP_FULL : NP_EMBED;
  npp_->ndata = this;

  NPError err = NPP_New(mode, param_count,
      const_cast<char **>(param_names), const_cast<char **>(param_values));
  return err == NPERR_NO_ERROR;
}

NPObject *PluginInstance::GetPluginScriptableObject() {
  NPObject *value;
  NPError error = NPP_GetValue(NPPVpluginScriptableNPObject, &value);
  if (error != NPERR_NO_ERROR)
    return NULL;
  return value;
}

void PluginInstance::SetURLLoadData(const GURL& url,
                                    void* notify_data) {
  get_url_ = url;
  get_notify_data_ = notify_data;
}

// WebPluginLoadDelegate methods
void PluginInstance::DidFinishLoadWithReason(NPReason reason) {
  if (!get_url_.is_empty()) {
    NPP_URLNotify(get_url_.spec().c_str(), reason, get_notify_data_);
  }

  get_url_ = GURL();
  get_notify_data_ = NULL;
}

// NPAPI methods
NPError PluginInstance::NPP_New(unsigned short mode,
                                short argc,
                                char *argn[],
                                char *argv[]) {
  DCHECK(npp_functions_ != 0);
  DCHECK(npp_functions_->newp != 0);
  DCHECK(argc >= 0);

  if (npp_functions_->newp != 0) {
    return npp_functions_->newp(
        (NPMIMEType)mime_type_.c_str(), npp_, mode,  argc, argn, argv, NULL);
  }
  return NPERR_INVALID_FUNCTABLE_ERROR;
}

void PluginInstance::NPP_Destroy() {
  DCHECK(npp_functions_ != 0);
  DCHECK(npp_functions_->newp != 0);

  if (npp_functions_->destroy != 0) {
    NPSavedData *savedData = 0;
    npp_functions_->destroy(npp_, &savedData);

    // TODO: Support savedData.  Technically, these need to be
    //       saved on a per-URL basis, and then only passed
    //       to new instances of the plugin at the same URL.
    //       Sounds like a huge security risk.  When we do support
    //       these, we should pass them back to the PluginLib
    //       to be stored there.
    DCHECK(savedData == 0);
  }

  // Clean up back references to this instance if any
  if (mozilla_extenstions_) {
    mozilla_extenstions_->DetachFromInstance();
    mozilla_extenstions_ = NULL;
  }
}

NPError PluginInstance::NPP_SetWindow(NPWindow *window) {
  DCHECK(npp_functions_ != 0);
  DCHECK(npp_functions_->setwindow != 0);

  if (npp_functions_->setwindow != 0) {
    return npp_functions_->setwindow(npp_, window);
  }
  return NPERR_INVALID_FUNCTABLE_ERROR;
}

NPError PluginInstance::NPP_NewStream(NPMIMEType type,
                                      NPStream *stream,
                                      NPBool seekable,
                                      unsigned short *stype) {
  DCHECK(npp_functions_ != 0);
  DCHECK(npp_functions_->newstream != 0);
  if (npp_functions_->newstream != 0) {
      return npp_functions_->newstream(npp_, type, stream, seekable, stype);
  }
  return NPERR_INVALID_FUNCTABLE_ERROR;
}

NPError PluginInstance::NPP_DestroyStream(NPStream *stream, NPReason reason) {
  DCHECK(npp_functions_ != 0);
  DCHECK(npp_functions_->destroystream != 0);

  if (stream == NULL || (stream->ndata == NULL) ||
      !IsValidStream(stream))
    return NPERR_INVALID_INSTANCE_ERROR;

  if (npp_functions_->destroystream != 0) {
    NPError result = npp_functions_->destroystream(npp_, stream, reason);
    stream->ndata = NULL;
    return result;
  }
  return NPERR_INVALID_FUNCTABLE_ERROR;
}

int PluginInstance::NPP_WriteReady(NPStream *stream) {
  DCHECK(npp_functions_ != 0);
  DCHECK(npp_functions_->writeready != 0);
  if (npp_functions_->writeready != 0) {
    return npp_functions_->writeready(npp_, stream);
  }
  return NULL;
}

int PluginInstance::NPP_Write(NPStream *stream,
                              int offset,
                              int len,
                              void *buffer) {
  DCHECK(npp_functions_ != 0);
  DCHECK(npp_functions_->write != 0);
  if (npp_functions_->write != 0) {
    return npp_functions_->write(npp_, stream, offset, len, buffer);
  }
  return NULL;
}

void PluginInstance::NPP_StreamAsFile(NPStream *stream, const char *fname) {
  DCHECK(npp_functions_ != 0);
  DCHECK(npp_functions_->asfile != 0);
  if (npp_functions_->asfile != 0) {
    npp_functions_->asfile(npp_, stream, fname);
  }
}

void PluginInstance::NPP_URLNotify(const char *url,
                                   NPReason reason,
                                   void *notifyData) {
  DCHECK(npp_functions_ != 0);
  DCHECK(npp_functions_->urlnotify != 0);
  if (npp_functions_->urlnotify != 0) {
    npp_functions_->urlnotify(npp_, url, reason, notifyData);
  }
}

NPError PluginInstance::NPP_GetValue(NPPVariable variable, void *value) {
  DCHECK(npp_functions_ != 0);
  // getvalue is NULL for Shockwave
  if (npp_functions_->getvalue != 0) {
    return npp_functions_->getvalue(npp_, variable, value);
  }
  return NPERR_INVALID_FUNCTABLE_ERROR;
}

NPError PluginInstance::NPP_SetValue(NPNVariable variable, void *value) {
  DCHECK(npp_functions_ != 0);
  if (npp_functions_->setvalue != 0) {
    return npp_functions_->setvalue(npp_, variable, value);
  }
  return NPERR_INVALID_FUNCTABLE_ERROR;
}

short PluginInstance::NPP_HandleEvent(NPEvent *event) {
  DCHECK(npp_functions_ != 0);
  DCHECK(npp_functions_->event != 0);
  if (npp_functions_->event != 0) {
    return npp_functions_->event(npp_, (void*)event);
  }
  return false;
}

bool PluginInstance::NPP_Print(NPPrint* platform_print) {
  DCHECK(npp_functions_ != 0);
  if (npp_functions_->print != 0) {
    npp_functions_->print(npp_, platform_print);
    return true;
  }
  return false;
}

void PluginInstance::SendJavaScriptStream(const std::string& url,
                                          const std::wstring& result,
                                          bool success,
                                          bool notify_needed,
                                          int notify_data) {
  if (success) {
    PluginStringStream *stream =
      new PluginStringStream(this, url, notify_needed,
                             reinterpret_cast<void*>(notify_data));
    AddStream(stream);
    stream->SendToPlugin(WideToUTF8(result), "text/html");
  } else {
    // NOTE: Sending an empty stream here will crash MacroMedia
    // Flash 9.  Just send the URL Notify.
    if (notify_needed) {
      this->NPP_URLNotify(url.c_str(), NPRES_DONE,
                          reinterpret_cast<void*>(notify_data));
    }
  }
}

void PluginInstance::DidReceiveManualResponse(const std::string& url,
                                              const std::string& mime_type,
                                              const std::string& headers,
                                              uint32 expected_length,
                                              uint32 last_modified) {
  DCHECK(load_manually_);
  std::string response_url = url;
  if (response_url.empty()) {
    response_url = instance_url_.spec();
  }

  plugin_data_stream_ = new PluginDataStream(this, response_url, mime_type,
                                             headers, expected_length,
                                             last_modified);
  AddStream(plugin_data_stream_.get());
}

void PluginInstance::DidReceiveManualData(const char* buffer, int length) {
  DCHECK(load_manually_);
  DCHECK(plugin_data_stream_.get() != NULL);
  plugin_data_stream_->SendToPlugin(buffer, length);
}

void PluginInstance::DidFinishManualLoading() {
  DCHECK(load_manually_);
  DCHECK(plugin_data_stream_);
  plugin_data_stream_->Close(NPRES_DONE);
  RemoveStream(plugin_data_stream_.get());
  plugin_data_stream_ = NULL;
}

void PluginInstance::DidManualLoadFail() {
  DCHECK(load_manually_);
  DCHECK(plugin_data_stream_);
  plugin_data_stream_->Close(NPRES_NETWORK_ERR);
  RemoveStream(plugin_data_stream_.get());
  plugin_data_stream_ = NULL;
}

void PluginInstance::PluginThreadAsyncCall(void (*func)(void *),
                                           void *userData) {
  message_loop_->PostTask(FROM_HERE, NewRunnableMethod(
      this, &PluginInstance::OnPluginThreadAsyncCall, func, userData));
}

void PluginInstance::OnPluginThreadAsyncCall(void (*func)(void *),
                                             void *userData) {
  // We are invoking an arbitrary callback provided by a third
  // party plugin. It's better to wrap this into an exception
  // block to protect us from crashes.
  __try {
    func(userData);
  } __except(EXCEPTION_EXECUTE_HANDLER) {
    // Maybe we can disable a crashing plugin.
    // But for now, just continue.
  }
}

PluginInstance* PluginInstance::SetInitializingInstance(
    PluginInstance* instance) {
  PluginInstance* old_instance =
      static_cast<PluginInstance*>(plugin_instance_tls_index_.Get());
  plugin_instance_tls_index_.Set(instance);
  return old_instance;
}

PluginInstance* PluginInstance::GetInitializingInstance() {
  PluginInstance* instance =
      static_cast<PluginInstance*>(plugin_instance_tls_index_.Get());
  return instance;
}

NPError PluginInstance::GetServiceManager(void** service_manager) {
  if (!mozilla_extenstions_) {
    mozilla_extenstions_ = new MozillaExtensionApi(this);
  }

  DCHECK(mozilla_extenstions_);
  mozilla_extenstions_->QueryInterface(nsIServiceManager::GetIID(),
                                       service_manager);
  return NPERR_NO_ERROR;
}

void PluginInstance::PushPopupsEnabledState(bool enabled) {
  popups_enabled_stack_.push(enabled);
}

void PluginInstance::PopPopupsEnabledState() {
  popups_enabled_stack_.pop();
}

}  // namespace NPAPI