- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / omnibox / omnibox_ui_handler.h
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_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_
7
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"
15
16 class AutocompleteController;
17 class Profile;
18
19 namespace base {
20 class ListValue;
21 }
22
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
28 // update the page.
29 class OmniboxUIHandler : public AutocompleteControllerDelegate,
30                          public content::WebUIMessageHandler {
31  public:
32   explicit OmniboxUIHandler(Profile* profile);
33   virtual ~OmniboxUIHandler();
34
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;
42
43  protected:
44   // WebUIMessageHandler implementation.
45   // Register our handler to get callbacks from javascript for
46   // startOmniboxQuery().
47   virtual void RegisterMessages() OVERRIDE;
48
49  private:
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   void StartOmniboxQuery(const base::ListValue* input);
59
60   // Helper function for OnResultChanged().
61   // Takes an iterator over AutocompleteMatches and packages them into
62   // the DictionaryValue output, all stored under the given prefix.
63   void AddResultToDictionary(const std::string& prefix,
64                              ACMatches::const_iterator result_it,
65                              ACMatches::const_iterator end,
66                              base::DictionaryValue* output);
67
68   // Looks up whether the hostname is a typed host (i.e., has received
69   // typed visits).  Return true if the lookup succeeded; if so, the
70   // value of |is_typed_host| is set appropriately.
71   bool LookupIsTypedHost(const string16& host, bool* is_typed_host) const;
72
73   // Re-initializes the AutocompleteController in preparation for the
74   // next query.
75   void ResetController();
76
77   // The omnibox AutocompleteController that collects/sorts/dup-
78   // eliminates the results as they come in.
79   scoped_ptr<AutocompleteController> controller_;
80
81   // Time the user's input was sent to the omnibox to start searching.
82   // Needed because we also pass timing information in the object we
83   // hand back to the javascript.
84   base::Time time_omnibox_started_;
85
86   // The Profile* handed to us in our constructor.
87   Profile* profile_;
88
89   DISALLOW_COPY_AND_ASSIGN(OmniboxUIHandler);
90 };
91
92 #endif  // CHROME_BROWSER_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_