Uploaded image for project: 'Shindig'
  1. Shindig
  2. SHINDIG-1995

GadgetSite setModuleId_ only sets the module id if rendering the first time

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.5.2
    • 2.5.3
    • Javascript
    • None

    Description

      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.

        public Long validate(Uri gadgetUri, AuthContext containerAuthContext, Long moduleId) {
          return moduleId;
        }
      
      <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>
      

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

      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();
        }
      };
      

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

      Attachments

        Activity

          People

            Unassigned Unassigned
            csddavies Doug Davies
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: