1 // Copyright 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_UI_SEARCH_SEARCH_TAB_HELPER_H_ 6 #define CHROME_BROWSER_UI_SEARCH_SEARCH_TAB_HELPER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/compiler_specific.h" 10 #include "base/gtest_prod_util.h" 11 #include "chrome/browser/ui/search/search_model.h" 12 #include "content/public/browser/notification_observer.h" 13 #include "content/public/browser/notification_registrar.h" 14 #include "content/public/browser/web_contents_observer.h" 15 #include "content/public/browser/web_contents_user_data.h" 16 17 namespace content { 18 class WebContents; 19 } 20 21 class InstantPageTest; 22 23 // Per-tab search "helper". Acts as the owner and controller of the tab's 24 // search UI model. 25 // 26 // When the page is finished loading, SearchTabHelper determines the instant 27 // support for the page. When a navigation entry is committed (except for 28 // in-page navigations), SearchTabHelper resets the instant support state to 29 // INSTANT_SUPPORT_UNKNOWN and cause support to be determined again. 30 class SearchTabHelper : public content::NotificationObserver, 31 public content::WebContentsObserver, 32 public content::WebContentsUserData<SearchTabHelper> { 33 public: 34 virtual ~SearchTabHelper(); 35 36 SearchModel* model() { 37 return &model_; 38 } 39 40 // Sets up the initial state correctly for a preloaded NTP. 41 void InitForPreloadedNTP(); 42 43 // Invoked when the OmniboxEditModel changes state in some way that might 44 // affect the search mode. 45 void OmniboxEditModelChanged(bool user_input_in_progress, bool cancelling); 46 47 // Invoked when the active navigation entry is updated in some way that might 48 // affect the search mode. This is used by Instant when it "fixes up" the 49 // virtual URL of the active entry. Regular navigations are captured through 50 // the notification system and shouldn't call this method. 51 void NavigationEntryUpdated(); 52 53 // Invoked to update the instant support state. 54 void InstantSupportChanged(bool supports_instant); 55 56 // Returns true if the page supports instant. If the instant support state is 57 // not determined or if the page does not support instant returns false. 58 bool SupportsInstant() const; 59 60 private: 61 friend class content::WebContentsUserData<SearchTabHelper>; 62 friend class InstantPageTest; 63 FRIEND_TEST_ALL_PREFIXES(InstantPageTest, 64 DetermineIfPageSupportsInstant_Local); 65 FRIEND_TEST_ALL_PREFIXES(InstantPageTest, 66 DetermineIfPageSupportsInstant_NonLocal); 67 FRIEND_TEST_ALL_PREFIXES(InstantPageTest, 68 PageURLDoesntBelongToInstantRenderer); 69 FRIEND_TEST_ALL_PREFIXES(InstantPageTest, PageSupportsInstant); 70 71 explicit SearchTabHelper(content::WebContents* web_contents); 72 73 // Overridden from content::NotificationObserver: 74 virtual void Observe(int type, 75 const content::NotificationSource& source, 76 const content::NotificationDetails& details) OVERRIDE; 77 78 // Overridden from contents::WebContentsObserver: 79 virtual void DidNavigateMainFrame( 80 const content::LoadCommittedDetails& details, 81 const content::FrameNavigateParams& params) OVERRIDE; 82 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 83 virtual void DidFinishLoad( 84 int64 frame_id, 85 const GURL& validated_url, 86 bool is_main_frame, 87 content::RenderViewHost* render_view_host) OVERRIDE; 88 89 // Sets the mode of the model based on the current URL of web_contents(). 90 // Only updates the origin part of the mode if |update_origin| is true, 91 // otherwise keeps the current origin. If |is_preloaded_ntp| is true, the mode 92 // is set to NTP regardless of the current URL; this is used to ensure that 93 // InstantController can bind InstantTab to new tab pages immediately. 94 void UpdateMode(bool update_origin, bool is_preloaded_ntp); 95 96 // Tells the renderer to determine if the page supports the Instant API, which 97 // results in a call to OnInstantSupportDetermined() when the reply 98 // is received. 99 void DetermineIfPageSupportsInstant(); 100 101 // Handler for when Instant support has been determined. 102 void OnInstantSupportDetermined(int page_id, bool supports_instant); 103 104 // Sets whether the page supports voice search on the model. 105 void OnSetVoiceSearchSupported(int page_id, bool supported); 106 107 const bool is_search_enabled_; 108 109 // Tracks the last value passed to OmniboxEditModelChanged(). 110 bool user_input_in_progress_; 111 112 // Model object for UI that cares about search state. 113 SearchModel model_; 114 115 content::NotificationRegistrar registrar_; 116 117 content::WebContents* web_contents_; 118 119 DISALLOW_COPY_AND_ASSIGN(SearchTabHelper); 120 }; 121 122 #endif // CHROME_BROWSER_UI_SEARCH_SEARCH_TAB_HELPER_H_ 123