@Override
 public void onOpenInOtherWindow(String url, Referrer referrer) {
   TabDelegate tabDelegate = new TabDelegate(mTab.isIncognito());
   LoadUrlParams loadUrlParams = new LoadUrlParams(url);
   loadUrlParams.setReferrer(referrer);
   tabDelegate.createTabInOtherWindow(loadUrlParams, mTab.getActivity(), mTab.getParentId());
 }
 @Override
 public void onOpenImageUrl(String url, Referrer referrer) {
   LoadUrlParams loadUrlParams = new LoadUrlParams(url);
   loadUrlParams.setTransitionType(PageTransition.LINK);
   loadUrlParams.setReferrer(referrer);
   mTab.loadUrl(loadUrlParams);
 }
 @Override
 public void onOpenImageInNewTab(String url, Referrer referrer) {
   boolean useOriginal = isSpdyProxyEnabledForUrl(url);
   LoadUrlParams loadUrlParams = new LoadUrlParams(url);
   loadUrlParams.setVerbatimHeaders(useOriginal ? PAGESPEED_PASSTHROUGH_HEADERS : null);
   loadUrlParams.setReferrer(referrer);
   mTab.getActivity()
       .getTabModelSelector()
       .openNewTab(loadUrlParams, TabLaunchType.FROM_LONGPRESS_BACKGROUND, mTab, isIncognito());
 }
  @Override
  public void onOpenInNewTab(String url, Referrer referrer) {
    RecordUserAction.record("MobileNewTabOpened");
    LoadUrlParams loadUrlParams = new LoadUrlParams(url);
    loadUrlParams.setReferrer(referrer);
    Tab newTab =
        mTab.getTabModelSelector()
            .openNewTab(
                loadUrlParams, TabLaunchType.FROM_LONGPRESS_BACKGROUND, mTab, isIncognito());

    // {@code newTab} is null in document mode. Do not record metrics for document mode.
    if (mTab.getTabUma() != null && newTab != null) {
      mTab.getTabUma().onBackgroundTabOpenedFromContextMenu(newTab);
    }
  }
