| 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 #include "chrome/browser/ui/webui/omnibox/omnibox_ui_handler.h" | 5 #include "chrome/browser/ui/webui/omnibox/omnibox_ui_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 16 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
| 17 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 17 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
| 18 #include "chrome/browser/autocomplete/autocomplete_match.h" | 18 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 19 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 19 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 20 #include "chrome/browser/history/history_service.h" | 20 #include "chrome/browser/history/history_service.h" |
| 21 #include "chrome/browser/history/history_service_factory.h" | 21 #include "chrome/browser/history/history_service_factory.h" |
| 22 #include "chrome/browser/history/url_database.h" | 22 #include "chrome/browser/history/url_database.h" |
| 23 #include "chrome/browser/search/search.h" | 23 #include "chrome/browser/search/search.h" |
| 24 #include "chrome/browser/search_engines/template_url.h" | 24 #include "chrome/browser/search_engines/template_url.h" |
| 25 #include "content/public/browser/web_ui.h" | 25 #include "content/public/browser/web_ui.h" |
| 26 #include "mojo/common/common_type_converters.h" | 26 #include "mojo/common/common_type_converters.h" |
| 27 #include "mojo/public/cpp/bindings/allocation_scope.h" | |
| 28 | 27 |
| 29 namespace mojo { | 28 namespace mojo { |
| 30 | 29 |
| 31 template <> | 30 template <> |
| 32 class TypeConverter<mojo::Array<AutocompleteAdditionalInfo>, | 31 class TypeConverter<mojo::Array<AutocompleteAdditionalInfoPtr>, |
| 33 AutocompleteMatch::AdditionalInfo> { | 32 AutocompleteMatch::AdditionalInfo> { |
| 34 public: | 33 public: |
| 35 static mojo::Array<AutocompleteAdditionalInfo> ConvertFrom( | 34 static mojo::Array<AutocompleteAdditionalInfoPtr> ConvertFrom( |
| 36 const AutocompleteMatch::AdditionalInfo& input, | 35 const AutocompleteMatch::AdditionalInfo& input) { |
| 37 Buffer* buf) { | 36 mojo::Array<AutocompleteAdditionalInfoPtr> array(input.size()); |
| 38 mojo::Array<AutocompleteAdditionalInfo>::Builder array_builder( | |
| 39 input.size(), buf); | |
| 40 size_t index = 0; | 37 size_t index = 0; |
| 41 for (AutocompleteMatch::AdditionalInfo::const_iterator i = input.begin(); | 38 for (AutocompleteMatch::AdditionalInfo::const_iterator i = input.begin(); |
| 42 i != input.end(); ++i, index++) { | 39 i != input.end(); ++i, index++) { |
| 43 AutocompleteAdditionalInfo::Builder item_builder(buf); | 40 AutocompleteAdditionalInfoPtr item(AutocompleteAdditionalInfo::New()); |
| 44 item_builder.set_key(i->first); | 41 item->key = i->first; |
| 45 item_builder.set_value(i->second); | 42 item->value = i->second; |
| 46 array_builder[index] = item_builder.Finish(); | 43 array[index] = item.Pass(); |
| 47 } | 44 } |
| 48 return array_builder.Finish(); | 45 return array.Pass(); |
| 49 } | 46 } |
| 50 | |
| 51 MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION(); | |
| 52 }; | 47 }; |
| 53 | 48 |
| 54 template <> | 49 template <> |
| 55 class TypeConverter<AutocompleteMatchMojo, AutocompleteMatch> { | 50 class TypeConverter<AutocompleteMatchMojoPtr, AutocompleteMatch> { |
| 56 public: | 51 public: |
| 57 static AutocompleteMatchMojo ConvertFrom(const AutocompleteMatch& input, | 52 static AutocompleteMatchMojoPtr ConvertFrom(const AutocompleteMatch& input) { |
| 58 Buffer* buf) { | 53 AutocompleteMatchMojoPtr result(AutocompleteMatchMojo::New()); |
| 59 AutocompleteMatchMojo::Builder builder(buf); | |
| 60 if (input.provider != NULL) { | 54 if (input.provider != NULL) { |
| 61 builder.set_provider_name(input.provider->GetName()); | 55 result->provider_name = input.provider->GetName(); |
| 62 builder.set_provider_done(input.provider->done()); | 56 result->provider_done = input.provider->done(); |
| 63 } | 57 } |
| 64 builder.set_relevance(input.relevance); | 58 result->relevance = input.relevance; |
| 65 builder.set_deletable(input.deletable); | 59 result->deletable = input.deletable; |
| 66 builder.set_fill_into_edit(input.fill_into_edit); | 60 result->fill_into_edit = mojo::String::From(input.fill_into_edit); |
| 67 builder.set_inline_autocompletion(input.inline_autocompletion); | 61 result->inline_autocompletion = |
| 68 builder.set_destination_url(input.destination_url.spec()); | 62 mojo::String::From(input.inline_autocompletion); |
| 69 builder.set_contents(input.contents); | 63 result->destination_url = input.destination_url.spec(); |
| 64 result->contents = mojo::String::From(input.contents); |
| 70 // At this time, we're not bothering to send along the long vector that | 65 // At this time, we're not bothering to send along the long vector that |
| 71 // represent contents classification. i.e., for each character, what | 66 // represent contents classification. i.e., for each character, what |
| 72 // type of text it is. | 67 // type of text it is. |
| 73 builder.set_description(input.description); | 68 result->description = mojo::String::From(input.description); |
| 74 // At this time, we're not bothering to send along the long vector that | 69 // At this time, we're not bothering to send along the long vector that |
| 75 // represents description classification. i.e., for each character, what | 70 // represents description classification. i.e., for each character, what |
| 76 // type of text it is. | 71 // type of text it is. |
| 77 builder.set_transition(input.transition); | 72 result->transition = input.transition; |
| 78 builder.set_is_history_what_you_typed_match( | 73 result->is_history_what_you_typed_match = |
| 79 input.is_history_what_you_typed_match); | 74 input.is_history_what_you_typed_match; |
| 80 builder.set_allowed_to_be_default_match(input.allowed_to_be_default_match); | 75 result->allowed_to_be_default_match = input.allowed_to_be_default_match; |
| 81 builder.set_type(AutocompleteMatchType::ToString(input.type)); | 76 result->type = AutocompleteMatchType::ToString(input.type); |
| 82 if (input.associated_keyword.get() != NULL) | 77 if (input.associated_keyword.get() != NULL) { |
| 83 builder.set_associated_keyword(input.associated_keyword->keyword); | 78 result->associated_keyword = |
| 84 builder.set_keyword(input.keyword); | 79 mojo::String::From(input.associated_keyword->keyword); |
| 85 builder.set_starred(input.starred); | 80 } |
| 86 builder.set_duplicates(static_cast<int32>(input.duplicate_matches.size())); | 81 result->keyword = mojo::String::From(input.keyword); |
| 87 builder.set_from_previous(input.from_previous); | 82 result->starred = input.starred; |
| 83 result->duplicates = static_cast<int32>(input.duplicate_matches.size()); |
| 84 result->from_previous = input.from_previous; |
| 88 | 85 |
| 89 builder.set_additional_info(input.additional_info); | 86 result->additional_info = |
| 90 return builder.Finish(); | 87 mojo::Array<AutocompleteAdditionalInfoPtr>::From(input.additional_info); |
| 88 return result.Pass(); |
| 91 } | 89 } |
| 92 | |
| 93 MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION(); | |
| 94 }; | 90 }; |
| 95 | 91 |
| 96 template <> | 92 template <> |
| 97 class TypeConverter<AutocompleteResultsForProviderMojo, AutocompleteProvider*> { | 93 class TypeConverter<AutocompleteResultsForProviderMojoPtr, |
| 94 AutocompleteProvider*> { |
| 98 public: | 95 public: |
| 99 static AutocompleteResultsForProviderMojo ConvertFrom( | 96 static AutocompleteResultsForProviderMojoPtr ConvertFrom( |
| 100 const AutocompleteProvider* input, | 97 const AutocompleteProvider* input) { |
| 101 Buffer* buf) { | 98 AutocompleteResultsForProviderMojoPtr result( |
| 102 AutocompleteResultsForProviderMojo::Builder builder(buf); | 99 AutocompleteResultsForProviderMojo::New()); |
| 103 builder.set_provider_name(input->GetName()); | 100 result->provider_name = input->GetName(); |
| 104 builder.set_results( | 101 result->results = |
| 105 mojo::Array<AutocompleteMatchMojo>::From(input->matches())); | 102 mojo::Array<AutocompleteMatchMojoPtr>::From(input->matches()); |
| 106 return builder.Finish(); | 103 return result.Pass(); |
| 107 } | 104 } |
| 108 | |
| 109 MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION(); | |
| 110 }; | 105 }; |
| 111 | 106 |
| 112 } // namespace mojo | 107 } // namespace mojo |
| 113 | 108 |
| 114 OmniboxUIHandler::OmniboxUIHandler(Profile* profile) | 109 OmniboxUIHandler::OmniboxUIHandler(Profile* profile) |
| 115 : profile_(profile) { | 110 : profile_(profile) { |
| 116 ResetController(); | 111 ResetController(); |
| 117 } | 112 } |
| 118 | 113 |
| 119 OmniboxUIHandler::~OmniboxUIHandler() {} | 114 OmniboxUIHandler::~OmniboxUIHandler() {} |
| 120 | 115 |
| 121 void OmniboxUIHandler::OnResultChanged(bool default_match_changed) { | 116 void OmniboxUIHandler::OnResultChanged(bool default_match_changed) { |
| 122 mojo::AllocationScope scope; | 117 OmniboxResultMojoPtr result(OmniboxResultMojo::New()); |
| 123 OmniboxResultMojo::Builder builder; | 118 result->done = controller_->done(); |
| 124 builder.set_done(controller_->done()); | 119 result->time_since_omnibox_started_ms = |
| 125 builder.set_time_since_omnibox_started_ms( | 120 (base::Time::Now() - time_omnibox_started_).InMilliseconds(); |
| 126 (base::Time::Now() - time_omnibox_started_).InMilliseconds()); | |
| 127 const base::string16 host = input_.text().substr( | 121 const base::string16 host = input_.text().substr( |
| 128 input_.parts().host.begin, | 122 input_.parts().host.begin, |
| 129 input_.parts().host.len); | 123 input_.parts().host.len); |
| 130 builder.set_host(host); | 124 result->host = mojo::String::From(host); |
| 131 bool is_typed_host; | 125 bool is_typed_host; |
| 132 if (!LookupIsTypedHost(host, &is_typed_host)) | 126 if (!LookupIsTypedHost(host, &is_typed_host)) |
| 133 is_typed_host = false; | 127 is_typed_host = false; |
| 134 builder.set_is_typed_host(is_typed_host); | 128 result->is_typed_host = is_typed_host; |
| 135 | 129 |
| 136 { | 130 { |
| 137 // Copy to an ACMatches to make conversion easier. Since this isn't | 131 // Copy to an ACMatches to make conversion easier. Since this isn't |
| 138 // performance critical we don't worry about the cost here. | 132 // performance critical we don't worry about the cost here. |
| 139 ACMatches matches(controller_->result().begin(), | 133 ACMatches matches(controller_->result().begin(), |
| 140 controller_->result().end()); | 134 controller_->result().end()); |
| 141 builder.set_combined_results( | 135 result->combined_results = |
| 142 mojo::Array<AutocompleteMatchMojo>::From(matches)); | 136 mojo::Array<AutocompleteMatchMojoPtr>::From(matches); |
| 143 } | 137 } |
| 144 builder.set_results_by_provider( | 138 result->results_by_provider = |
| 145 mojo::Array<AutocompleteResultsForProviderMojo>::From( | 139 mojo::Array<AutocompleteResultsForProviderMojoPtr>::From( |
| 146 *controller_->providers())); | 140 *controller_->providers()); |
| 147 client()->HandleNewAutocompleteResult(builder.Finish()); | 141 client()->HandleNewAutocompleteResult(result.Pass()); |
| 148 } | 142 } |
| 149 | 143 |
| 150 bool OmniboxUIHandler::LookupIsTypedHost(const base::string16& host, | 144 bool OmniboxUIHandler::LookupIsTypedHost(const base::string16& host, |
| 151 bool* is_typed_host) const { | 145 bool* is_typed_host) const { |
| 152 HistoryService* const history_service = | 146 HistoryService* const history_service = |
| 153 HistoryServiceFactory::GetForProfile(profile_, | 147 HistoryServiceFactory::GetForProfile(profile_, |
| 154 Profile::EXPLICIT_ACCESS); | 148 Profile::EXPLICIT_ACCESS); |
| 155 if (!history_service) | 149 if (!history_service) |
| 156 return false; | 150 return false; |
| 157 history::URLDatabase* url_db = history_service->InMemoryDatabase(); | 151 history::URLDatabase* url_db = history_service->InMemoryDatabase(); |
| 158 if (!url_db) | 152 if (!url_db) |
| 159 return false; | 153 return false; |
| 160 *is_typed_host = url_db->IsTypedHost(base::UTF16ToUTF8(host)); | 154 *is_typed_host = url_db->IsTypedHost(base::UTF16ToUTF8(host)); |
| 161 return true; | 155 return true; |
| 162 } | 156 } |
| 163 | 157 |
| 164 void OmniboxUIHandler::StartOmniboxQuery(const mojo::String& input_string, | 158 void OmniboxUIHandler::StartOmniboxQuery(const mojo::String& input_string, |
| 165 int32_t cursor_position, | 159 int32_t cursor_position, |
| 166 bool prevent_inline_autocomplete, | 160 bool prevent_inline_autocomplete, |
| 167 bool prefer_keyword, | 161 bool prefer_keyword, |
| 168 int32_t page_classification) { | 162 int32_t page_classification) { |
| 169 // Reset the controller. If we don't do this, then the | 163 // Reset the controller. If we don't do this, then the |
| 170 // AutocompleteController might inappropriately set its |minimal_changes| | 164 // AutocompleteController might inappropriately set its |minimal_changes| |
| 171 // variable (or something else) and some providers will short-circuit | 165 // variable (or something else) and some providers will short-circuit |
| 172 // important logic and return stale results. In short, we want the | 166 // important logic and return stale results. In short, we want the |
| 173 // actual results to not depend on the state of the previous request. | 167 // actual results to not depend on the state of the previous request. |
| 174 ResetController(); | 168 ResetController(); |
| 175 time_omnibox_started_ = base::Time::Now(); | 169 time_omnibox_started_ = base::Time::Now(); |
| 176 input_ = AutocompleteInput( | 170 input_ = AutocompleteInput( |
| 177 input_string, | 171 input_string.To<base::string16>(), |
| 178 cursor_position, | 172 cursor_position, |
| 179 base::string16(), // user's desired tld (top-level domain) | 173 base::string16(), // user's desired tld (top-level domain) |
| 180 GURL(), | 174 GURL(), |
| 181 static_cast<AutocompleteInput::PageClassification>( | 175 static_cast<AutocompleteInput::PageClassification>( |
| 182 page_classification), | 176 page_classification), |
| 183 prevent_inline_autocomplete, | 177 prevent_inline_autocomplete, |
| 184 prefer_keyword, | 178 prefer_keyword, |
| 185 true, // allow exact keyword matches | 179 true, // allow exact keyword matches |
| 186 true); | 180 true); |
| 187 controller_->Start(input_); // want all matches | 181 controller_->Start(input_); // want all matches |
| 188 } | 182 } |
| 189 | 183 |
| 190 void OmniboxUIHandler::ResetController() { | 184 void OmniboxUIHandler::ResetController() { |
| 191 controller_.reset(new AutocompleteController(profile_, this, | 185 controller_.reset(new AutocompleteController(profile_, this, |
| 192 AutocompleteClassifier::kDefaultOmniboxProviders)); | 186 AutocompleteClassifier::kDefaultOmniboxProviders)); |
| 193 } | 187 } |
| OLD | NEW |