| OLD | NEW | 
|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 // Shim that simulates a <adview> tag via Mutation Observers. | 5 // Shim that simulates a <adview> tag via Mutation Observers. | 
| 6 // | 6 // | 
| 7 // The actual tag is implemented via the browser plugin. The internals of this | 7 // The actual tag is implemented via the browser plugin. The internals of this | 
| 8 // are hidden via Shadow DOM. | 8 // are hidden via Shadow DOM. | 
| 9 | 9 | 
| 10 // TODO(rpaquay): This file is currently very similar to "web_view.js". Do we | 10 // TODO(rpaquay): This file is currently very similar to "web_view.js". Do we | 
| 11 //                want to refactor to extract common pieces? | 11 //                want to refactor to extract common pieces? | 
| 12 | 12 | 
| 13 var eventBindings = require('event_bindings'); | 13 var eventBindings = require('event_bindings'); | 
| 14 var process = requireNative('process'); | 14 var process = requireNative('process'); | 
| 15 var addTagWatcher = require('tagWatcher').addTagWatcher; | 15 var addTagWatcher = require('tagWatcher').addTagWatcher; | 
| 16 | 16 | 
| 17 /** | 17 /** | 
| 18  * Define "allowCustomAdNetworks" function such that the |  | 
| 19  * "kEnableAdviewSrcAttribute" flag is respected. |  | 
| 20  */ |  | 
| 21 function allowCustomAdNetworks() { |  | 
| 22   return process.HasSwitch('enable-adview-src-attribute'); |  | 
| 23 } |  | 
| 24 |  | 
| 25 /** |  | 
| 26  * List of attribute names to "blindly" sync between <adview> tag and internal | 18  * List of attribute names to "blindly" sync between <adview> tag and internal | 
| 27  * browser plugin. | 19  * browser plugin. | 
| 28  */ | 20  */ | 
| 29 var AD_VIEW_ATTRIBUTES = [ | 21 var AD_VIEW_ATTRIBUTES = [ | 
| 30   'name', | 22   'name', | 
| 31 ]; | 23 ]; | 
| 32 | 24 | 
| 33 /** | 25 /** | 
| 34  * List of custom attributes (and their behavior). | 26  * List of custom attributes (and their behavior). | 
| 35  * | 27  * | 
| 36  * name: attribute name. | 28  * name: attribute name. | 
| 37  * onMutation(adview, mutation): callback invoked when attribute is mutated. | 29  * onMutation(adview, mutation): callback invoked when attribute is mutated. | 
| 38  * isProperty: True if the attribute should be exposed as a property. | 30  * isProperty: True if the attribute should be exposed as a property. | 
| 39  */ | 31  */ | 
| 40 var AD_VIEW_CUSTOM_ATTRIBUTES = [ | 32 var AD_VIEW_CUSTOM_ATTRIBUTES = [ | 
| 41   { | 33   { | 
| 42     name: 'ad-network', | 34     name: 'ad-network', | 
| 43     onMutation: function(adview, mutation) { | 35     onMutation: function(adview, mutation) { | 
| 44       adview.handleAdNetworkMutation(mutation); | 36       adview.handleAdNetworkMutation(mutation); | 
| 45     }, | 37     }, | 
| 46     isProperty: function() { | 38     isProperty: function() { | 
| 47       return true; | 39       return true; | 
| 48     } | 40     } | 
| 49   }, |  | 
| 50   { |  | 
| 51     name: 'src', |  | 
| 52     onMutation: function(adview, mutation) { |  | 
| 53       adview.handleSrcMutation(mutation); |  | 
| 54     }, |  | 
| 55     isProperty: function() { |  | 
| 56       return allowCustomAdNetworks(); |  | 
| 57     } |  | 
| 58   } | 41   } | 
| 59 ]; | 42 ]; | 
| 60 | 43 | 
| 61 /** | 44 /** | 
| 62  * List of api methods. These are forwarded to the browser plugin. | 45  * List of api methods. These are forwarded to the browser plugin. | 
| 63  */ | 46  */ | 
| 64 var AD_VIEW_API_METHODS = [ | 47 var AD_VIEW_API_METHODS = [ | 
| 65  // Empty for now. | 48  // Empty for now. | 
| 66 ]; | 49 ]; | 
| 67 | 50 | 
| 68 var createEvent = function(name) { | 51 var createEvent = function(name) { | 
| 69   var eventOpts = {supportsListeners: true, supportsFilters: true}; | 52   var eventOpts = {supportsListeners: true, supportsFilters: true}; | 
| 70   return new eventBindings.Event(name, undefined, eventOpts); | 53   return new eventBindings.Event(name, undefined, eventOpts); | 
| 71 }; | 54 }; | 
| 72 | 55 | 
| 73 var AdviewLoadAbortEvent = createEvent('adview.onLoadAbort'); | 56 var AdviewLoadAbortEvent = createEvent('adview.onLoadAbort'); | 
| 74 var AdviewLoadCommitEvent = createEvent('adview.onLoadCommit'); | 57 var AdviewLoadCommitEvent = createEvent('adview.onLoadCommit'); | 
| 75 | 58 | 
| 76 var AD_VIEW_EXT_EVENTS = { | 59 var AD_VIEW_EXT_EVENTS = { | 
| 77   'loadabort': { | 60   'loadabort': { | 
| 78     evt: AdviewLoadAbortEvent, | 61     evt: AdviewLoadAbortEvent, | 
| 79     fields: ['url', 'isTopLevel', 'reason'] | 62     fields: ['url', 'isTopLevel', 'reason'] | 
| 80   }, | 63   }, | 
| 81   'loadcommit': { | 64   'loadcommit': { | 
| 82     customHandler: function(adview, event) { |  | 
| 83       if (event.isTopLevel) { |  | 
| 84         adview.browserPluginNode_.setAttribute('src', event.url); |  | 
| 85       } |  | 
| 86     }, |  | 
| 87     evt: AdviewLoadCommitEvent, | 65     evt: AdviewLoadCommitEvent, | 
| 88     fields: ['url', 'isTopLevel'] | 66     fields: ['url', 'isTopLevel'] | 
| 89   } | 67   } | 
| 90 }; | 68 }; | 
| 91 | 69 | 
| 92 /** | 70 /** | 
| 93  * List of supported ad-networks. | 71  * List of supported ad-networks. | 
| 94  * | 72  * | 
| 95  * name: identifier of the ad-network, corresponding to a valid value | 73  * name: identifier of the ad-network, corresponding to a valid value | 
| 96  *       of the "ad-network" attribute of an <adview> element. | 74  *       of the "ad-network" attribute of an <adview> element. | 
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 337     // if the BrowserPlugin attributes differs from it. | 315     // if the BrowserPlugin attributes differs from it. | 
| 338     var oldValue = this.adviewNode_.getAttribute(mutation.attributeName); | 316     var oldValue = this.adviewNode_.getAttribute(mutation.attributeName); | 
| 339     var newValue = this.browserPluginNode_.getAttribute(mutation.attributeName); | 317     var newValue = this.browserPluginNode_.getAttribute(mutation.attributeName); | 
| 340     if (newValue != oldValue) { | 318     if (newValue != oldValue) { | 
| 341       this.adviewNode_.setAttribute(mutation.attributeName, newValue); | 319       this.adviewNode_.setAttribute(mutation.attributeName, newValue); | 
| 342     } | 320     } | 
| 343   } | 321   } | 
| 344 }; | 322 }; | 
| 345 | 323 | 
| 346 /** | 324 /** | 
| 347  * @private |  | 
| 348  */ |  | 
| 349 AdView.prototype.navigateToUrl_ = function(url) { |  | 
| 350   var newValue = url; |  | 
| 351   var oldValue = this.browserPluginNode_.getAttribute('src'); |  | 
| 352 |  | 
| 353   if (newValue === oldValue) |  | 
| 354     return; |  | 
| 355 |  | 
| 356   if (url != null) { |  | 
| 357     // Note: Setting the 'src' property directly, as calling setAttribute has no |  | 
| 358     // effect due to implementation details of BrowserPlugin. |  | 
| 359     this.browserPluginNode_['src'] = url; |  | 
| 360     if (allowCustomAdNetworks()) { |  | 
| 361       this.adviewNode_.setAttribute('src', url); |  | 
| 362     } |  | 
| 363   } |  | 
| 364   else { |  | 
| 365     // Note: Setting the 'src' property directly, as calling setAttribute has no |  | 
| 366     // effect due to implementation details of BrowserPlugin. |  | 
| 367     // TODO(rpaquay): Due to another implementation detail of BrowserPlugin, |  | 
| 368     // this line will leave the "src" attribute value untouched. |  | 
| 369     this.browserPluginNode_['src'] = null; |  | 
| 370     if (allowCustomAdNetworks()) { |  | 
| 371       this.adviewNode_.removeAttribute('src'); |  | 
| 372     } |  | 
| 373   } |  | 
| 374 } |  | 
| 375 |  | 
| 376 /** |  | 
| 377  * @public | 325  * @public | 
| 378  */ | 326  */ | 
| 379 AdView.prototype.handleAdNetworkMutation = function(mutation) { | 327 AdView.prototype.handleAdNetworkMutation = function(mutation) { | 
| 380   if (this.adviewNode_.hasAttribute('ad-network')) { | 328   if (this.adviewNode_.hasAttribute('ad-network')) { | 
| 381     var value = this.adviewNode_.getAttribute('ad-network'); | 329     var value = this.adviewNode_.getAttribute('ad-network'); | 
| 382     var item = getAdNetworkInfo(value); | 330     var item = getAdNetworkInfo(value); | 
| 383     if (item) { | 331     if (!item) { | 
| 384       this.navigateToUrl_(item.url); |  | 
| 385     } |  | 
| 386     else if (allowCustomAdNetworks()) { |  | 
| 387       console.log('The ad-network "' + value + '" is not recognized, ' + |  | 
| 388         'but custom ad-networks are enabled.'); |  | 
| 389 |  | 
| 390       if (mutation) { |  | 
| 391         this.navigateToUrl_(''); |  | 
| 392       } |  | 
| 393     } |  | 
| 394     else { |  | 
| 395       // Ignore the new attribute value and set it to empty string. | 332       // Ignore the new attribute value and set it to empty string. | 
| 396       // Avoid infinite loop by checking for empty string as new value. | 333       // Avoid infinite loop by checking for empty string as new value. | 
| 397       if (value != '') { | 334       if (value != '') { | 
| 398         console.error('The ad-network "' + value + '" is not recognized.'); | 335         console.error('The ad-network "' + value + '" is not recognized.'); | 
| 399         this.adviewNode_.setAttribute('ad-network', ''); | 336         this.adviewNode_.setAttribute('ad-network', ''); | 
| 400       } | 337       } | 
| 401       this.navigateToUrl_(''); |  | 
| 402     } |  | 
| 403   } |  | 
| 404   else { |  | 
| 405     this.navigateToUrl_(''); |  | 
| 406   } |  | 
| 407 } |  | 
| 408 |  | 
| 409 /** |  | 
| 410  * @public |  | 
| 411  */ |  | 
| 412 AdView.prototype.handleSrcMutation = function(mutation) { |  | 
| 413   if (allowCustomAdNetworks()) { |  | 
| 414     if (this.adviewNode_.hasAttribute('src')) { |  | 
| 415       var newValue = this.adviewNode_.getAttribute('src'); |  | 
| 416       // Note: Setting the 'src' property directly, as calling setAttribute has |  | 
| 417       // no effect due to implementation details of BrowserPlugin. |  | 
| 418       this.browserPluginNode_['src'] = newValue; |  | 
| 419     } |  | 
| 420     else { |  | 
| 421       // If an attribute is removed from the <adview>, then remove it |  | 
| 422       // from the BrowserPlugin as well. |  | 
| 423       // Note: Setting the 'src' property directly, as calling setAttribute has |  | 
| 424       // no effect due to implementation details of BrowserPlugin. |  | 
| 425       // TODO(rpaquay): Due to another implementation detail of BrowserPlugin, |  | 
| 426       // this line will leave the "src" attribute value untouched. |  | 
| 427       this.browserPluginNode_['src'] = null; |  | 
| 428     } |  | 
| 429   } |  | 
| 430   else { |  | 
| 431     if (this.adviewNode_.hasAttribute('src')) { |  | 
| 432       var value = this.adviewNode_.getAttribute('src'); |  | 
| 433       // Ignore the new attribute value and set it to empty string. |  | 
| 434       // Avoid infinite loop by checking for empty string as new value. |  | 
| 435       if (value != '') { |  | 
| 436         console.error('Setting the "src" attribute of an <adview> ' + |  | 
| 437           'element is not supported.  Use the "ad-network" attribute ' + |  | 
| 438           'instead.'); |  | 
| 439         this.adviewNode_.setAttribute('src', ''); |  | 
| 440       } |  | 
| 441     } | 338     } | 
| 442   } | 339   } | 
| 443 } | 340 } | 
| 444 | 341 | 
| 445 /** | 342 /** | 
| 446  * @private | 343  * @private | 
| 447  */ | 344  */ | 
| 448 AdView.prototype.setupAdviewNodeEvents_ = function() { | 345 AdView.prototype.setupAdviewNodeEvents_ = function() { | 
| 449   var self = this; | 346   var self = this; | 
| 450   var onInstanceIdAllocated = function(e) { | 347   var onInstanceIdAllocated = function(e) { | 
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 489   var evt = new Event(eventname, { bubbles: true }); | 386   var evt = new Event(eventname, { bubbles: true }); | 
| 490   for(var item in detail) { | 387   for(var item in detail) { | 
| 491       evt[item] = detail[item]; | 388       evt[item] = detail[item]; | 
| 492   } | 389   } | 
| 493 | 390 | 
| 494   // Dispatch event. | 391   // Dispatch event. | 
| 495   this.adviewNode_.dispatchEvent(evt); | 392   this.adviewNode_.dispatchEvent(evt); | 
| 496 } | 393 } | 
| 497 | 394 | 
| 498 addTagWatcher('ADVIEW', function(addedNode) { new AdView(addedNode); }); | 395 addTagWatcher('ADVIEW', function(addedNode) { new AdView(addedNode); }); | 
| OLD | NEW | 
|---|