summaryrefslogtreecommitdiffstats
path: root/chrome/browser/history/android/bookmark_model_sql_handler.cc
blob: ad4c2bf763df1085ea78068a51a5dbea40889d3d (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) 2012 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 "chrome/browser/history/android/bookmark_model_sql_handler.h"

#include "base/logging.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/history/url_database.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/browser_thread.h"

using base::Time;
using content::BrowserThread;

namespace history {

namespace {

// The interesting columns of this handler.
const HistoryAndBookmarkRow::ColumnID kInterestingColumns[] = {
    HistoryAndBookmarkRow::BOOKMARK, HistoryAndBookmarkRow::TITLE };

} // namespace

BookmarkModelSQLHandler::Task::Task() {
}

void BookmarkModelSQLHandler::Task::AddBookmarkToMobileFolder(
    const GURL& url,
    const string16& title) {
  BookmarkModel* bookmark_model = GetBookmarkModel();
  if (!bookmark_model)
    return;
  const BookmarkNode* mobile_node = bookmark_model->mobile_node();
  if (mobile_node)
    bookmark_model->AddURL(mobile_node, 0, title, url);
}

void BookmarkModelSQLHandler::Task::AddBookmark(const GURL& url,
                                                const string16& title,
                                                int64 parent_id) {
  BookmarkModel* bookmark_model = GetBookmarkModel();
  if (!bookmark_model)
    return;
  const BookmarkNode* parent = bookmark_model->GetNodeByID(parent_id);
  if (parent)
    bookmark_model->AddURL(parent, 0, title, url);
}

void BookmarkModelSQLHandler::Task::RemoveBookmark(const GURL& url) {
  BookmarkModel* bookmark_model = GetBookmarkModel();
  if (!bookmark_model)
    return;
  std::vector<const BookmarkNode*> nodes;
  bookmark_model->GetNodesByURL(url, &nodes);
  for (std::vector<const BookmarkNode*>::iterator i = nodes.begin();
       i != nodes.end(); ++i) {
    const BookmarkNode* parent_node = (*i)->parent();
    bookmark_model->Remove(parent_node, parent_node->GetIndexOf(*i));
  }
}

void BookmarkModelSQLHandler::Task::UpdateBookmarkTitle(const GURL& url,
                                                        const string16&title) {
  BookmarkModel* bookmark_model = GetBookmarkModel();
  if (!bookmark_model)
    return;
  std::vector<const BookmarkNode*> nodes;
  bookmark_model->GetNodesByURL(url, &nodes);
  for (std::vector<const BookmarkNode*>::iterator i = nodes.begin();
       i != nodes.end(); ++i) {
    bookmark_model->SetTitle(*i, title);
  }
}


BookmarkModelSQLHandler::Task::~Task() {
}

BookmarkModel* BookmarkModelSQLHandler::Task::GetBookmarkModel() {
  Profile* profile = ProfileManager::GetLastUsedProfile();
  if (!profile)
    return NULL;
  return profile->GetBookmarkModel();
}

BookmarkModelSQLHandler::BookmarkModelSQLHandler(
    URLDatabase* url_database)
    : SQLHandler(kInterestingColumns, arraysize(kInterestingColumns)),
      url_database_(url_database) {
}

BookmarkModelSQLHandler::~BookmarkModelSQLHandler() {
}

bool BookmarkModelSQLHandler::Update(const HistoryAndBookmarkRow& row,
                                     const TableIDRows& ids_set) {
  for (TableIDRows::const_iterator i = ids_set.begin();
      i != ids_set.end(); ++i) {
    if (row.is_value_set_explicitly(HistoryAndBookmarkRow::BOOKMARK)) {
      if (row.is_bookmark()) {
        URLRow url_row;
        if (!url_database_->GetURLRow(i->url_id, &url_row))
          return false;
        if (row.is_value_set_explicitly(HistoryAndBookmarkRow::PARENT_ID)) {
          BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
              &BookmarkModelSQLHandler::Task::AddBookmark,
              scoped_refptr<BookmarkModelSQLHandler::Task>(
                  new BookmarkModelSQLHandler::Task()),
              i->url, url_row.title(), row.parent_id()));
        } else {
          BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
              &BookmarkModelSQLHandler::Task::AddBookmarkToMobileFolder,
              scoped_refptr<BookmarkModelSQLHandler::Task>(
                  new BookmarkModelSQLHandler::Task()),
              i->url, url_row.title()));
        }
      } else {
        BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
            &BookmarkModelSQLHandler::Task::RemoveBookmark,
            scoped_refptr<BookmarkModelSQLHandler::Task>(
                new BookmarkModelSQLHandler::Task()),
            i->url));
      }
    } else if (row.is_value_set_explicitly(HistoryAndBookmarkRow::TITLE)) {
      BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
          &BookmarkModelSQLHandler::Task::UpdateBookmarkTitle,
          scoped_refptr<BookmarkModelSQLHandler::Task>(
              new BookmarkModelSQLHandler::Task()),
          i->url, row.title()));
    }
  }
  return true;
}

bool BookmarkModelSQLHandler::Delete(const TableIDRows& ids_set) {
  for (TableIDRows::const_iterator i = ids_set.begin();
       i != ids_set.end(); ++i) {
    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
        &BookmarkModelSQLHandler::Task::RemoveBookmark,
        scoped_refptr<BookmarkModelSQLHandler::Task>(
            new BookmarkModelSQLHandler::Task()),
        i->url));
  }
  return true;
}

bool BookmarkModelSQLHandler::Insert(HistoryAndBookmarkRow* row) {
  DCHECK(row->is_value_set_explicitly(HistoryAndBookmarkRow::URL));
  if (!row->is_value_set_explicitly(HistoryAndBookmarkRow::BOOKMARK) ||
      !row->is_bookmark())
    return true;
  if (row->is_value_set_explicitly(HistoryAndBookmarkRow::PARENT_ID)) {
    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
        &BookmarkModelSQLHandler::Task::AddBookmark,
        scoped_refptr<BookmarkModelSQLHandler::Task>(
            new BookmarkModelSQLHandler::Task()),
        row->url(), row->title(), row->parent_id()));
  } else {
    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
        &BookmarkModelSQLHandler::Task::AddBookmarkToMobileFolder,
        scoped_refptr<BookmarkModelSQLHandler::Task>(
            new BookmarkModelSQLHandler::Task()),
        row->url(), row->title()));
  }
  return true;
}

}  // namespace history.