| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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/contextualsearch/contextual_search_wrapper.h" |
| 6 |
| 7 #include "base/strings/string_util.h" |
| 8 #include "gin/arguments.h" |
| 9 #include "gin/handle.h" |
| 10 #include "gin/object_template_builder.h" |
| 11 #include "third_party/WebKit/public/web/WebFrame.h" |
| 12 #include "third_party/WebKit/public/web/WebKit.h" |
| 13 #include "v8/include/v8.h" |
| 14 |
| 15 namespace search { |
| 16 |
| 17 static const char kContextualSearchObjectName[] = "contextualSearch"; |
| 18 static const char kSetCaptionMethodName[] = "setCaption"; |
| 19 |
| 20 // TODO(donnd): merge with the identical code in |
| 21 // chrome_object_extensions_utils (which chrome-android can't access). |
| 22 v8::Local<v8::Object> GetOrCreateChromeObject(v8::Isolate* isolate, |
| 23 v8::Local<v8::Object> global) { |
| 24 v8::Local<v8::Object> chrome; |
| 25 v8::Local<v8::Value> chrome_value = |
| 26 global->Get(gin::StringToV8(isolate, "chrome")); |
| 27 if (chrome_value.IsEmpty() || !chrome_value->IsObject()) { |
| 28 chrome = v8::Object::New(isolate); |
| 29 global->Set(gin::StringToSymbol(isolate, "chrome"), chrome); |
| 30 } else { |
| 31 chrome = v8::Local<v8::Object>::Cast(chrome_value); |
| 32 } |
| 33 return chrome; |
| 34 } |
| 35 |
| 36 gin::WrapperInfo ContextualSearchWrapper::kWrapperInfo = { |
| 37 gin::kEmbedderNativeGin}; |
| 38 |
| 39 // static |
| 40 void ContextualSearchWrapper::Install(blink::WebFrame* frame) { |
| 41 // TODO(donnd): refactor some of this boilerplate into a reusable |
| 42 // method. This was cribbed from MemoryBenchmarkingExtension. |
| 43 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 44 v8::HandleScope handle_scope(isolate); |
| 45 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 46 if (context.IsEmpty()) |
| 47 return; |
| 48 |
| 49 v8::Context::Scope context_scope(context); |
| 50 gin::Handle<ContextualSearchWrapper> wrapper = |
| 51 gin::CreateHandle(isolate, new ContextualSearchWrapper()); |
| 52 if (wrapper.IsEmpty()) |
| 53 return; |
| 54 |
| 55 v8::Local<v8::Object> chrome = |
| 56 GetOrCreateChromeObject(isolate, context->Global()); |
| 57 chrome->Set(gin::StringToV8(isolate, kContextualSearchObjectName), |
| 58 wrapper.ToV8()); |
| 59 } |
| 60 |
| 61 ContextualSearchWrapper::ContextualSearchWrapper() {} |
| 62 |
| 63 ContextualSearchWrapper::~ContextualSearchWrapper() {} |
| 64 |
| 65 gin::ObjectTemplateBuilder ContextualSearchWrapper::GetObjectTemplateBuilder( |
| 66 v8::Isolate* isolate) { |
| 67 return gin::Wrappable<ContextualSearchWrapper>::GetObjectTemplateBuilder( |
| 68 isolate) |
| 69 .SetMethod(kSetCaptionMethodName, &ContextualSearchWrapper::SetCaption); |
| 70 } |
| 71 |
| 72 // static |
| 73 void ContextualSearchWrapper::SetCaption(const std::string& caption) { |
| 74 DVLOG(0) << "ctxs SetCaption: caption: " << caption; |
| 75 // TODO(donnd): send the caption to the Browser! |
| 76 } |
| 77 |
| 78 } // namespace search |
| OLD | NEW |