/**
   * Update the primary color used by the model to the given color.
   *
   * @param color The primary color for the current tab.
   */
  public void updatePrimaryColor(int color) {
    boolean colorChanged = mToolbarModel.getPrimaryColor() != color;
    if (!colorChanged) return;

    mToolbarModel.setPrimaryColor(color);
    mToolbar.onPrimaryColorChanged();
  }
 private void updateBookmarkButtonStatus() {
   Tab currentTab = mToolbarModel.getTab();
   boolean isBookmarked =
       currentTab != null
           && currentTab.getBookmarkId() != ChromeBrowserProviderClient.INVALID_BOOKMARK_ID;
   mToolbar.updateBookmarkButtonVisibility(isBookmarked);
 }
  /** 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;
    }
  }
 private void updateReloadState(boolean tabCrashed) {
   Tab currentTab = mToolbarModel.getTab();
   boolean isLoading = false;
   if (!tabCrashed) {
     isLoading = (currentTab != null && currentTab.isLoading()) || !mNativeLibraryReady;
   }
   mToolbar.updateReloadButtonVisibility(isLoading);
   if (mMenuDelegatePhone != null) mMenuDelegatePhone.updateReloadButtonState(isLoading);
 }
 @Override
 public boolean forward() {
   Tab tab = mToolbarModel.getTab();
   if (tab != null && tab.canGoForward()) {
     tab.goForward();
     updateButtonStatus();
     return true;
   }
   return false;
 }
 @Override
 public void openHomepage() {
   Tab currentTab = mToolbarModel.getTab();
   assert currentTab != null;
   Context context = mToolbar.getContext();
   String homePageUrl = HomepageManager.getHomepageUri(context);
   if (TextUtils.isEmpty(homePageUrl)) {
     homePageUrl = UrlConstants.NTP_URL;
   }
   currentTab.loadUrl(new LoadUrlParams(homePageUrl, PageTransition.HOME_PAGE));
 }
  /**
   * Updates the current button states and calls appropriate abstract visibility methods, giving
   * inheriting classes the chance to update the button visuals as well.
   */
  private void updateButtonStatus() {
    Tab currentTab = mToolbarModel.getTab();
    boolean tabCrashed = currentTab != null && currentTab.isShowingSadTab();

    mToolbar.updateBackButtonVisibility(currentTab != null && currentTab.canGoBack());
    mToolbar.updateForwardButtonVisibility(currentTab != null && currentTab.canGoForward());
    updateReloadState(tabCrashed);
    updateBookmarkButtonStatus();

    mToolbar
        .getMenuButton()
        .setVisibility(mToolbar.shouldShowMenuButton() ? View.VISIBLE : View.GONE);
  }
 @Override
 public void stopOrReloadCurrentTab() {
   Tab currentTab = mToolbarModel.getTab();
   if (currentTab != null) {
     if (currentTab.isLoading()) {
       currentTab.stopLoading();
     } else {
       currentTab.reload();
       RecordUserAction.record("MobileToolbarReload");
     }
   }
   updateButtonStatus();
 }
 private void updateLoadProgress(int progress) {
   mLoadProgressSimulator.cancel();
   progress = Math.max(progress, MINIMUM_LOAD_PROGRESS);
   Tab tab = mToolbarModel.getTab();
   if (tab != null && NativePageFactory.isNativePageUrl(tab.getUrl(), tab.isIncognito())) {
     progress = 0;
   }
   updateLoadProgressInternal(progress);
   if (progress == 100 || progress == 0) {
     updateButtonStatus();
   } else {
     // Update the reload state regardless or whether or not the progress is 100.
     updateReloadState(false);
   }
 }
 private void onPageLoadFinished() {
   Tab currentTab = mToolbarModel.getTab();
   updateTabLoadingState(false, true);
   int currentProgress = currentTab.getProgress();
   if (currentProgress != 100) {
     // If we made some progress, fast-forward to complete, otherwise just dismiss any
     // MINIMUM_LOAD_PROGRESS that had been set.
     if (currentProgress > MINIMUM_LOAD_PROGRESS) {
       updateLoadProgress(100);
     } else {
       updateLoadProgressInternal(0);
     }
   }
   updateButtonStatus();
 }
  private void updateCurrentTabDisplayStatus() {
    Tab currentTab = mToolbarModel.getTab();
    mLocationBar.setUrlToPageUrl();
    if (currentTab == null) {
      updateLoadProgressInternal(0);
      updateButtonStatus();
      return;
    }
    boolean isLoading = currentTab.isLoading();
    updateTabLoadingState(isLoading, true);

    if (currentTab.getProgress() == 100 || currentTab.isShowingInterstitialPage()) {
      // We are switching to a tab that is fully loaded. Don't set the load progress to 1.0,
      // that would cause the load progress bar to show briefly.
      updateLoadProgressInternal(0);
    } else {
      updateLoadProgress(currentTab.getProgress());
    }
    updateButtonStatus();
  }
 /** Call to tear down all of the toolbar dependencies. */
 public void destroy() {
   Tab currentTab = mToolbarModel.getTab();
   if (currentTab != null) currentTab.removeObserver(mTabObserver);
 }
  /**
   * Initialize the manager with the components that had native initialization dependencies.
   *
   * <p>Calling this must occur after the native library have completely loaded.
   *
   * @param tabModelSelector The selector that handles tab management.
   * @param fullscreenManager The manager in charge of interacting with the fullscreen feature.
   * @param findToolbarManager The manager for find in page.
   * @param overviewModeBehavior The overview mode manager.
   * @param layoutDriver A {@link LayoutManager} instance used to watch for scene changes.
   */
  public void initializeWithNative(
      TabModelSelector tabModelSelector,
      ChromeFullscreenManager fullscreenManager,
      final FindToolbarManager findToolbarManager,
      final OverviewModeBehavior overviewModeBehavior,
      final LayoutManager layoutDriver,
      OnClickListener tabSwitcherClickHandler,
      OnClickListener newTabClickHandler,
      OnClickListener bookmarkClickHandler,
      OnClickListener customTabsBackClickHandler) {
    assert !mInitializedWithNative;
    mTabModelSelector = tabModelSelector;

    mToolbar.getLocationBar().updateVisualsForState();
    mToolbar.getLocationBar().setUrlToPageUrl();
    mToolbar.setOnTabSwitcherClickHandler(tabSwitcherClickHandler);
    mToolbar.setOnNewTabClickHandler(newTabClickHandler);
    mToolbar.setBookmarkClickHandler(bookmarkClickHandler);
    mToolbar.setCustomTabCloseClickHandler(customTabsBackClickHandler);

    mToolbarModel.initializeWithNative();

    mToolbar.addOnAttachStateChangeListener(
        new OnAttachStateChangeListener() {
          @Override
          public void onViewDetachedFromWindow(View v) {
            Context context = mToolbar.getContext();
            HomepageManager.getInstance(context).removeListener(mHomepageStateListener);
            mTabModelSelector.removeObserver(mTabModelSelectorObserver);
            for (TabModel model : mTabModelSelector.getModels()) {
              model.removeObserver(mTabModelObserver);
            }
            if (mBookmarksBridge != null) {
              mBookmarksBridge.destroy();
              mBookmarksBridge = null;
            }
            if (mTemplateUrlObserver != null) {
              TemplateUrlService.getInstance().removeObserver(mTemplateUrlObserver);
              mTemplateUrlObserver = null;
            }

            findToolbarManager.removeObserver(mFindToolbarObserver);
            if (overviewModeBehavior != null) {
              overviewModeBehavior.removeOverviewModeObserver(mOverviewModeObserver);
            }
            if (layoutDriver != null) {
              layoutDriver.removeSceneChangeObserver(mSceneChangeObserver);
            }
          }

          @Override
          public void onViewAttachedToWindow(View v) {
            // As we have only just registered for notifications, any that were sent prior to
            // this may have been missed.
            // Calling refreshSelectedTab in case we missed the initial selection notification.
            refreshSelectedTab();
          }
        });

    mFindToolbarManager = findToolbarManager;

    assert fullscreenManager != null;
    mFullscreenManager = fullscreenManager;

    mNativeLibraryReady = false;

    findToolbarManager.addObserver(mFindToolbarObserver);
    if (overviewModeBehavior != null) {
      overviewModeBehavior.addOverviewModeObserver(mOverviewModeObserver);
    }
    if (layoutDriver != null) layoutDriver.addSceneChangeObserver(mSceneChangeObserver);

    onNativeLibraryReady();
    mInitializedWithNative = true;
  }
 private void updateLoadProgressInternal(int progress) {
   if (progress == mToolbarModel.getLoadProgress()) return;
   mToolbarModel.setLoadProgress(progress);
   mToolbar.setLoadProgress(progress);
   if (progress == 0) mLoadProgressSimulator.cancel();
 }
 private void updateTabLoadingState(boolean isLoading, boolean updateUrl) {
   Tab currentTab = mToolbarModel.getTab();
   mLocationBar.updateLoadingState(updateUrl);
   if (isLoading) updateLoadProgress(currentTab.getProgress());
   if (updateUrl) updateButtonStatus();
 }