1// Copyright (c) 2012 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 "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
6
7#include "base/logging.h"
8#include "base/pickle.h"
9#include "base/strings/string_util.h"
10#include "chrome/browser/profiles/profile.h"
11#include "ui/base/clipboard/clipboard.h"
12
13namespace {
14
15// The MIME type for the clipboard format for BrowserActionDragData.
16const char kClipboardFormatString[] = "chromium/x-browser-actions";
17
18}
19
20BrowserActionDragData::BrowserActionDragData()
21    : profile_(NULL), index_(static_cast<size_t>(-1)) {
22}
23
24BrowserActionDragData::BrowserActionDragData(
25    const std::string& id, int index)
26    : profile_(NULL), id_(id), index_(index) {
27}
28
29bool BrowserActionDragData::GetDropFormats(
30    std::set<ui::OSExchangeData::CustomFormat>* custom_formats) {
31  custom_formats->insert(GetBrowserActionCustomFormat());
32  return true;
33}
34
35bool BrowserActionDragData::AreDropTypesRequired() {
36  return true;
37}
38
39bool BrowserActionDragData::CanDrop(const ui::OSExchangeData& data,
40                                    const Profile* profile) {
41  BrowserActionDragData drop_data;
42  return drop_data.Read(data) && drop_data.IsFromProfile(profile);
43}
44
45bool BrowserActionDragData::IsFromProfile(const Profile* profile) const {
46  return profile_ == profile;
47}
48
49#if defined(TOOLKIT_VIEWS)
50void BrowserActionDragData::Write(
51    Profile* profile, ui::OSExchangeData* data) const {
52  DCHECK(data);
53  Pickle data_pickle;
54  WriteToPickle(profile, &data_pickle);
55  data->SetPickledData(GetBrowserActionCustomFormat(), data_pickle);
56}
57
58bool BrowserActionDragData::Read(const ui::OSExchangeData& data) {
59  if (!data.HasCustomFormat(GetBrowserActionCustomFormat()))
60    return false;
61
62  Pickle drag_data_pickle;
63  if (!data.GetPickledData(GetBrowserActionCustomFormat(), &drag_data_pickle))
64    return false;
65
66  if (!ReadFromPickle(&drag_data_pickle))
67    return false;
68
69  return true;
70}
71
72// static
73const ui::OSExchangeData::CustomFormat&
74BrowserActionDragData::GetBrowserActionCustomFormat() {
75  CR_DEFINE_STATIC_LOCAL(
76      ui::OSExchangeData::CustomFormat,
77      format,
78      (ui::Clipboard::GetFormatType(kClipboardFormatString)));
79
80  return format;
81}
82#endif
83
84void BrowserActionDragData::WriteToPickle(
85    Profile* profile, Pickle* pickle) const {
86  pickle->WriteBytes(&profile, sizeof(profile));
87  pickle->WriteString(id_);
88  pickle->WriteUInt64(index_);
89}
90
91bool BrowserActionDragData::ReadFromPickle(Pickle* pickle) {
92  PickleIterator data_iterator(*pickle);
93
94  const char* tmp;
95  if (!pickle->ReadBytes(&data_iterator, &tmp, sizeof(profile_)))
96    return false;
97  memcpy(&profile_, tmp, sizeof(profile_));
98
99  if (!pickle->ReadString(&data_iterator, &id_))
100    return false;
101
102  uint64 index;
103  if (!pickle->ReadUInt64(&data_iterator, &index))
104    return false;
105  index_ = static_cast<size_t>(index);
106
107  return true;
108}
109