KHtml

kjs_navigator.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 2000 Harri Porten ([email protected])
4  * Copyright (c) 2000 Daniel Molkentin ([email protected])
5  * Copyright (c) 2000 Stefan Schimanski ([email protected])
6  * Copyright (C) 2003 Apple Computer, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "kjs_navigator.h"
24 #include "kjs_navigator.lut.h"
25 
26 #include <QLocale>
27 
28 #include <kconfig.h>
29 #include <kconfiggroup.h>
30 #include <ksharedconfig.h>
31 #include "khtml_debug.h"
32 
33 #include <kprotocolmanager.h>
34 #include <kservice.h>
35 #include <kservicetypetrader.h>
36 #include <kjs/lookup.h>
37 #include <kjs_binding.h>
38 #include <khtml_part.h>
39 #include <qsysinfo.h>
40 #include <qstandardpaths.h>
41 
42 using namespace KJS;
43 
44 namespace KJS
45 {
46 
47 // All objects that need plugin info must inherit from PluginBase
48 // Its ctor and dtor take care of the refcounting on the static lists.
49 class PluginBase : public JSObject
50 {
51 public:
52  PluginBase(ExecState *exec, bool loadPluginInfo);
53  virtual ~PluginBase();
54 
55  struct MimeClassInfo;
56  struct PluginInfo;
57 
58  struct MimeClassInfo {
59  QString type;
60  QString desc;
61  QString suffixes;
62  PluginInfo *plugin;
63  };
64 
65  struct PluginInfo {
66  QString name;
67  QString file;
68  QString desc;
70  };
71 
72  static QList<const PluginInfo *> *plugins;
73  static QList<const MimeClassInfo *> *mimes;
74 
75 private:
76  static int m_refCount;
77 };
78 
79 class Plugins : public PluginBase
80 {
81 public:
82  Plugins(ExecState *exec, bool pluginsEnabled)
83  : PluginBase(exec, pluginsEnabled),
84  m_pluginsEnabled(pluginsEnabled) {}
85 
86  using KJS::JSObject::getOwnPropertySlot;
87  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
88  JSValue *getValueProperty(ExecState *exec, int token) const;
89  const ClassInfo *classInfo() const override
90  {
91  return &info;
92  }
93  static const ClassInfo info;
94  static JSValue *pluginByName(ExecState *exec, const QString &name);
95  bool pluginsEnabled() const
96  {
97  return m_pluginsEnabled;
98  }
99 private:
100  static JSValue *indexGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
101  static JSValue *nameGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
102  bool m_pluginsEnabled;
103 };
104 
105 class MimeTypes : public PluginBase
106 {
107 public:
108  MimeTypes(ExecState *exec, bool pluginsEnabled)
109  : PluginBase(exec, pluginsEnabled),
110  m_pluginsEnabled(pluginsEnabled) {}
111  using KJS::JSObject::getOwnPropertySlot;
112  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
113  const ClassInfo *classInfo() const override
114  {
115  return &info;
116  }
117  static const ClassInfo info;
118  JSValue *getValueProperty(ExecState *exec, int token) const;
119  static JSValue *mimeTypeByName(ExecState *exec, const QString &name);
120  bool pluginsEnabled() const
121  {
122  return m_pluginsEnabled;
123  }
124 private:
125  static JSValue *indexGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
126  static JSValue *nameGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
127  bool m_pluginsEnabled;
128 };
129 
130 class Plugin : public PluginBase
131 {
132 public:
133  Plugin(ExecState *exec, const PluginBase::PluginInfo *info)
134  : PluginBase(exec, true)
135  {
136  m_info = info;
137  }
138  using KJS::JSObject::getOwnPropertySlot;
139  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
140  const ClassInfo *classInfo() const override
141  {
142  return &info;
143  }
144  static const ClassInfo info;
145  JSValue *mimeByName(ExecState *exec, const QString &name) const;
146  JSValue *getValueProperty(ExecState *exec, int token) const;
147  const PluginBase::PluginInfo *pluginInfo() const
148  {
149  return m_info;
150  }
151 private:
152  const PluginBase::PluginInfo *m_info;
153  static JSValue *indexGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
154  static JSValue *nameGetter(ExecState *, JSObject *, const Identifier &, const PropertySlot &);
155 };
156 
157 class MimeType : public PluginBase
158 {
159 public:
160  MimeType(ExecState *exec, const PluginBase::MimeClassInfo *info)
161  : PluginBase(exec, true)
162  {
163  m_info = info;
164  }
165  using KJS::JSObject::getOwnPropertySlot;
166  bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
167  const ClassInfo *classInfo() const override
168  {
169  return &info;
170  }
171  static const ClassInfo info;
172  JSValue *getValueProperty(ExecState *exec, int token) const;
173 private:
174  const PluginBase::MimeClassInfo *m_info;
175 };
176 
177 }
178 
179 QList<const PluginBase::PluginInfo *> *KJS::PluginBase::plugins;
180 QList<const PluginBase::MimeClassInfo *> *KJS::PluginBase::mimes;
181 int KJS::PluginBase::m_refCount;
182 
183 const ClassInfo Navigator::info = { "Navigator", nullptr, &NavigatorTable, nullptr };
184 /*
185 @begin NavigatorTable 12
186  appCodeName Navigator::AppCodeName DontDelete|ReadOnly
187  appName Navigator::AppName DontDelete|ReadOnly
188  appVersion Navigator::AppVersion DontDelete|ReadOnly
189  language Navigator::Language DontDelete|ReadOnly
190  userAgent Navigator::UserAgent DontDelete|ReadOnly
191  userLanguage Navigator::UserLanguage DontDelete|ReadOnly
192  browserLanguage Navigator::BrowserLanguage DontDelete|ReadOnly
193  platform Navigator::Platform DontDelete|ReadOnly
194  cpuClass Navigator::CpuClass DontDelete|ReadOnly
195  plugins Navigator::_Plugins DontDelete|ReadOnly
196  mimeTypes Navigator::_MimeTypes DontDelete|ReadOnly
197  product Navigator::Product DontDelete|ReadOnly
198  vendor Navigator::Vendor DontDelete|ReadOnly
199  vendorSub Navigator::VendorSub DontDelete|ReadOnly
200  productSub Navigator::ProductSub DontDelete|ReadOnly
201  cookieEnabled Navigator::CookieEnabled DontDelete|ReadOnly
202  javaEnabled Navigator::JavaEnabled DontDelete|Function 0
203 @end
204 */
205 KJS_IMPLEMENT_PROTOFUNC(NavigatorFunc)
206 
207 Navigator::Navigator(ExecState *exec, KHTMLPart *p)
208  : JSObject(exec->lexicalInterpreter()->builtinObjectPrototype()), m_part(p) { }
209 
210 bool Navigator::getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot)
211 {
212 #ifdef KJS_VERBOSE
213  qCDebug(KHTML_LOG) << "Navigator::getOwnPropertySlot " << propertyName.ascii();
214 #endif
215  return getStaticPropertySlot<NavigatorFunc, Navigator, JSObject>(exec, &NavigatorTable, this, propertyName, slot);
216 }
217 
218 JSValue *Navigator::getValueProperty(ExecState *exec, int token) const
219 {
220  QUrl url = m_part->url();
221  QString host = url.host();
222  if (host.isEmpty()) {
223  host = "localhost";
224  }
226 // ### get the following from the spoofing UA files as well.
227  QString appName = "Netscape";
228  QString product = "Gecko";
229  QString productSub = "20030107";
230  QString vendor = "KDE";
231  QString vendorSub = "";
232 
233  switch (token) {
234  case AppCodeName:
235  return jsString("Mozilla");
236  case AppName:
237  // If we find "Mozilla" but not "(compatible, ...)" we are a real Netscape
238  if (userAgent.indexOf(QLatin1String("Mozilla")) >= 0 &&
239  userAgent.indexOf(QLatin1String("compatible")) == -1) {
240  //qCDebug(KHTML_LOG) << "appName -> Mozilla";
241  return jsString("Netscape");
242  }
243  if (userAgent.indexOf(QLatin1String("Microsoft")) >= 0 ||
244  userAgent.indexOf(QLatin1String("MSIE")) >= 0) {
245  //qCDebug(KHTML_LOG) << "appName -> IE";
246  return jsString("Microsoft Internet Explorer");
247  }
248  //qCDebug(KHTML_LOG) << "appName -> Default";
249  return jsString(appName);
250  case AppVersion:
251  // We assume the string is something like Mozilla/version (properties)
252  return jsString(userAgent.mid(userAgent.indexOf('/') + 1));
253  case Product:
254  // We are pretending to be Mozilla or Safari
255  if (userAgent.indexOf(QLatin1String("Mozilla")) >= 0 &&
256  userAgent.indexOf(QLatin1String("compatible")) == -1) {
257  return jsString("Gecko");
258  }
259  // When spoofing as IE, we use jsUndefined().
260  if (userAgent.indexOf(QLatin1String("Microsoft")) >= 0 ||
261  userAgent.indexOf(QLatin1String("MSIE")) >= 0) {
262  return jsUndefined();
263  }
264  return jsString(product);
265  case ProductSub: {
266  int ix = userAgent.indexOf("Gecko");
267  if (ix >= 0 && userAgent.length() >= ix + 14 && userAgent.at(ix + 5) == '/' &&
268  userAgent.indexOf(QRegExp("\\d{8}"), ix + 6) == ix + 6) {
269  // We have Gecko/<productSub> in the UA string
270  return jsString(userAgent.mid(ix + 6, 8));
271  }
272  }
273  return jsString(productSub);
274  case Vendor:
275  if (userAgent.indexOf(QLatin1String("Safari")) >= 0) {
276  return jsString("Apple Computer, Inc.");
277  }
278  return jsString(vendor);
279  case VendorSub:
280  return jsString(vendorSub);
281  case BrowserLanguage:
282  case Language:
283  case UserLanguage:
284  return jsString(QLocale::languageToString(QLocale().language()));
285  case UserAgent:
286  return jsString(userAgent);
287  case Platform:
288  // yet another evil hack, but necessary to spoof some sites...
289  if ((userAgent.indexOf(QLatin1String("Win"), 0, Qt::CaseInsensitive) >= 0)) {
290  return jsString("Win32");
291  } else if ((userAgent.indexOf(QLatin1String("Macintosh"), 0, Qt::CaseInsensitive) >= 0) ||
292  (userAgent.indexOf(QLatin1String("Mac_PowerPC"), 0, Qt::CaseInsensitive) >= 0)) {
293  return jsString("MacPPC");
294  } else {
295  return jsString(QString::fromLatin1("%1 %2").arg(QSysInfo::kernelType()).arg(QSysInfo::currentCpuArchitecture()));
296  }
297  case CpuClass:
298  return jsString(QSysInfo::currentCpuArchitecture());
299  case _Plugins:
300  return new Plugins(exec, m_part->pluginsEnabled());
301  case _MimeTypes:
302  return new MimeTypes(exec, m_part->pluginsEnabled());
303  case CookieEnabled:
304  return jsBoolean(true); /// ##### FIXME
305  default:
306  // qCDebug(KHTML_LOG) << "WARNING: Unhandled token in DOMEvent::getValueProperty : " << token;
307  return jsNull();
308  }
309 }
310 
311 /*******************************************************************/
312 
313 PluginBase::PluginBase(ExecState *exec, bool loadPluginInfo)
314  : JSObject(exec->lexicalInterpreter()->builtinObjectPrototype())
315 {
316  if (loadPluginInfo && !plugins) {
317  plugins = new QList<const PluginInfo *>;
318  mimes = new QList<const MimeClassInfo *>;
319 
320  // read in using KServiceTypeTrader
321  const KService::List offers = KServiceTypeTrader::self()->query("Browser/View");
322  KService::List::const_iterator it;
323  for (it = offers.begin(); it != offers.end(); ++it) {
324 
325  QVariant pluginsinfo = (**it).property("X-KDE-BrowserView-PluginsInfo");
326  if (!pluginsinfo.isValid()) {
327  // <backwards compatible>
328  if ((**it).library() == QLatin1String("libnsplugin")) {
329  pluginsinfo = QVariant("nsplugins/pluginsinfo");
330  } else
331  // </backwards compatible>
332  {
333  continue;
334  }
335  }
336  // read configuration
338  const KSharedConfig::Ptr sc = KSharedConfig::openConfig(fn);
339  const int num = sc->group("").readEntry("number", 0);
340  for (int n = 0; n < num; n++) {
341  const KConfigGroup kc = sc->group(QString::number(n));
342  PluginInfo *plugin = new PluginInfo;
343 
344  plugin->name = kc.readEntry("name");
345  plugin->file = kc.readPathEntry("file", QString());
346  plugin->desc = kc.readEntry("description");
347 
348  plugins->append(plugin);
349 
350  const QStringList types = kc.readXdgListEntry("mime");
352  for (type = types.begin(); type != types.end(); ++type) {
353 
354  // get mime information
355  const QStringList tokens = (*type).split(':', QString::KeepEmptyParts);
356  if (tokens.count() < 3) { // we need 3 items
357  continue;
358  }
359 
360  MimeClassInfo *mime = new MimeClassInfo;
361  QStringList::ConstIterator token = tokens.begin();
362  mime->type = (*token).toLower();
363  //qCDebug(KHTML_LOG) << "mime->type=" << mime->type;
364  ++token;
365 
366  mime->suffixes = *token;
367  ++token;
368 
369  mime->desc = *token;
370  ++token;
371 
372  mime->plugin = plugin;
373 
374  mimes->append(mime);
375  plugin->mimes.append(mime);
376 
377  }
378  }
379  }
380  }
381 
382  m_refCount++;
383 }
384 
385 PluginBase::~PluginBase()
386 {
387  m_refCount--;
388  if (m_refCount == 0) {
389  if (plugins) {
390  qDeleteAll(*plugins);
391  }
392  if (mimes) {
393  qDeleteAll(*mimes);
394  }
395  delete plugins;
396  delete mimes;
397  plugins = nullptr;
398  mimes = nullptr;
399  }
400 }
401 
402 /*******************************************************************/
403 
404 const ClassInfo Plugins::info = { "PluginArray", nullptr, &PluginsTable, nullptr };
405 /*
406 @begin PluginsTable 4
407  length Plugins_Length DontDelete|ReadOnly
408  refresh Plugins_Refresh DontDelete|Function 0
409  item Plugins_Item DontDelete|Function 1
410  namedItem Plugins_NamedItem DontDelete|Function 1
411 @end
412 */
413 KJS_IMPLEMENT_PROTOFUNC(PluginsFunc)
414 
415 JSValue *Plugins::getValueProperty(ExecState *, int token) const
416 {
417  assert(token == Plugins_Length);
418  Q_UNUSED(token);
419  if (pluginsEnabled()) {
420  return jsNumber(plugins->count());
421  } else {
422  return jsNumber(0);
423  }
424 }
425 
426 JSValue *Plugins::indexGetter(ExecState *exec, JSObject *, const Identifier & /*propertyName*/, const PropertySlot &slot)
427 {
428  return new Plugin(exec, plugins->at(slot.index()));
429 }
430 
431 JSValue *Plugins::nameGetter(ExecState *exec, JSObject *, const Identifier &propertyName, const PropertySlot & /*slot*/)
432 {
433  return pluginByName(exec, propertyName.qstring());
434 }
435 
436 bool Plugins::getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot)
437 {
438 #ifdef KJS_VERBOSE
439  qCDebug(KHTML_LOG) << "Plugins::getOwnPropertySlot " << propertyName.qstring();
440 #endif
441  if (getStaticOwnPropertySlot<PluginsFunc, Plugins>(&PluginsTable, this, propertyName, slot)) {
442  return true;
443  }
444 
445  if (pluginsEnabled()) {
446  // plugins[#]
447  bool ok;
448  unsigned int i = propertyName.toArrayIndex(&ok);
449  if (ok && i < static_cast<unsigned>(plugins->count())) {
450  slot.setCustomIndex(this, i, indexGetter);
451  return true;
452  }
453 
454  // plugin[name]
455  QList<const PluginInfo *>::const_iterator it, end = plugins->constEnd();
456  for (it = plugins->constBegin(); it != end; ++it) {
457  if ((*it)->name == propertyName.qstring()) {
458  slot.setCustom(this, nameGetter);
459  return true;
460  }
461  }
462  }
463 
464  return PluginBase::getOwnPropertySlot(exec, propertyName, slot);
465 }
466 
467 JSValue *Plugins::pluginByName(ExecState *exec, const QString &name)
468 {
469  QList<const PluginInfo *>::const_iterator it, end = plugins->constEnd();
470  for (it = plugins->constBegin(); it != end; ++it) {
471  if ((*it)->name == name) {
472  return new Plugin(exec, *it);
473  }
474  }
475  return jsUndefined();
476 }
477 
478 JSValue *PluginsFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
479 {
480  KJS_CHECK_THIS(KJS::Plugins, thisObj);
481 
482  KJS::Plugins *base = static_cast<KJS::Plugins *>(thisObj);
483  if (!base->pluginsEnabled()) {
484  return jsUndefined();
485  }
486 
487  switch (id) {
488  case Plugins_Refresh:
489  return jsUndefined(); //## TODO
490  case Plugins_Item: {
491  bool ok;
492  unsigned int i = args[0]->toString(exec).toArrayIndex(&ok);
493  if (ok && i < static_cast<unsigned>(base->plugins->count())) {
494  return new Plugin(exec, base->plugins->at(i));
495  }
496  return jsUndefined();
497  }
498  case Plugins_NamedItem: {
499  UString s = args[0]->toString(exec);
500  return base->pluginByName(exec, s.qstring());
501  }
502  default:
503  // qCDebug(KHTML_LOG) << "WARNING: Unhandled token in PluginsFunc::callAsFunction : " << id;
504  return jsUndefined();
505  }
506 }
507 
508 /*******************************************************************/
509 
510 const ClassInfo MimeTypes::info = { "MimeTypeArray", nullptr, &MimeTypesTable, nullptr };
511 /*
512 @begin MimeTypesTable 3
513  length MimeTypes_Length DontDelete|ReadOnly
514  item MimeTypes_Item DontDelete|Function 1
515  namedItem MimeTypes_NamedItem DontDelete|Function 1
516 @end
517 */
518 KJS_IMPLEMENT_PROTOFUNC(MimeTypesFunc)
519 
520 JSValue *MimeTypes::indexGetter(ExecState *exec, JSObject *, const Identifier & /*propertyName*/, const PropertySlot &slot)
521 {
522  return new MimeType(exec, mimes->at(slot.index()));
523 }
524 
525 JSValue *MimeTypes::nameGetter(ExecState *exec, JSObject *, const Identifier &propertyName, const PropertySlot & /*slot*/)
526 {
527  return mimeTypeByName(exec, propertyName.qstring());
528 }
529 
530 bool MimeTypes::getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot)
531 {
532 #ifdef KJS_VERBOSE
533  qCDebug(KHTML_LOG) << "MimeTypes::getOwnPropertySlot " << propertyName.qstring();
534 #endif
535  if (getStaticOwnPropertySlot<MimeTypesFunc, MimeTypes>(&MimeTypesTable, this, propertyName, slot)) {
536  return true;
537  }
538 
539  if (pluginsEnabled()) {
540  // mimeTypes[#]
541  bool ok;
542  unsigned int i = propertyName.toArrayIndex(&ok);
543  if (ok && i < static_cast<unsigned>(mimes->count())) {
544  slot.setCustomIndex(this, i, indexGetter);
545  return true;
546  }
547 
548  // mimeTypes[name]
549  QList<const MimeClassInfo *>::const_iterator it, end = mimes->constEnd();
550  for (it = mimes->constBegin(); it != end; ++it) {
551  if ((*it)->type == propertyName.qstring()) {
552  slot.setCustom(this, nameGetter);
553  return true;
554  }
555  }
556  }
557 
558  return PluginBase::getOwnPropertySlot(exec, propertyName, slot);
559 }
560 
561 JSValue *MimeTypes::mimeTypeByName(ExecState *exec, const QString &name)
562 {
563  //qCDebug(KHTML_LOG) << "MimeTypes[" << name << "]";
564  QList<const MimeClassInfo *>::const_iterator it, end = mimes->constEnd();
565  for (it = mimes->constBegin(); it != end; ++it) {
566  if ((*it)->type == name) {
567  return new MimeType(exec, (*it));
568  }
569  }
570  return jsUndefined();
571 }
572 
573 JSValue *MimeTypes::getValueProperty(ExecState * /*exec*/, int token) const
574 {
575  assert(token == MimeTypes_Length);
576  Q_UNUSED(token);
577  if (pluginsEnabled()) {
578  return jsNumber(mimes->count());
579  } else {
580  return jsNumber(0);
581  }
582 }
583 
584 JSValue *MimeTypesFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
585 {
586  KJS_CHECK_THIS(KJS::MimeTypes, thisObj);
587  KJS::MimeTypes *base = static_cast<KJS::MimeTypes *>(thisObj);
588 
589  if (!base->pluginsEnabled()) {
590  return jsUndefined();
591  }
592 
593  switch (id) {
594  case MimeTypes_Item: {
595  bool ok;
596  unsigned int i = args[0]->toString(exec).toArrayIndex(&ok);
597  if (ok && i < static_cast<unsigned>(base->mimes->count())) {
598  return new MimeType(exec, base->mimes->at(i));
599  }
600  return jsUndefined();
601  }
602  case MimeTypes_NamedItem: {
603  UString s = args[0]->toString(exec);
604  return base->mimeTypeByName(exec, s.qstring());
605  }
606  default:
607  // qCDebug(KHTML_LOG) << "WARNING: Unhandled token in MimeTypesFunc::callAsFunction : " << id;
608  return jsUndefined();
609  }
610 }
611 
612 /************************************************************************/
613 const ClassInfo Plugin::info = { "Plugin", nullptr, &PluginTable, nullptr };
614 /*
615 @begin PluginTable 7
616  name Plugin_Name DontDelete|ReadOnly
617  filename Plugin_FileName DontDelete|ReadOnly
618  description Plugin_Description DontDelete|ReadOnly
619  length Plugin_Length DontDelete|ReadOnly
620  item Plugin_Item DontDelete|Function 1
621  namedItem Plugin_NamedItem DontDelete|Function 1
622 @end
623 */
624 KJS_IMPLEMENT_PROTOFUNC(PluginFunc)
625 
626 JSValue *Plugin::indexGetter(ExecState *exec, JSObject *, const Identifier & /*propertyName*/, const PropertySlot &slot)
627 {
628  Plugin *thisObj = static_cast<Plugin *>(slot.slotBase());
629  return new MimeType(exec, thisObj->m_info->mimes.at(slot.index()));
630 }
631 
632 JSValue *Plugin::nameGetter(ExecState *exec, JSObject *, const Identifier &propertyName, const PropertySlot &slot)
633 {
634  Plugin *thisObj = static_cast<Plugin *>(slot.slotBase());
635  return thisObj->mimeByName(exec, propertyName.qstring());
636 }
637 
638 bool Plugin::getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot)
639 {
640 #ifdef KJS_VERBOSE
641  qCDebug(KHTML_LOG) << "Plugin::getOwnPropertySlot " << propertyName.qstring();
642 #endif
643  if (getStaticOwnPropertySlot<PluginFunc, Plugin>(&PluginTable, this, propertyName, slot)) {
644  return true;
645  }
646 
647  // plugin[#]
648  bool ok;
649  unsigned int i = propertyName.toArrayIndex(&ok);
650  if (ok && i < static_cast<unsigned>(m_info->mimes.count())) {
651  slot.setCustomIndex(this, i, indexGetter);
652  return true;
653  }
654 
655  // plugin["name"]
656  QList<const MimeClassInfo *>::const_iterator it, end = mimes->constEnd();
657  for (it = mimes->constBegin(); it != end; ++it) {
658  if ((*it)->type == propertyName.qstring()) {
659  slot.setCustom(this, nameGetter);
660  return true;
661  }
662  }
663 
664  return PluginBase::getOwnPropertySlot(exec, propertyName, slot);
665 }
666 
667 JSValue *Plugin::mimeByName(ExecState *exec, const QString &name) const
668 {
669  const QList<const MimeClassInfo *> &mimes = m_info->mimes;
671  for (it = mimes.begin(); it != end; ++it) {
672  if ((*it)->type == name) {
673  return new MimeType(exec, (*it));
674  }
675  }
676  return jsUndefined();
677 }
678 
679 JSValue *Plugin::getValueProperty(ExecState * /*exec*/, int token) const
680 {
681  switch (token) {
682  case Plugin_Name:
683  return jsString(UString(m_info->name));
684  case Plugin_FileName:
685  return jsString(UString(m_info->file));
686  case Plugin_Description:
687  return jsString(UString(m_info->desc));
688  case Plugin_Length:
689  return jsNumber(m_info->mimes.count());
690  default:
691  // qCDebug(KHTML_LOG) << "WARNING: Unhandled token in Plugin::getValueProperty : " << token;
692  return jsUndefined();
693  }
694 }
695 
696 JSValue *PluginFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
697 {
698  KJS_CHECK_THIS(KJS::Plugin, thisObj);
699  KJS::Plugin *plugin = static_cast<KJS::Plugin *>(thisObj);
700  switch (id) {
701  case Plugin_Item: {
702  bool ok;
703  unsigned int i = args[0]->toString(exec).toArrayIndex(&ok);
704  if (ok && i < static_cast<unsigned>(plugin->pluginInfo()->mimes.count())) {
705  return new MimeType(exec, plugin->pluginInfo()->mimes.at(i));
706  }
707  return jsUndefined();
708  }
709  case Plugin_NamedItem: {
710  UString s = args[0]->toString(exec);
711  return plugin->mimeByName(exec, s.qstring());
712  }
713  default:
714  // qCDebug(KHTML_LOG) << "WARNING: Unhandled token in PluginFunc::callAsFunction : " << id;
715  return jsUndefined();
716  }
717 }
718 
719 /*****************************************************************************/
720 
721 const ClassInfo MimeType::info = { "MimeType", nullptr, &MimeTypeTable, nullptr };
722 /*
723 @begin MimeTypeTable 4
724  description MimeType_Description DontDelete|ReadOnly
725  enabledPlugin MimeType_EnabledPlugin DontDelete|ReadOnly
726  suffixes MimeType_Suffixes DontDelete|ReadOnly
727  type MimeType_Type DontDelete|ReadOnly
728 @end
729 */
730 
731 bool MimeType::getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot)
732 {
733 #ifdef KJS_VERBOSE
734  qCDebug(KHTML_LOG) << "MimeType::get " << propertyName.qstring();
735 #endif
736  return getStaticValueSlot<MimeType, JSObject>(exec, &MimeTypeTable, this, propertyName, slot);
737 }
738 
739 JSValue *MimeType::getValueProperty(ExecState *exec, int token) const
740 {
741  switch (token) {
742  case MimeType_Type:
743  return jsString(UString(m_info->type));
744  case MimeType_Suffixes:
745  return jsString(UString(m_info->suffixes));
746  case MimeType_Description:
747  return jsString(UString(m_info->desc));
748  case MimeType_EnabledPlugin:
749  return new Plugin(exec, m_info->plugin);
750  default:
751  // qCDebug(KHTML_LOG) << "WARNING: Unhandled token in MimeType::getValueProperty : " << token;
752  return jsUndefined();
753  }
754 }
755 
756 JSValue *NavigatorFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &)
757 {
758  KJS_CHECK_THIS(KJS::Navigator, thisObj);
759  Navigator *nav = static_cast<Navigator *>(thisObj);
760  // javaEnabled()
761  return jsBoolean(nav->part()->javaEnabled());
762 }
QString url(QUrl::FormattingOptions options) const const
QString readPathEntry(const QString &pKey, const QString &aDefault) const
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
KJS_EXTERNAL_EXPORT QString qstring() const
QString name(const QVariant &location)
KJS_EXTERNAL_EXPORT QString qstring() const
static KServiceTypeTrader * self()
static QString userAgentForHost(const QString &hostname)
QString currentCpuArchitecture()
QString host(QUrl::ComponentFormattingOptions options) const const
QString languageToString(QLocale::Language language)
This class is khtml&#39;s main class.
Definition: khtml_part.h:208
QString number(int n, int base)
int count(const T &value) const const
Type type(const QSqlDatabase &db)
CaseInsensitive
bool isEmpty() const const
const char * ascii() const
QString kernelType()
QStringList readXdgListEntry(const QString &pKey, const QStringList &aDefault=QStringList()) const
QList::iterator end()
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
KConfigGroup group(const QString &group)
const QList< QKeySequence > & end()
KService::List query(const QString &servicetype, const QString &constraint=QString()) const
QString mid(int position, int n) const const
const QChar at(int position) const const
typedef ConstIterator
int length() const const
Language
QString fromLatin1(const char *str, int size)
bool isValid() const const
QString toString() const const
T readEntry(const QString &key, const T &aDefault) const
QList::iterator begin()
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
Types types(const QStringList &mimeTypes)
QCA_EXPORT QString appName()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 22:48:18 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.