/** Triggered when the selected tab has changed. */ private void refreshSelectedTab() { ChromeTab tab = null; if (mPreselectedTabId != Tab.INVALID_TAB_ID) { tab = ChromeTab.fromTab(mTabModelSelector.getTabById(mPreselectedTabId)); } if (tab == null) tab = ChromeTab.fromTab(mTabModelSelector.getCurrentTab()); boolean wasIncognito = mToolbarModel.isIncognito(); ChromeTab previousTab = ChromeTab.fromTab(mToolbarModel.getTab()); boolean isIncognito = tab != null ? tab.isIncognito() : mTabModelSelector.isIncognitoSelected(); mToolbarModel.setTab(tab, isIncognito); updateCurrentTabDisplayStatus(); if (previousTab != tab || wasIncognito != isIncognito) { if (previousTab != tab) { if (previousTab != null) previousTab.removeObserver(mTabObserver); if (tab != null) tab.addObserver(mTabObserver); } int defaultPrimaryColor = isIncognito ? mToolbar.getResources().getColor(R.color.incognito_primary_color) : mToolbar.getResources().getColor(R.color.default_primary_color); int primaryColor = (tab != null && tab.getWebContents() != null) ? tab.getWebContents().getThemeColor(defaultPrimaryColor) : defaultPrimaryColor; updatePrimaryColor(primaryColor); mToolbar.onTabOrModelChanged(); if (tab != null && tab.getWebContents() != null && tab.getWebContents().isLoadingToDifferentDocument()) { mToolbar.onNavigatedToDifferentPage(); } } Profile profile = mTabModelSelector.getModel(isIncognito).getProfile(); if (mCurrentProfile != profile) { if (mBookmarksBridge != null) mBookmarksBridge.destroy(); mBookmarksBridge = new BookmarksBridge(profile); mBookmarksBridge.addObserver(mBookmarksObserver); mLocationBar.setAutocompleteProfile(profile); mCurrentProfile = profile; } }
/** * 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"); } }