(function(define, require, requireNative, requireAsync, exports, console, privates,$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {'use strict';// 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.
var exceptionHandler = require('uncaught_exception_handler');
var eventNatives = requireNative('event_natives');
var logging = requireNative('logging');
var schemaRegistry = requireNative('schema_registry');
var sendRequest = require('sendRequest').sendRequest;
var utils = require('utils');
var validate = require('schemaUtils').validate;
// Schemas for the rule-style functions on the events API that
// only need to be generated occasionally, so populate them lazily.
var ruleFunctionSchemas = {
// These values are set lazily:
// addRules: {},
// getRules: {},
// removeRules: {}
};
// This function ensures that |ruleFunctionSchemas| is populated.
function ensureRuleSchemasLoaded() {
if (ruleFunctionSchemas.addRules)
return;
var eventsSchema = schemaRegistry.GetSchema("events");
var eventType = utils.lookup(eventsSchema.types, 'id', 'events.Event');
ruleFunctionSchemas.addRules =
utils.lookup(eventType.functions, 'name', 'addRules');
ruleFunctionSchemas.getRules =
utils.lookup(eventType.functions, 'name', 'getRules');
ruleFunctionSchemas.removeRules =
utils.lookup(eventType.functions, 'name', 'removeRules');
}
// A map of event names to the event object that is registered to that name.
var attachedNamedEvents = {};
// A map of functions that massage event arguments before they are dispatched.
// Key is event name, value is function.
var eventArgumentMassagers = {};
// An attachment strategy for events that aren't attached to the browser.
// This applies to events with the "unmanaged" option and events without
// names.
var NullAttachmentStrategy = function(event) {
this.event_ = event;
};
NullAttachmentStrategy.prototype.onAddedListener =
function(listener) {
};
NullAttachmentStrategy.prototype.onRemovedListener =
function(listener) {
};
NullAttachmentStrategy.prototype.detach = function(manual) {
};
NullAttachmentStrategy.prototype.getListenersByIDs = function(ids) {
// |ids| is for filtered events only.
return this.event_.listeners;
};
// Handles adding/removing/dispatching listeners for unfiltered events.
var UnfilteredAttachmentStrategy = function(event) {
this.event_ = event;
};
UnfilteredAttachmentStrategy.prototype.onAddedListener =
function(listener) {
// Only attach / detach on the first / last listener removed.
if (this.event_.listeners.length == 0)
eventNatives.AttachEvent(this.event_.eventName);
};
UnfilteredAttachmentStrategy.prototype.onRemovedListener =
function(listener) {
if (this.event_.listeners.length == 0)
this.detach(true);
};
UnfilteredAttachmentStrategy.prototype.detach = function(manual) {
eventNatives.DetachEvent(this.event_.eventName, manual);
};
UnfilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) {
// |ids| is for filtered events only.
return this.event_.listeners;
};
var FilteredAttachmentStrategy = function(event) {
this.event_ = event;
this.listenerMap_ = {};
};
FilteredAttachmentStrategy.idToEventMap = {};
FilteredAttachmentStrategy.prototype.onAddedListener = function(listener) {
var id = eventNatives.AttachFilteredEvent(this.event_.eventName,
listener.filters || {});
if (id == -1)
throw new Error("Can't add listener");
listener.id = id;
this.listenerMap_[id] = listener;
FilteredAttachmentStrategy.idToEventMap[id] = this.event_;
};
FilteredAttachmentStrategy.prototype.onRemovedListener = function(listener) {
this.detachListener(listener, true);
};
FilteredAttachmentStrategy.prototype.detachListener =
function(listener, manual) {
if (listener.id == undefined)
throw new Error("listener.id undefined - '" + listener + "'");
var id = listener.id;
delete this.listenerMap_[id];
delete FilteredAttachmentStrategy.idToEventMap[id];
eventNatives.DetachFilteredEvent(id, manual);
};
FilteredAttachmentStrategy.prototype.detach = function(manual) {
for (var i in this.listenerMap_)
this.detachListener(this.listenerMap_[i], manual);
};
FilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) {
var result = [];
for (var i = 0; i < ids.length; i++)
$Array.push(result, this.listenerMap_[ids[i]]);
return result;
};
function parseEventOptions(opt_eventOptions) {
function merge(dest, src) {
for (var k in src) {
if (!$Object.hasOwnProperty(dest, k)) {
dest[k] = src[k];
}
}
}
var options = opt_eventOptions || {};
merge(options, {
// Event supports adding listeners with filters ("filtered events"), for
// example as used in the webNavigation API.
//
// event.addListener(listener, [filter1, filter2]);
supportsFilters: false,
// Events supports vanilla events. Most APIs use these.
//
// event.addListener(listener);
supportsListeners: true,
// Event supports adding rules ("declarative events") rather than
// listeners, for example as used in the declarativeWebRequest API.
//
// event.addRules([rule1, rule2]);
supportsRules: false,
// Event is unmanaged in that the browser has no knowledge of its
// existence; it's never invoked, doesn't keep the renderer alive, and
// the bindings system has no knowledge of it.
//
// Both events created by user code (new chrome.Event()) and messaging
// events are unmanaged, though in the latter case the browser *does*
// interact indirectly with them via IPCs written by hand.
unmanaged: false,
});
return options;
};
// Event object. If opt_eventName is provided, this object represents
// the unique instance of that named event, and dispatching an event
// with that name will route through this object's listeners. Note that
// opt_eventName is required for events that support rules.
//
// Example:
// var Event = require('event_bindings').Event;
// chrome.tabs.onChanged = new Event("tab-changed");
// chrome.tabs.onChanged.addListener(function(data) { alert(data); });
// Event.dispatch("tab-changed", "hi");
// will result in an alert dialog that says 'hi'.
//
// If opt_eventOptions exists, it is a dictionary that contains the boolean
// entries "supportsListeners" and "supportsRules".
// If opt_webViewInstanceId exists, it is an integer uniquely identifying a
// tag within the embedder. If it does not exist, then this is an
// extension event rather than a event.
var EventImpl = function(opt_eventName, opt_argSchemas, opt_eventOptions,
opt_webViewInstanceId) {
this.eventName = opt_eventName;
this.argSchemas = opt_argSchemas;
this.listeners = [];
this.eventOptions = parseEventOptions(opt_eventOptions);
this.webViewInstanceId = opt_webViewInstanceId || 0;
if (!this.eventName) {
if (this.eventOptions.supportsRules)
throw new Error("Events that support rules require an event name.");
// Events without names cannot be managed by the browser by definition
// (the browser has no way of identifying them).
this.eventOptions.unmanaged = true;
}
// Track whether the event has been destroyed to help track down the cause
// of http://crbug.com/258526.
// This variable will eventually hold the stack trace of the destroy call.
// TODO(kalman): Delete this and replace with more sound logic that catches
// when events are used without being *attached*.
this.destroyed = null;
if (this.eventOptions.unmanaged)
this.attachmentStrategy = new NullAttachmentStrategy(this);
else if (this.eventOptions.supportsFilters)
this.attachmentStrategy = new FilteredAttachmentStrategy(this);
else
this.attachmentStrategy = new UnfilteredAttachmentStrategy(this);
};
// callback is a function(args, dispatch). args are the args we receive from
// dispatchEvent(), and dispatch is a function(args) that dispatches args to
// its listeners.
function registerArgumentMassager(name, callback) {
if (eventArgumentMassagers[name])
throw new Error("Massager already registered for event: " + name);
eventArgumentMassagers[name] = callback;
}
// Dispatches a named event with the given argument array. The args array is
// the list of arguments that will be sent to the event callback.
function dispatchEvent(name, args, filteringInfo) {
var listenerIDs = [];
if (filteringInfo)
listenerIDs = eventNatives.MatchAgainstEventFilter(name, filteringInfo);
var event = attachedNamedEvents[name];
if (!event)
return;
var dispatchArgs = function(args) {
var result = event.dispatch_(args, listenerIDs);
if (result)
logging.DCHECK(!result.validationErrors, result.validationErrors);
return result;
};
if (eventArgumentMassagers[name])
eventArgumentMassagers[name](args, dispatchArgs);
else
dispatchArgs(args);
}
// Registers a callback to be called when this event is dispatched.
EventImpl.prototype.addListener = function(cb, filters) {
if (!this.eventOptions.supportsListeners)
throw new Error("This event does not support listeners.");
if (this.eventOptions.maxListeners &&
this.getListenerCount_() >= this.eventOptions.maxListeners) {
throw new Error("Too many listeners for " + this.eventName);
}
if (filters) {
if (!th
النتائج (
العربية) 1:
[نسخ]نسخ!
(الدالة (تعريف، تتطلب، ريكويريناتيفي، ريكويريسينك، والصادرات، ووحدة التحكم، المجندون، $Array، $Function، $JSON، $Object، $RegExp، $String، $Error) {'الاستخدام الصارم'؛//حقوق الطبع والنشر عام 2014 الكروم المؤلفين. جميع الحقوق محفوظة.ويخضع استخدام هذه التعليمات البرمجية المصدر ترخيص نمط BSD التي يمكن أن تكونوجدت في ملف الترخيص. var اكسسيبتيونهاندلير = require('uncaught_exception_handler')؛ var افينتناتيفيس = requireNative('event_natives')؛ تسجيل فأر = requireNative('logging')؛ var شيماريجيستري = requireNative('schema_registry')؛ var سيندريكويست = require('sendRequest').sendRequest؛ تيلس فأر = require('utils')؛ التحقق من صحة فأر = require('schemaUtils').validate؛ مخططات للمهام على غرار القاعدة بأحداث API التي تحتاج فقط إلى يتم إنشاؤها في بعض الأحيان، لذا ملئهم تكاسل. var روليفونكتيونشيماس = { يتم تعيين هذه القيم تكاسل: أدروليس: {}، جيتروليس: {}، ريموفيروليس: {} }; هذه الدالة ويضمن أن |ruleFunctionSchemas| يتم ملؤها. الدالة ensureRuleSchemasLoaded() { إذا كان (ruleFunctionSchemas.addRules) العودة؛ var افينتسشيما = schemaRegistry.GetSchema("events")؛ eventType فأر = utils.lookup (eventsSchema.types، 'معرف'، ' الأحداث. الحدث ')؛ ruleFunctionSchemas.addRules = utils.lookup (eventType.functions, 'اسم', 'أدروليس')؛ ruleFunctionSchemas.getRules = utils.lookup (eventType.functions, 'اسم', 'جيتروليس')؛ ruleFunctionSchemas.removeRules = utils.lookup (eventType.functions, 'اسم', 'ريموفيروليس')؛ } خريطة لأسماء الحدث لكائن الحدث الذي تم تسجيله بهذا الاسم. var أتتاتشيدناميديفينتس = {}؛ خريطة للوظائف التي تدليك وسيطات الحدث قبل أنها يتم إرسال. المفتاح اسم الحدث، وهو قيمة الدالة. var افينتارجومينتماساجيرس = {}؛ استراتيجية مرفق للأحداث التي لا تعلق على المستعرض. وهذا ينطبق على الأحداث مع خيار "غير المدارة" والأحداث دون أسماء. var نولاتاتشمينتستراتيجي = function(event) { this.event_ = الحدث؛ }; NullAttachmentStrategy.prototype.onAddedListener = function(listener) { }; NullAttachmentStrategy.prototype.onRemovedListener = function(listener) { }; NullAttachmentStrategy.prototype.detach = function(manual) { }; NullAttachmentStrategy.prototype.getListenersByIDs = function(ids) { |ids| من أجل الأحداث التي تم تصفيتها فقط. إرجاع this.event_.listeners؛ }; يتعامل مع إضافة/إزالة/إرسال المستمعين للأحداث لم يتم تصفيتها. var أونفيلتيريداتاتشمينتستراتيجي = function(event) { this.event_ = الحدث؛ }; UnfilteredAttachmentStrategy.prototype.onAddedListener = function(listener) { فقط إرفاق/فصل على الإصغاء الأولى/الأخيرة من إزالتها. إذا كان (this.event_.listeners.length = = 0) eventNatives.AttachEvent(this.event_.eventName)؛ }; UnfilteredAttachmentStrategy.prototype.onRemovedListener = function(listener) { إذا كان (this.event_.listeners.length = = 0) this.detach(true)؛ }; UnfilteredAttachmentStrategy.prototype.detach = function(manual) { eventNatives.DetachEvent (this.event_.eventName، دليل)؛ }; UnfilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) { |ids| من أجل الأحداث التي تم تصفيتها فقط. إرجاع this.event_.listeners؛ }; var فيلتيريداتاتشمينتستراتيجي = function(event) { this.event_ = الحدث؛ this.listenerMap_ = {}؛ }; FilteredAttachmentStrategy.idToEventMap = {}؛ FilteredAttachmentStrategy.prototype.onAddedListener = function(listener) { معرف فأر = eventNatives.AttachFilteredEvent (this.event_.eventName، listener.filters || {}); إذا كان (معرف = =-1) رمي خطأ جديد ("لا يمكن إضافة وحدة إصغاء")؛ listener.id = معرف؛ this.listenerMap_[id] = المستمع؛ FilteredAttachmentStrategy.idToEventMap[id] = this.event_؛ }; FilteredAttachmentStrategy.prototype.onRemovedListener = function(listener) { this.detachListener (المستمع، صحيح)؛ }; FilteredAttachmentStrategy.prototype.detachListener = {الدالة (المستمع، دليل) إذا كان (listener.id = = غير معروف) رمي خطأ جديد ("listener.id غير معروف-'"+ المستمع +"'")؛ معرف فأر = listener.id؛ حذف this.listenerMap_[id؛] حذف FilteredAttachmentStrategy.idToEventMap[id؛] eventNatives.DetachFilteredEvent (معرف، دليل)؛ }; FilteredAttachmentStrategy.prototype.detach = function(manual) { ل (فأر في this.listenerMap_) this.detachListener (this.listenerMap_ [i]، دليل)؛ }; FilteredAttachmentStrategy.prototype.getListenersByIDs = function(ids) { النتيجة فأر = []؛ ل (فأر أنا = 0؛ أنا < ids.length; i + +) $Array.push (نتيجة لذلك، this.listenerMap_[ids[i]])؛ إرجاع النتيجة؛ }; الدالة parseEventOptions(opt_eventOptions) { {دمج (dest، src) الدالة ل {(ك فأر في src) إذا (! $Object.hasOwnProperty (dest، ك)) { dest [ك] = src [ك]؛ } } } خيارات فأر = opt_eventOptions || {}; دمج (خيارات، { الحدث يدعم إضافة المستمعين مع عوامل التصفية ("الأحداث التي تمت تصفيتها")، عن على سبيل المثال المستخدمة في ويبنافيجيشن API. // event.addListener (وحدة إصغاء، [filter1, filter2])؛ سوبورتسفيلتيرس: كاذبة، وتؤيد الأحداث الأحداث الفانيليا. تستخدم معظم واجهات برمجة التطبيقات هذه. // event.addListener(listener)؛ سوبورتسليستينيرس: صحيح، تدعم الحدث إضافة قواعد ("أحداث التعريفي") بدلاً من المستمعين، على سبيل المثال كالمستخدمة في ديكلاراتيفيويبريقويست API. // event.addRules ([rule1، القاعدة 2])؛ سوبورتسروليس: كاذبة، الحدث غير مدارة في هذا المستعرض لا علم له بما وجود؛ فإنه يتم استدعاء ابدأ، لا تبقى على قيد الحياة، في العارض و نظام الربط لا علم له بذلك. // كل الأحداث التي تم إنشاؤها بواسطة التعليمات البرمجية للمستخدم (كروم الجديد. Event()) والمراسلة الأحداث غير المدارة، على الرغم من أن في هذه الحالة المستعرض * يقوم * تفاعل غير مباشر معهم عبر IPCs مكتوبا بخط اليد. غير المدارة: كاذبة، }); إرجاع خيارات؛ }; كائن الحدث. إذا كان يتم توفير opt_eventName، يمثل هذا الكائن مثيل فريدة من نوعها لهذا الحدث المسمى، وإرسال حدث بهذا الاسم سيتم توجيه عن طريق المستمعين لهذا الكائن. لاحظ أنه opt_eventName مطلوب للأحداث التي تدعم النظام. // // Example: // var Event = require('event_bindings').Event; // chrome.tabs.onChanged = new Event("tab-changed"); // chrome.tabs.onChanged.addListener(function(data) { alert(data); }); // Event.dispatch("tab-changed", "hi"); // will result in an alert dialog that says 'hi'. // // If opt_eventOptions exists, it is a dictionary that contains the boolean // entries "supportsListeners" and "supportsRules". // If opt_webViewInstanceId exists, it is an integer uniquely identifying a // tag within the embedder. If it does not exist, then this is an // extension event rather than a event. var EventImpl = function(opt_eventName, opt_argSchemas, opt_eventOptions, opt_webViewInstanceId) { this.eventName = opt_eventName; this.argSchemas = opt_argSchemas; this.listeners = []; this.eventOptions = parseEventOptions(opt_eventOptions); this.webViewInstanceId = opt_webViewInstanceId || 0; if (!this.eventName) { if (this.eventOptions.supportsRules) throw new Error("Events that support rules require an event name."); // Events without names cannot be managed by the browser by definition // (the browser has no way of identifying them). this.eventOptions.unmanaged = true; } // Track whether the event has been destroyed to help track down the cause // of http://crbug.com/258526. // This variable will eventually hold the stack trace of the destroy call. // TODO(kalman): Delete this and replace with more sound logic that catches // when events are used without being *attached*. this.destroyed = null; if (this.eventOptions.unmanaged) this.attachmentStrategy = new NullAttachmentStrategy(this); else if (this.eventOptions.supportsFilters) this.attachmentStrategy = new FilteredAttachmentStrategy(this); else this.attachmentStrategy = new UnfilteredAttachmentStrategy(this); }; // callback is a function(args, dispatch). args are the args we receive from // dispatchEvent(), and dispatch is a function(args) that dispatches args to // its listeners. function registerArgumentMassager(name, callback) { if (eventArgumentMassagers[name]) throw new Error("Massager already registered for event: " + name); eventArgumentMassagers[name] = callback; } // Dispatches a named event with the given argument array. The args array is // the list of arguments that will be sent to the event callback. function dispatchEvent(name, args, filteringInfo) { var listenerIDs = []; if (filteringInfo) listenerIDs = eventNatives.MatchAgainstEventFilter(name, filteringInfo); var event = attachedNamedEvents[name]; if (!event) return; var dispatchArgs = function(args) { var result = event.dispatch_(args, listenerIDs); if (result) logging.DCHECK(!result.validationErrors, result.validationErrors); return result; }; if (eventArgumentMassagers[name]) eventArgumentMassagers[name](args, dispatchArgs); else dispatchArgs(args); } // Registers a callback to be called when this event is dispatched. EventImpl.prototype.addListener = function(cb, filters) { if (!this.eventOptions.supportsListeners) throw new Error("This event does not support listeners."); if (this.eventOptions.maxListeners && this.getListenerCount_() >= this.eventOptions.maxListeners) { throw new Error("Too many listeners for " + this.eventName); } if (filters) { if (!th
يجري ترجمتها، يرجى الانتظار ..
