OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/html_viewer/test_html_viewer_impl.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/json/json_writer.h" | |
10 #include "base/macros.h" | |
11 #include "base/stl_util.h" | |
12 #include "base/values.h" | |
13 #include "gin/converter.h" | |
14 #include "third_party/WebKit/public/platform/WebString.h" | |
15 #include "third_party/WebKit/public/web/WebDocument.h" | |
16 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
17 #include "third_party/WebKit/public/web/WebScriptExecutionCallback.h" | |
18 #include "third_party/WebKit/public/web/WebScriptSource.h" | |
19 | |
20 using blink::WebDocument; | |
21 using blink::WebFrame; | |
22 | |
23 namespace html_viewer { | |
24 | |
25 namespace { | |
26 | |
27 std::string V8ValueToJSONString( | |
28 blink::WebLocalFrame* local_frame, | |
29 const blink::WebVector<v8::Local<v8::Value>>& result) { | |
30 // TODO(sky): use V8ValueConverter when refactored to a common place. | |
31 DCHECK(!result.isEmpty()); | |
32 base::ListValue list_value; | |
33 for (auto& value : result) | |
34 list_value.Append(new base::StringValue(gin::V8ToString(value))); | |
35 std::string json_string; | |
36 return base::JSONWriter::Write(list_value, &json_string) ? json_string | |
37 : std::string(); | |
38 } | |
39 | |
40 } // namespace | |
41 | |
42 class TestHTMLViewerImpl::ExecutionCallbackImpl | |
43 : public blink::WebScriptExecutionCallback { | |
44 public: | |
45 ExecutionCallbackImpl(TestHTMLViewerImpl* host, | |
46 const mojo::Callback<void(mojo::String)>& callback) | |
47 : host_(host), callback_(callback) {} | |
48 ~ExecutionCallbackImpl() override {} | |
49 | |
50 private: | |
51 // blink::WebScriptExecutionCallback: | |
52 void completed( | |
53 const blink::WebVector<v8::Local<v8::Value>>& result) override { | |
54 mojo::String callback_result; | |
55 if (!result.isEmpty()) | |
56 callback_result = V8ValueToJSONString(host_->web_frame_, result); | |
57 callback_.Run(callback_result); | |
58 host_->CallbackCompleted(this); | |
59 } | |
60 | |
61 TestHTMLViewerImpl* host_; | |
62 const mojo::Callback<void(mojo::String)> callback_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(ExecutionCallbackImpl); | |
65 }; | |
66 | |
67 TestHTMLViewerImpl::TestHTMLViewerImpl( | |
68 blink::WebLocalFrame* web_frame, | |
69 mojo::InterfaceRequest<TestHTMLViewer> request) | |
70 : web_frame_(web_frame), binding_(this, std::move(request)) {} | |
71 | |
72 TestHTMLViewerImpl::~TestHTMLViewerImpl() { | |
73 STLDeleteElements(&callbacks_); | |
74 } | |
75 | |
76 void TestHTMLViewerImpl::CallbackCompleted( | |
77 ExecutionCallbackImpl* callback_impl) { | |
78 scoped_ptr<ExecutionCallbackImpl> owned_callback_impl(callback_impl); | |
79 callbacks_.erase(callback_impl); | |
80 } | |
81 | |
82 void TestHTMLViewerImpl::GetContentAsText( | |
83 const GetContentAsTextCallback& callback) { | |
84 callback.Run(web_frame_->document().contentAsTextForTesting().utf8()); | |
85 } | |
86 | |
87 void TestHTMLViewerImpl::ExecuteScript(const mojo::String& script, | |
88 const ExecuteScriptCallback& callback) { | |
89 ExecutionCallbackImpl* callback_impl = | |
90 new ExecutionCallbackImpl(this, callback); | |
91 callbacks_.insert(callback_impl); | |
92 web_frame_->requestExecuteScriptAndReturnValue( | |
93 blink::WebScriptSource(blink::WebString::fromUTF8(script)), false, | |
94 callback_impl); | |
95 } | |
96 | |
97 } // namespace html_viewer | |
OLD | NEW |