Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2388)

Unified Diff: chrome/browser/autocomplete/zero_suggest_provider.h

Issue 10877021: Experimental AutocompleteProvider for zerosuggest. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/autocomplete/zero_suggest_provider.h
diff --git a/chrome/browser/autocomplete/zero_suggest_provider.h b/chrome/browser/autocomplete/zero_suggest_provider.h
new file mode 100644
index 0000000000000000000000000000000000000000..9240374d458786a06fd77ef9f2d8769f648f168d
--- /dev/null
+++ b/chrome/browser/autocomplete/zero_suggest_provider.h
@@ -0,0 +1,120 @@
+// Copyright (c) 2012 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.
+//
+// This file contains the zero-suggest autocomplete provider. This experimental
+// provider is invoked when the user focuses in the omnibox prior to editing,
+// and generates search query suggestions based on the current URL. To enable
+// this provider, point --experimental-zero-suggest-url-prefix at an
+// appropriate suggestion service.
+//
+// HUGE DISCLAIMER: This is just here for experimenting and will probably be
+// deleted entirely as we revise how suggestions work with the omnibox.
+
+#ifndef CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
+#define CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
+
+#include <string>
+#include <vector>
+
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/string16.h"
+#include "chrome/browser/autocomplete/autocomplete_provider.h"
+#include "net/url_request/url_fetcher_delegate.h"
+
+class AutocompleteInput;
+class GURL;
+class TemplateURLService;
+
+namespace base {
+class Value;
+}
+
+namespace net {
+class URLFetcher;
+}
+
+// Autocomplete provider for searches based on the current URL.
+//
+// The controller will call StartZeroSuggest when the user focuses in the
+// omnibox. After construction, the autocomplete controller repeatedly calls
+// Start() with some user input, each time expecting to receive an updated
+// set of matches.
+//
+// TODO(jered): Consider deleting this class and building this functionality
+// into SearchProvider after dogfood and after we break the association between
+// omnibox text and suggestions.
+class ZeroSuggestProvider : public AutocompleteProvider,
+ public net::URLFetcherDelegate {
+ public:
+ ZeroSuggestProvider(AutocompleteProviderListener* listener,
+ Profile* profile,
+ const std::string& url_prefix);
+
+ // AutocompleteProvider:
+ virtual void Start(const AutocompleteInput& input,
+ bool /*minimal_changes*/) OVERRIDE;
+ virtual void Stop(bool clear_cached_results) OVERRIDE;
+
+ // net::URLFetcherDelegate
+ virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
+
+ // Initiates a new fetch for the given |url|, limiting suggestions to those
+ // matching |user_text|. |user_text| may be non-empty if the user previously
+ // interacted with zero-suggest suggestions and then unfocused the omnibox.
+ // TODO(jered): Rip out |user_text| once the first match is decoupled from
+ // the current typing in the omnibox.
+ void StartZeroSuggest(const GURL& url, const string16& user_text);
+
+ private:
+ virtual ~ZeroSuggestProvider();
+
+ // Update matches given the user has typed |user_text|.
+ void UpdateMatches(const string16& user_text);
+
+ // Fetches zero-suggest suggestions for |current_query_|.
+ void Run();
+
+ // Parses results from the zero-suggest server and updates results.
+ // Returns true if results were updated.
+ bool ParseSuggestResults(base::Value* root_val);
+
+ // Converts the parsed results to a set of AutocompleteMatches and adds them
+ // to |matches_|.
+ void ConvertResultsToAutocompleteMatches();
+
+ // Adds a URL suggestion for the current URL. This should be in the top
+ // position so that pressing enter has the effect of reloading the page.
+ void AddMatchForCurrentURL();
+
+ // Adds a query suggestion from response position |result_index| with text
+ // |result| to |matches_|. Uses |search_provider| to build a search URL for
+ // this match.
+ void AddMatchForResult(const TemplateURL* search_provider,
+ size_t result_index,
+ const string16& result);
+
+ // Prefix of the URL from which to fetch zero-suggest suggestions.
+ const std::string url_prefix_;
+
+ // Used to build default search engine URLs for suggested queries.
+ TemplateURLService* template_url_service_;
+
+ // The URL for which a suggestion fetch is pending.
+ std::string current_query_;
+
+ // What the user has typed.
+ string16 user_text_;
+
+ // Fetcher used to retrieve results.
+ scoped_ptr<net::URLFetcher> fetcher_;
+
+ // Suggestions for the most recent query.
+ std::vector<string16> results_;
+
+ DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider);
+};
+
+#endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_

Powered by Google App Engine
This is Rietveld 408576698