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/i18n/rtl.h" |
8 #include "base/json/string_escape.h" | 8 #include "base/json/string_escape.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // Converts string16 to V8 String. | 64 // Converts string16 to V8 String. |
65 v8::Handle<v8::String> UTF16ToV8String(const string16& s) { | 65 v8::Handle<v8::String> UTF16ToV8String(const string16& s) { |
66 return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size()); | 66 return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size()); |
67 } | 67 } |
68 | 68 |
69 // Converts std::string to V8 String. | 69 // Converts std::string to V8 String. |
70 v8::Handle<v8::String> UTF8ToV8String(const std::string& s) { | 70 v8::Handle<v8::String> UTF8ToV8String(const std::string& s) { |
71 return v8::String::New(s.data(), s.size()); | 71 return v8::String::New(s.data(), s.size()); |
72 } | 72 } |
73 | 73 |
74 void Dispatch(WebKit::WebFrame* frame, const WebKit::WebString& script) { | 74 void Dispatch(blink::WebFrame* frame, const blink::WebString& script) { |
75 if (!frame) return; | 75 if (!frame) return; |
76 frame->executeScript(WebKit::WebScriptSource(script)); | 76 frame->executeScript(blink::WebScriptSource(script)); |
77 } | 77 } |
78 | 78 |
79 v8::Handle<v8::String> GenerateThumbnailURL( | 79 v8::Handle<v8::String> GenerateThumbnailURL( |
80 int render_view_id, | 80 int render_view_id, |
81 InstantRestrictedID most_visited_item_id) { | 81 InstantRestrictedID most_visited_item_id) { |
82 return UTF8ToV8String(base::StringPrintf("chrome-search://thumb/%d/%d", | 82 return UTF8ToV8String(base::StringPrintf("chrome-search://thumb/%d/%d", |
83 render_view_id, | 83 render_view_id, |
84 most_visited_item_id)); | 84 most_visited_item_id)); |
85 } | 85 } |
86 | 86 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 obj->Set(v8::String::New("direction"), UTF8ToV8String(direction)); | 123 obj->Set(v8::String::New("direction"), UTF8ToV8String(direction)); |
124 obj->Set(v8::String::New("url"), UTF8ToV8String(mv_item.url.spec())); | 124 obj->Set(v8::String::New("url"), UTF8ToV8String(mv_item.url.spec())); |
125 return obj; | 125 return obj; |
126 } | 126 } |
127 | 127 |
128 // Returns the render view for the current JS context if it matches |origin|, | 128 // Returns the render view for the current JS context if it matches |origin|, |
129 // otherwise returns NULL. Used to restrict methods that access suggestions and | 129 // otherwise returns NULL. Used to restrict methods that access suggestions and |
130 // most visited data to pages with origin chrome-search://most-visited and | 130 // most visited data to pages with origin chrome-search://most-visited and |
131 // chrome-search://suggestions. | 131 // chrome-search://suggestions. |
132 content::RenderView* GetRenderViewWithCheckedOrigin(const GURL& origin) { | 132 content::RenderView* GetRenderViewWithCheckedOrigin(const GURL& origin) { |
133 WebKit::WebFrame* webframe = WebKit::WebFrame::frameForCurrentContext(); | 133 blink::WebFrame* webframe = blink::WebFrame::frameForCurrentContext(); |
134 if (!webframe) | 134 if (!webframe) |
135 return NULL; | 135 return NULL; |
136 WebKit::WebView* webview = webframe->view(); | 136 blink::WebView* webview = webframe->view(); |
137 if (!webview) | 137 if (!webview) |
138 return NULL; // Can happen during closing. | 138 return NULL; // Can happen during closing. |
139 content::RenderView* render_view = content::RenderView::FromWebView(webview); | 139 content::RenderView* render_view = content::RenderView::FromWebView(webview); |
140 if (!render_view) | 140 if (!render_view) |
141 return NULL; | 141 return NULL; |
142 | 142 |
143 GURL url(webframe->document().url()); | 143 GURL url(webframe->document().url()); |
144 if (url.GetOrigin() != origin.GetOrigin()) | 144 if (url.GetOrigin() != origin.GetOrigin()) |
145 return NULL; | 145 return NULL; |
146 | 146 |
147 return render_view; | 147 return render_view; |
148 } | 148 } |
149 | 149 |
150 // Returns the current URL. | 150 // Returns the current URL. |
151 GURL GetCurrentURL(content::RenderView* render_view) { | 151 GURL GetCurrentURL(content::RenderView* render_view) { |
152 WebKit::WebView* webview = render_view->GetWebView(); | 152 blink::WebView* webview = render_view->GetWebView(); |
153 return webview ? GURL(webview->mainFrame()->document().url()) : GURL(); | 153 return webview ? GURL(webview->mainFrame()->document().url()) : GURL(); |
154 } | 154 } |
155 | 155 |
156 } // namespace | 156 } // namespace |
157 | 157 |
158 namespace internal { // for testing. | 158 namespace internal { // for testing. |
159 | 159 |
160 // Returns an array with the RGBA color components. | 160 // Returns an array with the RGBA color components. |
161 v8::Handle<v8::Value> RGBAColorToArray(const RGBAColor& color) { | 161 v8::Handle<v8::Value> RGBAColorToArray(const RGBAColor& color) { |
162 v8::Handle<v8::Array> color_array = v8::Array::New(4); | 162 v8::Handle<v8::Array> color_array = v8::Array::New(4); |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 DISALLOW_COPY_AND_ASSIGN(SearchBoxExtensionWrapper); | 423 DISALLOW_COPY_AND_ASSIGN(SearchBoxExtensionWrapper); |
424 }; | 424 }; |
425 | 425 |
426 // static | 426 // static |
427 v8::Extension* SearchBoxExtension::Get() { | 427 v8::Extension* SearchBoxExtension::Get() { |
428 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). | 428 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). |
429 GetRawDataResource(IDR_SEARCHBOX_API)); | 429 GetRawDataResource(IDR_SEARCHBOX_API)); |
430 } | 430 } |
431 | 431 |
432 // static | 432 // static |
433 bool SearchBoxExtension::PageSupportsInstant(WebKit::WebFrame* frame) { | 433 bool SearchBoxExtension::PageSupportsInstant(blink::WebFrame* frame) { |
434 if (!frame) return false; | 434 if (!frame) return false; |
435 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 435 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
436 v8::Handle<v8::Value> v = frame->executeScriptAndReturnValue( | 436 v8::Handle<v8::Value> v = frame->executeScriptAndReturnValue( |
437 WebKit::WebScriptSource(kSupportsInstantScript)); | 437 blink::WebScriptSource(kSupportsInstantScript)); |
438 return !v.IsEmpty() && v->BooleanValue(); | 438 return !v.IsEmpty() && v->BooleanValue(); |
439 } | 439 } |
440 | 440 |
441 // static | 441 // static |
442 void SearchBoxExtension::DispatchChromeIdentityCheckResult( | 442 void SearchBoxExtension::DispatchChromeIdentityCheckResult( |
443 WebKit::WebFrame* frame, const string16& identity, bool identity_match) { | 443 blink::WebFrame* frame, const string16& identity, bool identity_match) { |
444 std::string escaped_identity; | 444 std::string escaped_identity; |
445 base::JsonDoubleQuote(identity, true, &escaped_identity); | 445 base::JsonDoubleQuote(identity, true, &escaped_identity); |
446 WebKit::WebString script(UTF8ToUTF16(base::StringPrintf( | 446 blink::WebString script(UTF8ToUTF16(base::StringPrintf( |
447 kDispatchChromeIdentityCheckResult, | 447 kDispatchChromeIdentityCheckResult, |
448 escaped_identity.c_str(), | 448 escaped_identity.c_str(), |
449 identity_match ? "true" : "false"))); | 449 identity_match ? "true" : "false"))); |
450 Dispatch(frame, script); | 450 Dispatch(frame, script); |
451 } | 451 } |
452 | 452 |
453 // static | 453 // static |
454 void SearchBoxExtension::DispatchFocusChange(WebKit::WebFrame* frame) { | 454 void SearchBoxExtension::DispatchFocusChange(blink::WebFrame* frame) { |
455 Dispatch(frame, kDispatchFocusChangedScript); | 455 Dispatch(frame, kDispatchFocusChangedScript); |
456 } | 456 } |
457 | 457 |
458 // static | 458 // static |
459 void SearchBoxExtension::DispatchInputCancel(WebKit::WebFrame* frame) { | 459 void SearchBoxExtension::DispatchInputCancel(blink::WebFrame* frame) { |
460 Dispatch(frame, kDispatchInputCancelScript); | 460 Dispatch(frame, kDispatchInputCancelScript); |
461 } | 461 } |
462 | 462 |
463 // static | 463 // static |
464 void SearchBoxExtension::DispatchInputStart(WebKit::WebFrame* frame) { | 464 void SearchBoxExtension::DispatchInputStart(blink::WebFrame* frame) { |
465 Dispatch(frame, kDispatchInputStartScript); | 465 Dispatch(frame, kDispatchInputStartScript); |
466 } | 466 } |
467 | 467 |
468 // static | 468 // static |
469 void SearchBoxExtension::DispatchKeyCaptureChange(WebKit::WebFrame* frame) { | 469 void SearchBoxExtension::DispatchKeyCaptureChange(blink::WebFrame* frame) { |
470 Dispatch(frame, kDispatchKeyCaptureChangeScript); | 470 Dispatch(frame, kDispatchKeyCaptureChangeScript); |
471 } | 471 } |
472 | 472 |
473 // static | 473 // static |
474 void SearchBoxExtension::DispatchMarginChange(WebKit::WebFrame* frame) { | 474 void SearchBoxExtension::DispatchMarginChange(blink::WebFrame* frame) { |
475 Dispatch(frame, kDispatchMarginChangeEventScript); | 475 Dispatch(frame, kDispatchMarginChangeEventScript); |
476 } | 476 } |
477 | 477 |
478 // static | 478 // static |
479 void SearchBoxExtension::DispatchMostVisitedChanged( | 479 void SearchBoxExtension::DispatchMostVisitedChanged( |
480 WebKit::WebFrame* frame) { | 480 blink::WebFrame* frame) { |
481 Dispatch(frame, kDispatchMostVisitedChangedScript); | 481 Dispatch(frame, kDispatchMostVisitedChangedScript); |
482 } | 482 } |
483 | 483 |
484 // static | 484 // static |
485 void SearchBoxExtension::DispatchSubmit(WebKit::WebFrame* frame) { | 485 void SearchBoxExtension::DispatchSubmit(blink::WebFrame* frame) { |
486 Dispatch(frame, kDispatchSubmitEventScript); | 486 Dispatch(frame, kDispatchSubmitEventScript); |
487 } | 487 } |
488 | 488 |
489 // static | 489 // static |
490 void SearchBoxExtension::DispatchSuggestionChange(WebKit::WebFrame* frame) { | 490 void SearchBoxExtension::DispatchSuggestionChange(blink::WebFrame* frame) { |
491 Dispatch(frame, kDispatchSuggestionChangeEventScript); | 491 Dispatch(frame, kDispatchSuggestionChangeEventScript); |
492 } | 492 } |
493 | 493 |
494 // static | 494 // static |
495 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { | 495 void SearchBoxExtension::DispatchThemeChange(blink::WebFrame* frame) { |
496 Dispatch(frame, kDispatchThemeChangeEventScript); | 496 Dispatch(frame, kDispatchThemeChangeEventScript); |
497 } | 497 } |
498 | 498 |
499 // static | 499 // static |
500 void SearchBoxExtension::DispatchToggleVoiceSearch( | 500 void SearchBoxExtension::DispatchToggleVoiceSearch( |
501 WebKit::WebFrame* frame) { | 501 blink::WebFrame* frame) { |
502 Dispatch(frame, kDispatchToggleVoiceSearchScript); | 502 Dispatch(frame, kDispatchToggleVoiceSearchScript); |
503 } | 503 } |
504 | 504 |
505 SearchBoxExtensionWrapper::SearchBoxExtensionWrapper( | 505 SearchBoxExtensionWrapper::SearchBoxExtensionWrapper( |
506 const base::StringPiece& code) | 506 const base::StringPiece& code) |
507 : v8::Extension(kSearchBoxExtensionName, code.data(), 0, 0, code.size()) { | 507 : v8::Extension(kSearchBoxExtensionName, code.data(), 0, 0, code.size()) { |
508 } | 508 } |
509 | 509 |
510 v8::Handle<v8::FunctionTemplate> SearchBoxExtensionWrapper::GetNativeFunction( | 510 v8::Handle<v8::FunctionTemplate> SearchBoxExtensionWrapper::GetNativeFunction( |
511 v8::Handle<v8::String> name) { | 511 v8::Handle<v8::String> name) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 return v8::FunctionTemplate::New(UndoAllMostVisitedDeletions); | 553 return v8::FunctionTemplate::New(UndoAllMostVisitedDeletions); |
554 if (name->Equals(v8::String::New("UndoMostVisitedDeletion"))) | 554 if (name->Equals(v8::String::New("UndoMostVisitedDeletion"))) |
555 return v8::FunctionTemplate::New(UndoMostVisitedDeletion); | 555 return v8::FunctionTemplate::New(UndoMostVisitedDeletion); |
556 if (name->Equals(v8::String::New("GetDisplayInstantResults"))) | 556 if (name->Equals(v8::String::New("GetDisplayInstantResults"))) |
557 return v8::FunctionTemplate::New(GetDisplayInstantResults); | 557 return v8::FunctionTemplate::New(GetDisplayInstantResults); |
558 return v8::Handle<v8::FunctionTemplate>(); | 558 return v8::Handle<v8::FunctionTemplate>(); |
559 } | 559 } |
560 | 560 |
561 // static | 561 // static |
562 content::RenderView* SearchBoxExtensionWrapper::GetRenderView() { | 562 content::RenderView* SearchBoxExtensionWrapper::GetRenderView() { |
563 WebKit::WebFrame* webframe = WebKit::WebFrame::frameForCurrentContext(); | 563 blink::WebFrame* webframe = blink::WebFrame::frameForCurrentContext(); |
564 if (!webframe) return NULL; | 564 if (!webframe) return NULL; |
565 | 565 |
566 WebKit::WebView* webview = webframe->view(); | 566 blink::WebView* webview = webframe->view(); |
567 if (!webview) return NULL; // can happen during closing | 567 if (!webview) return NULL; // can happen during closing |
568 | 568 |
569 return content::RenderView::FromWebView(webview); | 569 return content::RenderView::FromWebView(webview); |
570 } | 570 } |
571 | 571 |
572 // static | 572 // static |
573 void SearchBoxExtensionWrapper::CheckIsUserSignedInToChromeAs( | 573 void SearchBoxExtensionWrapper::CheckIsUserSignedInToChromeAs( |
574 const v8::FunctionCallbackInfo<v8::Value>& args) { | 574 const v8::FunctionCallbackInfo<v8::Value>& args) { |
575 content::RenderView* render_view = GetRenderView(); | 575 content::RenderView* render_view = GetRenderView(); |
576 if (!render_view || args.Length() == 0 || args[0]->IsUndefined()) return; | 576 if (!render_view || args.Length() == 0 || args[0]->IsUndefined()) return; |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
997 if (!render_view) return; | 997 if (!render_view) return; |
998 | 998 |
999 bool display_instant_results = | 999 bool display_instant_results = |
1000 SearchBox::Get(render_view)->display_instant_results(); | 1000 SearchBox::Get(render_view)->display_instant_results(); |
1001 DVLOG(1) << render_view << " GetDisplayInstantResults" << | 1001 DVLOG(1) << render_view << " GetDisplayInstantResults" << |
1002 display_instant_results; | 1002 display_instant_results; |
1003 args.GetReturnValue().Set(display_instant_results); | 1003 args.GetReturnValue().Set(display_instant_results); |
1004 } | 1004 } |
1005 | 1005 |
1006 } // namespace extensions_v8 | 1006 } // namespace extensions_v8 |
OLD | NEW |