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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h"
13 #include "chrome/browser/autocomplete/autocomplete_match.h"
14 #include "content/public/browser/web_ui_message_handler.h"
16 class AutocompleteController
;
23 // UI Handler for chrome://omnibox/
24 // It listens for calls from javascript to StartOmniboxQuery() and
25 // passes those calls to its private AutocompleteController. It also
26 // listens for updates from the AutocompleteController to OnResultChanged()
27 // and passes those results on calling back into the Javascript to
29 class OmniboxUIHandler
: public AutocompleteControllerDelegate
,
30 public content::WebUIMessageHandler
{
32 explicit OmniboxUIHandler(Profile
* profile
);
33 virtual ~OmniboxUIHandler();
35 // AutocompleteControllerDelegate implementation.
36 // Gets called when the result set of the AutocompleteController changes.
37 // We transform the AutocompleteResult into a Javascript object and
38 // call the Javascript function gotNewAutocompleteResult with it.
39 // |default_match_changed| is given to us by the AutocompleteController
40 // but we don't need it. It's ignored.
41 virtual void OnResultChanged(bool default_match_changed
) OVERRIDE
;
44 // WebUIMessageHandler implementation.
45 // Register our handler to get callbacks from javascript for
46 // startOmniboxQuery().
47 virtual void RegisterMessages() OVERRIDE
;
50 // Gets called from the javascript when a user enters text into the
51 // chrome://omnibox/ text box and clicks submit or hits enter.
52 // |input| is expected to be a four-element list:
53 // - first element: input string.
54 // - second element: the cursor position.
55 // - third element: boolean indicating whether we should set
56 // prevent_inline_autocomplete or not.
57 // - fourth element: boolean indicating whether we should set prefer_keyword
58 // - fifth element: current page classification value (enum
59 // PageClassification from omnibox_event.proto)
60 void StartOmniboxQuery(const base::ListValue
* input
);
62 // Helper function for OnResultChanged().
63 // Takes an iterator over AutocompleteMatches and packages them into
64 // the DictionaryValue output, all stored under the given prefix.
65 void AddResultToDictionary(const std::string
& prefix
,
66 ACMatches::const_iterator result_it
,
67 ACMatches::const_iterator end
,
68 base::DictionaryValue
* output
);
70 // Looks up whether the hostname is a typed host (i.e., has received
71 // typed visits). Return true if the lookup succeeded; if so, the
72 // value of |is_typed_host| is set appropriately.
73 bool LookupIsTypedHost(const base::string16
& host
, bool* is_typed_host
) const;
75 // Re-initializes the AutocompleteController in preparation for the
77 void ResetController();
79 // The omnibox AutocompleteController that collects/sorts/dup-
80 // eliminates the results as they come in.
81 scoped_ptr
<AutocompleteController
> controller_
;
83 // Time the user's input was sent to the omnibox to start searching.
84 // Needed because we also pass timing information in the object we
85 // hand back to the javascript.
86 base::Time time_omnibox_started_
;
88 // The Profile* handed to us in our constructor.
91 DISALLOW_COPY_AND_ASSIGN(OmniboxUIHandler
);
94 #endif // CHROME_BROWSER_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_