<commit_before>// Copyright 2014 Samsung Electronics. All rights reseoved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/clipboard/clipboard.h"

#include <Elementary.h>

#include "base/logging.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "components/clipboard/clipboard_helper_efl.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/WebKit/Source/platform/clipboard/ClipboardMimeTypes.h"
#include "ui/gfx/size.h"

using base::string16;

namespace ui {
namespace {
const char kPlainTextFormat[] = "text";
const char kHTMLFormat[] = "html";
const char kRTFFormat[] = "rtf";
const char kBitmapFormat[] = "bitmap";
const char kWebKitSmartPasteFormat[] = "webkit_smart";
const char kBookmarkFormat[] = "bookmark";
const char kMimeTypePepperCustomData[] = "chromium/x-pepper-custom-data";
const char kMimeTypeWebCustomData[] = "chromium/x-web-custom-data";
} // namespace

Clipboard::FormatType::FormatType() {
}

Clipboard::FormatType::FormatType(const std::string& native_format)
    : data_(native_format) {
}

Clipboard::FormatType::~FormatType() {
}

std::string Clipboard::FormatType::Serialize() const {
  return data_;
}

// static
Clipboard::FormatType Clipboard::FormatType::Deserialize(
    const std::string& serialization) {
  return FormatType(serialization);
}

bool Clipboard::FormatType::Equals(const FormatType& other) const {
  return data_ == other.data_;
}

Clipboard::Clipboard() {
  DCHECK(CalledOnValidThread());
}

Clipboard::~Clipboard() {
  DCHECK(CalledOnValidThread());
}

// Main entry point used to write several values in the clipboard.
void Clipboard::WriteObjects(ClipboardType type, const ObjectMap& objects) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);

  for (ObjectMap::const_iterator iter = objects.begin();
       iter != objects.end(); ++iter) {
    DispatchObject(static_cast<ObjectType>(iter->first), iter->second);
  }
}

uint64 Clipboard::GetSequenceNumber(ClipboardType /* type */) {
  DCHECK(CalledOnValidThread());
  // TODO: implement this. For now this interface will advertise
  // that the clipboard never changes. That's fine as long as we
  // don't rely on this signal.
  return 0;
}

bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format,
                                  ClipboardType /* clipboard_type */) const {
  DCHECK(CalledOnValidThread());
  int count = ClipboardHelperEfl::GetInstance()->NumberOfItems();

  int type = ELM_SEL_FORMAT_NONE;
  std::string clipboard_data;

  for (int i = 0; i < count; i++) {
    bool ret = ClipboardHelperEfl::GetInstance()->RetrieveClipboardItem(i, &type, &clipboard_data);
    if (!ret)
      continue;

    switch (type) {
      case ELM_SEL_FORMAT_TEXT: {
        if (GetPlainTextFormatType().Equals(format))
          return true;
        break;
      }
      case ELM_SEL_FORMAT_HTML: {
        if (GetHtmlFormatType().Equals(format))
          return true;
        break;
      }
      case ELM_SEL_FORMAT_IMAGE: {
        if (GetBitmapFormatType().Equals(format))
          return true;
        break;
      }
      // If we support more ELM formats, we shall add it here.
    }
  }
  return false;
}

void Clipboard::Clear(ClipboardType /* type */) {
  ClipboardHelperEfl::GetInstance()->Clear();
}

void Clipboard::ReadAvailableTypes(ClipboardType type, std::vector<string16>* types,
                                   bool* contains_filenames) const {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);

  if (!types || !contains_filenames) {
    NOTREACHED();
    return;
  }

  types->clear();
  *contains_filenames = false;
  if (IsFormatAvailable(GetPlainTextFormatType(), type))
    types->push_back(base::ASCIIToUTF16(WebCore::mimeTypeTextPlain));
  if (IsFormatAvailable(GetHtmlFormatType(), type))
    types->push_back(base::ASCIIToUTF16(WebCore::mimeTypeTextHTML));
  if (IsFormatAvailable(GetBitmapFormatType(), type))
    types->push_back(base::ASCIIToUTF16(WebCore::mimeTypeImagePng));
}

void Clipboard::ReadText(ClipboardType type, string16* result) const {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
  std::string utf8;
  ReadAsciiText(type, &utf8);
  *result = base::UTF8ToUTF16(utf8);
}

