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"
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());
35 bool BrowserActionDragData::AreDropTypesRequired() {
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 {
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()))
62 base::Pickle drag_data_pickle
;
63 if (!data
.GetPickledData(GetBrowserActionCustomFormat(), &drag_data_pickle
))
66 if (!ReadFromPickle(&drag_data_pickle
))
73 const ui::OSExchangeData::CustomFormat
&
74 BrowserActionDragData::GetBrowserActionCustomFormat() {
75 CR_DEFINE_STATIC_LOCAL(
76 ui::OSExchangeData::CustomFormat
,
78 (ui::Clipboard::GetFormatType(kClipboardFormatString
)));
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
);
95 if (!data_iterator
.ReadBytes(&tmp
, sizeof(profile_
)))
97 memcpy(&profile_
, tmp
, sizeof(profile_
));
99 if (!data_iterator
.ReadString(&id_
))
103 if (!data_iterator
.ReadUInt64(&index
))
105 index_
= static_cast<size_t>(index
);