Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / extensions / browser_action_drag_data.cc
blob82f62c5dd679f7499a121c2a9a50f6e2ba41c940
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.
5 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
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"
13 namespace {
15 // The MIME type for the clipboard format for BrowserActionDragData.
16 const char kClipboardFormatString[] = "chromium/x-browser-actions";
20 BrowserActionDragData::BrowserActionDragData()
21 : profile_(NULL), index_(static_cast<size_t>(-1)) {
24 BrowserActionDragData::BrowserActionDragData(
25 const std::string& id, int index)
26 : profile_(NULL), id_(id), index_(index) {
29 bool BrowserActionDragData::GetDropFormats(
30 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) {
31 custom_formats->insert(GetBrowserActionCustomFormat());
32 return true;
35 bool BrowserActionDragData::AreDropTypesRequired() {
36 return true;
39 bool BrowserActionDragData::CanDrop(const ui::OSExchangeData& data,
40 const Profile* profile) {
41 BrowserActionDragData drop_data;
42 return drop_data.Read(data) && drop_data.IsFromProfile(profile);
45 bool BrowserActionDragData::IsFromProfile(const Profile* profile) const {
46 return profile_ == profile;
49 #if defined(TOOLKIT_VIEWS)
50 void BrowserActionDragData::Write(
51 Profile* profile, ui::OSExchangeData* data) const {
52 DCHECK(data);
53 base::Pickle data_pickle;
54 WriteToPickle(profile, &data_pickle);
55 data->SetPickledData(GetBrowserActionCustomFormat(), data_pickle);
58 bool BrowserActionDragData::Read(const ui::OSExchangeData& data) {
59 if (!data.HasCustomFormat(GetBrowserActionCustomFormat()))
60 return false;
62 base::Pickle drag_data_pickle;
63 if (!data.GetPickledData(GetBrowserActionCustomFormat(), &drag_data_pickle))
64 return false;
66 if (!ReadFromPickle(&drag_data_pickle))
67 return false;
69 return true;
72 // static
73 const ui::OSExchangeData::CustomFormat&
74 BrowserActionDragData::GetBrowserActionCustomFormat() {
75 CR_DEFINE_STATIC_LOCAL(
76 ui::OSExchangeData::CustomFormat,
77 format,
78 (ui::Clipboard::GetFormatType(kClipboardFormatString)));
80 return format;
82 #endif
84 void BrowserActionDragData::WriteToPickle(Profile* profile,
85 base::Pickle* pickle) const {
86 pickle->WriteBytes(&profile, sizeof(profile));
87 pickle->WriteString(id_);
88 pickle->WriteUInt64(index_);
91 bool BrowserActionDragData::ReadFromPickle(base::Pickle* pickle) {
92 base::PickleIterator data_iterator(*pickle);
94 const char* tmp;
95 if (!data_iterator.ReadBytes(&tmp, sizeof(profile_)))
96 return false;
97 memcpy(&profile_, tmp, sizeof(profile_));
99 if (!data_iterator.ReadString(&id_))
100 return false;
102 uint64 index;
103 if (!data_iterator.ReadUInt64(&index))
104 return false;
105 index_ = static_cast<size_t>(index);
107 return true;