1 // Copyright 2013 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_SEARCH_SEARCH_TERMS_TRACKER_H_ 6 #define CHROME_BROWSER_SEARCH_SEARCH_TERMS_TRACKER_H_ 7 8 #include <map> 9 10 #include "base/strings/string16.h" 11 #include "content/public/browser/notification_observer.h" 12 #include "content/public/browser/notification_registrar.h" 13 14 namespace content { 15 class NavigationController; 16 class WebContents; 17 } 18 19 namespace chrome { 20 21 // Observes navigation events (and WebContents destructions) to keep track of 22 // search terms associated with a WebContents. Essentially, as long as there are 23 // only web-triggerable navigations following a search results page, this class 24 // will consider the search terms from that SRP as the "current" search terms. 25 // Any other type of navigation will invalidate the search terms. The search 26 // terms are being tracked so they can be displayed in the location bar for 27 // related navigations that occur after a search. 28 class SearchTermsTracker : public content::NotificationObserver { 29 public: 30 SearchTermsTracker(); 31 virtual ~SearchTermsTracker(); 32 33 // Returns the current search terms and navigation index of the corresponding 34 // search results page for the specified WebContents. This function will 35 // return true if there are valid search terms for |contents|. |search_terms| 36 // and/or |navigation_index| can be NULL if not needed. 37 bool GetSearchTerms(const content::WebContents* contents, 38 base::string16* search_terms, 39 int* navigation_index) const; 40 41 // content::NotificationObserver 42 virtual void Observe(int type, 43 const content::NotificationSource& source, 44 const content::NotificationDetails& details) OVERRIDE; 45 46 private: 47 struct TabData { TabDataTabData48 TabData() : srp_navigation_index(-1) {} 49 base::string16 search_terms; 50 int srp_navigation_index; 51 }; 52 53 // Keeps information about the specified WebContents. 54 typedef std::map<const content::WebContents*, TabData> TabState; 55 56 // Searches for the most recent search and, if found, fills |tab_data| with 57 // information about that search and returns true. 58 bool FindMostRecentSearch(const content::NavigationController* controller, 59 TabData* tab_data); 60 61 // Removes the TabData entry associated to the specified |contents|. 62 void RemoveTabData(const content::WebContents* contents); 63 64 content::NotificationRegistrar registrar_; 65 TabState tabs_; 66 67 DISALLOW_COPY_AND_ASSIGN(SearchTermsTracker); 68 }; 69 70 } // namespace chrome 71 72 #endif // CHROME_BROWSER_SEARCH_SEARCH_TERMS_TRACKER_H_ 73