| 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 static v8::Handle<v8::Value> GetThemeBackgroundInfo( |
| 212 const v8::Arguments& args); |
| 213 |
| 214 // Gets the theme area height. |
| 215 static v8::Handle<v8::Value> GetThemeAreaHeight(const v8::Arguments& args); |
| 216 |
| 191 // Navigates the window to a URL represented by either a URL string or a | 217 // Navigates the window to a URL represented by either a URL string or a |
| 192 // restricted ID. | 218 // restricted ID. |
| 193 static v8::Handle<v8::Value> NavigateContentWindow(const v8::Arguments& args); | 219 static v8::Handle<v8::Value> NavigateContentWindow(const v8::Arguments& args); |
| 194 | 220 |
| 195 // Sets ordered suggestions. Valid for current |value|. | 221 // Sets ordered suggestions. Valid for current |value|. |
| 196 static v8::Handle<v8::Value> SetSuggestions(const v8::Arguments& args); | 222 static v8::Handle<v8::Value> SetSuggestions(const v8::Arguments& args); |
| 197 | 223 |
| 198 // Sets the text to be autocompleted into the search box. | 224 // Sets the text to be autocompleted into the search box. |
| 199 static v8::Handle<v8::Value> SetQuerySuggestion(const v8::Arguments& args); | 225 static v8::Handle<v8::Value> SetQuerySuggestion(const v8::Arguments& args); |
| 200 | 226 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 if (name->Equals(v8::String::New("GetWidth"))) | 264 if (name->Equals(v8::String::New("GetWidth"))) |
| 239 return v8::FunctionTemplate::New(GetWidth); | 265 return v8::FunctionTemplate::New(GetWidth); |
| 240 if (name->Equals(v8::String::New("GetHeight"))) | 266 if (name->Equals(v8::String::New("GetHeight"))) |
| 241 return v8::FunctionTemplate::New(GetHeight); | 267 return v8::FunctionTemplate::New(GetHeight); |
| 242 if (name->Equals(v8::String::New("GetAutocompleteResults"))) | 268 if (name->Equals(v8::String::New("GetAutocompleteResults"))) |
| 243 return v8::FunctionTemplate::New(GetAutocompleteResults); | 269 return v8::FunctionTemplate::New(GetAutocompleteResults); |
| 244 if (name->Equals(v8::String::New("GetIsFocused"))) | 270 if (name->Equals(v8::String::New("GetIsFocused"))) |
| 245 return v8::FunctionTemplate::New(GetIsFocused); | 271 return v8::FunctionTemplate::New(GetIsFocused); |
| 246 if (name->Equals(v8::String::New("GetContext"))) | 272 if (name->Equals(v8::String::New("GetContext"))) |
| 247 return v8::FunctionTemplate::New(GetContext); | 273 return v8::FunctionTemplate::New(GetContext); |
| 274 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) |
| 275 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); |
| 276 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) |
| 277 return v8::FunctionTemplate::New(GetThemeAreaHeight); |
| 248 if (name->Equals(v8::String::New("NavigateContentWindow"))) | 278 if (name->Equals(v8::String::New("NavigateContentWindow"))) |
| 249 return v8::FunctionTemplate::New(NavigateContentWindow); | 279 return v8::FunctionTemplate::New(NavigateContentWindow); |
| 250 if (name->Equals(v8::String::New("SetSuggestions"))) | 280 if (name->Equals(v8::String::New("SetSuggestions"))) |
| 251 return v8::FunctionTemplate::New(SetSuggestions); | 281 return v8::FunctionTemplate::New(SetSuggestions); |
| 252 if (name->Equals(v8::String::New("SetQuerySuggestion"))) | 282 if (name->Equals(v8::String::New("SetQuerySuggestion"))) |
| 253 return v8::FunctionTemplate::New(SetQuerySuggestion); | 283 return v8::FunctionTemplate::New(SetQuerySuggestion); |
| 254 if (name->Equals(v8::String::New("SetQuerySuggestionFromAutocompleteResult"))) | 284 if (name->Equals(v8::String::New("SetQuerySuggestionFromAutocompleteResult"))) |
| 255 return v8::FunctionTemplate::New(SetQuerySuggestionFromAutocompleteResult); | 285 return v8::FunctionTemplate::New(SetQuerySuggestionFromAutocompleteResult); |
| 256 if (name->Equals(v8::String::New("SetQuery"))) | 286 if (name->Equals(v8::String::New("SetQuery"))) |
| 257 return v8::FunctionTemplate::New(SetQuery); | 287 return v8::FunctionTemplate::New(SetQuery); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 content::RenderView* render_view = GetRenderView(); | 422 content::RenderView* render_view = GetRenderView(); |
| 393 if (!render_view) return v8::Undefined(); | 423 if (!render_view) return v8::Undefined(); |
| 394 | 424 |
| 395 v8::Handle<v8::Object> context = v8::Object::New(); | 425 v8::Handle<v8::Object> context = v8::Object::New(); |
| 396 context->Set( | 426 context->Set( |
| 397 v8::String::New("isNewTabPage"), | 427 v8::String::New("isNewTabPage"), |
| 398 v8::Boolean::New(SearchBox::Get(render_view)->active_tab_is_ntp())); | 428 v8::Boolean::New(SearchBox::Get(render_view)->active_tab_is_ntp())); |
| 399 return context; | 429 return context; |
| 400 } | 430 } |
| 401 | 431 |
| 432 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetThemeBackgroundInfo( |
| 433 const v8::Arguments& args) { |
| 434 content::RenderView* render_view = GetRenderView(); |
| 435 if (!render_view) return v8::Undefined(); |
| 436 |
| 437 const ThemeBackgroundInfo& theme_info = |
| 438 SearchBox::Get(render_view)->GetThemeBackgroundInfo(); |
| 439 v8::Handle<v8::Object> info = v8::Object::New(); |
| 440 info->Set(v8::String::New("color_rgba"), |
| 441 UTF16ToV8String(theme_info.color_rgba)); |
| 442 info->Set(v8::String::New("image_url"), |
| 443 UTF16ToV8String(theme_info.image_url)); |
| 444 info->Set(v8::String::New("image_horizontal_alignment"), |
| 445 UTF16ToV8String(theme_info.image_horizontal_alignment)); |
| 446 info->Set(v8::String::New("image_vertical_alignment"), |
| 447 UTF16ToV8String(theme_info.image_vertical_alignment)); |
| 448 info->Set(v8::String::New("image_tiling"), |
| 449 UTF16ToV8String(theme_info.image_tiling)); |
| 450 return info; |
| 451 } |
| 452 |
| 453 |
| 454 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetThemeAreaHeight( |
| 455 const v8::Arguments& args) { |
| 456 content::RenderView* render_view = GetRenderView(); |
| 457 if (!render_view) return v8::Undefined(); |
| 458 |
| 459 return v8::Int32::New(SearchBox::Get(render_view)->GetThemeAreaHeight()); |
| 460 } |
| 461 |
| 402 // static | 462 // static |
| 403 v8::Handle<v8::Value> SearchBoxExtensionWrapper::NavigateContentWindow( | 463 v8::Handle<v8::Value> SearchBoxExtensionWrapper::NavigateContentWindow( |
| 404 const v8::Arguments& args) { | 464 const v8::Arguments& args) { |
| 405 content::RenderView* render_view = GetRenderView(); | 465 content::RenderView* render_view = GetRenderView(); |
| 406 if (!render_view || !args.Length()) return v8::Undefined(); | 466 if (!render_view || !args.Length()) return v8::Undefined(); |
| 407 | 467 |
| 408 GURL destination_url; | 468 GURL destination_url; |
| 409 if (args[0]->IsNumber()) { | 469 if (args[0]->IsNumber()) { |
| 410 const InstantAutocompleteResult* result = SearchBox::Get(render_view)-> | 470 const InstantAutocompleteResult* result = SearchBox::Get(render_view)-> |
| 411 GetAutocompleteResultWithId(args[0]->Uint32Value()); | 471 GetAutocompleteResultWithId(args[0]->Uint32Value()); |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 void SearchBoxExtension::DispatchBlur(WebKit::WebFrame* frame) { | 696 void SearchBoxExtension::DispatchBlur(WebKit::WebFrame* frame) { |
| 637 Dispatch(frame, kDispatchBlurEventScript); | 697 Dispatch(frame, kDispatchBlurEventScript); |
| 638 } | 698 } |
| 639 | 699 |
| 640 // static | 700 // static |
| 641 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { | 701 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { |
| 642 Dispatch(frame, kDispatchContextChangeEventScript); | 702 Dispatch(frame, kDispatchContextChangeEventScript); |
| 643 } | 703 } |
| 644 | 704 |
| 645 // static | 705 // static |
| 706 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { |
| 707 Dispatch(frame, kDispatchThemeChangeEventScript); |
| 708 } |
| 709 |
| 710 // static |
| 711 void SearchBoxExtension::DispatchThemeAreaHeightChange( |
| 712 WebKit::WebFrame* frame) { |
| 713 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); |
| 714 } |
| 715 |
| 716 // static |
| 646 v8::Extension* SearchBoxExtension::Get() { | 717 v8::Extension* SearchBoxExtension::Get() { |
| 647 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). | 718 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). |
| 648 GetRawDataResource(IDR_SEARCHBOX_API)); | 719 GetRawDataResource(IDR_SEARCHBOX_API)); |
| 649 } | 720 } |
| 650 | 721 |
| 651 } // namespace extensions_v8 | 722 } // namespace extensions_v8 |
| OLD | NEW |