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/renderer/searchbox/searchbox_extension.h" | 5 #include "chrome/renderer/searchbox/searchbox_extension.h" |
6 | 6 |
7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/renderer/searchbox/searchbox.h" | 9 #include "chrome/renderer/searchbox/searchbox.h" |
10 #include "content/public/renderer/render_view.h" | 10 #include "content/public/renderer/render_view.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 | 129 |
130 static const char kDispatchContextChangeEventScript[] = | 130 static const char kDispatchContextChangeEventScript[] = |
131 "if (window.chrome &&" | 131 "if (window.chrome &&" |
132 " window.chrome.searchBox &&" | 132 " window.chrome.searchBox &&" |
133 " window.chrome.searchBox.oncontextchange &&" | 133 " window.chrome.searchBox.oncontextchange &&" |
134 " typeof window.chrome.searchBox.oncontextchange == 'function') {" | 134 " typeof window.chrome.searchBox.oncontextchange == 'function') {" |
135 " window.chrome.searchBox.oncontextchange();" | 135 " window.chrome.searchBox.oncontextchange();" |
136 " true;" | 136 " true;" |
137 "}"; | 137 "}"; |
138 | 138 |
139 static const char kDispatchThemeChangeEventScript[] = | |
140 "if (window.chrome &&" | |
141 " window.chrome.searchBox &&" | |
142 " window.chrome.searchBox.onthemechange &&" | |
143 " typeof window.chrome.searchBox.onthemechange == 'function') {" | |
144 " window.chrome.searchBox.onthemechange();" | |
145 " true;" | |
146 "}"; | |
147 | |
148 static const char kDispatchThemeAreaHeightChangeEventScript[] = | |
149 "if (window.chrome &&" | |
150 " window.chrome.searchBox &&" | |
151 " window.chrome.searchBox.onthemeareaheightchange &&" | |
152 " typeof window.chrome.searchBox.onthemeareaheightchange ==" | |
153 " 'function') {" | |
154 " window.chrome.searchBox.onthemeareaheightchange();" | |
155 " true;" | |
156 "}"; | |
157 | |
139 // ---------------------------------------------------------------------------- | 158 // ---------------------------------------------------------------------------- |
140 | 159 |
141 class SearchBoxExtensionWrapper : public v8::Extension { | 160 class SearchBoxExtensionWrapper : public v8::Extension { |
142 public: | 161 public: |
143 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); | 162 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); |
144 | 163 |
145 // Allows v8's javascript code to call the native functions defined | 164 // Allows v8's javascript code to call the native functions defined |
146 // in this class for window.chrome. | 165 // in this class for window.chrome. |
147 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 166 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
148 v8::Handle<v8::String> name); | 167 v8::Handle<v8::String> name); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 // Gets the autocomplete results from search box. | 200 // Gets the autocomplete results from search box. |
182 static v8::Handle<v8::Value> GetAutocompleteResults( | 201 static v8::Handle<v8::Value> GetAutocompleteResults( |
183 const v8::Arguments& args); | 202 const v8::Arguments& args); |
184 | 203 |
185 // Gets whether the search box is focused. | 204 // Gets whether the search box is focused. |
186 static v8::Handle<v8::Value> GetIsFocused(const v8::Arguments& args); | 205 static v8::Handle<v8::Value> GetIsFocused(const v8::Arguments& args); |
187 | 206 |
188 // Gets the current session context. | 207 // Gets the current session context. |
189 static v8::Handle<v8::Value> GetContext(const v8::Arguments& args); | 208 static v8::Handle<v8::Value> GetContext(const v8::Arguments& args); |
190 | 209 |
210 // Gets the background info of the theme currently adopted by browser. | |
211 // Call only when overlay is showing NTP page. | |
212 static v8::Handle<v8::Value> GetThemeBackgroundInfo( | |
213 const v8::Arguments& args); | |
214 | |
215 // Gets the theme area height that the entire theme background image should | |
216 // fill up. | |
217 // Call only when overlay is showing NTP page and GetThemeBackgroundInfo | |
218 // returns a non-empty image_url and an image_vertical_alignment that is not | |
219 // "top". | |
220 static v8::Handle<v8::Value> GetThemeAreaHeight(const v8::Arguments& args); | |
221 | |
191 // Navigates the window to a URL represented by either a URL string or a | 222 // Navigates the window to a URL represented by either a URL string or a |
192 // restricted ID. | 223 // restricted ID. |
193 static v8::Handle<v8::Value> NavigateContentWindow(const v8::Arguments& args); | 224 static v8::Handle<v8::Value> NavigateContentWindow(const v8::Arguments& args); |
194 | 225 |
195 // Sets ordered suggestions. Valid for current |value|. | 226 // Sets ordered suggestions. Valid for current |value|. |
196 static v8::Handle<v8::Value> SetSuggestions(const v8::Arguments& args); | 227 static v8::Handle<v8::Value> SetSuggestions(const v8::Arguments& args); |
197 | 228 |
198 // Sets the text to be autocompleted into the search box. | 229 // Sets the text to be autocompleted into the search box. |
199 static v8::Handle<v8::Value> SetQuerySuggestion(const v8::Arguments& args); | 230 static v8::Handle<v8::Value> SetQuerySuggestion(const v8::Arguments& args); |
200 | 231 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 if (name->Equals(v8::String::New("GetWidth"))) | 269 if (name->Equals(v8::String::New("GetWidth"))) |
239 return v8::FunctionTemplate::New(GetWidth); | 270 return v8::FunctionTemplate::New(GetWidth); |
240 if (name->Equals(v8::String::New("GetHeight"))) | 271 if (name->Equals(v8::String::New("GetHeight"))) |
241 return v8::FunctionTemplate::New(GetHeight); | 272 return v8::FunctionTemplate::New(GetHeight); |
242 if (name->Equals(v8::String::New("GetAutocompleteResults"))) | 273 if (name->Equals(v8::String::New("GetAutocompleteResults"))) |
243 return v8::FunctionTemplate::New(GetAutocompleteResults); | 274 return v8::FunctionTemplate::New(GetAutocompleteResults); |
244 if (name->Equals(v8::String::New("GetIsFocused"))) | 275 if (name->Equals(v8::String::New("GetIsFocused"))) |
245 return v8::FunctionTemplate::New(GetIsFocused); | 276 return v8::FunctionTemplate::New(GetIsFocused); |
246 if (name->Equals(v8::String::New("GetContext"))) | 277 if (name->Equals(v8::String::New("GetContext"))) |
247 return v8::FunctionTemplate::New(GetContext); | 278 return v8::FunctionTemplate::New(GetContext); |
279 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) | |
280 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); | |
281 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) | |
282 return v8::FunctionTemplate::New(GetThemeAreaHeight); | |
248 if (name->Equals(v8::String::New("NavigateContentWindow"))) | 283 if (name->Equals(v8::String::New("NavigateContentWindow"))) |
249 return v8::FunctionTemplate::New(NavigateContentWindow); | 284 return v8::FunctionTemplate::New(NavigateContentWindow); |
250 if (name->Equals(v8::String::New("SetSuggestions"))) | 285 if (name->Equals(v8::String::New("SetSuggestions"))) |
251 return v8::FunctionTemplate::New(SetSuggestions); | 286 return v8::FunctionTemplate::New(SetSuggestions); |
252 if (name->Equals(v8::String::New("SetQuerySuggestion"))) | 287 if (name->Equals(v8::String::New("SetQuerySuggestion"))) |
253 return v8::FunctionTemplate::New(SetQuerySuggestion); | 288 return v8::FunctionTemplate::New(SetQuerySuggestion); |
254 if (name->Equals(v8::String::New("SetQuerySuggestionFromAutocompleteResult"))) | 289 if (name->Equals(v8::String::New("SetQuerySuggestionFromAutocompleteResult"))) |
255 return v8::FunctionTemplate::New(SetQuerySuggestionFromAutocompleteResult); | 290 return v8::FunctionTemplate::New(SetQuerySuggestionFromAutocompleteResult); |
256 if (name->Equals(v8::String::New("SetQuery"))) | 291 if (name->Equals(v8::String::New("SetQuery"))) |
257 return v8::FunctionTemplate::New(SetQuery); | 292 return v8::FunctionTemplate::New(SetQuery); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
392 content::RenderView* render_view = GetRenderView(); | 427 content::RenderView* render_view = GetRenderView(); |
393 if (!render_view) return v8::Undefined(); | 428 if (!render_view) return v8::Undefined(); |
394 | 429 |
395 v8::Handle<v8::Object> context = v8::Object::New(); | 430 v8::Handle<v8::Object> context = v8::Object::New(); |
396 context->Set( | 431 context->Set( |
397 v8::String::New("isNewTabPage"), | 432 v8::String::New("isNewTabPage"), |
398 v8::Boolean::New(SearchBox::Get(render_view)->active_tab_is_ntp())); | 433 v8::Boolean::New(SearchBox::Get(render_view)->active_tab_is_ntp())); |
399 return context; | 434 return context; |
400 } | 435 } |
401 | 436 |
437 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetThemeBackgroundInfo( | |
438 const v8::Arguments& args) { | |
439 content::RenderView* render_view = GetRenderView(); | |
440 if (!render_view) return v8::Undefined(); | |
441 | |
442 const ThemeBackgroundInfo& theme_info = | |
443 SearchBox::Get(render_view)->GetThemeBackgroundInfo(); | |
444 v8::Handle<v8::Object> info = v8::Object::New(); | |
445 info->Set(v8::String::New("color_rgba"), | |
David Black
2012/11/16 00:57:17
These Javascript object properties should use came
kuan
2012/11/16 01:10:03
Done.
| |
446 UTF16ToV8String(theme_info.color_rgba)); | |
447 info->Set(v8::String::New("image_url"), | |
448 UTF16ToV8String(theme_info.image_url)); | |
449 info->Set(v8::String::New("image_horizontal_alignment"), | |
450 UTF16ToV8String(theme_info.image_horizontal_alignment)); | |
451 info->Set(v8::String::New("image_vertical_alignment"), | |
452 UTF16ToV8String(theme_info.image_vertical_alignment)); | |
453 info->Set(v8::String::New("image_tiling"), | |
454 UTF16ToV8String(theme_info.image_tiling)); | |
455 return info; | |
456 } | |
457 | |
458 | |
459 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetThemeAreaHeight( | |
460 const v8::Arguments& args) { | |
461 content::RenderView* render_view = GetRenderView(); | |
462 if (!render_view) return v8::Undefined(); | |
463 | |
464 return v8::Int32::New(SearchBox::Get(render_view)->GetThemeAreaHeight()); | |
465 } | |
466 | |
402 // static | 467 // static |
403 v8::Handle<v8::Value> SearchBoxExtensionWrapper::NavigateContentWindow( | 468 v8::Handle<v8::Value> SearchBoxExtensionWrapper::NavigateContentWindow( |
404 const v8::Arguments& args) { | 469 const v8::Arguments& args) { |
405 content::RenderView* render_view = GetRenderView(); | 470 content::RenderView* render_view = GetRenderView(); |
406 if (!render_view || !args.Length()) return v8::Undefined(); | 471 if (!render_view || !args.Length()) return v8::Undefined(); |
407 | 472 |
408 GURL destination_url; | 473 GURL destination_url; |
409 if (args[0]->IsNumber()) { | 474 if (args[0]->IsNumber()) { |
410 const InstantAutocompleteResult* result = SearchBox::Get(render_view)-> | 475 const InstantAutocompleteResult* result = SearchBox::Get(render_view)-> |
411 GetAutocompleteResultWithId(args[0]->Uint32Value()); | 476 GetAutocompleteResultWithId(args[0]->Uint32Value()); |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
636 void SearchBoxExtension::DispatchBlur(WebKit::WebFrame* frame) { | 701 void SearchBoxExtension::DispatchBlur(WebKit::WebFrame* frame) { |
637 Dispatch(frame, kDispatchBlurEventScript); | 702 Dispatch(frame, kDispatchBlurEventScript); |
638 } | 703 } |
639 | 704 |
640 // static | 705 // static |
641 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { | 706 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { |
642 Dispatch(frame, kDispatchContextChangeEventScript); | 707 Dispatch(frame, kDispatchContextChangeEventScript); |
643 } | 708 } |
644 | 709 |
645 // static | 710 // static |
711 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { | |
712 Dispatch(frame, kDispatchThemeChangeEventScript); | |
713 } | |
714 | |
715 // static | |
716 void SearchBoxExtension::DispatchThemeAreaHeightChange( | |
717 WebKit::WebFrame* frame) { | |
718 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); | |
719 } | |
720 | |
721 // static | |
646 v8::Extension* SearchBoxExtension::Get() { | 722 v8::Extension* SearchBoxExtension::Get() { |
647 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). | 723 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). |
648 GetRawDataResource(IDR_SEARCHBOX_API)); | 724 GetRawDataResource(IDR_SEARCHBOX_API)); |
649 } | 725 } |
650 | 726 |
651 } // namespace extensions_v8 | 727 } // namespace extensions_v8 |
OLD | NEW |