summaryrefslogtreecommitdiffstats
path: root/chrome/browser/bookmark_drag_data.cc
blob: 0c9a0facdc4ff3f9d48d35318de0d13fafd962d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/pickle.h"
#include "chrome/browser/bookmark_bar_model.h"
#include "chrome/browser/bookmark_drag_data.h"
#include "chrome/common/os_exchange_data.h"

static CLIPFORMAT clipboard_format = 0;

static void RegisterFormat() {
  if (clipboard_format == 0) {
    clipboard_format = RegisterClipboardFormat(L"chrome/x-bookmark-entry");
    DCHECK(clipboard_format);
  }
}

BookmarkDragData::BookmarkDragData() : is_url(false), is_valid(false) {
}

BookmarkDragData::BookmarkDragData(BookmarkBarNode* node)
    : is_url(node->GetType() == history::StarredEntry::URL),
      url(node->GetURL()),
      title(node->GetTitle()),
      is_valid(true),
      id_(node->id()) {
  if (!is_url)
    AddChildren(node);
}

void BookmarkDragData::Write(OSExchangeData* data) const {
  RegisterFormat();

  DCHECK(data);

  if (is_url) {
    data->SetURL(url, title);
  }
  Pickle data_pickle;
  WriteToPickle(&data_pickle);
  data->SetPickledData(clipboard_format, data_pickle);
}

bool BookmarkDragData::Read(const OSExchangeData& data) {
  RegisterFormat();

  is_valid = data.GetURLAndTitle(&url, &title) && url.is_valid();
  is_url = is_valid;
  profile_id.clear();

  if (data.HasFormat(clipboard_format)) {
    Pickle drag_data_pickle;
    if (data.GetPickledData(clipboard_format, &drag_data_pickle)) {
      void* data_iterator = NULL;
      if (ReadFromPickle(&drag_data_pickle, &data_iterator)) {
        is_valid = true;
      }
    }
  }
  return is_valid;
}

BookmarkBarNode* BookmarkDragData::GetNode(BookmarkBarModel* model) const {
  DCHECK(!is_url && id_ && is_valid);
  return model->GetNodeByID(id_);
}

void BookmarkDragData::WriteToPickle(Pickle* pickle) const {
  pickle->WriteBool(is_url);
  pickle->WriteWString(profile_id);
  pickle->WriteString(url.spec());
  pickle->WriteWString(title);
  if (!is_url) {
    pickle->WriteInt(id_);
    pickle->WriteInt(static_cast<int>(children.size()));
    for (std::vector<BookmarkDragData>::const_iterator i = children.begin();
         i != children.end(); ++i) {
      i->WriteToPickle(pickle);
    }
  }
}

bool BookmarkDragData::ReadFromPickle(Pickle* pickle, void** iterator) {
  std::string url_spec;
  is_valid = false;
  if (!pickle->ReadBool(iterator, &is_url) ||
      !pickle->ReadWString(iterator, &profile_id) ||
      !pickle->ReadString(iterator, &url_spec) ||
      !pickle->ReadWString(iterator, &title)) {
    return false;
  }
  url = GURL(url_spec);
  if (!is_url) {
    id_ = 0;
    children.clear();
    if (!pickle->ReadInt(iterator, &id_))
      return false;
    int children_count;
    if (!pickle->ReadInt(iterator, &children_count))
      return false;
    children.resize(children_count);
    for (std::vector<BookmarkDragData>::iterator i = children.begin();
         i != children.end(); ++i) {
      if (!i->ReadFromPickle(pickle, iterator))
        return false;
    }
  }
  is_valid = true;
  return true;
}

void BookmarkDragData::AddChildren(BookmarkBarNode* node) {
  for (int i = 0, max = node->GetChildCount(); i < max; ++i)
    children.push_back(BookmarkDragData(node->GetChild(i)));
}