1 // Copyright (c) 2012 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_GOOGLE_GOOGLE_SEARCH_COUNTER_H_ 6 #define CHROME_BROWSER_GOOGLE_GOOGLE_SEARCH_COUNTER_H_ 7 8 #include "base/memory/singleton.h" 9 #include "components/google/core/browser/google_search_metrics.h" 10 #include "content/public/browser/notification_observer.h" 11 #include "content/public/browser/notification_registrar.h" 12 13 namespace content { 14 class NavigationEntry; 15 } 16 17 // A listener for counting Google searches from various search access points. No 18 // actual search query content is observed. See GoogleSearchMetrics for more 19 // details about these access points. 20 class GoogleSearchCounter : content::NotificationObserver { 21 public: 22 // Initialize the global instance. 23 static void RegisterForNotifications(); 24 25 // Return the singleton instance of GoogleSearchCounter. 26 static GoogleSearchCounter* GetInstance(); 27 28 // Returns the Google search access point for the given |entry|. This method 29 // assumes that we have already verified that |entry|'s URL is a Google search 30 // URL. 31 GoogleSearchMetrics::AccessPoint GetGoogleSearchAccessPointForSearchNavEntry( 32 const content::NavigationEntry& entry) const; 33 34 // Returns true if |details| is valid and corresponds to a search results 35 // page. 36 bool ShouldRecordCommittedDetails( 37 const content::NotificationDetails& details) const; 38 search_metrics()39 const GoogleSearchMetrics* search_metrics() const { 40 return search_metrics_.get(); 41 } 42 43 private: 44 friend struct DefaultSingletonTraits<GoogleSearchCounter>; 45 friend class GoogleSearchCounterTest; 46 friend class GoogleSearchCounterAndroidTest; 47 48 GoogleSearchCounter(); 49 virtual ~GoogleSearchCounter(); 50 51 void ProcessCommittedEntry(const content::NotificationSource& source, 52 const content::NotificationDetails& details); 53 54 // Replace the internal metrics object with a dummy or a mock. This instance 55 // takes ownership of |search_metrics|. 56 void SetSearchMetricsForTesting(GoogleSearchMetrics* search_metrics); 57 58 // Register this counter for all notifications we care about. 59 void RegisterForNotificationsInternal(); 60 61 // content::NotificationObserver 62 virtual void Observe(int type, 63 const content::NotificationSource& source, 64 const content::NotificationDetails& details) OVERRIDE; 65 66 content::NotificationRegistrar registrar_; 67 scoped_ptr<GoogleSearchMetrics> search_metrics_; 68 69 DISALLOW_COPY_AND_ASSIGN(GoogleSearchCounter); 70 }; 71 72 #endif // CHROME_BROWSER_GOOGLE_GOOGLE_SEARCH_COUNTER_H_ 73