Beispiel #5
0
  @Override
  public Tab createNewTab(LoadUrlParams loadUrlParams, TabLaunchType type, Tab parent) {
    AsyncTabCreationParams asyncParams = new AsyncTabCreationParams(loadUrlParams);

    // Figure out how the page will be launched.
    if (TextUtils.equals(UrlConstants.NTP_URL, loadUrlParams.getUrl())) {
      asyncParams.setDocumentLaunchMode(ChromeLauncherActivity.LAUNCH_MODE_RETARGET);
    } else if (type == TabLaunchType.FROM_LONGPRESS_BACKGROUND) {
      if (!parent.isIncognito() && mIsIncognito) {
        // Incognito tabs opened from regular tabs open in the foreground for privacy
        // concerns.
        asyncParams.setDocumentLaunchMode(ChromeLauncherActivity.LAUNCH_MODE_FOREGROUND);
      } else {
        asyncParams.setDocumentLaunchMode(ChromeLauncherActivity.LAUNCH_MODE_AFFILIATED);
      }
    }

    // Classify the startup type.
    if (parent != null && TextUtils.equals(UrlConstants.NTP_URL, parent.getUrl())) {
      asyncParams.setDocumentStartedBy(DocumentMetricIds.STARTED_BY_CHROME_HOME_MOST_VISITED);
    } else if (type == TabLaunchType.FROM_LONGPRESS_BACKGROUND
        || type == TabLaunchType.FROM_LONGPRESS_FOREGROUND) {
      asyncParams.setDocumentStartedBy(DocumentMetricIds.STARTED_BY_CONTEXT_MENU);
    } else if (type == TabLaunchType.FROM_MENU_OR_OVERVIEW) {
      asyncParams.setDocumentStartedBy(DocumentMetricIds.STARTED_BY_OPTIONS_MENU);
    }

    // Tab is created aysnchronously.  Can't return anything, yet.
    createNewTab(asyncParams, type, parent == null ? Tab.INVALID_TAB_ID : parent.getId());
    return null;
  }
  /**
   * Verifies that the referrer is not set for "Open in new incognito tab". Bug:
   * crbug.com/413216 @MediumTest @Feature({"Browser"})
   */
  @FlakyTest
  public void testOpenInIncognitoTabNoReferrer() throws InterruptedException, TimeoutException {
    String url = TestHttpServerClient.getUrl("chrome/test/data/android/context_menu_test.html");
    triggerContextMenuLoad(url, "testLink", R.id.contextmenu_open_in_incognito_tab);

    assertNotNull(mOpenNewTabLoadUrlParams);
    assertNull(mOpenNewTabLoadUrlParams.getReferrer());
  }
  /**
   * Verifies that the referrer is stripped from username and password fields. Bug:
   * crbug.com/413216 @MediumTest @Feature({"Browser"})
   */
  @FlakyTest
  public void testOpenInNewTabSanitizeReferrer() throws InterruptedException, TimeoutException {
    String url =
        TestHttpServerClient.getUrl(
            "chrome/test/data/android/context_menu_test.html", "user", "pass");
    String expectedReferrer =
        TestHttpServerClient.getUrl("chrome/test/data/android/context_menu_test.html");
    assertTrue(url.contains("pass")); // Sanity check.
    triggerContextMenuLoad(url, "testLink", R.id.contextmenu_open_in_new_tab);

    assertNotNull(mOpenNewTabLoadUrlParams);
    assertEquals(expectedReferrer, mOpenNewTabLoadUrlParams.getReferrer().getUrl());
  }
 @Override
 public OverrideUrlLoadingResult clobberCurrentTab(String url, String referrerUrl, Tab tab) {
   int transitionType = PageTransition.LINK;
   LoadUrlParams loadUrlParams = new LoadUrlParams(url, transitionType);
   if (!TextUtils.isEmpty(referrerUrl)) {
     Referrer referrer = new Referrer(referrerUrl, 0 /* WebReferrerPolicyAlways */);
     loadUrlParams.setReferrer(referrer);
   }
   if (tab != null) {
     tab.loadUrl(loadUrlParams);
     return OverrideUrlLoadingResult.OVERRIDE_WITH_CLOBBERING_TAB;
   } else {
     assert false : "clobberCurrentTab was called with an empty tab.";
     Uri uri = Uri.parse(url);
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
     intent.addCategory(Intent.CATEGORY_BROWSABLE);
     intent.setPackage(getPackageName());
     startActivity(intent);
     return OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTENT;
   }
 }
  private void loadIntent(
      Intent intent, String referrerUrl, String fallbackUrl, Tab tab, boolean needsToCloseTab) {
    boolean needsToStartIntent = false;
    if (tab == null || tab.isClosing() || !tab.isInitialized()) {
      needsToStartIntent = true;
      needsToCloseTab = false;
    } else if (needsToCloseTab) {
      needsToStartIntent = true;
    }

    String url = fallbackUrl != null ? fallbackUrl : intent.getDataString();
    if (!UrlUtilities.isAcceptedScheme(url)) {
      if (needsToCloseTab) tab.getChromeWebContentsDelegateAndroid().closeContents();
      return;
    }

    if (needsToStartIntent) {
      intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
      intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
      intent.putExtra(IntentHandler.EXTRA_OPEN_NEW_INCOGNITO_TAB, true);
      intent.addCategory(Intent.CATEGORY_BROWSABLE);
      intent.setPackage(getPackageName());
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      IntentHandler.addTrustedIntentExtras(intent, mActivity);
      startActivity(intent);

      if (needsToCloseTab) tab.getChromeWebContentsDelegateAndroid().closeContents();
      return;
    }

    LoadUrlParams loadUrlParams = new LoadUrlParams(url, PageTransition.AUTO_TOPLEVEL);
    if (!TextUtils.isEmpty(referrerUrl)) {
      Referrer referrer = new Referrer(referrerUrl, 0 /* WebReferrerPolicyAlways */);
      loadUrlParams.setReferrer(referrer);
    }
    tab.loadUrl(loadUrlParams);
  }
  /**
   * 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;
  }
Beispiel #11
0
 @Override
 public void run() {
   awContents.loadUrl(LoadUrlParams.createLoadHttpPostParams(url, mPostData));
 }
 /**
  * Creates a new tab and loads the specified URL in it. This is a convenience method for {@link
  * #createNewTab} with the default {@link LoadUrlParams} and no parent tab.
  *
  * @param url the URL to open.
  * @param type the type of action that triggered that launch. Determines how the tab is opened
  *     (for example, in the foreground or background).
  * @param intent the source of url if it isn't null.
  * @param intentTimestamp the time the intent was received.
  * @return the created tab.
  */
 public Tab launchUrl(
     String url, TabModel.TabLaunchType type, Intent intent, long intentTimestamp) {
   LoadUrlParams loadUrlParams = new LoadUrlParams(url);
   loadUrlParams.setIntentReceivedTimestamp(intentTimestamp);
   return createNewTab(loadUrlParams, type, null, intent);
 }
  /**
   * Creates a new tab and posts to UI.
   *
   * @param loadUrlParams parameters of the url load.
   * @param type Information about how the tab was launched.
   * @param parent the parent tab, if present.
   * @param position the requested position (index in the tab model)
   * @param intent the source of the url if it isn't null.
   * @return The new tab.
   */
  public ChromeTab createNewTab(
      LoadUrlParams loadUrlParams,
      TabModel.TabLaunchType type,
      Tab parent,
      int position,
      Intent intent) {
    try {
      TraceEvent.begin("ChromeTabCreator.createNewTab");
      int parentId = parent != null ? parent.getId() : Tab.INVALID_TAB_ID;
      WebContents webContents = IntentHandler.getWebContentsFromIntent(intent);
      boolean isWebContentsPaused = false;
      if (webContents != null) {
        // The WebContents comes with additional data, but it shouldn't be used if the
        // WebContents itself couldn't be parsed out.
        parentId =
            IntentUtils.safeGetIntExtra(
                intent, IntentHandler.EXTRA_PARENT_TAB_ID, Tab.INVALID_TAB_ID);
        isWebContentsPaused =
            IntentUtils.safeGetBooleanExtra(intent, IntentHandler.EXTRA_WEB_CONTENTS_PAUSED, false);
      }

      // Sanitize the url.
      loadUrlParams.setUrl(UrlUtilities.fixupUrl(loadUrlParams.getUrl()));
      loadUrlParams.setTransitionType(getTransitionType(type));

      boolean openInForeground =
          mOrderController.willOpenInForeground(type, mIncognito) || webContents != null;
      ChromeTab tab;
      if (webContents != null) {
        tab =
            ChromeTab.createLiveTab(
                Tab.INVALID_TAB_ID,
                mActivity,
                mIncognito,
                mNativeWindow,
                type,
                parentId,
                !openInForeground);
        tab.initialize(webContents, mTabContentManager, !openInForeground);
        tab.getTabRedirectHandler().updateIntent(intent);

        if (isWebContentsPaused) webContents.resumeLoadingCreatedWebContents();
      } else if (!openInForeground && SysUtils.isLowEndDevice()) {
        // On low memory devices the tabs opened in background are not loaded automatically
        // to preserve resources (cpu, memory, strong renderer binding) for the foreground
        // tab.
        tab =
            ChromeTab.createTabForLazyLoad(
                mActivity, mIncognito, mNativeWindow, type, parentId, loadUrlParams);
        tab.initialize(null, mTabContentManager, !openInForeground);
        mTabSaver.addTabToSaveQueue(tab);
        tab.getTabRedirectHandler().updateIntent(intent);
      } else {
        tab =
            ChromeTab.createLiveTab(
                Tab.INVALID_TAB_ID,
                mActivity,
                mIncognito,
                mNativeWindow,
                type,
                parentId,
                !openInForeground);

        webContents =
            WarmupManager.getInstance().hasPrerenderedUrl(loadUrlParams.getUrl())
                ? WarmupManager.getInstance().takePrerenderedWebContents()
                : null;
        tab.initialize(webContents, mTabContentManager, !openInForeground);
        tab.getTabRedirectHandler().updateIntent(intent);
        tab.loadUrl(loadUrlParams);
      }

      if (intent != null && intent.hasExtra(ServiceTabLauncher.LAUNCH_REQUEST_ID_EXTRA)) {
        ServiceTabLauncher.onWebContentsForRequestAvailable(
            intent.getIntExtra(ServiceTabLauncher.LAUNCH_REQUEST_ID_EXTRA, 0),
            tab.getWebContents());
      }

      mTabModel.addTab(tab, position, type);

      return tab;
    } finally {
      TraceEvent.end("ChromeTabCreator.createNewTab");
    }
  }