/**
   * Triggered when the URL input field has gained or lost focus.
   *
   * @param hasFocus Whether the URL field has gained focus.
   */
  @Override
  public void onUrlFocusChange(boolean hasFocus) {
    mToolbar.onUrlFocusChange(hasFocus);

    if (mFindToolbarManager != null && hasFocus) mFindToolbarManager.hideToolbar();

    if (mFullscreenManager == null) return;
    if (hasFocus) {
      mFullscreenFocusToken =
          mFullscreenManager.showControlsPersistentAndClearOldToken(mFullscreenFocusToken);
    } else {
      mFullscreenManager.hideControlsPersistent(mFullscreenFocusToken);
      mFullscreenFocusToken = FullscreenManager.INVALID_TOKEN;
    }
  }
  /**
   * 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;
  }