summaryrefslogtreecommitdiffstats
path: root/chrome/profile_import/profile_import_thread.cc
blob: e9ba2a5bee023f75837be5eac5a95fa43c04bf15 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2010 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/profile_import/profile_import_thread.h"

#include <algorithm>

#include "base/values.h"
#include "chrome/browser/importer/importer.h"
#include "chrome/browser/importer/importer_bridge.h"
#include "chrome/browser/importer/importer_data_types.h"
#include "chrome/browser/importer/importer_list.h"
#include "chrome/browser/importer/importer_messages.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/common/child_process.h"

namespace {
// Rather than sending all import items over IPC at once we chunk them into
// separate requests.  This avoids the case of a large import causing
// oversized IPC messages.
const int kNumBookmarksToSend = 100;
const int kNumHistoryRowsToSend = 100;
const int kNumFavIconsToSend = 100;
}

ProfileImportThread::ProfileImportThread()
    : bridge_(NULL),
      browser_type_(0),
      items_to_import_(0),
      importer_(NULL) {
  ChildProcess::current()->AddRefProcess();  // Balanced in Cleanup().
}

void ProfileImportThread::OnControlMessageReceived(const IPC::Message& msg) {
  IPC_BEGIN_MESSAGE_MAP(ProfileImportThread, msg)
    IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_StartImport,
                        OnImportStart)
    IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_CancelImport,
                        OnImportCancel)
    IPC_MESSAGE_HANDLER(ProfileImportProcessMsg_ReportImportItemFinished,
                        OnImportItemFinished)
  IPC_END_MESSAGE_MAP()
}

void ProfileImportThread::OnImportStart(
    const ProfileInfo& profile_info,
    int items,
    const DictionaryValue& localized_strings,
    bool import_to_bookmark_bar) {
  bridge_ = new ExternalProcessImporterBridge(this, localized_strings);
  bridge_->AddRef();  // Balanced in Cleanup().

  ImporterList importer_list;
  importer_ = importer_list.CreateImporterByType(profile_info.browser_type);
  if (!importer_) {
    Send(new ProfileImportProcessHostMsg_Import_Finished(false,
        "Importer could not be created."));
    return;
  }

  importer_->AddRef();  // Balanced in Cleanup().
  importer_->set_import_to_bookmark_bar(import_to_bookmark_bar);
  items_to_import_ = items;

  // Create worker thread in which importer runs.
  import_thread_.reset(new base::Thread("import_thread"));
  base::Thread::Options options;
  options.message_loop_type = MessageLoop::TYPE_IO;
  if (!import_thread_->StartWithOptions(options)) {
    NOTREACHED();
    Cleanup();
  }
  import_thread_->message_loop()->PostTask(FROM_HERE,
      NewRunnableMethod(importer_, &Importer::StartImport,
          profile_info, items, bridge_));
}

void ProfileImportThread::OnImportCancel() {
  Cleanup();
}

void ProfileImportThread::OnImportItemFinished(uint16 item) {
  items_to_import_ ^= item;  // Remove finished item from mask.
  // If we've finished with all items, notify the browser process.
  if (items_to_import_ == 0)
    NotifyEnded();
}

void ProfileImportThread::NotifyItemStarted(ImportItem item) {
  Send(new ProfileImportProcessHostMsg_ImportItem_Started(item));
}

void ProfileImportThread::NotifyItemEnded(ImportItem item) {
  Send(new ProfileImportProcessHostMsg_ImportItem_Finished(item));
}

void ProfileImportThread::NotifyStarted() {
  Send(new ProfileImportProcessHostMsg_Import_Started());
}

void ProfileImportThread::NotifyEnded() {
  Send(new ProfileImportProcessHostMsg_Import_Finished(true, ""));
  Cleanup();
}

void ProfileImportThread::NotifyHistoryImportReady(
    const std::vector<history::URLRow> &rows) {
  Send(new ProfileImportProcessHostMsg_NotifyHistoryImportStart(rows.size()));

  std::vector<history::URLRow>::const_iterator it;
  for (it = rows.begin(); it < rows.end();
       it = it + kNumHistoryRowsToSend) {
    std::vector<history::URLRow> row_group;
    std::vector<history::URLRow>::const_iterator end_group =
        it + kNumHistoryRowsToSend < rows.end() ?
        it + kNumHistoryRowsToSend : rows.end();
    row_group.assign(it, end_group);

    Send(new ProfileImportProcessHostMsg_NotifyHistoryImportGroup(row_group));
  }
}

void ProfileImportThread::NotifyHomePageImportReady(
    const GURL& home_page) {
  NOTIMPLEMENTED();
}

void ProfileImportThread::NotifyBookmarksImportReady(
    const std::vector<ProfileWriter::BookmarkEntry>& bookmarks,
    const std::wstring& first_folder_name, int options) {
  Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportStart(
      first_folder_name, options, bookmarks.size()));

  std::vector<ProfileWriter::BookmarkEntry>::const_iterator it;
  for (it = bookmarks.begin(); it < bookmarks.end();
       it = it + kNumBookmarksToSend) {
    std::vector<ProfileWriter::BookmarkEntry> bookmark_group;
    std::vector<ProfileWriter::BookmarkEntry>::const_iterator end_group =
        it + kNumBookmarksToSend < bookmarks.end() ?
        it + kNumBookmarksToSend : bookmarks.end();
    bookmark_group.assign(it, end_group);

    Send(new ProfileImportProcessHostMsg_NotifyBookmarksImportGroup(
        bookmark_group));
  }
}

void ProfileImportThread::NotifyFavIconsImportReady(
    const std::vector<history::ImportedFavIconUsage>& fav_icons) {
  Send(new ProfileImportProcessHostMsg_NotifyFavIconsImportStart(
    fav_icons.size()));

  std::vector<history::ImportedFavIconUsage>::const_iterator it;
  for (it = fav_icons.begin(); it < fav_icons.end();
       it = it + kNumFavIconsToSend) {
    std::vector<history::ImportedFavIconUsage> fav_icons_group;
    std::vector<history::ImportedFavIconUsage>::const_iterator end_group =
        std::min(it + kNumFavIconsToSend, fav_icons.end());
    fav_icons_group.assign(it, end_group);

  Send(new ProfileImportProcessHostMsg_NotifyFavIconsImportGroup(
      fav_icons_group));
  }
}

void ProfileImportThread::NotifyPasswordFormReady(
    const webkit_glue::PasswordForm& form) {
  Send(new ProfileImportProcessHostMsg_NotifyPasswordFormReady(form));
}

void ProfileImportThread::NotifyKeywordsReady(
    const std::vector<TemplateURL*>& template_urls,
        int default_keyword_index, bool unique_on_host_and_path) {
  std::vector<TemplateURL> urls;
  for (size_t i = 0; i < template_urls.size(); ++i) {
    urls.push_back(*template_urls[i]);
  }
  Send(new ProfileImportProcessHostMsg_NotifyKeywordsReady(urls,
      default_keyword_index, unique_on_host_and_path));
}

void ProfileImportThread::Cleanup() {
  importer_->Cancel();
  importer_->Release();
  bridge_->Release();
  ChildProcess::current()->ReleaseProcess();
}