summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/search_extension.cc
blob: 384e94493aa820cfc2e3a574e356a022b302e7f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/search_extension.h"

#include <string>
#include <vector>

#include "base/command_line.h"
#include "chrome/renderer/searchbox.h"
#include "content/renderer/render_view.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "v8/include/v8.h"

using WebKit::WebFrame;
using WebKit::WebView;

namespace extensions_v8 {

const char* const kSearchExtensionName = "v8/InstantSearch";

class SearchExtensionWrapper : public v8::Extension {
 public:
  SearchExtensionWrapper();

  // Allows v8's javascript code to call the native functions defined
  // in this class for window.chrome.
  virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
      v8::Handle<v8::String> name);

  // Helper function to find the RenderView. May return NULL.
  static RenderView* GetRenderView();

  // Implementation of window.chrome.setSuggestResult.
  static v8::Handle<v8::Value> SetSuggestResult(const v8::Arguments& args);

 private:
  DISALLOW_COPY_AND_ASSIGN(SearchExtensionWrapper);
};

SearchExtensionWrapper::SearchExtensionWrapper()
    : v8::Extension(kSearchExtensionName,
      "var chrome;"
      "if (!chrome)"
      "  chrome = {};"
      "chrome.setSuggestResult = function(text) {"
      "  native function NativeSetSuggestResult();"
      "  NativeSetSuggestResult(text);"
      "};") {
}

v8::Handle<v8::FunctionTemplate> SearchExtensionWrapper::GetNativeFunction(
    v8::Handle<v8::String> name) {
  if (name->Equals(v8::String::New("NativeSetSuggestResult"))) {
    return v8::FunctionTemplate::New(SetSuggestResult);
  }

  return v8::Handle<v8::FunctionTemplate>();
}

// static
RenderView* SearchExtensionWrapper::GetRenderView() {
  WebFrame* webframe = WebFrame::frameForEnteredContext();
  DCHECK(webframe) << "There should be an active frame since we just got "
      "a native function called.";
  if (!webframe) return NULL;

  WebView* webview = webframe->view();
  if (!webview) return NULL;  // can happen during closing

  return RenderView::FromWebView(webview);
}

// static
v8::Handle<v8::Value> SearchExtensionWrapper::SetSuggestResult(
    const v8::Arguments& args) {
  if (!args.Length() || !args[0]->IsString()) return v8::Undefined();

  RenderView* render_view = GetRenderView();
  if (!render_view) return v8::Undefined();

  std::vector<std::string> suggestions;
  suggestions.push_back(std::string(*v8::String::Utf8Value(args[0])));
  SearchBox::Get(render_view)->SetSuggestions(suggestions,
                                              INSTANT_COMPLETE_NOW);
  return v8::Undefined();
}

v8::Extension* SearchExtension::Get() {
  return new SearchExtensionWrapper();
}

}  // namespace extensions_v8