Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1256)

Side by Side Diff: chrome/renderer/searchbox_extension.cc

Issue 9969191: Make SearchBox extension call through WebFrame to execute page script (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: cleanup Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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_extension.h" 5 #include "chrome/renderer/searchbox_extension.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 " this.setSuggestions = function(text) {" 52 " this.setSuggestions = function(text) {"
53 " SetSuggestions(text);" 53 " SetSuggestions(text);"
54 " };" 54 " };"
55 " this.onchange = null;" 55 " this.onchange = null;"
56 " this.onsubmit = null;" 56 " this.onsubmit = null;"
57 " this.oncancel = null;" 57 " this.oncancel = null;"
58 " this.onresize = null;" 58 " this.onresize = null;"
59 " };" 59 " };"
60 "}"; 60 "}";
61 61
62 static const char kChangeEventName[] = "chrome.searchBox.onchange"; 62 static const char kDispatchChangeEventScript[] =
63 "if (window.chrome &&"
64 " window.chrome.searchBox &&"
65 " window.chrome.searchBox.onchange &&"
66 " typeof window.chrome.searchBox.onchange == 'function') {"
67 " window.chrome.searchBox.onchange();"
68 " true;"
69 "}";
63 70
64 static const char kSubmitEventName[] = "chrome.searchBox.onsubmit"; 71 static const char kDispatchSubmitEventScript[] =
72 "if (window.chrome &&"
73 " window.chrome.searchBox &&"
74 " window.chrome.searchBox.onsubmit &&"
75 " typeof window.chrome.searchBox.onsubmit == 'function') {"
76 " window.chrome.searchBox.onsubmit();"
77 " true;"
78 "}";
65 79
66 static const char kCancelEventName[] = "chrome.searchBox.oncancel"; 80 static const char kDispatchCancelEventScript[] =
81 "if (window.chrome &&"
82 " window.chrome.searchBox &&"
83 " window.chrome.searchBox.oncancel &&"
84 " typeof window.chrome.searchBox.oncancel == 'function') {"
85 " window.chrome.searchBox.oncancel();"
86 " true;"
87 "}";
67 88
68 static const char kResizeEventName[] = "chrome.searchBox.onresize"; 89 static const char kDispatchResizeEventScript[] =
90 "if (window.chrome &&"
91 " window.chrome.searchBox &&"
92 " window.chrome.searchBox.onresize &&"
93 " typeof window.chrome.searchBox.onresize == 'function') {"
94 " window.chrome.searchBox.onresize();"
95 " true;"
96 "}";
69 97
70 // Deprecated API support. 98 // Deprecated API support.
71 // TODO(tonyg): Remove these when they are no longer used. 99 // TODO(tonyg): Remove these when they are no longer used.
72 // ---------------------------------------------------------------------------- 100 // ----------------------------------------------------------------------------
73 // Script sent as the user is typing and the provider supports instant. 101 // Script sent as the user is typing and the provider supports instant.
74 // Params: 102 // Params:
75 // . the text the user typed. 103 // . the text the user typed.
76 // '46' forces the server to give us verbatim results. 104 // '46' forces the server to give us verbatim results.
77 static const char kUserInputScript[] = 105 static const char kUserInputScript[] =
78 "if (window.chrome.userInput)" 106 "if (window.chrome.userInput)"
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 } 370 }
343 } 371 }
344 } 372 }
345 373
346 if (content::RenderView* render_view = GetRenderView()) 374 if (content::RenderView* render_view = GetRenderView())
347 SearchBox::Get(render_view)->SetSuggestions(suggestions, behavior); 375 SearchBox::Get(render_view)->SetSuggestions(suggestions, behavior);
348 return v8::Undefined(); 376 return v8::Undefined();
349 } 377 }
350 378
351 // static 379 // static
352 bool Dispatch(WebFrame* frame, const std::string& event_name) { 380 void Dispatch(WebFrame* frame,
381 WebString event_dispatch_script,
382 WebString no_event_handler_script) {
353 DCHECK(frame) << "Dispatch requires frame"; 383 DCHECK(frame) << "Dispatch requires frame";
354 if (!frame) return false; 384 if (!frame)
385 return;
355 386
356 v8::HandleScope handle_scope; 387 v8::Handle<v8::Value> result = frame->executeScriptAndReturnValue(
357 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); 388 WebScriptSource(event_dispatch_script));
358 if (context.IsEmpty()) 389 if (result.IsEmpty())
359 return false; 390 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.
360 v8::Context::Scope context_scope(context);
361
362 v8::Local<v8::Value> value =
363 context->Global()->Get(v8::String::New("window"));
364 std::vector<std::string> components;
365 base::SplitStringDontTrim(event_name, '.', &components);
366 for (size_t i = 0; i < components.size(); ++i) {
367 if (!value.IsEmpty() && value->IsObject())
368 value = value->ToObject()->Get(v8::String::New(components[i].c_str()));
369 }
370 if (value.IsEmpty() || !value->IsFunction())
371 return false;
372
373 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(value);
374 if (function.IsEmpty())
375 return false;
376
377 function->Call(v8::Object::New(), 0, NULL);
378 return true;
379 } 391 }
380 392
381 // static 393 // static
382 void SearchBoxExtension::DispatchChange(WebFrame* frame) { 394 void SearchBoxExtension::DispatchChange(WebFrame* frame) {
383 if (Dispatch(frame, kChangeEventName)) 395 Dispatch(frame, kDispatchChangeEventScript, kUserInputScript);
384 return;
385 frame->executeScript(WebScriptSource(kUserInputScript));
386 } 396 }
387 397
388 // static 398 // static
389 void SearchBoxExtension::DispatchSubmit(WebFrame* frame) { 399 void SearchBoxExtension::DispatchSubmit(WebFrame* frame) {
390 if (Dispatch(frame, kSubmitEventName)) 400 Dispatch(frame, kDispatchSubmitEventScript, kUserDoneScript);
391 return;
392 frame->executeScript(WebScriptSource(kUserDoneScript));
393 } 401 }
394 402
395 // static 403 // static
396 void SearchBoxExtension::DispatchCancel(WebFrame* frame) { 404 void SearchBoxExtension::DispatchCancel(WebFrame* frame) {
397 if (Dispatch(frame, kCancelEventName)) 405 Dispatch(frame, kDispatchCancelEventScript, kUserDoneScript);
398 return;
399 frame->executeScript(WebScriptSource(kUserDoneScript));
400 } 406 }
401 407
402 // static 408 // static
403 void SearchBoxExtension::DispatchResize(WebFrame* frame) { 409 void SearchBoxExtension::DispatchResize(WebFrame* frame) {
404 if (Dispatch(frame, kResizeEventName)) 410 Dispatch(frame, kDispatchResizeEventScript, kSetOmniboxBoundsScript);
405 return;
406 frame->executeScript(WebScriptSource(kSetOmniboxBoundsScript));
407 } 411 }
408 412
409 // static 413 // static
410 bool SearchBoxExtension::PageSupportsInstant(WebFrame* frame) { 414 bool SearchBoxExtension::PageSupportsInstant(WebFrame* frame) {
411 DCHECK(frame) << "PageSupportsInstant requires frame"; 415 DCHECK(frame) << "PageSupportsInstant requires frame";
412 if (!frame) return false; 416 if (!frame) return false;
413 417
414 v8::Handle<v8::Value> v = frame->executeScriptAndReturnValue( 418 v8::Handle<v8::Value> v = frame->executeScriptAndReturnValue(
415 WebScriptSource(kSupportsInstantScript)); 419 WebScriptSource(kSupportsInstantScript));
416 bool supports_deprecated_api = !v.IsEmpty() && v->BooleanValue(); 420 bool supports_deprecated_api = !v.IsEmpty() && v->BooleanValue();
(...skipping 11 matching lines...) Expand all
428 432
429 return supports_searchbox_api || supports_deprecated_api; 433 return supports_searchbox_api || supports_deprecated_api;
430 } 434 }
431 435
432 // static 436 // static
433 v8::Extension* SearchBoxExtension::Get() { 437 v8::Extension* SearchBoxExtension::Get() {
434 return new SearchBoxExtensionWrapper(); 438 return new SearchBoxExtensionWrapper();
435 } 439 }
436 440
437 } // namespace extensions_v8 441 } // namespace extensions_v8
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698