Commit 533ba2ca authored by rafaelw@chromium.org's avatar rafaelw@chromium.org

Make SearchBox extension call through WebFrame to execute page script

This cleans up another instance of renderer code directly invoking page/author script. When all instances are fixed, we can land https://bugs.webkit.org/show_bug.cgi?id=84094

BUG=
TEST=

Review URL: http://codereview.chromium.org/9969191

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132526 0039d316-1c4b-4281-b951-d872f2087c98
parent 2e4bafe9
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -59,13 +59,41 @@ static const char kSearchBoxExtensionScript[] = ...@@ -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. // Deprecated API support.
// TODO(tonyg): Remove these when they are no longer used. // TODO(tonyg): Remove these when they are no longer used.
...@@ -349,61 +377,37 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions( ...@@ -349,61 +377,37 @@ v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions(
} }
// static // 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"; DCHECK(frame) << "Dispatch requires frame";
if (!frame) return false; if (!frame)
return;
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;
function->Call(v8::Object::New(), 0, NULL); v8::Handle<v8::Value> result = frame->executeScriptAndReturnValue(
return true; WebScriptSource(event_dispatch_script));
if (result.IsEmpty())
frame->executeScript(WebScriptSource(no_event_handler_script));
} }
// static // static
void SearchBoxExtension::DispatchChange(WebFrame* frame) { void SearchBoxExtension::DispatchChange(WebFrame* frame) {
if (Dispatch(frame, kChangeEventName)) Dispatch(frame, kDispatchChangeEventScript, kUserInputScript);
return;
frame->executeScript(WebScriptSource(kUserInputScript));
} }
// static // static
void SearchBoxExtension::DispatchSubmit(WebFrame* frame) { void SearchBoxExtension::DispatchSubmit(WebFrame* frame) {
if (Dispatch(frame, kSubmitEventName)) Dispatch(frame, kDispatchSubmitEventScript, kUserDoneScript);
return;
frame->executeScript(WebScriptSource(kUserDoneScript));
} }
// static // static
void SearchBoxExtension::DispatchCancel(WebFrame* frame) { void SearchBoxExtension::DispatchCancel(WebFrame* frame) {
if (Dispatch(frame, kCancelEventName)) Dispatch(frame, kDispatchCancelEventScript, kUserDoneScript);
return;
frame->executeScript(WebScriptSource(kUserDoneScript));
} }
// static // static
void SearchBoxExtension::DispatchResize(WebFrame* frame) { void SearchBoxExtension::DispatchResize(WebFrame* frame) {
if (Dispatch(frame, kResizeEventName)) Dispatch(frame, kDispatchResizeEventScript, kSetOmniboxBoundsScript);
return;
frame->executeScript(WebScriptSource(kSetOmniboxBoundsScript));
} }
// static // static
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment