Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / components / html_viewer / test_html_viewer_impl.cc
blobb0062554cd415c178383d6f3fda2f32a365f6f8b
1 // Copyright 2014 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.
5 #include "components/html_viewer/test_html_viewer_impl.h"
7 #include "base/json/json_writer.h"
8 #include "base/stl_util.h"
9 #include "base/values.h"
10 #include "gin/converter.h"
11 #include "third_party/WebKit/public/platform/WebString.h"
12 #include "third_party/WebKit/public/web/WebDocument.h"
13 #include "third_party/WebKit/public/web/WebLocalFrame.h"
14 #include "third_party/WebKit/public/web/WebScriptExecutionCallback.h"
15 #include "third_party/WebKit/public/web/WebScriptSource.h"
17 using blink::WebDocument;
18 using blink::WebFrame;
20 namespace html_viewer {
22 namespace {
24 std::string V8ValueToJSONString(
25 blink::WebLocalFrame* local_frame,
26 const blink::WebVector<v8::Local<v8::Value>>& result) {
27 // TODO(sky): use V8ValueConverter when refactored to a common place.
28 DCHECK(!result.isEmpty());
29 base::ListValue list_value;
30 for (auto& value : result)
31 list_value.Append(new base::StringValue(gin::V8ToString(value)));
32 std::string json_string;
33 return base::JSONWriter::Write(list_value, &json_string) ? json_string
34 : std::string();
37 } // namespace
39 class TestHTMLViewerImpl::ExecutionCallbackImpl
40 : public blink::WebScriptExecutionCallback {
41 public:
42 ExecutionCallbackImpl(TestHTMLViewerImpl* host,
43 const mojo::Callback<void(mojo::String)>& callback)
44 : host_(host), callback_(callback) {}
45 virtual ~ExecutionCallbackImpl() {}
47 private:
48 // blink::WebScriptExecutionCallback:
49 virtual void completed(const blink::WebVector<v8::Local<v8::Value>>& result) {
50 mojo::String callback_result;
51 if (!result.isEmpty())
52 callback_result = V8ValueToJSONString(host_->web_frame_, result);
53 callback_.Run(callback_result);
54 host_->CallbackCompleted(this);
57 TestHTMLViewerImpl* host_;
58 const mojo::Callback<void(mojo::String)> callback_;
60 DISALLOW_COPY_AND_ASSIGN(ExecutionCallbackImpl);
63 TestHTMLViewerImpl::TestHTMLViewerImpl(
64 blink::WebLocalFrame* web_frame,
65 mojo::InterfaceRequest<TestHTMLViewer> request)
66 : web_frame_(web_frame), binding_(this, request.Pass()) {}
68 TestHTMLViewerImpl::~TestHTMLViewerImpl() {
69 STLDeleteElements(&callbacks_);
72 void TestHTMLViewerImpl::CallbackCompleted(
73 ExecutionCallbackImpl* callback_impl) {
74 scoped_ptr<ExecutionCallbackImpl> owned_callback_impl(callback_impl);
75 callbacks_.erase(callback_impl);
78 void TestHTMLViewerImpl::GetContentAsText(
79 const GetContentAsTextCallback& callback) {
80 callback.Run(web_frame_->document().contentAsTextForTesting().utf8());
83 void TestHTMLViewerImpl::ExecuteScript(const mojo::String& script,
84 const ExecuteScriptCallback& callback) {
85 ExecutionCallbackImpl* callback_impl =
86 new ExecutionCallbackImpl(this, callback);
87 callbacks_.insert(callback_impl);
88 web_frame_->requestExecuteScriptAndReturnValue(
89 blink::WebScriptSource(blink::WebString::fromUTF8(script)), false,
90 callback_impl);
93 } // namespace html_viewer