Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 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 "base/command_line.h" | |
| 8 #include "chrome/common/chrome_switches.h" | |
| 9 #include "chrome/common/render_messages_params.h" | |
| 10 #include "chrome/renderer/render_view.h" | |
| 11 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | |
| 12 #include "v8/include/v8.h" | |
| 13 | |
| 14 using WebKit::WebFrame; | |
| 15 using WebKit::WebView; | |
| 16 | |
| 17 namespace extensions_v8 { | |
| 18 | |
| 19 const char* const kSearchExtensionName = "v8/InstantSearch"; | |
| 20 | |
| 21 class SearchExtensionWrapper : public v8::Extension { | |
| 22 public: | |
| 23 SearchExtensionWrapper(); | |
| 24 | |
| 25 // Allows v8's javascript code to call the native functions defined | |
| 26 // in this class for window.chrome. | |
| 27 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | |
| 28 v8::Handle<v8::String> name); | |
| 29 | |
| 30 // Helper function to find the RenderView. May return NULL. | |
| 31 static RenderView* GetRenderView(); | |
| 32 | |
| 33 // Implementation of window.chrome.setSuggestResult. | |
| 34 static v8::Handle<v8::Value> SetSuggestResult(const v8::Arguments& args); | |
| 35 | |
| 36 private: | |
| 37 DISALLOW_COPY_AND_ASSIGN(SearchExtensionWrapper); | |
| 38 }; | |
| 39 | |
| 40 SearchExtensionWrapper::SearchExtensionWrapper() | |
| 41 : v8::Extension(kSearchExtensionName, | |
| 42 "var chrome;" | |
| 43 "if (!chrome)" | |
| 44 " chrome = {};" | |
| 45 "chrome.setSuggestResult = function(text) {" | |
|
Mike Belshe
2010/09/10 18:15:06
note: This function will be available to anyone th
| |
| 46 " native function NativeSetSuggestResult();" | |
| 47 " NativeSetSuggestResult(text);" | |
| 48 "};") { | |
| 49 } | |
| 50 | |
| 51 v8::Handle<v8::FunctionTemplate> SearchExtensionWrapper::GetNativeFunction( | |
| 52 v8::Handle<v8::String> name) { | |
| 53 if (name->Equals(v8::String::New("NativeSetSuggestResult"))) { | |
| 54 return v8::FunctionTemplate::New(SetSuggestResult); | |
| 55 } | |
| 56 | |
| 57 return v8::Handle<v8::FunctionTemplate>(); | |
| 58 } | |
| 59 | |
| 60 // static | |
| 61 RenderView* SearchExtensionWrapper::GetRenderView() { | |
| 62 WebFrame* webframe = WebFrame::frameForEnteredContext(); | |
| 63 DCHECK(webframe) << "There should be an active frame since we just got " | |
| 64 "a native function called."; | |
| 65 if (!webframe) return NULL; | |
|
Mike Belshe
2010/09/10 18:15:06
nit: are we supposed to break this to two lines?
| |
| 66 | |
| 67 WebView* webview = webframe->view(); | |
| 68 if (!webview) return NULL; // can happen during closing | |
| 69 | |
| 70 return RenderView::FromWebView(webview); | |
| 71 } | |
| 72 | |
| 73 // static | |
| 74 v8::Handle<v8::Value> SearchExtensionWrapper::SetSuggestResult( | |
| 75 const v8::Arguments& args) { | |
| 76 if (!args.Length()) return v8::Undefined(); | |
| 77 | |
| 78 std::string suggest = std::string(*v8::String::Utf8Value(args[0])); | |
| 79 if (!suggest.length()) return v8::Undefined(); | |
| 80 | |
| 81 RenderView* render_view = GetRenderView(); | |
| 82 if (!render_view) return v8::Undefined(); | |
| 83 | |
| 84 render_view->SetSuggestResult(suggest); | |
| 85 return v8::Undefined(); | |
| 86 } | |
| 87 | |
| 88 v8::Extension* SearchExtension::Get() { | |
| 89 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 90 switches::kEnableMatchPreview)) { | |
| 91 return new SearchExtensionWrapper(); | |
| 92 } | |
| 93 return NULL; | |
| 94 } | |
| 95 | |
| 96 } // namespace extensions_v8 | |
| OLD | NEW |