コード例 #1
0
 @Override
 public boolean closeTab(Tab tab) {
   for (int i = 0; i < getModels().size(); i++) {
     TabModel model = getModelAt(i);
     if (model.indexOf(tab) >= 0) {
       return model.closeTab(tab);
     }
   }
   assert false : "Tried to close a tab that is not in any model!";
   return false;
 }
コード例 #2
0
  /**
   * Opens the specified URL into a tab, potentially reusing a tab. Typically if a user opens
   * several link from the same application, we reuse the same tab so as to not open too many tabs.
   *
   * @param url the URL to open
   * @param referer The referer url if provided, null otherwise.
   * @param headers HTTP headers to send alongside the URL.
   * @param appId the ID of the application that triggered that URL navigation.
   * @param forceNewTab whether the URL should be opened in a new tab. If false, an existing tab
   *     already opened by the same app will be reused.
   * @param intent the source of url if it isn't null.
   * @param intentTimestamp the time the intent was received.
   * @return the tab the URL was opened in, could be a new tab or a reused one.
   */
  public Tab launchUrlFromExternalApp(
      String url,
      String referer,
      String headers,
      String appId,
      boolean forceNewTab,
      Intent intent,
      long intentTimestamp) {
    assert !mIncognito;
    boolean isLaunchedFromChrome = TextUtils.equals(appId, mActivity.getPackageName());
    if (forceNewTab && !isLaunchedFromChrome) {
      // We don't associate the tab with that app ID, as it is assumed that if the
      // application wanted to open this tab as a new tab, it probably does not want it
      // reused either.
      LoadUrlParams loadUrlParams = new LoadUrlParams(url);
      loadUrlParams.setIntentReceivedTimestamp(intentTimestamp);
      loadUrlParams.setVerbatimHeaders(headers);
      if (referer != null) {
        loadUrlParams.setReferrer(new Referrer(referer, Referrer.REFERRER_POLICY_DEFAULT));
      }
      return createNewTab(loadUrlParams, TabLaunchType.FROM_EXTERNAL_APP, null, intent);
    }

    if (appId == null) {
      // If we have no application ID, we use a made-up one so that these tabs can be
      // reused.
      appId = TabModelImpl.UNKNOWN_APP_ID;
    }
    // Let's try to find an existing tab that was started by that app.
    for (int i = 0; i < mTabModel.getCount(); i++) {
      Tab tab = mTabModel.getTabAt(i);
      if (appId.equals(tab.getAppAssociatedWith())) {
        // We don't reuse the tab, we create a new one at the same index instead.
        // Reusing a tab would require clearing the navigation history and clearing the
        // contents (we would not want the previous content to show).
        LoadUrlParams loadUrlParams = new LoadUrlParams(url);
        loadUrlParams.setIntentReceivedTimestamp(intentTimestamp);
        ChromeTab newTab =
            createNewTab(loadUrlParams, TabLaunchType.FROM_EXTERNAL_APP, null, i, intent);
        newTab.setAppAssociatedWith(appId);
        mTabModel.closeTab(tab, false, false, false);
        return newTab;
      }
    }

    // No tab for that app, we'll have to create a new one.
    Tab tab = launchUrl(url, TabLaunchType.FROM_EXTERNAL_APP, intent, intentTimestamp);
    tab.setAppAssociatedWith(appId);
    return tab;
  }