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