<commit_before>// Copyright 2015 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 "sky/engine/core/painting/painting.h"

#include "base/bind.h"
#include "base/logging.h"
#include "base/trace_event/trace_event.h"
#include "sky/engine/core/painting/CanvasImage.h"
#include "sky/engine/platform/SharedBuffer.h"
#include "sky/engine/platform/image-decoders/ImageDecoder.h"
#include "sky/engine/platform/mojo/data_pipe.h"
#include "sky/engine/tonic/dart_invoke.h"
#include "sky/engine/tonic/dart_persistent_value.h"
#include "sky/engine/tonic/mojo_converter.h"
#include "sky/engine/tonic/uint8_list.h"

namespace blink {
namespace {

void DecodeImage(scoped_ptr<DartPersistentValue> callback,
                 PassRefPtr<SharedBuffer> buffer) {
  TRACE_EVENT0("blink", "DecodeImage");

  DartState* dart_state = callback->dart_state().get();
  if (!dart_state)
    return;
  DartState::Scope scope(dart_state);

  OwnPtr<ImageDecoder> decoder =
      ImageDecoder::create(*buffer.get(), ImageSource::AlphaPremultiplied,
                           ImageSource::GammaAndColorProfileIgnored);

  // decoder can be null if the buffer was empty and we couldn't even guess
  // what type of image to decode.
  if (!decoder) {
    DartInvokeAppClosure(callback->value(), {Dart_Null()});
    return;
  }
  decoder->setData(buffer.get(), true);
  if (decoder->failed() || decoder->frameCount() == 0) {
    DartInvokeAppClosure(callback->value(), {Dart_Null()});
    return;
  }

  RefPtr<CanvasImage> resultImage = CanvasImage::create();
  ImageFrame* imageFrame = decoder->frameBufferAtIndex(0);
  RefPtr<SkImage> skImage = adoptRef(
      SkImage::NewFromBitmap(imageFrame->getSkBitmap()));
  resultImage->setImage(skImage.release());

  DartInvokeAppClosure(callback->value(), {ToDart(resultImage)});
}


void DecodeImageFromDataPipe(Dart_NativeArguments args) {
  Dart_Handle exception = nullptr;

  mojo::ScopedDataPipeConsumerHandle consumer =
      DartConverter<mojo::ScopedDataPipeConsumerHandle>::FromArgumentsWithNullCheck(
          args, 0, exception);
  if (exception) {<commit_msg>Check the image decoder's failed flag after obtaining the frame buffer<commit_after>// Copyright 2015 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 "sky/engine/core/painting/painting.h"

#include "base/bind.h"
#include "base/logging.h"
#include "base/trace_event/trace_event.h"
#include "sky/engine/core/painting/CanvasImage.h"
#include "sky/engine/platform/SharedBuffer.h"
#include "sky/engine/platform/image-decoders/ImageDecoder.h"
#include "sky/engine/platform/mojo/data_pipe.h"
#include "sky/engine/tonic/dart_invoke.h"
#include "sky/engine/tonic/dart_persistent_value.h"
#include "sky/engine/tonic/mojo_converter.h"
#include "sky/engine/tonic/uint8_list.h"

namespace blink {
namespace {

void DecodeImage(scoped_ptr<DartPersistentValue> callback,
                 PassRefPtr<SharedBuffer> buffer) {
  TRACE_EVENT0("blink", "DecodeImage");

  DartState* dart_state = callback->dart_state().get();
  if (!dart_state)
    return;
  DartState::Scope scope(dart_state);

  OwnPtr<ImageDecoder> decoder =
      ImageDecoder::create(*buffer.get(), ImageSource::AlphaPremultiplied,
                           ImageSource::GammaAndColorProfileIgnored);

  // decoder can be null if the buffer was empty and we couldn't even guess
  // what type of image to decode.
  if (!decoder) {
    DartInvokeAppClosure(callback->value(), {Dart_Null()});
    return;
  }
  decoder->setData(buffer.get(), true);
  if (decoder->failed() || decoder->frameCount() == 0) {
    DartInvokeAppClosure(callback->value(), {Dart_Null()});
    return;
  }
  ImageFrame* imageFrame = decoder->frameBufferAtIndex(0);
  if (decoder->failed()) {
    DartInvokeAppClosure(callback->value(), {Dart_Null()});
    return;
  }

  RefPtr<CanvasImage> resultImage = CanvasImage::create();
  RefPtr<SkImage> skImage = adoptRef(
      SkImage::NewFromBitmap(imageFrame->getSkBitmap()));
  resultImage->setImage(skImage.release());

  DartInvokeAppClosure(callback->value(), {ToDart(resultImage)});
}


void DecodeImageFromDataPipe(Dart_NativeArguments args) {
  Dart_Handle exception = nullptr;

  mojo::ScopedDataPipeConsumerHandle consumer =
      DartConverter<mojo::ScopedDataPipeConsumerHandle>::FromArgumentsWithNullCheck(
          args, 0, exception);
  if (exception) {
{'fraction_non_alphanumeric': 0.13083700440528634, 'fraction_numerical': 0.003964757709251102, 'mean_word_length': 4.748101265822785, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5814490', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1437, 'n_words': 287}