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