void Clipboard::ReadAsciiText(ClipboardType type, std::string* result) const {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
  int count = ClipboardHelperEfl::GetInstance()->NumberOfItems();

  int format = ELM_SEL_FORMAT_NONE;
  std::string clipboard_data;

  for (int i = 0; i < count; i++) {
    if (!ClipboardHelperEfl::GetInstance()->RetrieveClipboardItem(i, &format, &clipboard_data))
      continue;

    if (format == ELM_SEL_FORMAT_TEXT) {
      result->clear();
      result->assign(clipboard_data);<commit_msg>WebCore:: -> blink:: in clipboard_efl.cc .<commit_after>// Copyright 2014 Samsung Electronics. All rights reseoved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/clipboard/clipboard.h"

#include <Elementary.h>

#include "base/logging.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "components/clipboard/clipboard_helper_efl.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/WebKit/Source/platform/clipboard/ClipboardMimeTypes.h"
#include "ui/gfx/size.h"

using base::string16;

namespace ui {
namespace {
const char kPlainTextFormat[] = "text";
const char kHTMLFormat[] = "html";
const char kRTFFormat[] = "rtf";
const char kBitmapFormat[] = "bitmap";
const char kWebKitSmartPasteFormat[] = "webkit_smart";
const char kBookmarkFormat[] = "bookmark";
const char kMimeTypePepperCustomData[] = "chromium/x-pepper-custom-data";
const char kMimeTypeWebCustomData[] = "chromium/x-web-custom-data";
} // namespace

Clipboard::FormatType::FormatType() {
}

Clipboard::FormatType::FormatType(const std::string& native_format)
    : data_(native_format) {
}

Clipboard::FormatType::~FormatType() {
}

std::string Clipboard::FormatType::Serialize() const {
  return data_;
}

// static
Clipboard::FormatType Clipboard::FormatType::Deserialize(
    const std::string& serialization) {
  return FormatType(serialization);
}

bool Clipboard::FormatType::Equals(const FormatType& other) const {
  return data_ == other.data_;
}

Clipboard::Clipboard() {
  DCHECK(CalledOnValidThread());
}

Clipboard::~Clipboard() {
  DCHECK(CalledOnValidThread());
}

// Main entry point used to write several values in the clipboard.
void Clipboard::WriteObjects(ClipboardType type, const ObjectMap& objects) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);

  for (ObjectMap::const_iterator iter = objects.begin();
       iter != objects.end(); ++iter) {
    DispatchObject(static_cast<ObjectType>(iter->first), iter->second);
  }
}

uint64 Clipboard::GetSequenceNumber(ClipboardType /* type */) {
  DCHECK(CalledOnValidThread());
  // TODO: implement this. For now this interface will advertise
  // that the clipboard never changes. That's fine as long as we
  // don't rely on this signal.
  return 0;
}

bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format,
                                  ClipboardType /* clipboard_type */) const {
  DCHECK(CalledOnValidThread());
  int count = ClipboardHelperEfl::GetInstance()->NumberOfItems();

  int type = ELM_SEL_FORMAT_NONE;
  std::string clipboard_data;

  for (int i = 0; i < count; i++) {
    bool ret = ClipboardHelperEfl::GetInstance()->RetrieveClipboardItem(i, &type, &clipboard_data);
    if (!ret)
      continue;

    switch (type) {
      case ELM_SEL_FORMAT_TEXT: {
        if (GetPlainTextFormatType().Equals(format))
          return true;
        break;
      }
      case ELM_SEL_FORMAT_HTML: {
        if (GetHtmlFormatType().Equals(format))
          return true;
        break;
      }
      case ELM_SEL_FORMAT_IMAGE: {
        if (GetBitmapFormatType().Equals(format))
          return true;
        break;
      }
      // If we support more ELM formats, we shall add it here.
    }
  }
  return false;
}

void Clipboard::Clear(ClipboardType /* type */) {
  ClipboardHelperEfl::GetInstance()->Clear();
}

void Clipboard::ReadAvailableTypes(ClipboardType type, std::vector<string16>* types,
                                   bool* contains_filenames) const {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);

  if (!types || !contains_filenames) {
    NOTREACHED();
    return;
  }

  types->clear();
  *contains_filenames = false;
  if (IsFormatAvailable(GetPlainTextFormatType(), type))
    types->push_back(base::ASCIIToUTF16(blink::mimeTypeTextPlain));
  if (IsFormatAvailable(GetHtmlFormatType(), type))
    types->push_back(base::ASCIIToUTF16(blink::mimeTypeTextHTML));
  if (IsFormatAvailable(GetBitmapFormatType(), type))
    types->push_back(base::ASCIIToUTF16(blink::mimeTypeImagePng));
}

void Clipboard::ReadText(ClipboardType type, string16* result) const {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
  std::string utf8;
  ReadAsciiText(type, &utf8);
  *result = base::UTF8ToUTF16(utf8);
}

void Clipboard::ReadAsciiText(ClipboardType type, std::string* result) const {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(type, CLIPBOARD_TYPE_COPY_PASTE);
  int count = ClipboardHelperEfl::GetInstance()->NumberOfItems();

  int format = ELM_SEL_FORMAT_NONE;
  std::string clipboard_data;

  for (int i = 0; i < count; i++) {
    if (!ClipboardHelperEfl::GetInstance()->RetrieveClipboardItem(i, &format, &clipboard_data))
      continue;

    if (format == ELM_SEL_FORMAT_TEXT) {
      result->clear();
      result->assign(clipboard_data);
{'fraction_non_alphanumeric': 0.12966521695155253, 'fraction_numerical': 0.005866289066450895, 'mean_word_length': 4.508635097493036, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3942550', 'n_tokens_mistral': 3267, 'n_tokens_neox': 3208, 'n_words': 653}