summaryrefslogtreecommitdiffstats
path: root/components/html_viewer/test_html_viewer_impl.cc
blob: b0062554cd415c178383d6f3fda2f32a365f6f8b (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
// Copyright 2014 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 "components/html_viewer/test_html_viewer_impl.h"

#include "base/json/json_writer.h"
#include "base/stl_util.h"
#include "base/values.h"
#include "gin/converter.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebScriptExecutionCallback.h"
#include "third_party/WebKit/public/web/WebScriptSource.h"

using blink::WebDocument;
using blink::WebFrame;

namespace html_viewer {

namespace {

std::string V8ValueToJSONString(
    blink::WebLocalFrame* local_frame,
    const blink::WebVector<v8::Local<v8::Value>>& result) {
  // TODO(sky): use V8ValueConverter when refactored to a common place.
  DCHECK(!result.isEmpty());
  base::ListValue list_value;
  for (auto& value : result)
    list_value.Append(new base::StringValue(gin::V8ToString(value)));
  std::string json_string;
  return base::JSONWriter::Write(list_value, &json_string) ? json_string
                                                           : std::string();
}

}  // namespace

class TestHTMLViewerImpl::ExecutionCallbackImpl
    : public blink::WebScriptExecutionCallback {
 public:
  ExecutionCallbackImpl(TestHTMLViewerImpl* host,
                        const mojo::Callback<void(mojo::String)>& callback)
      : host_(host), callback_(callback) {}
  virtual ~ExecutionCallbackImpl() {}

 private:
  // blink::WebScriptExecutionCallback:
  virtual void completed(const blink::WebVector<v8::Local<v8::Value>>& result) {
    mojo::String callback_result;
    if (!result.isEmpty())
      callback_result = V8ValueToJSONString(host_->web_frame_, result);
    callback_.Run(callback_result);
    host_->CallbackCompleted(this);
  }

  TestHTMLViewerImpl* host_;
  const mojo::Callback<void(mojo::String)> callback_;

  DISALLOW_COPY_AND_ASSIGN(ExecutionCallbackImpl);
};

TestHTMLViewerImpl::TestHTMLViewerImpl(
    blink::WebLocalFrame* web_frame,
    mojo::InterfaceRequest<TestHTMLViewer> request)
    : web_frame_(web_frame), binding_(this, request.Pass()) {}

TestHTMLViewerImpl::~TestHTMLViewerImpl() {
  STLDeleteElements(&callbacks_);
}

void TestHTMLViewerImpl::CallbackCompleted(
    ExecutionCallbackImpl* callback_impl) {
  scoped_ptr<ExecutionCallbackImpl> owned_callback_impl(callback_impl);
  callbacks_.erase(callback_impl);
}

void TestHTMLViewerImpl::GetContentAsText(
    const GetContentAsTextCallback& callback) {
  callback.Run(web_frame_->document().contentAsTextForTesting().utf8());
}

void TestHTMLViewerImpl::ExecuteScript(const mojo::String& script,
                                       const ExecuteScriptCallback& callback) {
  ExecutionCallbackImpl* callback_impl =
      new ExecutionCallbackImpl(this, callback);
  callbacks_.insert(callback_impl);
  web_frame_->requestExecuteScriptAndReturnValue(
      blink::WebScriptSource(blink::WebString::fromUTF8(script)), false,
      callback_impl);
}

}  // namespace html_viewer