OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/html_viewer/test_html_viewer_impl.h" | 5 #include "components/html_viewer/test_html_viewer_impl.h" |
6 | 6 |
| 7 #include "base/json/json_writer.h" |
| 8 #include "base/stl_util.h" |
| 9 #include "base/values.h" |
| 10 #include "gin/converter.h" |
7 #include "third_party/WebKit/public/platform/WebString.h" | 11 #include "third_party/WebKit/public/platform/WebString.h" |
8 #include "third_party/WebKit/public/web/WebDocument.h" | 12 #include "third_party/WebKit/public/web/WebDocument.h" |
9 #include "third_party/WebKit/public/web/WebFrame.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" |
10 | 16 |
11 using blink::WebDocument; | 17 using blink::WebDocument; |
12 using blink::WebFrame; | 18 using blink::WebFrame; |
13 | 19 |
14 namespace html_viewer { | 20 namespace html_viewer { |
15 | 21 |
16 TestHTMLViewerImpl::TestHTMLViewerImpl( | 22 namespace { |
17 blink::WebFrame* web_frame, | 23 |
18 mojo::InterfaceRequest<TestHTMLViewer> request) | 24 std::string V8ValueToJSONString( |
19 : web_frame_(web_frame), binding_(this, request.Pass()) { | 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(); |
20 } | 35 } |
21 | 36 |
| 37 } // namespace |
| 38 |
| 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() {} |
| 46 |
| 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); |
| 55 } |
| 56 |
| 57 TestHTMLViewerImpl* host_; |
| 58 const mojo::Callback<void(mojo::String)> callback_; |
| 59 |
| 60 DISALLOW_COPY_AND_ASSIGN(ExecutionCallbackImpl); |
| 61 }; |
| 62 |
| 63 TestHTMLViewerImpl::TestHTMLViewerImpl( |
| 64 blink::WebLocalFrame* web_frame, |
| 65 mojo::InterfaceRequest<TestHTMLViewer> request) |
| 66 : web_frame_(web_frame), binding_(this, request.Pass()) {} |
| 67 |
22 TestHTMLViewerImpl::~TestHTMLViewerImpl() { | 68 TestHTMLViewerImpl::~TestHTMLViewerImpl() { |
| 69 STLDeleteElements(&callbacks_); |
| 70 } |
| 71 |
| 72 void TestHTMLViewerImpl::CallbackCompleted( |
| 73 ExecutionCallbackImpl* callback_impl) { |
| 74 scoped_ptr<ExecutionCallbackImpl> owned_callback_impl(callback_impl); |
| 75 callbacks_.erase(callback_impl); |
23 } | 76 } |
24 | 77 |
25 void TestHTMLViewerImpl::GetContentAsText( | 78 void TestHTMLViewerImpl::GetContentAsText( |
26 const GetContentAsTextCallback& callback) { | 79 const GetContentAsTextCallback& callback) { |
27 callback.Run(web_frame_->document().contentAsTextForTesting().utf8()); | 80 callback.Run(web_frame_->document().contentAsTextForTesting().utf8()); |
28 } | 81 } |
29 | 82 |
| 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); |
| 91 } |
| 92 |
30 } // namespace html_viewer | 93 } // namespace html_viewer |
OLD | NEW |