search.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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_SEARCH_SEARCH_H_ 6#define CHROME_BROWSER_SEARCH_SEARCH_H_ 7 8#include <string> 9#include <utility> 10#include <vector> 11 12#include "base/basictypes.h" 13#include "base/string16.h" 14 15class GURL; 16class PrefRegistrySyncable; 17class Profile; 18class TemplateURL; 19class TemplateURLRef; 20 21namespace content { 22class NavigationEntry; 23class WebContents; 24} 25 26namespace chrome { 27namespace search { 28 29// The key used to store search terms data in the NavigationEntry to be later 30// displayed in the omnibox. With the context of the user's exact query, 31// InstantController sets the correct search terms to be displayed. 32extern const char kInstantExtendedSearchTermsKey[]; 33 34// Use this value for "start margin" to prevent the "es_sm" parameter from 35// being used. 36extern const int kDisableStartMargin; 37 38// Returns whether the Instant Extended API is enabled. 39bool IsInstantExtendedAPIEnabled(); 40 41// Returns the value to pass to the &espv CGI parameter when loading the 42// embedded search page from the user's default search provider. Will be 43// 0 if the Instant Extended API is not enabled. 44uint64 EmbeddedSearchPageVersion(); 45 46// Returns whether query extraction is enabled. 47bool IsQueryExtractionEnabled(); 48 49// Returns the search terms attached to a specific NavigationEntry, or empty 50// string otherwise. Does not consider IsQueryExtractionEnabled(), so most 51// callers should use GetSearchTerms() below instead. 52string16 GetSearchTermsFromNavigationEntry( 53 const content::NavigationEntry* entry); 54 55// Returns search terms if this WebContents is a search results page. It looks 56// in the visible NavigationEntry first, to see if search terms have already 57// been extracted. Failing that, it tries to extract search terms from the URL. 58// Returns a blank string if search terms were not found, or if search terms 59// extraction is disabled for this WebContents or profile. 60string16 GetSearchTerms(const content::WebContents* contents); 61 62// Returns true if |url| should be rendered in the Instant renderer process. 63bool ShouldAssignURLToInstantRenderer(const GURL& url, Profile* profile); 64 65// Returns true if the visible entry of |contents| is a New Tab Page rendered 66// by Instant. A page that matches the search or Instant URL of the default 67// search provider but does not have any search terms is considered an Instant 68// New Tab Page. 69bool IsInstantNTP(const content::WebContents* contents); 70 71// Same as IsInstantNTP but uses |nav_entry| to determine the URL for the page 72// instead of using the visible entry. 73bool NavEntryIsInstantNTP(const content::WebContents* contents, 74 const content::NavigationEntry* nav_entry); 75 76// Registers Instant-related user preferences. Called at startup. 77void RegisterUserPrefs(PrefRegistrySyncable* registry); 78 79// Returns prefs::kInstantExtendedEnabled in extended mode; 80// prefs::kInstantEnabled otherwise. 81const char* GetInstantPrefName(); 82 83// Returns whether the Instant pref (as per GetInstantPrefName()) is enabled. 84bool IsInstantPrefEnabled(Profile* profile); 85 86// Sets the default value of prefs::kInstantExtendedEnabled, based on field 87// trials and the current value of prefs::kInstantEnabled. 88void SetInstantExtendedPrefDefault(Profile* profile); 89 90// Returns the Instant URL of the default search engine. Returns an empty GURL 91// if the engine doesn't have an Instant URL, or if it shouldn't be used (say 92// because it doesn't satisfy the requirements for extended mode or if Instant 93// is disabled through preferences). Callers must check that the returned URL is 94// valid before using it. The value of |start_margin| is used for the "es_sm" 95// parameter in the URL. 96// NOTE: This method expands the default search engine's instant_url template, 97// so it shouldn't be called from SearchTermsData or other such code that would 98// lead to an infinite recursion. 99GURL GetInstantURL(Profile* profile, int start_margin); 100 101// Instant (loading a remote server page and talking to it using the searchbox 102// API) is considered enabled if there's a valid Instant URL that can be used, 103// so this simply returns whether GetInstantURL() is a valid URL. 104// NOTE: This method expands the default search engine's instant_ur templatel, 105// so it shouldn't be called from SearchTermsData or other such code that would 106// lead to an infinite recursion. 107bool IsInstantEnabled(Profile* profile); 108 109// ----------------------------------------------------- 110// The following APIs are exposed for use in tests only. 111// ----------------------------------------------------- 112 113// Forces the Instant Extended API to be enabled for tests. 114void EnableInstantExtendedAPIForTesting(); 115 116// Type for a collection of experiment configuration parameters. 117typedef std::vector<std::pair<std::string, std::string> > FieldTrialFlags; 118 119// Given a field trial group name, parses out the group number and configuration 120// flags. On success, |flags| will be filled with the field trial flags. |flags| 121// must not be NULL. If not NULL, |group_number| will receive the experiment 122// group number. 123// Returns true iff field trial info was successfully parsed out of 124// |group_name|. 125// Exposed for testing only. 126bool GetFieldTrialInfo(const std::string& group_name, 127 FieldTrialFlags* flags, 128 uint64* group_number); 129 130// Given a FieldTrialFlags object, returns the string value of the provided 131// flag. 132// Exposed for testing only. 133std::string GetStringValueForFlagWithDefault(const std::string& flag, 134 const std::string& default_value, 135 const FieldTrialFlags& flags); 136 137// Given a FieldTrialFlags object, returns the uint64 value of the provided 138// flag. 139// Exposed for testing only. 140uint64 GetUInt64ValueForFlagWithDefault(const std::string& flag, 141 uint64 default_value, 142 const FieldTrialFlags& flags); 143 144// Given a FieldTrialFlags object, returns the bool value of the provided flag. 145// Exposed for testing only. 146bool GetBoolValueForFlagWithDefault(const std::string& flag, 147 bool default_value, 148 const FieldTrialFlags& flags); 149 150// Coerces the commandline Instant URL to look like a template URL, so that we 151// can extract search terms from it. Exposed for testing only. 152GURL CoerceCommandLineURLToTemplateURL(const GURL& instant_url, 153 const TemplateURLRef& ref, 154 int start_margin); 155 156} // namespace search 157} // namespace chrome 158 159#endif // CHROME_BROWSER_SEARCH_SEARCH_H_ 160