Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(862)

Side by Side Diff: chrome/browser/ui/webui/omnibox/omnibox_page_handler.cc

Issue 2481483002: Mojo C++ bindings: make chrome/browser/ui/webui mojom target use STL types. (Closed)
Patch Set: . Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_page_handler.h" 5 #include "chrome/browser/ui/webui/omnibox/omnibox_page_handler.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 } 57 }
58 return array; 58 return array;
59 } 59 }
60 }; 60 };
61 61
62 template <> 62 template <>
63 struct TypeConverter<mojom::AutocompleteMatchPtr, AutocompleteMatch> { 63 struct TypeConverter<mojom::AutocompleteMatchPtr, AutocompleteMatch> {
64 static mojom::AutocompleteMatchPtr Convert(const AutocompleteMatch& input) { 64 static mojom::AutocompleteMatchPtr Convert(const AutocompleteMatch& input) {
65 mojom::AutocompleteMatchPtr result(mojom::AutocompleteMatch::New()); 65 mojom::AutocompleteMatchPtr result(mojom::AutocompleteMatch::New());
66 if (input.provider != NULL) { 66 if (input.provider != NULL) {
67 result->provider_name = input.provider->GetName(); 67 result->provider_name = std::string(input.provider->GetName());
68 result->provider_done = input.provider->done(); 68 result->provider_done = input.provider->done();
69 } 69 }
70 result->relevance = input.relevance; 70 result->relevance = input.relevance;
71 result->deletable = input.deletable; 71 result->deletable = input.deletable;
72 result->fill_into_edit = mojo::String::From(input.fill_into_edit); 72 result->fill_into_edit = base::UTF16ToUTF8(input.fill_into_edit);
73 result->inline_autocompletion = 73 result->inline_autocompletion =
74 mojo::String::From(input.inline_autocompletion); 74 base::UTF16ToUTF8(input.inline_autocompletion);
75 result->destination_url = input.destination_url.spec(); 75 result->destination_url = input.destination_url.spec();
76 result->contents = mojo::String::From(input.contents); 76 result->contents = base::UTF16ToUTF8(input.contents);
77 // At this time, we're not bothering to send along the long vector that 77 // At this time, we're not bothering to send along the long vector that
78 // represent contents classification. i.e., for each character, what 78 // represent contents classification. i.e., for each character, what
79 // type of text it is. 79 // type of text it is.
80 result->description = mojo::String::From(input.description); 80 result->description = base::UTF16ToUTF8(input.description);
81 // At this time, we're not bothering to send along the long vector that 81 // At this time, we're not bothering to send along the long vector that
82 // represents description classification. i.e., for each character, what 82 // represents description classification. i.e., for each character, what
83 // type of text it is. 83 // type of text it is.
84 result->transition = input.transition; 84 result->transition = input.transition;
85 result->allowed_to_be_default_match = input.allowed_to_be_default_match; 85 result->allowed_to_be_default_match = input.allowed_to_be_default_match;
86 result->type = AutocompleteMatchType::ToString(input.type); 86 result->type = AutocompleteMatchType::ToString(input.type);
87 if (input.associated_keyword.get() != NULL) { 87 if (input.associated_keyword.get() != NULL) {
88 result->associated_keyword = 88 result->associated_keyword =
89 mojo::String::From(input.associated_keyword->keyword); 89 base::UTF16ToUTF8(input.associated_keyword->keyword);
90 } 90 }
91 result->keyword = mojo::String::From(input.keyword); 91 result->keyword = base::UTF16ToUTF8(input.keyword);
92 result->duplicates = static_cast<int32_t>(input.duplicate_matches.size()); 92 result->duplicates = static_cast<int32_t>(input.duplicate_matches.size());
93 result->from_previous = input.from_previous; 93 result->from_previous = input.from_previous;
94 94
95 result->additional_info = 95 result->additional_info =
96 mojo::Array<mojom::AutocompleteAdditionalInfoPtr>::From( 96 mojo::Array<mojom::AutocompleteAdditionalInfoPtr>::From(
97 input.additional_info); 97 input.additional_info)
98 .PassStorage();
98 return result; 99 return result;
99 } 100 }
100 }; 101 };
101 102
102 template <> 103 template <>
103 struct TypeConverter<mojom::AutocompleteResultsForProviderPtr, 104 struct TypeConverter<mojom::AutocompleteResultsForProviderPtr,
104 scoped_refptr<AutocompleteProvider>> { 105 scoped_refptr<AutocompleteProvider>> {
105 static mojom::AutocompleteResultsForProviderPtr Convert( 106 static mojom::AutocompleteResultsForProviderPtr Convert(
106 const scoped_refptr<AutocompleteProvider>& input) { 107 const scoped_refptr<AutocompleteProvider>& input) {
107 mojom::AutocompleteResultsForProviderPtr result( 108 mojom::AutocompleteResultsForProviderPtr result(
108 mojom::AutocompleteResultsForProvider::New()); 109 mojom::AutocompleteResultsForProvider::New());
109 result->provider_name = input->GetName(); 110 result->provider_name = input->GetName();
110 result->results = 111 result->results =
111 mojo::Array<mojom::AutocompleteMatchPtr>::From(input->matches()); 112 mojo::Array<mojom::AutocompleteMatchPtr>::From(input->matches())
113 .PassStorage();
112 return result; 114 return result;
113 } 115 }
114 }; 116 };
115 117
116 } // namespace mojo 118 } // namespace mojo
117 119
118 OmniboxPageHandler::OmniboxPageHandler( 120 OmniboxPageHandler::OmniboxPageHandler(
119 Profile* profile, 121 Profile* profile,
120 mojo::InterfaceRequest<mojom::OmniboxPageHandler> request) 122 mojo::InterfaceRequest<mojom::OmniboxPageHandler> request)
121 : profile_(profile), binding_(this, std::move(request)) { 123 : profile_(profile), binding_(this, std::move(request)) {
122 ResetController(); 124 ResetController();
123 } 125 }
124 126
125 OmniboxPageHandler::~OmniboxPageHandler() {} 127 OmniboxPageHandler::~OmniboxPageHandler() {}
126 128
127 void OmniboxPageHandler::OnResultChanged(bool default_match_changed) { 129 void OmniboxPageHandler::OnResultChanged(bool default_match_changed) {
128 mojom::OmniboxResultPtr result(mojom::OmniboxResult::New()); 130 mojom::OmniboxResultPtr result(mojom::OmniboxResult::New());
129 result->done = controller_->done(); 131 result->done = controller_->done();
130 result->time_since_omnibox_started_ms = 132 result->time_since_omnibox_started_ms =
131 (base::Time::Now() - time_omnibox_started_).InMilliseconds(); 133 (base::Time::Now() - time_omnibox_started_).InMilliseconds();
132 const base::string16 host = 134 const base::string16 host =
133 input_.text().substr(input_.parts().host.begin, input_.parts().host.len); 135 input_.text().substr(input_.parts().host.begin, input_.parts().host.len);
134 result->host = mojo::String::From(host); 136 result->host = base::UTF16ToUTF8(host);
135 bool is_typed_host; 137 bool is_typed_host;
136 if (!LookupIsTypedHost(host, &is_typed_host)) 138 if (!LookupIsTypedHost(host, &is_typed_host))
137 is_typed_host = false; 139 is_typed_host = false;
138 result->is_typed_host = is_typed_host; 140 result->is_typed_host = is_typed_host;
139 141
140 { 142 {
141 // Copy to an ACMatches to make conversion easier. Since this isn't 143 // Copy to an ACMatches to make conversion easier. Since this isn't
142 // performance critical we don't worry about the cost here. 144 // performance critical we don't worry about the cost here.
143 ACMatches matches(controller_->result().begin(), 145 ACMatches matches(controller_->result().begin(),
144 controller_->result().end()); 146 controller_->result().end());
145 result->combined_results = 147 result->combined_results =
146 mojo::Array<mojom::AutocompleteMatchPtr>::From(matches); 148 mojo::Array<mojom::AutocompleteMatchPtr>::From(matches).PassStorage();
147 } 149 }
148 result->results_by_provider = 150 result->results_by_provider =
149 mojo::Array<mojom::AutocompleteResultsForProviderPtr>::From( 151 mojo::Array<mojom::AutocompleteResultsForProviderPtr>::From(
150 controller_->providers()); 152 controller_->providers())
153 .PassStorage();
151 154
152 // Fill AutocompleteMatch::starred. 155 // Fill AutocompleteMatch::starred.
153 BookmarkModel* bookmark_model = 156 BookmarkModel* bookmark_model =
154 BookmarkModelFactory::GetForBrowserContext(profile_); 157 BookmarkModelFactory::GetForBrowserContext(profile_);
155 if (bookmark_model) { 158 if (bookmark_model) {
156 for (size_t i = 0; i < result->combined_results.size(); ++i) { 159 for (size_t i = 0; i < result->combined_results.size(); ++i) {
157 result->combined_results[i]->starred = bookmark_model->IsBookmarked( 160 result->combined_results[i]->starred = bookmark_model->IsBookmarked(
158 GURL(result->combined_results[i]->destination_url.get())); 161 GURL(result->combined_results[i]->destination_url));
159 } 162 }
160 for (size_t i = 0; i < result->results_by_provider.size(); ++i) { 163 for (size_t i = 0; i < result->results_by_provider.size(); ++i) {
161 const mojom::AutocompleteResultsForProvider& result_by_provider = 164 const mojom::AutocompleteResultsForProvider& result_by_provider =
162 *result->results_by_provider[i]; 165 *result->results_by_provider[i];
163 for (size_t j = 0; j < result_by_provider.results.size(); ++j) { 166 for (size_t j = 0; j < result_by_provider.results.size(); ++j) {
164 result_by_provider.results[j]->starred = bookmark_model->IsBookmarked( 167 result_by_provider.results[j]->starred = bookmark_model->IsBookmarked(
165 GURL(result_by_provider.results[j]->destination_url.get())); 168 GURL(result_by_provider.results[j]->destination_url));
166 } 169 }
167 } 170 }
168 } 171 }
169 172
170 page_->HandleNewAutocompleteResult(std::move(result)); 173 page_->HandleNewAutocompleteResult(std::move(result));
171 } 174 }
172 175
173 bool OmniboxPageHandler::LookupIsTypedHost(const base::string16& host, 176 bool OmniboxPageHandler::LookupIsTypedHost(const base::string16& host,
174 bool* is_typed_host) const { 177 bool* is_typed_host) const {
175 history::HistoryService* const history_service = 178 history::HistoryService* const history_service =
176 HistoryServiceFactory::GetForProfile(profile_, 179 HistoryServiceFactory::GetForProfile(profile_,
177 ServiceAccessType::EXPLICIT_ACCESS); 180 ServiceAccessType::EXPLICIT_ACCESS);
178 if (!history_service) 181 if (!history_service)
179 return false; 182 return false;
180 history::URLDatabase* url_db = history_service->InMemoryDatabase(); 183 history::URLDatabase* url_db = history_service->InMemoryDatabase();
181 if (!url_db) 184 if (!url_db)
182 return false; 185 return false;
183 *is_typed_host = url_db->IsTypedHost(base::UTF16ToUTF8(host)); 186 *is_typed_host = url_db->IsTypedHost(base::UTF16ToUTF8(host));
184 return true; 187 return true;
185 } 188 }
186 189
187 void OmniboxPageHandler::SetClientPage(mojom::OmniboxPagePtr page) { 190 void OmniboxPageHandler::SetClientPage(mojom::OmniboxPagePtr page) {
188 page_ = std::move(page); 191 page_ = std::move(page);
189 } 192 }
190 193
191 void OmniboxPageHandler::StartOmniboxQuery(const mojo::String& input_string, 194 void OmniboxPageHandler::StartOmniboxQuery(const std::string& input_string,
192 int32_t cursor_position, 195 int32_t cursor_position,
193 bool prevent_inline_autocomplete, 196 bool prevent_inline_autocomplete,
194 bool prefer_keyword, 197 bool prefer_keyword,
195 int32_t page_classification) { 198 int32_t page_classification) {
196 // Reset the controller. If we don't do this, then the 199 // Reset the controller. If we don't do this, then the
197 // AutocompleteController might inappropriately set its |minimal_changes| 200 // AutocompleteController might inappropriately set its |minimal_changes|
198 // variable (or something else) and some providers will short-circuit 201 // variable (or something else) and some providers will short-circuit
199 // important logic and return stale results. In short, we want the 202 // important logic and return stale results. In short, we want the
200 // actual results to not depend on the state of the previous request. 203 // actual results to not depend on the state of the previous request.
201 ResetController(); 204 ResetController();
202 time_omnibox_started_ = base::Time::Now(); 205 time_omnibox_started_ = base::Time::Now();
203 input_ = AutocompleteInput( 206 input_ = AutocompleteInput(
204 input_string.To<base::string16>(), cursor_position, std::string(), GURL(), 207 base::UTF8ToUTF16(input_string), cursor_position, std::string(), GURL(),
205 static_cast<metrics::OmniboxEventProto::PageClassification>( 208 static_cast<metrics::OmniboxEventProto::PageClassification>(
206 page_classification), 209 page_classification),
207 prevent_inline_autocomplete, prefer_keyword, true, true, false, 210 prevent_inline_autocomplete, prefer_keyword, true, true, false,
208 ChromeAutocompleteSchemeClassifier(profile_)); 211 ChromeAutocompleteSchemeClassifier(profile_));
209 controller_->Start(input_); 212 controller_->Start(input_);
210 } 213 }
211 214
212 void OmniboxPageHandler::ResetController() { 215 void OmniboxPageHandler::ResetController() {
213 controller_.reset(new AutocompleteController( 216 controller_.reset(new AutocompleteController(
214 base::MakeUnique<ChromeAutocompleteProviderClient>(profile_), this, 217 base::MakeUnique<ChromeAutocompleteProviderClient>(profile_), this,
215 AutocompleteClassifier::kDefaultOmniboxProviders)); 218 AutocompleteClassifier::kDefaultOmniboxProviders));
216 } 219 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/omnibox/omnibox_page_handler.h ('k') | chrome/browser/ui/webui/plugins/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698