Chromium Code Reviews| 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()); |
|
sky
2014/05/22 14:12:05
It's too bad the empty constructor can't create th
| |
| 44 item_builder.set_key(i->first); | 41 item->key = mojo::String::From(i->first); |
|
sky
2014/05/22 14:12:05
I like the old in this case. How come mojo::String
| |
| 45 item_builder.set_value(i->second); | 42 item->value = mojo::String::From(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 = mojo::String::From(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 = mojo::String::From(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 = |
| 82 if (input.associated_keyword.get() != NULL) | 77 mojo::String::From(AutocompleteMatchType::ToString(input.type)); |
| 83 builder.set_associated_keyword(input.associated_keyword->keyword); | 78 if (input.associated_keyword.get() != NULL) { |
| 84 builder.set_keyword(input.keyword); | 79 result->associated_keyword = |
| 85 builder.set_starred(input.starred); | 80 mojo::String::From(input.associated_keyword->keyword); |
| 86 builder.set_duplicates(static_cast<int32>(input.duplicate_matches.size())); | 81 } |
| 87 builder.set_from_previous(input.from_previous); | 82 result->keyword = mojo::String::From(input.keyword); |
| 83 result->starred = input.starred; | |
| 84 result->duplicates = static_cast<int32>(input.duplicate_matches.size()); | |
| 85 result->from_previous = input.from_previous; | |
| 88 | 86 |
| 89 builder.set_additional_info(input.additional_info); | 87 result->additional_info = |
| 90 return builder.Finish(); | 88 mojo::Array<AutocompleteAdditionalInfoPtr>::From(input.additional_info); |
| 89 return result.Pass(); | |
| 91 } | 90 } |
| 92 | |
| 93 MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION(); | |
| 94 }; | 91 }; |
| 95 | 92 |
| 96 template <> | 93 template <> |
| 97 class TypeConverter<AutocompleteResultsForProviderMojo, AutocompleteProvider*> { | 94 class TypeConverter<AutocompleteResultsForProviderMojoPtr, |
| 95 AutocompleteProvider*> { | |
| 98 public: | 96 public: |
| 99 static AutocompleteResultsForProviderMojo ConvertFrom( | 97 static AutocompleteResultsForProviderMojoPtr ConvertFrom( |
| 100 const AutocompleteProvider* input, | 98 const AutocompleteProvider* input) { |
| 101 Buffer* buf) { | 99 AutocompleteResultsForProviderMojoPtr result( |
| 102 AutocompleteResultsForProviderMojo::Builder builder(buf); | 100 AutocompleteResultsForProviderMojo::New()); |
| 103 builder.set_provider_name(input->GetName()); | 101 result->provider_name = mojo::String::From(input->GetName()); |
| 104 builder.set_results( | 102 result->results = |
| 105 mojo::Array<AutocompleteMatchMojo>::From(input->matches())); | 103 mojo::Array<AutocompleteMatchMojoPtr>::From(input->matches()); |
| 106 return builder.Finish(); | 104 return result.Pass(); |
| 107 } | 105 } |
| 108 | |
| 109 MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION(); | |
| 110 }; | 106 }; |
| 111 | 107 |
| 112 } // namespace mojo | 108 } // namespace mojo |
| 113 | 109 |
| 114 OmniboxUIHandler::OmniboxUIHandler(Profile* profile) | 110 OmniboxUIHandler::OmniboxUIHandler(Profile* profile) |
| 115 : profile_(profile) { | 111 : profile_(profile) { |
| 116 ResetController(); | 112 ResetController(); |
| 117 } | 113 } |
| 118 | 114 |
| 119 OmniboxUIHandler::~OmniboxUIHandler() {} | 115 OmniboxUIHandler::~OmniboxUIHandler() {} |
| 120 | 116 |
| 121 void OmniboxUIHandler::OnResultChanged(bool default_match_changed) { | 117 void OmniboxUIHandler::OnResultChanged(bool default_match_changed) { |
| 122 mojo::AllocationScope scope; | 118 OmniboxResultMojoPtr result(OmniboxResultMojo::New()); |
| 123 OmniboxResultMojo::Builder builder; | 119 result->done = controller_->done(); |
| 124 builder.set_done(controller_->done()); | 120 result->time_since_omnibox_started_ms = |
| 125 builder.set_time_since_omnibox_started_ms( | 121 (base::Time::Now() - time_omnibox_started_).InMilliseconds(); |
| 126 (base::Time::Now() - time_omnibox_started_).InMilliseconds()); | |
| 127 const base::string16 host = input_.text().substr( | 122 const base::string16 host = input_.text().substr( |
| 128 input_.parts().host.begin, | 123 input_.parts().host.begin, |
| 129 input_.parts().host.len); | 124 input_.parts().host.len); |
| 130 builder.set_host(host); | 125 result->host = mojo::String::From(host); |
| 131 bool is_typed_host; | 126 bool is_typed_host; |
| 132 if (!LookupIsTypedHost(host, &is_typed_host)) | 127 if (!LookupIsTypedHost(host, &is_typed_host)) |
| 133 is_typed_host = false; | 128 is_typed_host = false; |
| 134 builder.set_is_typed_host(is_typed_host); | 129 result->is_typed_host = is_typed_host; |
| 135 | 130 |
| 136 { | 131 { |
| 137 // Copy to an ACMatches to make conversion easier. Since this isn't | 132 // Copy to an ACMatches to make conversion easier. Since this isn't |
| 138 // performance critical we don't worry about the cost here. | 133 // performance critical we don't worry about the cost here. |
| 139 ACMatches matches(controller_->result().begin(), | 134 ACMatches matches(controller_->result().begin(), |
| 140 controller_->result().end()); | 135 controller_->result().end()); |
| 141 builder.set_combined_results( | 136 result->combined_results = |
| 142 mojo::Array<AutocompleteMatchMojo>::From(matches)); | 137 mojo::Array<AutocompleteMatchMojoPtr>::From(matches); |
| 143 } | 138 } |
| 144 builder.set_results_by_provider( | 139 result->results_by_provider = |
| 145 mojo::Array<AutocompleteResultsForProviderMojo>::From( | 140 mojo::Array<AutocompleteResultsForProviderMojoPtr>::From( |
| 146 *controller_->providers())); | 141 *controller_->providers()); |
| 147 client()->HandleNewAutocompleteResult(builder.Finish()); | 142 client()->HandleNewAutocompleteResult(result.Pass()); |
| 148 } | 143 } |
| 149 | 144 |
| 150 bool OmniboxUIHandler::LookupIsTypedHost(const base::string16& host, | 145 bool OmniboxUIHandler::LookupIsTypedHost(const base::string16& host, |
| 151 bool* is_typed_host) const { | 146 bool* is_typed_host) const { |
| 152 HistoryService* const history_service = | 147 HistoryService* const history_service = |
| 153 HistoryServiceFactory::GetForProfile(profile_, | 148 HistoryServiceFactory::GetForProfile(profile_, |
| 154 Profile::EXPLICIT_ACCESS); | 149 Profile::EXPLICIT_ACCESS); |
| 155 if (!history_service) | 150 if (!history_service) |
| 156 return false; | 151 return false; |
| 157 history::URLDatabase* url_db = history_service->InMemoryDatabase(); | 152 history::URLDatabase* url_db = history_service->InMemoryDatabase(); |
| 158 if (!url_db) | 153 if (!url_db) |
| 159 return false; | 154 return false; |
| 160 *is_typed_host = url_db->IsTypedHost(base::UTF16ToUTF8(host)); | 155 *is_typed_host = url_db->IsTypedHost(base::UTF16ToUTF8(host)); |
| 161 return true; | 156 return true; |
| 162 } | 157 } |
| 163 | 158 |
| 164 void OmniboxUIHandler::StartOmniboxQuery(const mojo::String& input_string, | 159 void OmniboxUIHandler::StartOmniboxQuery(mojo::String input_string, |
| 165 int32_t cursor_position, | 160 int32_t cursor_position, |
| 166 bool prevent_inline_autocomplete, | 161 bool prevent_inline_autocomplete, |
| 167 bool prefer_keyword, | 162 bool prefer_keyword, |
| 168 int32_t page_classification) { | 163 int32_t page_classification) { |
| 169 // Reset the controller. If we don't do this, then the | 164 // Reset the controller. If we don't do this, then the |
| 170 // AutocompleteController might inappropriately set its |minimal_changes| | 165 // AutocompleteController might inappropriately set its |minimal_changes| |
| 171 // variable (or something else) and some providers will short-circuit | 166 // variable (or something else) and some providers will short-circuit |
| 172 // important logic and return stale results. In short, we want the | 167 // important logic and return stale results. In short, we want the |
| 173 // actual results to not depend on the state of the previous request. | 168 // actual results to not depend on the state of the previous request. |
| 174 ResetController(); | 169 ResetController(); |
| 175 time_omnibox_started_ = base::Time::Now(); | 170 time_omnibox_started_ = base::Time::Now(); |
| 176 input_ = AutocompleteInput( | 171 input_ = AutocompleteInput( |
| 177 input_string, | 172 input_string.To<base::string16>(), |
| 178 cursor_position, | 173 cursor_position, |
| 179 base::string16(), // user's desired tld (top-level domain) | 174 base::string16(), // user's desired tld (top-level domain) |
| 180 GURL(), | 175 GURL(), |
| 181 static_cast<AutocompleteInput::PageClassification>( | 176 static_cast<AutocompleteInput::PageClassification>( |
| 182 page_classification), | 177 page_classification), |
| 183 prevent_inline_autocomplete, | 178 prevent_inline_autocomplete, |
| 184 prefer_keyword, | 179 prefer_keyword, |
| 185 true, // allow exact keyword matches | 180 true, // allow exact keyword matches |
| 186 true); | 181 true); |
| 187 controller_->Start(input_); // want all matches | 182 controller_->Start(input_); // want all matches |
| 188 } | 183 } |
| 189 | 184 |
| 190 void OmniboxUIHandler::ResetController() { | 185 void OmniboxUIHandler::ResetController() { |
| 191 controller_.reset(new AutocompleteController(profile_, this, | 186 controller_.reset(new AutocompleteController(profile_, this, |
| 192 AutocompleteClassifier::kDefaultOmniboxProviders)); | 187 AutocompleteClassifier::kDefaultOmniboxProviders)); |
| 193 } | 188 } |
| OLD | NEW |