OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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 "base/pickle.h" | |
6 #include "chrome/browser/bookmark_bar_model.h" | |
7 #include "chrome/browser/bookmark_drag_data.h" | |
8 #include "chrome/common/os_exchange_data.h" | |
9 | |
10 static CLIPFORMAT clipboard_format = 0; | |
11 | |
12 static void RegisterFormat() { | |
13 if (clipboard_format == 0) { | |
14 clipboard_format = RegisterClipboardFormat(L"chrome/x-bookmark-entry"); | |
15 DCHECK(clipboard_format); | |
16 } | |
17 } | |
18 | |
19 BookmarkDragData::BookmarkDragData() : is_url(false), is_valid(false) { | |
20 } | |
21 | |
22 BookmarkDragData::BookmarkDragData(BookmarkBarNode* node) | |
23 : is_url(node->GetType() == history::StarredEntry::URL), | |
24 url(node->GetURL()), | |
25 title(node->GetTitle()), | |
26 is_valid(true), | |
27 id_(node->id()) { | |
28 if (!is_url) | |
29 AddChildren(node); | |
30 } | |
31 | |
32 void BookmarkDragData::Write(OSExchangeData* data) const { | |
33 RegisterFormat(); | |
34 | |
35 DCHECK(data); | |
36 | |
37 if (is_url) { | |
38 data->SetURL(url, title); | |
39 } | |
40 Pickle data_pickle; | |
41 WriteToPickle(&data_pickle); | |
42 data->SetPickledData(clipboard_format, data_pickle); | |
43 } | |
44 | |
45 bool BookmarkDragData::Read(const OSExchangeData& data) { | |
46 RegisterFormat(); | |
47 | |
48 is_valid = data.GetURLAndTitle(&url, &title) && url.is_valid(); | |
49 is_url = is_valid; | |
50 profile_id.clear(); | |
51 | |
52 if (data.HasFormat(clipboard_format)) { | |
53 Pickle drag_data_pickle; | |
54 if (data.GetPickledData(clipboard_format, &drag_data_pickle)) { | |
55 void* data_iterator = NULL; | |
56 if (ReadFromPickle(&drag_data_pickle, &data_iterator)) { | |
57 is_valid = true; | |
58 } | |
59 } | |
60 } | |
61 return is_valid; | |
62 } | |
63 | |
64 BookmarkBarNode* BookmarkDragData::GetNode(BookmarkBarModel* model) const { | |
65 DCHECK(!is_url && id_ && is_valid); | |
66 return model->GetNodeByID(id_); | |
67 } | |
68 | |
69 void BookmarkDragData::WriteToPickle(Pickle* pickle) const { | |
70 pickle->WriteBool(is_url); | |
71 pickle->WriteWString(profile_id); | |
72 pickle->WriteString(url.spec()); | |
73 pickle->WriteWString(title); | |
74 if (!is_url) { | |
75 pickle->WriteInt(id_); | |
76 pickle->WriteInt(static_cast<int>(children.size())); | |
77 for (std::vector<BookmarkDragData>::const_iterator i = children.begin(); | |
78 i != children.end(); ++i) { | |
79 i->WriteToPickle(pickle); | |
80 } | |
81 } | |
82 } | |
83 | |
84 bool BookmarkDragData::ReadFromPickle(Pickle* pickle, void** iterator) { | |
85 std::string url_spec; | |
86 is_valid = false; | |
87 if (!pickle->ReadBool(iterator, &is_url) || | |
88 !pickle->ReadWString(iterator, &profile_id) || | |
89 !pickle->ReadString(iterator, &url_spec) || | |
90 !pickle->ReadWString(iterator, &title)) { | |
91 return false; | |
92 } | |
93 url = GURL(url_spec); | |
94 if (!is_url) { | |
95 id_ = 0; | |
96 children.clear(); | |
97 if (!pickle->ReadInt(iterator, &id_)) | |
98 return false; | |
99 int children_count; | |
100 if (!pickle->ReadInt(iterator, &children_count)) | |
101 return false; | |
102 children.resize(children_count); | |
103 for (std::vector<BookmarkDragData>::iterator i = children.begin(); | |
104 i != children.end(); ++i) { | |
105 if (!i->ReadFromPickle(pickle, iterator)) | |
106 return false; | |
107 } | |
108 } | |
109 is_valid = true; | |
110 return true; | |
111 } | |
112 | |
113 void BookmarkDragData::AddChildren(BookmarkBarNode* node) { | |
114 for (int i = 0, max = node->GetChildCount(); i < max; ++i) | |
115 children.push_back(BookmarkDragData(node->GetChild(i))); | |
116 } | |
117 | |
OLD | NEW |