Doug Davies created SHINDIG-1995:
------------------------------------

             Summary: GadgetSite setModuleId_ only sets the module id if 
rendering the first time
                 Key: SHINDIG-1995
                 URL: https://issues.apache.org/jira/browse/SHINDIG-1995
             Project: Shindig
          Issue Type: Bug
          Components: Javascript 
    Affects Versions: 2.5.2
            Reporter: Doug Davies
             Fix For: 2.5.3


If osapi.container.GadgetSite.prototype.setModuleId_ is called (again) for a 
gadget that already has a cached token, then self.moduleId_ is not set and thus 
0.  This showed up in our container because of the way we re-render gadgets.  
When you delete a gadget, all the other gadgets are re-rendered.  On the 
re-render the moduleId is getting reset to 0.  Here's a simple container that 
shows the issue.  You will need to also modify ModuleIdManagerImpl to return 
the moduleId (instead of 0) to see this behavior.

{code}
  public Long validate(Uri gadgetUri, AuthContext containerAuthContext, Long 
moduleId) {
    return moduleId;
  }
{code}

{code}
<html>
    <body>
        <a href="javascript:add()">add</a>&nbsp;<a 
href="javascript:remove()">remove</a>
        <p/>
        <div id="gadget"/>
    </body>
</html>

<script type="text/javascript" 
src="../../../gadgets/js/container.js?c=1&debug=1&container=default"></script>
<script type="text/javascript">

    var container = new osapi.container.Container({});
    var gadgetXml = 
'https://dl.dropboxusercontent.com/u/445894/gadgets/settitle.xml';
    var gadgetSite = null;

    function remove() {
        if (gadgetSite == null) return;
        container.closeGadget(gadgetSite);
        gadgetSite = null;
    }

    function add() {
        if (gadgetSite != null) return;
        gadgetSite = container.newGadgetSite(document.getElementById("gadget"));
        var renderParams = {};
        renderParams[osapi.container.RenderParam.MODULE_ID] = 1234;
        container.navigateGadget(gadgetSite, gadgetXml, {}, renderParams, 
function(){
            console.log("moduleId=" + gadgetSite.getModuleId());
        });
    }

</script>
{code}

If you modify gadget_site.js to set the moduleId_ in the re-render flow, then 
everything works properly.

{code}
osapi.container.GadgetSite.prototype.setModuleId_ = function(url, mid, 
opt_callback) {
  if (mid && this.moduleId_ != mid) {
    var self = this,
        url = osapi.container.util.buildTokenRequestUrl(url, mid);

    if (!self.service_.getCachedGadgetToken(url)) {
      // We need to request a security token for this gadget instance.
      var request = osapi.container.util.newTokenRequest([url]);
      self.service_.getGadgetToken(request, function(response) {
        var ttl, mid;
        if (response && response[url]) {
          if (ttl = response[url][osapi.container.TokenResponse.TOKEN_TTL]) {
            self.container_.scheduleRefreshTokens_(ttl);
          }
          var mid = response[url][osapi.container.TokenResponse.MODULE_ID];
          if (mid || mid == 0) {
            self.moduleId_ = mid;
          }
        }
        if (opt_callback) {
          opt_callback();
        }
      });
      return;
    }
    self.moduleId_ = mid;   // THIS LINE FIXES IT!!!!
  }
  if (opt_callback) {
    opt_callback();
  }
};
{code}

I will submit a code review momentarily with that patch and we can decide if 
it's the right fix.




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to