DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d1ed7de67f5a)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;

this.EXPORTED_SYMBOLS = ["InterAppCommService"];

Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/AppsUtils.jsm");

const DEBUG = false;
function debug(aMsg) {
  dump("-- InterAppCommService: " + Date.now() + ": " + aMsg + "\n");
}

XPCOMUtils.defineLazyServiceGetter(this, "appsService",
                                   "@mozilla.org/AppsService;1",
                                   "nsIAppsService");

XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
                                   "@mozilla.org/parentprocessmessagemanager;1",
                                   "nsIMessageBroadcaster");

XPCOMUtils.defineLazyServiceGetter(this, "UUIDGenerator",
                                   "@mozilla.org/uuid-generator;1",
                                   "nsIUUIDGenerator");

XPCOMUtils.defineLazyServiceGetter(this, "messenger",
                                   "@mozilla.org/system-message-internal;1",
                                   "nsISystemMessagesInternal");

const kMessages =["Webapps:Connect",
                  "Webapps:GetConnections",
                  "InterAppConnection:Cancel",
                  "InterAppMessagePort:PostMessage",
                  "InterAppMessagePort:Register",
                  "InterAppMessagePort:Unregister",
                  "child-process-shutdown"];

/**
 * This module contains helpers for Inter-App Communication API [1] related
 * purposes, which plays the role of the central service receiving messages
 * from and interacting with the content processes.
 *
 * [1] https://wiki.mozilla.org/WebAPI/Inter_App_Communication_Alt_proposal
 */

