Chromium Code Reviews| Index: chrome/renderer/searchbox_extension.cc |
| diff --git a/chrome/renderer/searchbox_extension.cc b/chrome/renderer/searchbox_extension.cc |
| index e04bf9131ba327c3c45e5a8f0ba1c9ba0f3d2709..e69a4c436524809289299931c6246f46d32e74e9 100644 |
| --- a/chrome/renderer/searchbox_extension.cc |
| +++ b/chrome/renderer/searchbox_extension.cc |
| @@ -59,13 +59,41 @@ static const char kSearchBoxExtensionScript[] = |
| " };" |
| "}"; |
| -static const char kChangeEventName[] = "chrome.searchBox.onchange"; |
| +static const char kDispatchChangeEventScript[] = |
| + "if (window.chrome &&" |
| + " window.chrome.searchBox &&" |
| + " window.chrome.searchBox.onchange &&" |
| + " typeof window.chrome.searchBox.onchange == 'function') {" |
| + " window.chrome.searchBox.onchange();" |
| + " true;" |
| + "}"; |
| -static const char kSubmitEventName[] = "chrome.searchBox.onsubmit"; |
| +static const char kDispatchSubmitEventScript[] = |
| + "if (window.chrome &&" |
| + " window.chrome.searchBox &&" |
| + " window.chrome.searchBox.onsubmit &&" |
| + " typeof window.chrome.searchBox.onsubmit == 'function') {" |
| + " window.chrome.searchBox.onsubmit();" |
| + " true;" |
| + "}"; |
| -static const char kCancelEventName[] = "chrome.searchBox.oncancel"; |
| +static const char kDispatchCancelEventScript[] = |
| + "if (window.chrome &&" |
| + " window.chrome.searchBox &&" |
| + " window.chrome.searchBox.oncancel &&" |
| + " typeof window.chrome.searchBox.oncancel == 'function') {" |
| + " window.chrome.searchBox.oncancel();" |
| + " true;" |
| + "}"; |
| -static const char kResizeEventName[] = "chrome.searchBox.onresize"; |
| +static const char kDispatchResizeEventScript[] = |
| + "if (window.chrome &&" |
| + " window.chrome.searchBox &&" |
| + " window.chrome.searchBox.onresize &&" |
| + " typeof window.chrome.searchBox.onresize == 'function') {" |
| + " window.chrome.searchBox.onresize();" |
| + " true;" |
| + "}"; |
| // Deprecated API support. |
| // TODO(tonyg): Remove these when they are no longer used. |
| @@ -349,61 +377,37 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions( |
| } |
| // static |
| -bool Dispatch(WebFrame* frame, const std::string& event_name) { |
| +void Dispatch(WebFrame* frame, |
| + WebString event_dispatch_script, |
| + WebString no_event_handler_script) { |
| DCHECK(frame) << "Dispatch requires frame"; |
| - if (!frame) return false; |
| - |
| - v8::HandleScope handle_scope; |
| - v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| - if (context.IsEmpty()) |
| - return false; |
| - v8::Context::Scope context_scope(context); |
| - |
| - v8::Local<v8::Value> value = |
| - context->Global()->Get(v8::String::New("window")); |
| - std::vector<std::string> components; |
| - base::SplitStringDontTrim(event_name, '.', &components); |
| - for (size_t i = 0; i < components.size(); ++i) { |
| - if (!value.IsEmpty() && value->IsObject()) |
| - value = value->ToObject()->Get(v8::String::New(components[i].c_str())); |
| - } |
| - if (value.IsEmpty() || !value->IsFunction()) |
| - return false; |
| - |
| - v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value); |
| - if (function.IsEmpty()) |
| - return false; |
| + if (!frame) |
| + return; |
| - function->Call(v8::Object::New(), 0, NULL); |
| - return true; |
| + v8::Handle<v8::Value> result = frame->executeScriptAndReturnValue( |
| + WebScriptSource(event_dispatch_script)); |
| + if (result.IsEmpty()) |
| + frame->executeScript(WebScriptSource(no_event_handler_script)); |
|
sky
2012/04/16 23:37:16
nit: 2 space indent.
rafaelw
2012/04/16 23:41:29
Done.
|
| } |
| // static |
| void SearchBoxExtension::DispatchChange(WebFrame* frame) { |
| - if (Dispatch(frame, kChangeEventName)) |
| - return; |
| - frame->executeScript(WebScriptSource(kUserInputScript)); |
| + Dispatch(frame, kDispatchChangeEventScript, kUserInputScript); |
| } |
| // static |
| void SearchBoxExtension::DispatchSubmit(WebFrame* frame) { |
| - if (Dispatch(frame, kSubmitEventName)) |
| - return; |
| - frame->executeScript(WebScriptSource(kUserDoneScript)); |
| + Dispatch(frame, kDispatchSubmitEventScript, kUserDoneScript); |
| } |
| // static |
| void SearchBoxExtension::DispatchCancel(WebFrame* frame) { |
| - if (Dispatch(frame, kCancelEventName)) |
| - return; |
| - frame->executeScript(WebScriptSource(kUserDoneScript)); |
| + Dispatch(frame, kDispatchCancelEventScript, kUserDoneScript); |
| } |
| // static |
| void SearchBoxExtension::DispatchResize(WebFrame* frame) { |
| - if (Dispatch(frame, kResizeEventName)) |
| - return; |
| - frame->executeScript(WebScriptSource(kSetOmniboxBoundsScript)); |
| + Dispatch(frame, kDispatchResizeEventScript, kSetOmniboxBoundsScript); |
| } |
| // static |