Home | History | Annotate | Download | only in html_viewer
      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 "mojo/examples/html_viewer/html_document_view.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/location.h"
      9 #include "base/single_thread_task_runner.h"
     10 #include "base/thread_task_runner_handle.h"
     11 #include "mojo/services/public/cpp/view_manager/node.h"
     12 #include "mojo/services/public/cpp/view_manager/view.h"
     13 #include "skia/ext/refptr.h"
     14 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
     15 #include "third_party/WebKit/public/web/WebDocument.h"
     16 #include "third_party/WebKit/public/web/WebElement.h"
     17 #include "third_party/WebKit/public/web/WebLocalFrame.h"
     18 #include "third_party/WebKit/public/web/WebScriptSource.h"
     19 #include "third_party/WebKit/public/web/WebSettings.h"
     20 #include "third_party/WebKit/public/web/WebView.h"
     21 #include "third_party/skia/include/core/SkCanvas.h"
     22 #include "third_party/skia/include/core/SkColor.h"
     23 #include "third_party/skia/include/core/SkDevice.h"
     24 
     25 namespace mojo {
     26 namespace examples {
     27 namespace {
     28 
     29 blink::WebData CopyToWebData(DataPipeConsumerHandle handle) {
     30   std::vector<char> data;
     31   for (;;) {
     32     char buf[4096];
     33     uint32_t num_bytes = sizeof(buf);
     34     MojoResult result = ReadDataRaw(
     35         handle,
     36         buf,
     37         &num_bytes,
     38         MOJO_READ_DATA_FLAG_NONE);
     39     if (result == MOJO_RESULT_SHOULD_WAIT) {
     40       Wait(handle,
     41            MOJO_HANDLE_SIGNAL_READABLE,
     42            MOJO_DEADLINE_INDEFINITE);
     43     } else if (result == MOJO_RESULT_OK) {
     44       data.insert(data.end(), buf, buf + num_bytes);
     45     } else {
     46       break;
     47     }
     48   }
     49   return blink::WebData(data);
     50 }
     51 
     52 void ConfigureSettings(blink::WebSettings* settings) {
     53   settings->setAcceleratedCompositingEnabled(false);
     54   settings->setLoadsImagesAutomatically(true);
     55   settings->setJavaScriptEnabled(true);
     56 }
     57 
     58 }  // namespace
     59 
     60 HTMLDocumentView::HTMLDocumentView(view_manager::ViewManager* view_manager)
     61     : view_manager_(view_manager),
     62       view_(view_manager::View::Create(view_manager_)),
     63       web_view_(NULL),
     64       repaint_pending_(false),
     65       weak_factory_(this) {
     66 }
     67 
     68 HTMLDocumentView::~HTMLDocumentView() {
     69   if (web_view_)
     70     web_view_->close();
     71 }
     72 
     73 void HTMLDocumentView::AttachToNode(view_manager::Node* node) {
     74   node->SetActiveView(view_);
     75   view_->SetColor(SK_ColorCYAN);  // Dummy background color.
     76 
     77   web_view_ = blink::WebView::create(this);
     78   ConfigureSettings(web_view_->settings());
     79   web_view_->setMainFrame(blink::WebLocalFrame::create(this));
     80 
     81   // TODO(darin): Track size of view_manager::Node.
     82   web_view_->resize(gfx::Size(800, 600));
     83 }
     84 
     85 void HTMLDocumentView::Load(URLResponsePtr response,
     86                             ScopedDataPipeConsumerHandle response_body_stream) {
     87   DCHECK(web_view_);
     88 
     89   // TODO(darin): A better solution would be to use loadRequest, but intercept
     90   // the network request and connect it to the response we already have.
     91   blink::WebData data = CopyToWebData(response_body_stream.get());
     92   web_view_->mainFrame()->loadHTMLString(
     93       data, GURL(response->url), GURL(response->url));
     94 }
     95 
     96 void HTMLDocumentView::didInvalidateRect(const blink::WebRect& rect) {
     97   if (!repaint_pending_) {
     98     repaint_pending_ = true;
     99     base::ThreadTaskRunnerHandle::Get()->PostTask(
    100         FROM_HERE,
    101         base::Bind(&HTMLDocumentView::Repaint, weak_factory_.GetWeakPtr()));
    102   }
    103 }
    104 
    105 bool HTMLDocumentView::allowsBrokenNullLayerTreeView() const {
    106   // TODO(darin): Switch to using compositor bindings.
    107   //
    108   // NOTE: Note to Blink maintainers, feel free to just break this code if it
    109   // is the last using compositor bindings and you want to delete the old path.
    110   //
    111   return true;
    112 }
    113 
    114 void HTMLDocumentView::didAddMessageToConsole(
    115     const blink::WebConsoleMessage& message,
    116     const blink::WebString& source_name,
    117     unsigned source_line,
    118     const blink::WebString& stack_trace) {
    119   printf("### console: %s\n", std::string(message.text.utf8()).c_str());
    120 }
    121 
    122 void HTMLDocumentView::Repaint() {
    123   repaint_pending_ = false;
    124 
    125   web_view_->animate(0.0);
    126   web_view_->layout();
    127 
    128   int width = web_view_->size().width;
    129   int height = web_view_->size().height;
    130 
    131   skia::RefPtr<SkCanvas> canvas = skia::AdoptRef(SkCanvas::NewRaster(
    132       SkImageInfo::MakeN32(width, height, kOpaque_SkAlphaType)));
    133 
    134   web_view_->paint(canvas.get(), gfx::Rect(0, 0, width, height));
    135 
    136   view_->SetContents(canvas->getDevice()->accessBitmap(false));
    137 }
    138 
    139 }  // namespace examples
    140 }  // namespace mojo
    141