Index: chrome/renderer/searchbox_extension.cc |
diff --git a/chrome/renderer/searchbox_extension.cc b/chrome/renderer/searchbox_extension.cc |
index 29bddcfecf0669d5b96f5d5210f016fbc3dda72c..fe027bba74ddbe2091eb03c4ad58d14fd176a087 100644 |
--- a/chrome/renderer/searchbox_extension.cc |
+++ b/chrome/renderer/searchbox_extension.cc |
@@ -4,27 +4,14 @@ |
#include "chrome/renderer/searchbox_extension.h" |
-#include <string> |
-#include <vector> |
- |
-#include "base/basictypes.h" |
-#include "base/command_line.h" |
-#include "base/string_split.h" |
-#include "base/stringprintf.h" |
#include "chrome/renderer/searchbox.h" |
#include "content/public/renderer/render_view.h" |
#include "grit/renderer_resources.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" |
-#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
#include "ui/base/resource/resource_bundle.h" |
#include "v8/include/v8.h" |
-using WebKit::WebFrame; |
-using WebKit::WebScriptSource; |
-using WebKit::WebString; |
-using WebKit::WebView; |
- |
namespace extensions_v8 { |
static const char kSearchBoxExtensionName[] = "v8/SearchBox"; |
@@ -65,59 +52,17 @@ static const char kDispatchResizeEventScript[] = |
" true;" |
"}"; |
-// Deprecated API support. |
-// TODO(tonyg): Remove these when they are no longer used. |
-// ---------------------------------------------------------------------------- |
-// Script sent as the user is typing and the provider supports instant. |
-// Params: |
-// . the text the user typed. |
-// '46' forces the server to give us verbatim results. |
-static const char kUserInputScript[] = |
- "if (window.chrome.userInput)" |
- " window.chrome.userInput(" |
- " window.chrome.searchBox.value," |
- " window.chrome.searchBox.verbatim ? 46 : 0," |
- " window.chrome.searchBox.selectionStart);"; |
- |
-// Script sent when the page is committed and the provider supports instant. |
-// Params: |
-// . the text the user typed. |
-// . boolean indicating if the user pressed enter to accept the text. |
-static const char kUserDoneScript[] = |
- "if (window.chrome.userWantsQuery)" |
- " window.chrome.userWantsQuery(" |
- " window.chrome.searchBox.value," |
- " window.chrome.searchBox.verbatim);"; |
- |
-// Script sent when the bounds of the omnibox changes and the provider supports |
-// instant. The params are the bounds relative to the origin of the preview |
-// (x, y, width, height). |
-static const char kSetOmniboxBoundsScript[] = |
- "if (window.chrome.setDropdownDimensions)" |
- " window.chrome.setDropdownDimensions(" |
- " window.chrome.searchBox.x," |
- " window.chrome.searchBox.y," |
- " window.chrome.searchBox.width," |
- " window.chrome.searchBox.height);"; |
- |
// We first send this script down to determine if the page supports instant. |
static const char kSupportsInstantScript[] = |
- "if (window.chrome.sv) true; else false;"; |
- |
-// The google.y.first array is a list of functions which are to be executed |
-// after the external JavaScript used by Google web search loads. The deprecated |
-// API requires setDropdownDimensions and userInput to be invoked after |
-// the external JavaScript loads. So if they are not already registered, we add |
-// them to the array of functions the page will execute after load. This tight |
-// coupling discourages proliferation of the deprecated API. |
-static const char kInitScript[] = |
- "(function() {" |
- "var initScript = function(){%s%s};" |
- "if (window.chrome.setDropdownDimensions)" |
- " initScript();" |
- "else if (window.google && window.google.y)" |
- " window.google.y.first.push(initScript);" |
- "})();"; |
+ "if (window.chrome &&" |
+ " window.chrome.searchBox &&" |
+ " window.chrome.searchBox.onsubmit &&" |
+ " typeof window.chrome.searchBox.onsubmit == 'function') {" |
+ " true;" |
+ "} else {" |
+ " false;" |
+ "}"; |
+ |
// ---------------------------------------------------------------------------- |
class SearchBoxExtensionWrapper : public v8::Extension { |
@@ -169,7 +114,8 @@ class SearchBoxExtensionWrapper : public v8::Extension { |
SearchBoxExtensionWrapper::SearchBoxExtensionWrapper( |
const base::StringPiece& code) |
- : v8::Extension(kSearchBoxExtensionName, code.data(), 0, 0, code.size()) {} |
+ : v8::Extension(kSearchBoxExtensionName, code.data(), 0, 0, code.size()) { |
+} |
v8::Handle<v8::FunctionTemplate> SearchBoxExtensionWrapper::GetNativeFunction( |
v8::Handle<v8::String> name) { |
@@ -197,12 +143,12 @@ v8::Handle<v8::FunctionTemplate> SearchBoxExtensionWrapper::GetNativeFunction( |
// static |
content::RenderView* SearchBoxExtensionWrapper::GetRenderView() { |
- WebFrame* webframe = WebFrame::frameForEnteredContext(); |
+ WebKit::WebFrame* webframe = WebKit::WebFrame::frameForEnteredContext(); |
DCHECK(webframe) << "There should be an active frame since we just got " |
- "a native function called."; |
+ "a native function called."; |
if (!webframe) return NULL; |
- WebView* webview = webframe->view(); |
+ WebKit::WebView* webview = webframe->view(); |
if (!webview) return NULL; // can happen during closing |
return content::RenderView::FromWebView(webview); |
@@ -215,7 +161,7 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetValue( |
if (!render_view) return v8::Undefined(); |
return v8::String::New( |
reinterpret_cast<const uint16_t*>( |
- SearchBox::Get(render_view)->value().c_str()), |
+ SearchBox::Get(render_view)->value().data()), |
SearchBox::Get(render_view)->value().length()); |
} |
@@ -232,7 +178,7 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetSelectionStart( |
const v8::Arguments& args) { |
content::RenderView* render_view = GetRenderView(); |
if (!render_view) return v8::Undefined(); |
- return v8::Int32::New(SearchBox::Get(render_view)->selection_start()); |
+ return v8::Uint32::New(SearchBox::Get(render_view)->selection_start()); |
} |
// static |
@@ -240,7 +186,7 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetSelectionEnd( |
const v8::Arguments& args) { |
content::RenderView* render_view = GetRenderView(); |
if (!render_view) return v8::Undefined(); |
- return v8::Int32::New(SearchBox::Get(render_view)->selection_end()); |
+ return v8::Uint32::New(SearchBox::Get(render_view)->selection_end()); |
} |
// static |
@@ -275,36 +221,30 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( |
return v8::Int32::New(SearchBox::Get(render_view)->GetRect().height()); |
} |
-// Accepts a single argument in form: |
-// { |
-// suggestions: [ |
-// { |
-// value: "..." |
-// } |
-// ] |
-// } |
// static |
v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions( |
const v8::Arguments& args) { |
- std::vector<std::string> suggestions; |
+ std::vector<string16> suggestions; |
InstantCompleteBehavior behavior = INSTANT_COMPLETE_NOW; |
- if (args.Length() && args[0]->IsArray()) { |
- // For backwards compatibility, also accept an array of strings. |
- // TODO(tonyg): Remove this when it is confirmed to be unused. |
- v8::Local<v8::Array> suggestions_array = |
- v8::Local<v8::Array>::Cast(args[0]); |
- uint32_t length = suggestions_array->Length(); |
- for (uint32_t i = 0; i < length; i++) { |
- std::string suggestion = *v8::String::Utf8Value( |
- suggestions_array->Get(v8::Integer::New(i))->ToString()); |
- if (!suggestion.length()) continue; |
- suggestions.push_back(suggestion); |
+ if (args.Length() && args[0]->IsObject()) { |
+ v8::Local<v8::Object> suggestion_json = args[0]->ToObject(); |
+ |
+ v8::Local<v8::Value> complete_value = |
+ suggestion_json->Get(v8::String::New("complete_behavior")); |
+ if (complete_value->IsString()) { |
+ if (complete_value->Equals(v8::String::New("now"))) { |
+ behavior = INSTANT_COMPLETE_NOW; |
+ } else if (complete_value->Equals(v8::String::New("never"))) { |
+ behavior = INSTANT_COMPLETE_NEVER; |
+ } else if (complete_value->Equals(v8::String::New("delayed"))) { |
+ behavior = INSTANT_COMPLETE_DELAYED; |
+ } else { |
+ VLOG(1) << "Unsupported complete behavior '" |
+ << *v8::String::Utf8Value(complete_value) << "'"; |
+ } |
} |
- } else if (args.Length() && args[0]->IsObject()) { |
- // Standard version, object argument. |
- v8::Local<v8::Object> suggestion_json = |
- v8::Local<v8::Object>::Cast(args[0]); |
+ |
v8::Local<v8::Value> suggestions_field = |
suggestion_json->Get(v8::String::New("suggestions")); |
@@ -312,34 +252,21 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions( |
v8::Local<v8::Array> suggestions_array = |
suggestions_field.As<v8::Array>(); |
- uint32_t length = suggestions_array->Length(); |
- for (uint32_t i = 0; i < length; i++) { |
- v8::Local<v8::Value> suggestion_value = |
- suggestions_array->Get(v8::Integer::New(i)); |
+ size_t length = suggestions_array->Length(); |
+ for (size_t i = 0; i < length; i++) { |
+ v8::Local<v8::Value> suggestion_value = suggestions_array->Get(i); |
if (!suggestion_value->IsObject()) continue; |
- v8::Local<v8::Object> suggestion_object = |
- suggestion_value.As<v8::Object>(); |
+ v8::Local<v8::Object> suggestion_object = suggestion_value->ToObject(); |
v8::Local<v8::Value> suggestion_object_value = |
suggestion_object->Get(v8::String::New("value")); |
if (!suggestion_object_value->IsString()) continue; |
- std::string suggestion = *v8::String::Utf8Value( |
- suggestion_object_value->ToString()); |
- if (!suggestion.length()) continue; |
+ string16 suggestion(reinterpret_cast<char16*>(*v8::String::Value( |
+ suggestion_object_value->ToString()))); |
suggestions.push_back(suggestion); |
} |
} |
- if (suggestion_json->Has(v8::String::New("complete_behavior"))) { |
- v8::Local<v8::Value> complete_value = |
- suggestion_json->Get(v8::String::New("complete_behavior")); |
- if (complete_value->IsString()) { |
- if (complete_value->Equals(v8::String::New("never"))) |
- behavior = INSTANT_COMPLETE_NEVER; |
- else if (complete_value->Equals(v8::String::New("delayed"))) |
- behavior = INSTANT_COMPLETE_DELAYED; |
- } |
- } |
} |
if (content::RenderView* render_view = GetRenderView()) |
@@ -348,62 +275,48 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions( |
} |
// static |
-void Dispatch(WebFrame* frame, |
- WebString event_dispatch_script, |
- WebString no_event_handler_script) { |
+void Dispatch(WebKit::WebFrame* frame, WebKit::WebString script) { |
DCHECK(frame) << "Dispatch requires frame"; |
- if (!frame) |
- return; |
- |
- v8::Handle<v8::Value> result = frame->executeScriptAndReturnValue( |
- WebScriptSource(event_dispatch_script)); |
- if (result.IsEmpty() || result->IsUndefined() || result->IsNull() || |
- result->IsFalse()) { |
- frame->executeScript(WebScriptSource(no_event_handler_script)); |
- } |
+ if (!frame) return; |
+ frame->executeScript(WebKit::WebScriptSource(script)); |
} |
// static |
-void SearchBoxExtension::DispatchChange(WebFrame* frame) { |
- Dispatch(frame, kDispatchChangeEventScript, kUserInputScript); |
+void SearchBoxExtension::DispatchChange(WebKit::WebFrame* frame) { |
+ Dispatch(frame, kDispatchChangeEventScript); |
} |
// static |
-void SearchBoxExtension::DispatchSubmit(WebFrame* frame) { |
- Dispatch(frame, kDispatchSubmitEventScript, kUserDoneScript); |
+void SearchBoxExtension::DispatchSubmit(WebKit::WebFrame* frame) { |
+ Dispatch(frame, kDispatchSubmitEventScript); |
} |
// static |
-void SearchBoxExtension::DispatchCancel(WebFrame* frame) { |
- Dispatch(frame, kDispatchCancelEventScript, kUserDoneScript); |
+void SearchBoxExtension::DispatchCancel(WebKit::WebFrame* frame) { |
+ Dispatch(frame, kDispatchCancelEventScript); |
} |
// static |
-void SearchBoxExtension::DispatchResize(WebFrame* frame) { |
- Dispatch(frame, kDispatchResizeEventScript, kSetOmniboxBoundsScript); |
+void SearchBoxExtension::DispatchResize(WebKit::WebFrame* frame) { |
+ Dispatch(frame, kDispatchResizeEventScript); |
} |
// static |
-bool SearchBoxExtension::PageSupportsInstant(WebFrame* frame) { |
+bool SearchBoxExtension::PageSupportsInstant(WebKit::WebFrame* frame) { |
DCHECK(frame) << "PageSupportsInstant requires frame"; |
if (!frame) return false; |
v8::Handle<v8::Value> v = frame->executeScriptAndReturnValue( |
- WebScriptSource(kSupportsInstantScript)); |
- bool supports_deprecated_api = !v.IsEmpty() && v->BooleanValue(); |
- // TODO(tonyg): Add way of detecting instant support to SearchBox API. |
- bool supports_searchbox_api = supports_deprecated_api; |
- |
- // The deprecated API needs to notify the page of events it may have missed. |
- // This isn't necessary in the SearchBox API, since the page can query the |
- // API at any time. |
- CR_DEFINE_STATIC_LOCAL(std::string, init_script, |
- (StringPrintf(kInitScript, kSetOmniboxBoundsScript, kUserInputScript))); |
- if (supports_deprecated_api) { |
- frame->executeScript(WebScriptSource(WebString::fromUTF8(init_script))); |
- } |
+ WebKit::WebScriptSource(kSupportsInstantScript)); |
+ bool supports_instant = !v.IsEmpty() && v->BooleanValue(); |
+ |
+ // Send a resize message to tell the page that Chrome is actively using the |
+ // searchbox API with it. The page uses the message to transition from |
+ // "homepage" mode to "search" mode. |
+ if (supports_instant) |
+ DispatchResize(frame); |
- return supports_searchbox_api || supports_deprecated_api; |
+ return supports_instant; |
} |
// static |