summaryrefslogtreecommitdiffstats
path: root/chrome/browser/search/search_terms_tracker.h
blob: 635ebeff70e96e5ebd6f5ea734966b4a40f12afa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SEARCH_SEARCH_TERMS_TRACKER_H_
#define CHROME_BROWSER_SEARCH_SEARCH_TERMS_TRACKER_H_

#include <map>

#include "base/macros.h"
#include "base/strings/string16.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"

namespace content {
class NavigationController;
class WebContents;
}

namespace chrome {

// Observes navigation events (and WebContents destructions) to keep track of
// search terms associated with a WebContents. Essentially, as long as there are
// only web-triggerable navigations following a search results page, this class
// will consider the search terms from that SRP as the "current" search terms.
// Any other type of navigation will invalidate the search terms. The search
// terms are being tracked so they can be displayed in the location bar for
// related navigations that occur after a search.
class SearchTermsTracker : public content::NotificationObserver {
 public:
  SearchTermsTracker();
  ~SearchTermsTracker() override;

  // Returns the current search terms and navigation index of the corresponding
  // search results page for the specified WebContents. This function will
  // return true if there are valid search terms for |contents|. |search_terms|
  // and/or |navigation_index| can be NULL if not needed.
  bool GetSearchTerms(const content::WebContents* contents,
                      base::string16* search_terms,
                      int* navigation_index) const;

  // content::NotificationObserver
  void Observe(int type,
               const content::NotificationSource& source,
               const content::NotificationDetails& details) override;

 private:
  struct TabData {
    TabData() : srp_navigation_index(-1) {}
    base::string16 search_terms;
    int srp_navigation_index;
  };

  // Keeps information about the specified WebContents.
  typedef std::map<const content::WebContents*, TabData> TabState;

  // Searches for the most recent search and, if found, fills |tab_data| with
  // information about that search and returns true.
  bool FindMostRecentSearch(const content::NavigationController* controller,
                            TabData* tab_data);

  // Removes the TabData entry associated to the specified |contents|.
  void RemoveTabData(const content::WebContents* contents);

  content::NotificationRegistrar registrar_;
  TabState tabs_;

  DISALLOW_COPY_AND_ASSIGN(SearchTermsTracker);
};

}  // namespace chrome

#endif  // CHROME_BROWSER_SEARCH_SEARCH_TERMS_TRACKER_H_