OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/renderer/search_extension.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/command_line.h" | |
11 #include "chrome/renderer/searchbox.h" | |
12 #include "content/public/renderer/render_view.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
14 #include "v8/include/v8.h" | |
15 | |
16 using WebKit::WebFrame; | |
17 using WebKit::WebView; | |
18 | |
19 namespace extensions_v8 { | |
20 | |
21 const char* const kSearchExtensionName = "v8/InstantSearch"; | |
22 | |
23 class SearchExtensionWrapper : public v8::Extension { | |
24 public: | |
25 SearchExtensionWrapper(); | |
26 | |
27 // Allows v8's javascript code to call the native functions defined | |
28 // in this class for window.chrome. | |
29 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | |
30 v8::Handle<v8::String> name); | |
31 | |
32 // Helper function to find the RenderView. May return NULL. | |
33 static content::RenderView* GetRenderView(); | |
34 | |
35 // Implementation of window.chrome.setSuggestResult. | |
36 static v8::Handle<v8::Value> SetSuggestResult(const v8::Arguments& args); | |
37 | |
38 private: | |
39 DISALLOW_COPY_AND_ASSIGN(SearchExtensionWrapper); | |
40 }; | |
41 | |
42 SearchExtensionWrapper::SearchExtensionWrapper() | |
43 : v8::Extension(kSearchExtensionName, | |
44 "var chrome;" | |
45 "if (!chrome)" | |
46 " chrome = {};" | |
47 "chrome.setSuggestResult = function(text) {" | |
48 " native function NativeSetSuggestResult();" | |
49 " NativeSetSuggestResult(text);" | |
50 "};") { | |
51 } | |
52 | |
53 v8::Handle<v8::FunctionTemplate> SearchExtensionWrapper::GetNativeFunction( | |
54 v8::Handle<v8::String> name) { | |
55 if (name->Equals(v8::String::New("NativeSetSuggestResult"))) { | |
56 return v8::FunctionTemplate::New(SetSuggestResult); | |
57 } | |
58 | |
59 return v8::Handle<v8::FunctionTemplate>(); | |
60 } | |
61 | |
62 // static | |
63 content::RenderView* SearchExtensionWrapper::GetRenderView() { | |
64 WebFrame* webframe = WebFrame::frameForEnteredContext(); | |
65 DCHECK(webframe) << "There should be an active frame since we just got " | |
66 "a native function called."; | |
67 if (!webframe) return NULL; | |
68 | |
69 WebView* webview = webframe->view(); | |
70 if (!webview) return NULL; // can happen during closing | |
71 | |
72 return content::RenderView::FromWebView(webview); | |
73 } | |
74 | |
75 // static | |
76 v8::Handle<v8::Value> SearchExtensionWrapper::SetSuggestResult( | |
77 const v8::Arguments& args) { | |
78 if (!args.Length() || !args[0]->IsString()) return v8::Undefined(); | |
79 | |
80 content::RenderView* render_view = GetRenderView(); | |
81 if (!render_view) return v8::Undefined(); | |
82 | |
83 std::vector<std::string> suggestions; | |
84 suggestions.push_back(std::string(*v8::String::Utf8Value(args[0]))); | |
85 SearchBox::Get(render_view)->SetSuggestions(suggestions, | |
86 INSTANT_COMPLETE_NOW); | |
87 return v8::Undefined(); | |
88 } | |
89 | |
90 v8::Extension* SearchExtension::Get() { | |
91 return new SearchExtensionWrapper(); | |
92 } | |
93 | |
94 } // namespace extensions_v8 | |
OLD | NEW |