this.InterAppCommService = {
  init: function() {
    Services.obs.addObserver(this, "xpcom-shutdown", false);
    Services.obs.addObserver(this, "webapps-clear-data", false);

    kMessages.forEach(function(aMsg) {
      ppmm.addMessageListener(aMsg, this);
    }, this);

    // This matrix is used for saving the inter-app connection info registered in
    // the app manifest. The object literal is defined as below:
    //
    // {
    //   "keyword1": {
    //     "subAppManifestURL1": {
    //       /* subscribed info */
    //     },
    //     "subAppManifestURL2": {
    //       /* subscribed info */
    //     },
    //     ...
    //   },
    //   "keyword2": {
    //     "subAppManifestURL3": {
    //       /* subscribed info */
    //     },
    //     ...
    //   },
    //   ...
    // }
    //
    // For example:
    //
    // {
    //   "foo": {
    //     "app://subApp1.gaiamobile.org/manifest.webapp": {
    //       pageURL: "app://subApp1.gaiamobile.org/handler.html",
    //       description: "blah blah",
    //       rules: { ... }
    //     },
    //     "app://subApp2.gaiamobile.org/manifest.webapp": {
    //       pageURL: "app://subApp2.gaiamobile.org/handler.html",
    //       description: "blah blah",
    //       rules: { ... }
    //     }
    //   },
    //   "bar": {
    //     "app://subApp3.gaiamobile.org/manifest.webapp": {
    //       pageURL: "app://subApp3.gaiamobile.org/handler.html",
    //       description: "blah blah",
    //       rules: { ... }
    //     }
    //   }
    // }
    //
    // TODO Bug 908999 - Update registered connections when app gets uninstalled.
    this._registeredConnections = {};

    // This matrix is used for saving the permitted connections, which allows
    // the messaging between publishers and subscribers. The object literal is
    // defined as below:
    //
    // {
    //   "keyword1": {
    //     "pubAppManifestURL1": [
    //       "subAppManifestURL1",
    //       "subAppManifestURL2",
    //       ...
    //     ],
    //     "pubAppManifestURL2": [
    //       "subAppManifestURL3",
    //       "subAppManifestURL4",
    //       ...
    //     ],
    //     ...
    //   },
    //   "keyword2": {
    //     "pubAppManifestURL3": [
    //       "subAppManifestURL5",
    //       ...
    //     ],
    //     ...
    //   },
    //   ...
    // }
    //
    // For example:
    //
    // {
    //   "foo": {
    //     "app://pubApp1.gaiamobile.org/manifest.webapp": [
    //       "app://subApp1.gaiamobile.org/manifest.webapp",
    //       "app://subApp2.gaiamobile.org/manifest.webapp"
    //     ],
    //     "app://pubApp2.gaiamobile.org/manifest.webapp": [
    //       "app://subApp3.gaiamobile.org/manifest.webapp",
    //       "app://subApp4.gaiamobile.org/manifest.webapp"
    //     ]
    //   },
    //   "bar": {
    //     "app://pubApp3.gaiamobile.org/manifest.webapp": [
    //       "app://subApp5.gaiamobile.org/manifest.webapp",
    //     ]
    //   }
    // }
    //
    // TODO Bug 908999 - Update allowed connections when app gets uninstalled.
    this._allowedConnections = {};

    // This matrix is used for saving the caller info from the content process,
    // which is indexed by a random UUID, to know where to return the promise
    // resolvser's callback when the prompt UI for allowing connections returns.
    // An example of the object literal is shown as below:
    //
    // {
    //   "fooID": {
    //     outerWindowID: 12,
    //     requestID: 34,
    //     target: pubAppTarget1
    //   },
    //   "barID": {
    //     outerWindowID: 56,
    //     requestID: 78,
    //     target: pubAppTarget2
    //   }
    // }
    //
    // where |outerWindowID| is the ID of the window requesting the connection,
    //       |requestID| is the ID specifying the promise resolver to return,
    //       |target| is the target of the process requesting the connection.
    this._promptUICallers = {};

    // This matrix is used for saving the pair of message ports, which is indexed
    // by a random UUID, so that each port can know whom it should talk to.
    // An example of the object literal is shown as below:
    //
    // {
    //   "UUID1": {
    //     keyword: "keyword1",
    //     publisher: {
    //       manifestURL: "app://pubApp1.gaiamobile.org/manifest.webapp",
    //       target: pubAppTarget1,
    //       pageURL: "app://pubApp1.gaiamobile.org/caller.html",
    //       messageQueue: [...]
    //     },
    //     subscriber: {
    //       manifestURL: "app://subApp1.gaiamobile.org/manifest.webapp",
    //       target: subAppTarget1,
    //       pageURL: "app://pubApp1.gaiamobile.org/handler.html",
    //       messageQueue: [...]
    //     }
    //   },
    //   "UUID2": {
    //     keyword: "keyword2",
    //     publisher: {
    //       manifestURL: "app://pubApp2.gaiamobile.org/manifest.webapp",
    //       target: pubAppTarget2,
    //       pageURL: "app://pubApp2.gaiamobile.org/caller.html",
    //       messageQueue: [...]
    //     },
    //     subscriber: {
    //       manifestURL: "app://subApp2.gaiamobile.org/manifest.webapp",
    //       target: subAppTarget2,
    //       pageURL: "app://pubApp2.gaiamobile.org/handler.html",
    //       messageQueue: [...]
    //     }
    //   }
    // }
    this._messagePortPairs = {};
  },

  /**
   * Registration of a page that wants to be connected to other apps through
   * the Inter-App Communication API.
   *
   * @param aKeyword        The connection's keyword.
   * @param aHandlerPageURI The URI of the handler's page.
   * @param aManifestURI    The webapp's manifest URI.
   * @param aDescription    The connection's description.
   * @param aRules          The connection's rules.
   */
  registerConnection: function(aKeyword, aHandlerPageURI, aManifestURI,
                               aDescription, aRules) {
    let manifestURL = aManifestURI.spec;
    let pageURL = aHandlerPageURI.spec;

    if (DEBUG) {
      debug("registerConnection: aKeyword: " + aKeyword +
            " manifestURL: " + manifestURL + " pageURL: " + pageURL +
            " aDescription: " + aDescription +
            " aRules.minimumAccessLevel: " + aRules.minimumAccessLevel +
            " aRules.manifestURLs: " + aRules.manifestURLs +
            " aRules.installOrigins: " + aRules.installOrigins);
    }

    let subAppManifestURLs = this._registeredConnections[aKeyword];
    if (!subAppManifestURLs) {
      subAppManifestURLs = this._registeredConnections[aKeyword] = {};
    }

    subAppManifestURLs[manifestURL] = {
      pageURL: pageURL,
      description: aDescription,
      rules: aRules,
      manifestURL: manifestURL
    };
  },

  _matchMinimumAccessLevel: function(aRules, aAppStatus) {
    if (!aRules || !aRules.minimumAccessLevel) {
      if (DEBUG) {
        debug("rules.minimumAccessLevel is not available. No need to match.");
      }
      return true;
    }

    let minAccessLevel = aRules.minimumAccessLevel;
    switch (minAccessLevel) {
      case "web":
        if (aAppStatus == Ci.nsIPrincipal.APP_STATUS_INSTALLED ||
            aAppStatus == Ci.nsIPrincipal.APP_STATUS_PRIVILEGED ||
            aAppStatus == Ci.nsIPrincipal.APP_STATUS_CERTIFIED) {
          return true;
        }
        break;
      case "privileged":
        if (aAppStatus == Ci.nsIPrincipal.APP_STATUS_PRIVILEGED ||
            aAppStatus == Ci.nsIPrincipal.APP_STATUS_CERTIFIED) {
          return true;
        }
        break;
      case "certified":
        if (aAppStatus == Ci.nsIPrincipal.APP_STATUS_CERTIFIED) {
          return true;
        }
        break;
    }

    if (DEBUG) {
      debug("rules.minimumAccessLevel is not matched!" +
            " minAccessLevel: " + minAccessLevel +
            " aAppStatus : " + aAppStatus);
    }
    return false;
  },

  _matchManifestURLs: function(aRules, aManifestURL) {
    if (!aRules || !Array.isArray(aRules.manifestURLs)) {
      if (DEBUG) {
        debug("rules.manifestURLs is not available. No need to match.");
      }
      return true;
    }

    let manifestURLs = aRules.manifestURLs;
    if (manifestURLs.indexOf(aManifestURL) != -1) {
      return true;
    }

    if (DEBUG) {
      debug("rules.manifestURLs is not matched!" +
            " manifestURLs: " + manifestURLs +
            " aManifestURL : " + aManifestURL);
    }
    return false;
  },

  _matchInstallOrigins: function(aRules, aInstallOrigin) {
    if (!aRules || !Array.isArray(aRules.installOrigins)) {
      if (DEBUG) {
        debug("rules.installOrigins is not available. No need to match.");
      }
      return true;
    }

    let installOrigins = aRules.installOrigins;
    if (installOrigins.indexOf(aInstallOrigin) != -1) {
      return true;
    }

    if (DEBUG) {
      debug("rules.installOrigins is not matched!" +
            " installOrigins: " + installOrigins +
            " installOrigin : " + aInstallOrigin);
    }
    return false;
  },

  _matchRules: function(aPubAppManifestURL, aPubRules,
                        aSubAppManifestURL, aSubRules) {
    let pubApp = appsService.getAppByManifestURL(aPubAppManifestURL);
    let subApp = appsService.getAppByManifestURL(aSubAppManifestURL);

    let isPubAppCertified =
      (pubApp.appStatus == Ci.nsIPrincipal.APP_STATUS_CERTIFIED);

    let isSubAppCertified =
      (subApp.appStatus == Ci.nsIPrincipal.APP_STATUS_CERTIFIED);

    // TODO Bug 907068 In the initiative step, we only expose this API to
    // certified apps to meet the time line. Eventually, we need to make
    // it available for the non-certified apps as well. For now, only the
    // certified apps can match the rules.
    if (!isPubAppCertified || !isSubAppCertified) {
      if (DEBUG) {
        debug("Only certified apps are allowed to do connections.");
      }
      return false;
    }

    if (!aPubRules && !aSubRules) {
      if (DEBUG) {
        debug("No rules for publisher and subscriber. No need to match.");
      }
      return true;
    }

    // Check minimumAccessLevel.
    if (!this._matchMinimumAccessLevel(aPubRules, subApp.appStatus) ||
        !this._matchMinimumAccessLevel(aSubRules, pubApp.appStatus)) {
      return false;
    }

    // Check manifestURLs.
    if (!this._matchManifestURLs(aPubRules, aSubAppManifestURL) ||
        !this._matchManifestURLs(aSubRules, aPubAppManifestURL)) {
      return false;
    }

    // Check installOrigins. Note that we only check the install origin for the
    // non-certified app, because the certified app doesn't have install origin.
    if ((!isSubAppCertified &&
         !this._matchInstallOrigins(aPubRules, subApp.installOrigin)) ||
        (!isPubAppCertified &&
         !this._matchInstallOrigins(aSubRules, pubApp.installOrigin))) {
      return false;
    }

    if (DEBUG) debug("All rules are matched.");
    return true;
  },

  _dispatchMessagePorts: function(aKeyword, aPubAppManifestURL,
                                  aAllowedSubAppManifestURLs,
                                  aTarget, aOuterWindowID, aRequestID) {
    if (DEBUG) {
      debug("_dispatchMessagePorts: aKeyword: " + aKeyword +
            " aPubAppManifestURL: " + aPubAppManifestURL +
            " aAllowedSubAppManifestURLs: " + aAllowedSubAppManifestURLs);
    }

    if (aAllowedSubAppManifestURLs.length == 0) {
      if (DEBUG) debug("No apps are allowed to connect. Returning.");
      aTarget.sendAsyncMessage("Webapps:Connect:Return:KO",
                               { oid: aOuterWindowID, requestID: aRequestID });
      return;
    }

    let subAppManifestURLs = this._registeredConnections[aKeyword];
    if (!subAppManifestURLs) {
      if (DEBUG) debug("No apps are subscribed to connect. Returning.");
      aTarget.sendAsyncMessage("Webapps:Connect:Return:KO",
                               { oid: aOuterWindowID, requestID: aRequestID });
      return;
    }

    let messagePortIDs = [];
    aAllowedSubAppManifestURLs.forEach(function(aAllowedSubAppManifestURL) {
      let subscribedInfo = subAppManifestURLs[aAllowedSubAppManifestURL];
      if (!subscribedInfo) {
        if (DEBUG) {
          debug("The sunscribed info is not available. Skipping: " +
                aAllowedSubAppManifestURL);
        }
        return;
      }

      // The message port ID is aimed for identifying the coupling targets
      // to deliver messages with each other. This ID is centrally generated
      // by the parent and dispatched to both the sender and receiver ends
      // for creating their own message ports respectively.
      let messagePortID = UUIDGenerator.generateUUID().toString();
      this._messagePortPairs[messagePortID] = {
        keyword: aKeyword,
        publisher: {
          manifestURL: aPubAppManifestURL
        },
        subscriber: {
          manifestURL: aAllowedSubAppManifestURL
        }
      };

      // Fire system message to deliver the message port to the subscriber.
      messenger.sendMessage("connection",
        { keyword: aKeyword,
          messagePortID: messagePortID },
        Services.io.newURI(subscribedInfo.pageURL, null, null),
        Services.io.newURI(subscribedInfo.manifestURL, null, null));

      messagePortIDs.push(messagePortID);
    }, this);

    if (messagePortIDs.length == 0) {
      if (DEBUG) debug("No apps are subscribed to connect. Returning.");
      aTarget.sendAsyncMessage("Webapps:Connect:Return:KO",
                               { oid: aOuterWindowID, requestID: aRequestID });
      return;
    }

    // Return the message port IDs to open the message ports for the publisher.
    if (DEBUG) debug("messagePortIDs: " + messagePortIDs);
    aTarget.sendAsyncMessage("Webapps:Connect:Return:OK",
                             { keyword: aKeyword,
                               messagePortIDs: messagePortIDs,
                               oid: aOuterWindowID, requestID: aRequestID });
  },

  /**
   * Fetch the subscribers that are currently allowed to connect.
   *
   * @param aKeyword           The connection's keyword.
   * @param aPubAppManifestURL The manifest URL of the publisher.
   *
   * @param return an array of manifest URLs of the subscribers.
   */
  _getAllowedSubAppManifestURLs: function(aKeyword, aPubAppManifestURL) {
    let allowedPubAppManifestURLs = this._allowedConnections[aKeyword];
    if (!allowedPubAppManifestURLs) {
      return [];
    }

    let allowedSubAppManifestURLs =
      allowedPubAppManifestURLs[aPubAppManifestURL];
    if (!allowedSubAppManifestURLs) {
      return [];
    }

    return allowedSubAppManifestURLs;
  },

  /**
   * Add the newly selected apps into the allowed connections and return the
   * aggregated allowed connections.
   *
   * @param aKeyword           The connection's keyword.
   * @param aPubAppManifestURL The manifest URL of the publisher.
   * @param aSelectedApps      An array of the subscribers' information.
   *
   * @param return an array of manifest URLs of the subscribers.
   */
  _addSelectedApps: function(aKeyword, aPubAppManifestURL, aSelectedApps) {
    let allowedPubAppManifestURLs = this._allowedConnections[aKeyword];

    // Add a new entry for |aKeyword|.
    if (!allowedPubAppManifestURLs) {
      allowedPubAppManifestURLs = this._allowedConnections[aKeyword] = {};
    }

    let allowedSubAppManifestURLs =
      allowedPubAppManifestURLs[aPubAppManifestURL];

    // Add a new entry for |aPubAppManifestURL|.
    if (!allowedSubAppManifestURLs) {
      allowedSubAppManifestURLs =
        allowedPubAppManifestURLs[aPubAppManifestURL] = [];
    }

    // Add the selected apps into the existing set of allowed connections.
    aSelectedApps.forEach(function(aSelectedApp) {
      let allowedSubAppManifestURL = aSelectedApp.manifestURL;
      if (allowedSubAppManifestURLs.indexOf(allowedSubAppManifestURL) == -1) {
        allowedSubAppManifestURLs.push(allowedSubAppManifestURL);
      }
    });

    return allowedSubAppManifestURLs;
  },

  _connect: function(aMessage, aTarget) {
    let keyword = aMessage.keyword;
    let pubRules = aMessage.rules;
    let pubAppManifestURL = aMessage.manifestURL;
    let outerWindowID = aMessage.outerWindowID;
    let requestID = aMessage.requestID;

    let subAppManifestURLs = this._registeredConnections[keyword];
    if (!subAppManifestURLs) {
      if (DEBUG) {
        debug("No apps are subscribed for this connection. Returning.");
      }
      this._dispatchMessagePorts(keyword, pubAppManifestURL, [],
                                 aTarget, outerWindowID, requestID);
      return;
    }

    // Fetch the apps that are currently allowed to connect, so that users
    // don't need to select/allow them again, which means we only pop up the
    // prompt UI for the *new* connections.
    let allowedSubAppManifestURLs =
      this._getAllowedSubAppManifestURLs(keyword, pubAppManifestURL);

    // Check rules to see if a subscribed app is allowed to connect.
    let appsToSelect = [];
    for (let subAppManifestURL in subAppManifestURLs) {
      if (allowedSubAppManifestURLs.indexOf(subAppManifestURL) != -1) {
        if (DEBUG) {
          debug("Don't need to select again. Skipping: " + subAppManifestURL);
        }
        continue;
      }

      // Only rule-matched publishers/subscribers are allowed to connect.
      let subscribedInfo = subAppManifestURLs[subAppManifestURL];
      let subRules = subscribedInfo.rules;

      let matched =
        this._matchRules(pubAppManifestURL, pubRules,
                         subAppManifestURL, subRules);
      if (!matched) {
        if (DEBUG) {
          debug("Rules are not matched. Skipping: " + subAppManifestURL);
        }
        continue;
      }

      appsToSelect.push({
        manifestURL: subAppManifestURL,
        description: subscribedInfo.description
      });
    }

    if (appsToSelect.length == 0) {
      if (DEBUG) {
        debug("No additional apps need to be selected for this connection. " +
              "Just dispatch message ports for the existing connections.");
      }

      this._dispatchMessagePorts(keyword, pubAppManifestURL,
                                 allowedSubAppManifestURLs,
                                 aTarget, outerWindowID, requestID);
      return;
    }

    // Remember the caller info with an UUID so that we can know where to
    // return the promise resolver's callback when the prompt UI returns.
    let callerID = UUIDGenerator.generateUUID().toString();
    this._promptUICallers[callerID] = {
      outerWindowID: outerWindowID,
      requestID: requestID,
      target: aTarget
    };

    let glue = Cc["@mozilla.org/dom/apps/inter-app-comm-ui-glue;1"]
                 .createInstance(Ci.nsIInterAppCommUIGlue);
    if (glue) {
      glue.selectApps(callerID, pubAppManifestURL, keyword, appsToSelect).then(
        function(aData) {
          this._handleSelectedApps(aData);
        }.bind(this),
        function(aError) {
          if (DEBUG) {
            debug("Error occurred in the UI glue component. " + aError)
          }

          // Resolve the caller as if there were no selected apps.
          this._handleSelectedApps({ callerID: callerID,
                                     keyword: keyword,
                                     manifestURL: pubAppManifestURL,
                                     selectedApps: [] });
        }.bind(this)
      );
    } else {
      if (DEBUG) {
        debug("Error! The UI glue component is not implemented.")
      }

      // Resolve the caller as if there were no selected apps.
      this._handleSelectedApps({ callerID: callerID,
                                 keyword: keyword,
                                 manifestURL: pubAppManifestURL,
                                 selectedApps: [] });
    }
  },

  _getConnections: function(aMessage, aTarget) {
    let outerWindowID = aMessage.outerWindowID;
    let requestID = aMessage.requestID;

    let connections = [];
    for (let keyword in this._allowedConnections) {
      let allowedPubAppManifestURLs = this._allowedConnections[keyword];
      for (let allowedPubAppManifestURL in allowedPubAppManifestURLs) {
        let allowedSubAppManifestURLs =
          allowedPubAppManifestURLs[allowedPubAppManifestURL];
        allowedSubAppManifestURLs.forEach(function(allowedSubAppManifestURL) {
          connections.push({ keyword: keyword,
                             pubAppManifestURL: allowedPubAppManifestURL,
                             subAppManifestURL: allowedSubAppManifestURL });
        });
      }
    }

    aTarget.sendAsyncMessage("Webapps:GetConnections:Return:OK",
                             { connections: connections,
                               oid: outerWindowID, requestID: requestID });
  },

  _cancelConnection: function(aMessage) {
    let keyword = aMessage.keyword;
    let pubAppManifestURL = aMessage.pubAppManifestURL;
    let subAppManifestURL = aMessage.subAppManifestURL;

    let allowedPubAppManifestURLs = this._allowedConnections[keyword];
    if (!allowedPubAppManifestURLs) {
      if (DEBUG) debug("keyword is not found: " + keyword);
      return;
    }

    let allowedSubAppManifestURLs =
      allowedPubAppManifestURLs[pubAppManifestURL];
    if (!allowedSubAppManifestURLs) {
      if (DEBUG) debug("publisher is not found: " + pubAppManifestURL);
      return;
    }

    let index = allowedSubAppManifestURLs.indexOf(subAppManifestURL);
    if (index == -1) {
      if (DEBUG) debug("subscriber is not found: " + subAppManifestURL);
      return;
    }

    if (DEBUG) debug("Cancelling the connection.");
    allowedSubAppManifestURLs.splice(index, 1);

    // Clean up the parent entries if needed.
    if (allowedSubAppManifestURLs.length == 0) {
      delete allowedPubAppManifestURLs[pubAppManifestURL];
      if (Object.keys(allowedPubAppManifestURLs).length == 0) {
        delete this._allowedConnections[keyword];
      }
    }

    if (DEBUG) debug("Unregistering message ports based on this connection.");
    let messagePortIDs = [];
    for (let messagePortID in this._messagePortPairs) {
      let pair = this._messagePortPairs[messagePortID];
      if (pair.keyword == keyword &&
          pair.publisher.manifestURL == pubAppManifestURL &&
          pair.subscriber.manifestURL == subAppManifestURL) {
        messagePortIDs.push(messagePortID);
      }
    }
    messagePortIDs.forEach(function(aMessagePortID) {
      delete this._messagePortPairs[aMessagePortID];
    }, this);
  },

  _identifyMessagePort: function(aMessagePortID, aManifestURL) {
    let pair = this._messagePortPairs[aMessagePortID];
    if (!pair) {
      if (DEBUG) {
        debug("Error! The message port ID is invalid: " + aMessagePortID +
              ", which should have been generated by parent.");
      }
      return null;
    }

    // Check it the message port is for publisher.
    if (pair.publisher.manifestURL == aManifestURL) {
      return { pair: pair, isPublisher: true };
    }

    // Check it the message port is for subscriber.
    if (pair.subscriber.manifestURL == aManifestURL) {
      return { pair: pair, isPublisher: false };
    }

    if (DEBUG) {
      debug("Error! The manifest URL is invalid: " + aManifestURL +
            ", which might be a hacked app.");
    }
    return null;
  },

  _registerMessagePort: function(aMessage, aTarget) {
    let messagePortID = aMessage.messagePortID;
    let manifestURL = aMessage.manifestURL;
    let pageURL = aMessage.pageURL;

    let identity = this._identifyMessagePort(messagePortID, manifestURL);
    if (!identity) {
      if (DEBUG) {
        debug("Cannot identify the message port. Failed to register.");
      }
      return;
    }

    if (DEBUG) debug("Registering message port for " + manifestURL);
    let pair = identity.pair;
    let isPublisher = identity.isPublisher;

    let sender = isPublisher ? pair.publisher : pair.subscriber;
    sender.target = aTarget;
    sender.pageURL = pageURL;
    sender.messageQueue = [];

    // Check if the other port has queued messages. Deliver them if needed.
    if (DEBUG) {
      debug("Checking if the other port used to send messages but queued.");
    }
    let receiver = isPublisher ? pair.subscriber : pair.publisher;
    if (receiver.messageQueue) {
      while (receiver.messageQueue.length) {
        let message = receiver.messageQueue.shift();
        if (DEBUG) debug("Delivering message: " + JSON.stringify(message));
        sender.target.sendAsyncMessage("InterAppMessagePort:OnMessage",
                                       { message: message,
                                         manifestURL: sender.manifestURL,
                                         pageURL: sender.pageURL,
                                         messagePortID: messagePortID });
      }
    }
  },

  _unregisterMessagePort: function(aMessage) {
    let messagePortID = aMessage.messagePortID;
    let manifestURL = aMessage.manifestURL;

    let identity = this._identifyMessagePort(messagePortID, manifestURL);
    if (!identity) {
      if (DEBUG) {
        debug("Cannot identify the message port. Failed to unregister.");
      }
      return;
    }

    if (DEBUG) {
      debug("Unregistering message port for " + manifestURL);
    }
    delete this._messagePortPairs[messagePortID];
  },

  _removeTarget: function(aTarget) {
    if (!aTarget) {
      if (DEBUG) debug("Error! aTarget cannot be null/undefined in any way.");
      return
    }

    if (DEBUG) debug("Unregistering message ports based on this target.");
    let messagePortIDs = [];
    for (let messagePortID in this._messagePortPairs) {
      let pair = this._messagePortPairs[messagePortID];
      if (pair.publisher.target === aTarget ||
          pair.subscriber.target === aTarget) {
        messagePortIDs.push(messagePortID);
        // Send a shutdown message to the part of the pair that is still alive.
        let actor = pair.publisher.target === aTarget ? pair.subscriber
                                                       : pair.publisher;
        actor.target.sendAsyncMessage("InterAppMessagePort:Shutdown",
          { manifestURL: actor.manifestURL,
            pageURL: actor.pageURL,
            messagePortID: messagePortID });
      }
    }
    messagePortIDs.forEach(function(aMessagePortID) {
      delete this._messagePortPairs[aMessagePortID];
    }, this);
  },

  _postMessage: function(aMessage) {
    let messagePortID = aMessage.messagePortID;
    let manifestURL = aMessage.manifestURL;
    let message = aMessage.message;

    let identity = this._identifyMessagePort(messagePortID, manifestURL);
    if (!identity) {
      if (DEBUG) debug("Cannot identify the message port. Failed to post.");
      return;
    }

    let pair = identity.pair;
    let isPublisher = identity.isPublisher;

    let receiver = isPublisher ? pair.subscriber : pair.publisher;
    if (!receiver.target) {
      if (DEBUG) {
        debug("The receiver's target is not ready yet. Queuing the message.");
      }
      let sender = isPublisher ? pair.publisher : pair.subscriber;
      sender.messageQueue.push(message);
      return;
    }

    if (DEBUG) debug("Delivering message: " + JSON.stringify(message));
    receiver.target.sendAsyncMessage("InterAppMessagePort:OnMessage",
                                     { manifestURL: receiver.manifestURL,
                                       pageURL: receiver.pageURL,
                                       messagePortID: messagePortID,
                                       message: message });
  },

  _handleSelectedApps: function(aData) {
    let callerID = aData.callerID;
    let caller = this._promptUICallers[callerID];
    if (!caller) {
      if (DEBUG) debug("Error! Cannot find the caller.");
      return;
    }

    delete this._promptUICallers[callerID];

    let outerWindowID = caller.outerWindowID;
    let requestID = caller.requestID;
    let target = caller.target;

    let pubAppManifestURL = aData.manifestURL;
    let keyword = aData.keyword;
    let selectedApps = aData.selectedApps;

    let allowedSubAppManifestURLs;
    if (selectedApps.length == 0) {
      // Only do the connections for the existing allowed subscribers because
      // no new apps are selected to connect.
      if (DEBUG) debug("No new apps are selected to connect.")

      allowedSubAppManifestURLs =
        this._getAllowedSubAppManifestURLs(keyword, pubAppManifestURL);
    } else {
      // Do connections for for the existing allowed subscribers and the newly
      // selected subscribers.
      if (DEBUG) debug("Some new apps are selected to connect.")

      allowedSubAppManifestURLs =
        this._addSelectedApps(keyword, pubAppManifestURL, selectedApps);
    }

    // Finally, dispatch the message ports for the allowed connections,
    // including the old connections and the newly selected connection.
    this._dispatchMessagePorts(keyword, pubAppManifestURL,
                               allowedSubAppManifestURLs,
                               target, outerWindowID, requestID);
  },

  receiveMessage: function(aMessage) {
    if (DEBUG) debug("receiveMessage: name: " + aMessage.name);
    let message = aMessage.json;
    let target = aMessage.target;

    // To prevent the hacked child process from sending commands to parent
    // to do illegal connections, we need to check its manifest URL.
    if (aMessage.name !== "child-process-shutdown" &&
        // TODO: fix bug 988142 to re-enable "InterAppMessagePort:Unregister".
        aMessage.name !== "InterAppMessagePort:Unregister" &&
        kMessages.indexOf(aMessage.name) != -1) {
      if (!target.assertContainApp(message.manifestURL)) {
        if (DEBUG) {
          debug("Got message from a process carrying illegal manifest URL.");
        }
        return null;
      }
    }

    switch (aMessage.name) {
      case "Webapps:Connect":
        this._connect(message, target);
        break;
      case "Webapps:GetConnections":
        this._getConnections(message, target);
        break;
      case "InterAppConnection:Cancel":
        this._cancelConnection(message);
        break;
      case "InterAppMessagePort:PostMessage":
        this._postMessage(message);
        break;
      case "InterAppMessagePort:Register":
        this._registerMessagePort(message, target);
        break;
      case "InterAppMessagePort:Unregister":
        this._unregisterMessagePort(message);
        break;
      case "child-process-shutdown":
        this._removeTarget(target);
        break;
    }
  },

  observe: function(aSubject, aTopic, aData) {
    switch (aTopic) {
      case "xpcom-shutdown":
        Services.obs.removeObserver(this, "xpcom-shutdown");
        Services.obs.removeObserver(this, "webapps-clear-data");
        kMessages.forEach(function(aMsg) {
          ppmm.removeMessageListener(aMsg, this);
        }, this);
        ppmm = null;
        break;
      case "webapps-clear-data":
        let params =
          aSubject.QueryInterface(Ci.mozIApplicationClearPrivateDataParams);
        if (!params) {
          if (DEBUG) {
            debug("Error updating registered/allowed connections for an " +
                  "uninstalled app.");
          }
          return;
        }

        // Only update registered/allowed connections for apps.
        if (params.browserOnly) {
          if (DEBUG) {
            debug("Only update registered/allowed connections for apps.");
          }
          return;
        }

        let manifestURL = appsService.getManifestURLByLocalId(params.appId);
        if (!manifestURL) {
          if (DEBUG) {
            debug("Error updating registered/allowed connections for an " +
                  "uninstalled app.");
          }
          return;
        }

        // Update registered connections.
        for (let keyword in this._registeredConnections) {
          let subAppManifestURLs = this._registeredConnections[keyword];
          if (subAppManifestURLs[manifestURL]) {
            delete subAppManifestURLs[manifestURL];
            if (DEBUG) {
              debug("Remove " + manifestURL + " from registered connections " +
                    "due to app uninstallation.");
            }
          }
        }

        // Update allowed connections.
        for (let keyword in this._allowedConnections) {
          let allowedPubAppManifestURLs = this._allowedConnections[keyword];
          if (allowedPubAppManifestURLs[manifestURL]) {
            delete allowedPubAppManifestURLs[manifestURL];
            if (DEBUG) {
              debug("Remove " + manifestURL + " (as a pub app) from allowed " +
                    "connections due to app uninstallation.");
            }
          }

          for (let pubAppManifestURL in allowedPubAppManifestURLs) {
            let subAppManifestURLs = allowedPubAppManifestURLs[pubAppManifestURL];
            for (let i = subAppManifestURLs.length - 1; i >= 0; i--) {
              if (subAppManifestURLs[i] === manifestURL) {
                subAppManifestURLs.splice(i, 1);
                if (DEBUG) {
                  debug("Remove " + manifestURL + " (as a sub app to pub " +
                        pubAppManifestURL + ") from allowed connections " +
                        "due to app uninstallation.");
                }
              }
            }
          }
        }
        debug("Finish updating registered/allowed connections for an " +
              "uninstalled app.");
        break;
    }
  }
};

InterAppCommService.init();