OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_ |
6 #define CHROME_BROWSER_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
| 11 #include "chrome/browser/autocomplete/autocomplete.h" |
11 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" | 12 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" |
12 #include "content/public/browser/web_ui_message_handler.h" | 13 #include "content/public/browser/web_ui_message_handler.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 class ListValue; | 16 class ListValue; |
16 } // namespace base | 17 } // namespace base |
17 | 18 |
18 class AutocompleteController; | 19 class AutocompleteController; |
19 class Profile; | 20 class Profile; |
20 | 21 |
21 // UI Handler for chrome://omnibox/ | 22 // UI Handler for chrome://omnibox/ |
22 // It listens for calls from javascript to StartOmniboxQuery() and | 23 // It listens for calls from javascript to StartOmniboxQuery() and |
23 // passes those calls to its private AutocompleteController. It also | 24 // passes those calls to its private AutocompleteController. It also |
24 // listens for updates from the AutocompleteController to | 25 // listens for updates from the AutocompleteController to OnResultChanged() |
25 // OnResultChanged() and passes those results on calling back into the | 26 // and passes those results on calling back into the Javascript to |
26 // javascript to update the page. | 27 // update the page. |
27 class OmniboxUIHandler : public AutocompleteControllerDelegate, | 28 class OmniboxUIHandler : public AutocompleteControllerDelegate, |
28 public content::WebUIMessageHandler { | 29 public content::WebUIMessageHandler { |
29 public: | 30 public: |
30 explicit OmniboxUIHandler(Profile* profile); | 31 explicit OmniboxUIHandler(Profile* profile); |
31 virtual ~OmniboxUIHandler(); | 32 virtual ~OmniboxUIHandler(); |
32 | 33 |
33 // AutocompleteControllerDelegate implementation. | 34 // AutocompleteControllerDelegate implementation. |
34 // Gets called when the result set of the AutocompleteController changes. | 35 // Gets called when the result set of the AutocompleteController changes. |
35 // We transform the AutocompleteResult into a javascript object and | 36 // We transform the AutocompleteResult into a Javascript object and |
36 // call the javascript function gotNewAutocompleteResult with it. | 37 // call the Javascript function gotNewAutocompleteResult with it. |
37 // |default_match_changed| is given to us by the AutocompleteController | 38 // |default_match_changed| is given to us by the AutocompleteController |
38 // but we don't need it. It's ignored. | 39 // but we don't need it. It's ignored. |
39 virtual void OnResultChanged(bool default_match_changed) OVERRIDE; | 40 virtual void OnResultChanged(bool default_match_changed) OVERRIDE; |
40 | 41 |
41 protected: | 42 protected: |
42 // WebUIMessageHandler implementation. | 43 // WebUIMessageHandler implementation. |
43 // Register our handler to get callbacks from javascript for | 44 // Register our handler to get callbacks from javascript for |
44 // startOmniboxQuery(). | 45 // startOmniboxQuery(). |
45 virtual void RegisterMessages() OVERRIDE; | 46 virtual void RegisterMessages() OVERRIDE; |
46 | 47 |
47 private: | 48 private: |
48 // Gets called from the javascript when a user enters text into the | 49 // Gets called from the javascript when a user enters text into the |
49 // chrome://omnibox/ text box and clicks submit or hits enter. | 50 // chrome://omnibox/ text box and clicks submit or hits enter. |
50 // |one_element_input_string| is expected to be a one-element list | 51 // |one_element_input_string| is expected to be a one-element list |
51 // where the first element is the input string. | 52 // where the first element is the input string. |
52 void StartOmniboxQuery(const base::ListValue* one_element_input_string); | 53 void StartOmniboxQuery(const base::ListValue* one_element_input_string); |
53 | 54 |
| 55 // Helper function for OnResultChanged(). |
| 56 // Takes an iterator over AutocompleteMatches and packages them into |
| 57 // the DictionaryValue output, all stored under the given prefix. |
| 58 void AddResultToDictionary(const std::string prefix, |
| 59 ACMatches::const_iterator result_it, |
| 60 ACMatches::const_iterator end, |
| 61 base::DictionaryValue* output); |
| 62 |
54 // The omnibox AutocompleteController that collects/sorts/dup- | 63 // The omnibox AutocompleteController that collects/sorts/dup- |
55 // eliminates the results as they come in. | 64 // eliminates the results as they come in. |
56 scoped_ptr<AutocompleteController> controller_; | 65 scoped_ptr<AutocompleteController> controller_; |
57 | 66 |
58 // Time the user's input was sent to the omnibox to start searching. | 67 // Time the user's input was sent to the omnibox to start searching. |
59 // Needed because we also pass timing information in the object we | 68 // Needed because we also pass timing information in the object we |
60 // hand back to the javascript. | 69 // hand back to the javascript. |
61 base::Time time_omnibox_started_; | 70 base::Time time_omnibox_started_; |
62 | 71 |
63 DISALLOW_COPY_AND_ASSIGN(OmniboxUIHandler); | 72 DISALLOW_COPY_AND_ASSIGN(OmniboxUIHandler); |
64 }; | 73 }; |
65 | 74 |
66 #endif // CHROME_BROWSER_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_ | 75 #endif // CHROME_BROWSER_UI_WEBUI_OMNIBOX_OMNIBOX_UI_HANDLER_H_ |
OLD | NEW |