Home | History | Annotate | Download | only in tabs
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_TABS_TAB_FINDER_H_
      6 #define CHROME_BROWSER_TABS_TAB_FINDER_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <set>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/memory/singleton.h"
     14 #include "chrome/browser/history/history_types.h"
     15 #include "content/browser/cancelable_request.h"
     16 #include "content/browser/tab_contents/navigation_controller.h"
     17 #include "content/common/notification_observer.h"
     18 #include "content/common/notification_registrar.h"
     19 
     20 class Browser;
     21 class GURL;
     22 class TabContents;
     23 
     24 // TabFinder is used to locate a tab by URL. TabFinder matches tabs based
     25 // on the tabs current url, or the start of the redirect chain.
     26 //
     27 // TODO: if we end up keeping this (moving it out of about:flags) then we
     28 // should persist the start of the redirect chain in the navigation entry.
     29 class TabFinder : public NotificationObserver {
     30  public:
     31   // Returns the TabFinder, or NULL if TabFinder is not enabled.
     32   static TabFinder* GetInstance();
     33 
     34   // Returns true if TabFinder is enabled.
     35   static bool IsEnabled();
     36 
     37   // Returns the tab that matches the specified url. If a tab is found the
     38   // browser containing the tab is set in |existing_browser|. This searches
     39   // in |browser| first before checking any other browsers.
     40   TabContents* FindTab(Browser* browser,
     41                        const GURL& url,
     42                        Browser** existing_browser);
     43 
     44   // NotificationObserver overrides:
     45   virtual void Observe(NotificationType type,
     46                        const NotificationSource& source,
     47                        const NotificationDetails& details) OVERRIDE;
     48 
     49  private:
     50   friend struct DefaultSingletonTraits<TabFinder>;
     51 
     52   class TabContentsObserverImpl;
     53 
     54   typedef std::map<TabContents*, GURL> TabContentsToURLMap;
     55   typedef std::set<TabContentsObserverImpl*> TabContentsObservers;
     56 
     57   TabFinder();
     58   ~TabFinder();
     59 
     60   void Init();
     61 
     62   // Forwarded from TabContentsObserverImpl.
     63   void DidNavigateAnyFramePostCommit(
     64       TabContents* source,
     65       const NavigationController::LoadCommittedDetails& details,
     66       const ViewHostMsg_FrameNavigate_Params& params);
     67 
     68   // Returns true if the tab's current url is |url|, or the start of the
     69   // redirect chain for the tab is |url|.
     70   bool TabMatchesURL(TabContents* tab_contents, const GURL& url);
     71 
     72   // Returns the first tab in the specified browser that matches the specified
     73   // url.  Returns NULL if there are no tabs matching the specified url.
     74   TabContents* FindTabInBrowser(Browser* browser, const GURL& url);
     75 
     76   // If we're not currently tracking |tab| this creates a
     77   // TabContentsObserverImpl to listen for navigations.
     78   void TrackTab(TabContents* tab);
     79 
     80   // Queries all the tabs in |browser| for the start of the redirect chain.
     81   void TrackBrowser(Browser* browser);
     82 
     83   // Invoked when a TabContents is being destroyed.
     84   void TabDestroyed(TabContentsObserverImpl* observer);
     85 
     86   // Cancels any pending requests for the specified tabs redirect chain.
     87   void CancelRequestsFor(TabContents* tab_contents);
     88 
     89   // Starts the fetch for the redirect chain of the specified TabContents.
     90   // QueryRedirectsToComplete is invoked when the redirect chain is retrieved.
     91   void FetchRedirectStart(TabContents* tab);
     92 
     93   // Callback when we get the redirect list for a tab.
     94   void QueryRedirectsToComplete(CancelableRequestProvider::Handle handle,
     95                                 GURL url,
     96                                 bool success,
     97                                 history::RedirectList* redirects);
     98 
     99   // Maps from TabContents to the start of the redirect chain.
    100   TabContentsToURLMap tab_contents_to_url_;
    101 
    102   CancelableRequestConsumerTSimple<TabContents*> callback_consumer_;
    103 
    104   NotificationRegistrar registrar_;
    105 
    106   TabContentsObservers tab_contents_observers_;
    107 
    108   DISALLOW_COPY_AND_ASSIGN(TabFinder);
    109 };
    110 
    111 #endif  // CHROME_BROWSER_TABS_TAB_FINDER_H_
    112