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 const char* BrowserActionDragData::kClipboardFormatString
=
14 "chromium/x-browser-actions";
16 BrowserActionDragData::BrowserActionDragData()
17 : profile_(NULL
), index_(-1) {
20 BrowserActionDragData::BrowserActionDragData(
21 const std::string
& id
, int index
)
22 : profile_(NULL
), id_(id
), index_(index
) {
25 bool BrowserActionDragData::IsFromProfile(Profile
* profile
) const {
26 return profile_
== profile
;
29 #if defined(TOOLKIT_VIEWS)
30 void BrowserActionDragData::Write(
31 Profile
* profile
, ui::OSExchangeData
* data
) const {
34 WriteToPickle(profile
, &data_pickle
);
35 data
->SetPickledData(GetBrowserActionCustomFormat(), data_pickle
);
38 bool BrowserActionDragData::Read(const ui::OSExchangeData
& data
) {
39 if (!data
.HasCustomFormat(GetBrowserActionCustomFormat()))
42 Pickle drag_data_pickle
;
43 if (!data
.GetPickledData(GetBrowserActionCustomFormat(), &drag_data_pickle
))
46 if (!ReadFromPickle(&drag_data_pickle
))
53 const ui::OSExchangeData::CustomFormat
&
54 BrowserActionDragData::GetBrowserActionCustomFormat() {
55 CR_DEFINE_STATIC_LOCAL(
56 ui::OSExchangeData::CustomFormat
,
58 (ui::Clipboard::GetFormatType(kClipboardFormatString
)));
64 void BrowserActionDragData::WriteToPickle(
65 Profile
* profile
, Pickle
* pickle
) const {
66 pickle
->WriteBytes(&profile
, sizeof(profile
));
67 pickle
->WriteString(id_
);
68 pickle
->WriteUInt64(index_
);
71 bool BrowserActionDragData::ReadFromPickle(Pickle
* pickle
) {
72 PickleIterator
data_iterator(*pickle
);
75 if (!pickle
->ReadBytes(&data_iterator
, &tmp
, sizeof(profile_
)))
77 memcpy(&profile_
, tmp
, sizeof(profile_
));
79 if (!pickle
->ReadString(&data_iterator
, &id_
))
83 if (!pickle
->ReadUInt64(&data_iterator
, &index
))
85 index_
= static_cast<size_t>(index
);