OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/i18n/rtl.h" |
7 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
8 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
9 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
10 #include "chrome/renderer/searchbox/searchbox.h" | 11 #include "chrome/renderer/searchbox/searchbox.h" |
11 #include "content/public/renderer/render_view.h" | 12 #include "content/public/renderer/render_view.h" |
12 #include "grit/renderer_resources.h" | 13 #include "grit/renderer_resources.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 static const char kDispatchThemeAreaHeightChangeEventScript[] = | 153 static const char kDispatchThemeAreaHeightChangeEventScript[] = |
153 "if (window.chrome &&" | 154 "if (window.chrome &&" |
154 " window.chrome.searchBox &&" | 155 " window.chrome.searchBox &&" |
155 " window.chrome.searchBox.onthemeareaheightchange &&" | 156 " window.chrome.searchBox.onthemeareaheightchange &&" |
156 " typeof window.chrome.searchBox.onthemeareaheightchange ==" | 157 " typeof window.chrome.searchBox.onthemeareaheightchange ==" |
157 " 'function') {" | 158 " 'function') {" |
158 " window.chrome.searchBox.onthemeareaheightchange();" | 159 " window.chrome.searchBox.onthemeareaheightchange();" |
159 " true;" | 160 " true;" |
160 "}"; | 161 "}"; |
161 | 162 |
| 163 static const char kDispatchMarginChangeEventScript[] = |
| 164 "if (window.chrome &&" |
| 165 " window.chrome.searchBox &&" |
| 166 " window.chrome.searchBox.onmarginchange &&" |
| 167 " typeof window.chrome.searchBox.onmarginchange == 'function') {" |
| 168 " window.chrome.searchBox.onmarginchange();" |
| 169 " true;" |
| 170 "}"; |
| 171 |
162 // ---------------------------------------------------------------------------- | 172 // ---------------------------------------------------------------------------- |
163 | 173 |
164 class SearchBoxExtensionWrapper : public v8::Extension { | 174 class SearchBoxExtensionWrapper : public v8::Extension { |
165 public: | 175 public: |
166 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); | 176 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); |
167 | 177 |
168 // Allows v8's javascript code to call the native functions defined | 178 // Allows v8's javascript code to call the native functions defined |
169 // in this class for window.chrome. | 179 // in this class for window.chrome. |
170 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 180 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
171 v8::Handle<v8::String> name); | 181 v8::Handle<v8::String> name); |
(...skipping 22 matching lines...) Expand all Loading... |
194 // Gets the y coordinate (relative to |window|) of the right edge of the | 204 // Gets the y coordinate (relative to |window|) of the right edge of the |
195 // region of the search box that overlaps the window. | 205 // region of the search box that overlaps the window. |
196 static v8::Handle<v8::Value> GetY(const v8::Arguments& args); | 206 static v8::Handle<v8::Value> GetY(const v8::Arguments& args); |
197 | 207 |
198 // Gets the width of the region of the search box that overlaps the window. | 208 // Gets the width of the region of the search box that overlaps the window. |
199 static v8::Handle<v8::Value> GetWidth(const v8::Arguments& args); | 209 static v8::Handle<v8::Value> GetWidth(const v8::Arguments& args); |
200 | 210 |
201 // Gets the height of the region of the search box that overlaps the window. | 211 // Gets the height of the region of the search box that overlaps the window. |
202 static v8::Handle<v8::Value> GetHeight(const v8::Arguments& args); | 212 static v8::Handle<v8::Value> GetHeight(const v8::Arguments& args); |
203 | 213 |
| 214 // Gets the width of the margin from the start-edge of the page to the start |
| 215 // of the suggestions dropdown. |
| 216 static v8::Handle<v8::Value> GetStartMargin(const v8::Arguments& args); |
| 217 |
| 218 // Gets the width of the margin from the end-edge of the page to the end of |
| 219 // the suggestions dropdown. |
| 220 static v8::Handle<v8::Value> GetEndMargin(const v8::Arguments& args); |
| 221 |
| 222 // Returns true if the Searchbox itself is oriented right-to-left. |
| 223 static v8::Handle<v8::Value> GetRightToLeft(const v8::Arguments& args); |
| 224 |
204 // Gets the autocomplete results from search box. | 225 // Gets the autocomplete results from search box. |
205 static v8::Handle<v8::Value> GetAutocompleteResults( | 226 static v8::Handle<v8::Value> GetAutocompleteResults( |
206 const v8::Arguments& args); | 227 const v8::Arguments& args); |
207 | 228 |
208 // Gets the current session context. | 229 // Gets the current session context. |
209 static v8::Handle<v8::Value> GetContext(const v8::Arguments& args); | 230 static v8::Handle<v8::Value> GetContext(const v8::Arguments& args); |
210 | 231 |
211 // Gets the background info of the theme currently adopted by browser. | 232 // Gets the background info of the theme currently adopted by browser. |
212 // Call only when overlay is showing NTP page. | 233 // Call only when overlay is showing NTP page. |
213 static v8::Handle<v8::Value> GetThemeBackgroundInfo( | 234 static v8::Handle<v8::Value> GetThemeBackgroundInfo( |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 if (name->Equals(v8::String::New("GetSelectionEnd"))) | 285 if (name->Equals(v8::String::New("GetSelectionEnd"))) |
265 return v8::FunctionTemplate::New(GetSelectionEnd); | 286 return v8::FunctionTemplate::New(GetSelectionEnd); |
266 if (name->Equals(v8::String::New("GetX"))) | 287 if (name->Equals(v8::String::New("GetX"))) |
267 return v8::FunctionTemplate::New(GetX); | 288 return v8::FunctionTemplate::New(GetX); |
268 if (name->Equals(v8::String::New("GetY"))) | 289 if (name->Equals(v8::String::New("GetY"))) |
269 return v8::FunctionTemplate::New(GetY); | 290 return v8::FunctionTemplate::New(GetY); |
270 if (name->Equals(v8::String::New("GetWidth"))) | 291 if (name->Equals(v8::String::New("GetWidth"))) |
271 return v8::FunctionTemplate::New(GetWidth); | 292 return v8::FunctionTemplate::New(GetWidth); |
272 if (name->Equals(v8::String::New("GetHeight"))) | 293 if (name->Equals(v8::String::New("GetHeight"))) |
273 return v8::FunctionTemplate::New(GetHeight); | 294 return v8::FunctionTemplate::New(GetHeight); |
| 295 if (name->Equals(v8::String::New("GetStartMargin"))) |
| 296 return v8::FunctionTemplate::New(GetStartMargin); |
| 297 if (name->Equals(v8::String::New("GetEndMargin"))) |
| 298 return v8::FunctionTemplate::New(GetEndMargin); |
| 299 if (name->Equals(v8::String::New("GetRightToLeft"))) |
| 300 return v8::FunctionTemplate::New(GetRightToLeft); |
274 if (name->Equals(v8::String::New("GetAutocompleteResults"))) | 301 if (name->Equals(v8::String::New("GetAutocompleteResults"))) |
275 return v8::FunctionTemplate::New(GetAutocompleteResults); | 302 return v8::FunctionTemplate::New(GetAutocompleteResults); |
276 if (name->Equals(v8::String::New("GetContext"))) | 303 if (name->Equals(v8::String::New("GetContext"))) |
277 return v8::FunctionTemplate::New(GetContext); | 304 return v8::FunctionTemplate::New(GetContext); |
278 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) | 305 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) |
279 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); | 306 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); |
280 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) | 307 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) |
281 return v8::FunctionTemplate::New(GetThemeAreaHeight); | 308 return v8::FunctionTemplate::New(GetThemeAreaHeight); |
282 if (name->Equals(v8::String::New("NavigateContentWindow"))) | 309 if (name->Equals(v8::String::New("NavigateContentWindow"))) |
283 return v8::FunctionTemplate::New(NavigateContentWindow); | 310 return v8::FunctionTemplate::New(NavigateContentWindow); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 | 371 |
345 return v8::Uint32::New(SearchBox::Get(render_view)->selection_end()); | 372 return v8::Uint32::New(SearchBox::Get(render_view)->selection_end()); |
346 } | 373 } |
347 | 374 |
348 // static | 375 // static |
349 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetX( | 376 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetX( |
350 const v8::Arguments& args) { | 377 const v8::Arguments& args) { |
351 content::RenderView* render_view = GetRenderView(); | 378 content::RenderView* render_view = GetRenderView(); |
352 if (!render_view) return v8::Undefined(); | 379 if (!render_view) return v8::Undefined(); |
353 | 380 |
354 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().x()); | 381 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().x()); |
355 } | 382 } |
356 | 383 |
357 // static | 384 // static |
358 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetY( | 385 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetY( |
359 const v8::Arguments& args) { | 386 const v8::Arguments& args) { |
360 content::RenderView* render_view = GetRenderView(); | 387 content::RenderView* render_view = GetRenderView(); |
361 if (!render_view) return v8::Undefined(); | 388 if (!render_view) return v8::Undefined(); |
362 | 389 |
363 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().y()); | 390 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().y()); |
364 } | 391 } |
365 | 392 |
366 // static | 393 // static |
367 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetWidth( | 394 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetWidth( |
368 const v8::Arguments& args) { | 395 const v8::Arguments& args) { |
369 content::RenderView* render_view = GetRenderView(); | 396 content::RenderView* render_view = GetRenderView(); |
370 if (!render_view) return v8::Undefined(); | 397 if (!render_view) return v8::Undefined(); |
371 | 398 |
372 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().width()); | 399 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().width()); |
373 } | 400 } |
374 | 401 |
375 // static | 402 // static |
376 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( | 403 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( |
377 const v8::Arguments& args) { | 404 const v8::Arguments& args) { |
378 content::RenderView* render_view = GetRenderView(); | 405 content::RenderView* render_view = GetRenderView(); |
379 if (!render_view) return v8::Undefined(); | 406 if (!render_view) return v8::Undefined(); |
380 | 407 |
381 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().height()); | 408 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().height()); |
382 } | 409 } |
383 | 410 |
384 // static | 411 // static |
| 412 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetStartMargin( |
| 413 const v8::Arguments& args) { |
| 414 content::RenderView* render_view = GetRenderView(); |
| 415 if (!render_view) return v8::Undefined(); |
| 416 return v8::Int32::New(SearchBox::Get(render_view)->GetStartMargin()); |
| 417 } |
| 418 |
| 419 // static |
| 420 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetEndMargin( |
| 421 const v8::Arguments& args) { |
| 422 content::RenderView* render_view = GetRenderView(); |
| 423 if (!render_view) return v8::Undefined(); |
| 424 return v8::Int32::New(SearchBox::Get(render_view)->GetEndMargin()); |
| 425 } |
| 426 |
| 427 // static |
| 428 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetRightToLeft( |
| 429 const v8::Arguments& args) { |
| 430 return v8::Boolean::New(base::i18n::IsRTL()); |
| 431 } |
| 432 |
| 433 // static |
385 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetAutocompleteResults( | 434 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetAutocompleteResults( |
386 const v8::Arguments& args) { | 435 const v8::Arguments& args) { |
387 DVLOG(1) << "GetAutocompleteResults"; | 436 DVLOG(1) << "GetAutocompleteResults"; |
388 content::RenderView* render_view = GetRenderView(); | 437 content::RenderView* render_view = GetRenderView(); |
389 if (!render_view) return v8::Undefined(); | 438 if (!render_view) return v8::Undefined(); |
390 | 439 |
391 const std::vector<InstantAutocompleteResult>& results = | 440 const std::vector<InstantAutocompleteResult>& results = |
392 SearchBox::Get(render_view)->GetAutocompleteResults(); | 441 SearchBox::Get(render_view)->GetAutocompleteResults(); |
393 const size_t results_base = SearchBox::Get(render_view)->results_base(); | 442 const size_t results_base = SearchBox::Get(render_view)->results_base(); |
394 | 443 |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
779 DVLOG(1) << "DispatchContextChange"; | 828 DVLOG(1) << "DispatchContextChange"; |
780 Dispatch(frame, kDispatchContextChangeEventScript); | 829 Dispatch(frame, kDispatchContextChangeEventScript); |
781 } | 830 } |
782 | 831 |
783 // static | 832 // static |
784 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { | 833 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { |
785 Dispatch(frame, kDispatchThemeChangeEventScript); | 834 Dispatch(frame, kDispatchThemeChangeEventScript); |
786 } | 835 } |
787 | 836 |
788 // static | 837 // static |
| 838 void SearchBoxExtension::DispatchMarginChange(WebKit::WebFrame* frame) { |
| 839 Dispatch(frame, kDispatchMarginChangeEventScript); |
| 840 } |
| 841 |
| 842 // static |
789 void SearchBoxExtension::DispatchThemeAreaHeightChange( | 843 void SearchBoxExtension::DispatchThemeAreaHeightChange( |
790 WebKit::WebFrame* frame) { | 844 WebKit::WebFrame* frame) { |
791 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); | 845 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); |
792 } | 846 } |
793 | 847 |
794 // static | 848 // static |
795 v8::Extension* SearchBoxExtension::Get() { | 849 v8::Extension* SearchBoxExtension::Get() { |
796 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). | 850 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). |
797 GetRawDataResource(IDR_SEARCHBOX_API)); | 851 GetRawDataResource(IDR_SEARCHBOX_API)); |
798 } | 852 } |
799 | 853 |
800 } // namespace extensions_v8 | 854 } // namespace extensions_v8 |
OLD | NEW |