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.
4
5 #include "chrome/browser/bookmarks/bookmark_html_writer.h"
6
7 #include "base/base64.h"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/platform_file.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/time/time.h"
16 #include "base/values.h"
17 #include "chrome/browser/bookmarks/bookmark_codec.h"
18 #include "chrome/browser/bookmarks/bookmark_model.h"
19 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
20 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/favicon/favicon_service.h"
22 #include "chrome/browser/favicon/favicon_service_factory.h"
23 #include "chrome/common/favicon/favicon_types.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/notification_source.h"
26 #include "grit/generated_resources.h"
27 #include "net/base/escape.h"
28 #include "net/base/file_stream.h"
29 #include "net/base/net_errors.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/gfx/favicon_size.h"
32
33 using content::BrowserThread;
34
35 namespace {
36
37 static BookmarkFaviconFetcher* fetcher = NULL;
38
39 // File header.
40 const char kHeader[] =
41 "<!DOCTYPE NETSCAPE-Bookmark-file-1>\r\n"
42 "<!-- This is an automatically generated file.\r\n"
43 " It will be read and overwritten.\r\n"
44 " DO NOT EDIT! -->\r\n"
45 "<META HTTP-EQUIV=\"Content-Type\""
46 " CONTENT=\"text/html; charset=UTF-8\">\r\n"
47 "<TITLE>Bookmarks</TITLE>\r\n"
48 "<H1>Bookmarks</H1>\r\n"
49 "<DL><p>\r\n";
50
51 // Newline separator.
52 const char kNewline[] = "\r\n";
53
54 // The following are used for bookmarks.
55
56 // Start of a bookmark.
57 const char kBookmarkStart[] = "<DT><A HREF=\"";
58 // After kBookmarkStart.
59 const char kAddDate[] = "\" ADD_DATE=\"";
60 // After kAddDate.
61 const char kIcon[] = "\" ICON=\"";
62 // After kIcon.
63 const char kBookmarkAttributeEnd[] = "\">";
64 // End of a bookmark.
65 const char kBookmarkEnd[] = "</A>";
66
67 // The following are used when writing folders.
68
69 // Start of a folder.
70 const char kFolderStart[] = "<DT><H3 ADD_DATE=\"";
71 // After kFolderStart.
72 const char kLastModified[] = "\" LAST_MODIFIED=\"";
73 // After kLastModified when writing the bookmark bar.
74 const char kBookmarkBar[] = "\" PERSONAL_TOOLBAR_FOLDER=\"true\">";
75 // After kLastModified when writing a user created folder.
76 const char kFolderAttributeEnd[] = "\">";
77 // End of the folder.
78 const char kFolderEnd[] = "</H3>";
79 // Start of the children of a folder.
80 const char kFolderChildren[] = "<DL><p>";
81 // End of the children for a folder.
82 const char kFolderChildrenEnd[] = "</DL><p>";
83
84 // Number of characters to indent by.
85 const size_t kIndentSize = 4;
86
87 // Class responsible for the actual writing. Takes ownership of favicons_map.
88 class Writer : public base::RefCountedThreadSafe<Writer> {
89 public:
Writer(base::Value * bookmarks,const base::FilePath & path,BookmarkFaviconFetcher::URLFaviconMap * favicons_map,BookmarksExportObserver * observer)90 Writer(base::Value* bookmarks,
91 const base::FilePath& path,
92 BookmarkFaviconFetcher::URLFaviconMap* favicons_map,
93 BookmarksExportObserver* observer)
94 : bookmarks_(bookmarks),
95 path_(path),
96 favicons_map_(favicons_map),
97 observer_(observer) {
98 }
99
100 // Writing bookmarks and favicons data to file.
DoWrite()101 void DoWrite() {
102 if (!OpenFile())
103 return;
104
105 Value* roots = NULL;
106 if (!Write(kHeader) ||
107 bookmarks_->GetType() != Value::TYPE_DICTIONARY ||
108 !static_cast<DictionaryValue*>(bookmarks_.get())->Get(
109 BookmarkCodec::kRootsKey, &roots) ||
110 roots->GetType() != Value::TYPE_DICTIONARY) {
111 NOTREACHED();
112 return;
113 }
114
115 DictionaryValue* roots_d_value = static_cast<DictionaryValue*>(roots);
116 Value* root_folder_value;
117 Value* other_folder_value = NULL;
118 Value* mobile_folder_value = NULL;
119 if (!roots_d_value->Get(BookmarkCodec::kRootFolderNameKey,
120 &root_folder_value) ||
121 root_folder_value->GetType() != Value::TYPE_DICTIONARY ||
122 !roots_d_value->Get(BookmarkCodec::kOtherBookmarkFolderNameKey,
123 &other_folder_value) ||
124 other_folder_value->GetType() != Value::TYPE_DICTIONARY ||
125 !roots_d_value->Get(BookmarkCodec::kMobileBookmarkFolderNameKey,
126 &mobile_folder_value) ||
127 mobile_folder_value->GetType() != Value::TYPE_DICTIONARY) {
128 NOTREACHED();
129 return; // Invalid type for root folder and/or other folder.
130 }
131
132 IncrementIndent();
133
134 if (!WriteNode(*static_cast<DictionaryValue*>(root_folder_value),
135 BookmarkNode::BOOKMARK_BAR) ||
136 !WriteNode(*static_cast<DictionaryValue*>(other_folder_value),
137 BookmarkNode::OTHER_NODE) ||
138 !WriteNode(*static_cast<DictionaryValue*>(mobile_folder_value),
139 BookmarkNode::MOBILE)) {
140 return;
141 }
142
143 DecrementIndent();
144
145 Write(kFolderChildrenEnd);
146 Write(kNewline);
147 // File stream close is forced so that unit test could read it.
148 file_stream_.reset();
149
150 NotifyOnFinish();
151 }
152
153 private:
154 friend class base::RefCountedThreadSafe<Writer>;
155
156 // Types of text being written out. The type dictates how the text is
157 // escaped.
158 enum TextType {
159 // The text is the value of an html attribute, eg foo in
160 // <a href="foo">.
161 ATTRIBUTE_VALUE,
162
163 // Actual content, eg foo in <h1>foo</h2>.
164 CONTENT
165 };
166
~Writer()167 ~Writer() {}
168
169 // Opens the file, returning true on success.
OpenFile()170 bool OpenFile() {
171 file_stream_.reset(new net::FileStream(NULL));
172 int flags = base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE;
173 return (file_stream_->OpenSync(path_, flags) == net::OK);
174 }
175
176 // Increments the indent.
IncrementIndent()177 void IncrementIndent() {
178 indent_.resize(indent_.size() + kIndentSize, ' ');
179 }
180
181 // Decrements the indent.
DecrementIndent()182 void DecrementIndent() {
183 DCHECK(!indent_.empty());
184 indent_.resize(indent_.size() - kIndentSize, ' ');
185 }
186
187 // Called at the end of the export process.
NotifyOnFinish()188 void NotifyOnFinish() {
189 if (observer_ != NULL) {
190 observer_->OnExportFinished();
191 }
192 }
193
194 // Writes raw text out returning true on success. This does not escape
195 // the text in anyway.
Write(const std::string & text)196 bool Write(const std::string& text) {
197 // net::FileStream does not allow 0-byte writes.
198 if (!text.length())
199 return true;
200 size_t wrote = file_stream_->WriteSync(text.c_str(), text.length());
201 bool result = (wrote == text.length());
202 DCHECK(result);
203 return result;
204 }
205
206 // Writes out the text string (as UTF8). The text is escaped based on
207 // type.
Write(const std::string & text,TextType type)208 bool Write(const std::string& text, TextType type) {
209 DCHECK(IsStringUTF8(text));
210 std::string utf8_string;
211
212 switch (type) {
213 case ATTRIBUTE_VALUE:
214 // Convert " to "
215 utf8_string = text;
216 ReplaceSubstringsAfterOffset(&utf8_string, 0, "\"", """);
217 break;
218
219 case CONTENT:
220 utf8_string = net::EscapeForHTML(text);
221 break;
222
223 default:
224 NOTREACHED();
225 }
226
227 return Write(utf8_string);
228 }
229
230 // Indents the current line.
WriteIndent()231 bool WriteIndent() {
232 return Write(indent_);
233 }
234
235 // Converts a time string written to the JSON codec into a time_t string
236 // (used by bookmarks.html) and writes it.
WriteTime(const std::string & time_string)237 bool WriteTime(const std::string& time_string) {
238 int64 internal_value;
239 base::StringToInt64(time_string, &internal_value);
240 return Write(base::Int64ToString(
241 base::Time::FromInternalValue(internal_value).ToTimeT()));
242 }
243
244 // Writes the node and all its children, returning true on success.
WriteNode(const DictionaryValue & value,BookmarkNode::Type folder_type)245 bool WriteNode(const DictionaryValue& value,
246 BookmarkNode::Type folder_type) {
247 std::string title, date_added_string, type_string;
248 if (!value.GetString(BookmarkCodec::kNameKey, &title) ||
249 !value.GetString(BookmarkCodec::kDateAddedKey, &date_added_string) ||
250 !value.GetString(BookmarkCodec::kTypeKey, &type_string) ||
251 (type_string != BookmarkCodec::kTypeURL &&
252 type_string != BookmarkCodec::kTypeFolder)) {
253 NOTREACHED();
254 return false;
255 }
256
257 if (type_string == BookmarkCodec::kTypeURL) {
258 std::string url_string;
259 if (!value.GetString(BookmarkCodec::kURLKey, &url_string)) {
260 NOTREACHED();
261 return false;
262 }
263
264 std::string favicon_string;
265 BookmarkFaviconFetcher::URLFaviconMap::iterator itr =
266 favicons_map_->find(url_string);
267 if (itr != favicons_map_->end()) {
268 scoped_refptr<base::RefCountedMemory> data(itr->second.get());
269 std::string favicon_data;
270 favicon_data.assign(reinterpret_cast<const char*>(data->front()),
271 data->size());
272 std::string favicon_base64_encoded;
273 base::Base64Encode(favicon_data, &favicon_base64_encoded);
274 GURL favicon_url("data:image/png;base64," + favicon_base64_encoded);
275 favicon_string = favicon_url.spec();
276 }
277
278 if (!WriteIndent() ||
279 !Write(kBookmarkStart) ||
280 !Write(url_string, ATTRIBUTE_VALUE) ||
281 !Write(kAddDate) ||
282 !WriteTime(date_added_string) ||
283 (!favicon_string.empty() &&
284 (!Write(kIcon) ||
285 !Write(favicon_string, ATTRIBUTE_VALUE))) ||
286 !Write(kBookmarkAttributeEnd) ||
287 !Write(title, CONTENT) ||
288 !Write(kBookmarkEnd) ||
289 !Write(kNewline)) {
290 return false;
291 }
292 return true;
293 }
294
295 // Folder.
296 std::string last_modified_date;
297 const Value* child_values = NULL;
298 if (!value.GetString(BookmarkCodec::kDateModifiedKey,
299 &last_modified_date) ||
300 !value.Get(BookmarkCodec::kChildrenKey, &child_values) ||
301 child_values->GetType() != Value::TYPE_LIST) {
302 NOTREACHED();
303 return false;
304 }
305 if (folder_type != BookmarkNode::OTHER_NODE &&
306 folder_type != BookmarkNode::MOBILE) {
307 // The other/mobile folder name are not written out. This gives the effect
308 // of making the contents of the 'other folder' be a sibling to the
309 // bookmark bar folder.
310 if (!WriteIndent() ||
311 !Write(kFolderStart) ||
312 !WriteTime(date_added_string) ||
313 !Write(kLastModified) ||
314 !WriteTime(last_modified_date)) {
315 return false;
316 }
317 if (folder_type == BookmarkNode::BOOKMARK_BAR) {
318 if (!Write(kBookmarkBar))
319 return false;
320 title = l10n_util::GetStringUTF8(IDS_BOOKMARK_BAR_FOLDER_NAME);
321 } else if (!Write(kFolderAttributeEnd)) {
322 return false;
323 }
324 if (!Write(title, CONTENT) ||
325 !Write(kFolderEnd) ||
326 !Write(kNewline) ||
327 !WriteIndent() ||
328 !Write(kFolderChildren) ||
329 !Write(kNewline)) {
330 return false;
331 }
332 IncrementIndent();
333 }
334
335 // Write the children.
336 const ListValue* children = static_cast<const ListValue*>(child_values);
337 for (size_t i = 0; i < children->GetSize(); ++i) {
338 const Value* child_value;
339 if (!children->Get(i, &child_value) ||
340 child_value->GetType() != Value::TYPE_DICTIONARY) {
341 NOTREACHED();
342 return false;
343 }
344 if (!WriteNode(*static_cast<const DictionaryValue*>(child_value),
345 BookmarkNode::FOLDER)) {
346 return false;
347 }
348 }
349 if (folder_type != BookmarkNode::OTHER_NODE &&
350 folder_type != BookmarkNode::MOBILE) {
351 // Close out the folder.
352 DecrementIndent();
353 if (!WriteIndent() ||
354 !Write(kFolderChildrenEnd) ||
355 !Write(kNewline)) {
356 return false;
357 }
358 }
359 return true;
360 }
361
362 // The BookmarkModel as a Value. This value was generated from the
363 // BookmarkCodec.
364 scoped_ptr<Value> bookmarks_;
365
366 // Path we're writing to.
367 base::FilePath path_;
368
369 // Map that stores favicon per URL.
370 scoped_ptr<BookmarkFaviconFetcher::URLFaviconMap> favicons_map_;
371
372 // Observer to be notified on finish.
373 BookmarksExportObserver* observer_;
374
375 // File we're writing to.
376 scoped_ptr<net::FileStream> file_stream_;
377
378 // How much we indent when writing a bookmark/folder. This is modified
379 // via IncrementIndent and DecrementIndent.
380 std::string indent_;
381
382 DISALLOW_COPY_AND_ASSIGN(Writer);
383 };
384
385 } // namespace
386
BookmarkFaviconFetcher(Profile * profile,const base::FilePath & path,BookmarksExportObserver * observer)387 BookmarkFaviconFetcher::BookmarkFaviconFetcher(
388 Profile* profile,
389 const base::FilePath& path,
390 BookmarksExportObserver* observer)
391 : profile_(profile),
392 path_(path),
393 observer_(observer) {
394 favicons_map_.reset(new URLFaviconMap());
395 registrar_.Add(this,
396 chrome::NOTIFICATION_PROFILE_DESTROYED,
397 content::Source<Profile>(profile_));
398 }
399
~BookmarkFaviconFetcher()400 BookmarkFaviconFetcher::~BookmarkFaviconFetcher() {
401 }
402
ExportBookmarks()403 void BookmarkFaviconFetcher::ExportBookmarks() {
404 ExtractUrls(BookmarkModelFactory::GetForProfile(
405 profile_)->bookmark_bar_node());
406 ExtractUrls(BookmarkModelFactory::GetForProfile(profile_)->other_node());
407 ExtractUrls(BookmarkModelFactory::GetForProfile(profile_)->mobile_node());
408 if (!bookmark_urls_.empty())
409 FetchNextFavicon();
410 else
411 ExecuteWriter();
412 }
413
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)414 void BookmarkFaviconFetcher::Observe(
415 int type,
416 const content::NotificationSource& source,
417 const content::NotificationDetails& details) {
418 if (chrome::NOTIFICATION_PROFILE_DESTROYED == type && fetcher != NULL) {
419 base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher);
420 fetcher = NULL;
421 }
422 }
423
ExtractUrls(const BookmarkNode * node)424 void BookmarkFaviconFetcher::ExtractUrls(const BookmarkNode* node) {
425 if (node->is_url()) {
426 std::string url = node->url().spec();
427 if (!url.empty())
428 bookmark_urls_.push_back(url);
429 } else {
430 for (int i = 0; i < node->child_count(); ++i)
431 ExtractUrls(node->GetChild(i));
432 }
433 }
434
ExecuteWriter()435 void BookmarkFaviconFetcher::ExecuteWriter() {
436 // BookmarkModel isn't thread safe (nor would we want to lock it down
437 // for the duration of the write), as such we make a copy of the
438 // BookmarkModel using BookmarkCodec then write from that.
439 BookmarkCodec codec;
440 BrowserThread::PostTask(
441 BrowserThread::FILE, FROM_HERE,
442 base::Bind(&Writer::DoWrite,
443 new Writer(codec.Encode(BookmarkModelFactory::GetForProfile(
444 profile_)),
445 path_, favicons_map_.release(), observer_)));
446 if (fetcher != NULL) {
447 base::MessageLoop::current()->DeleteSoon(FROM_HERE, fetcher);
448 fetcher = NULL;
449 }
450 }
451
FetchNextFavicon()452 bool BookmarkFaviconFetcher::FetchNextFavicon() {
453 if (bookmark_urls_.empty()) {
454 return false;
455 }
456 do {
457 std::string url = bookmark_urls_.front();
458 // Filter out urls that we've already got favicon for.
459 URLFaviconMap::const_iterator iter = favicons_map_->find(url);
460 if (favicons_map_->end() == iter) {
461 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile(
462 profile_, Profile::EXPLICIT_ACCESS);
463 favicon_service->GetRawFaviconForURL(
464 FaviconService::FaviconForURLParams(
465 GURL(url), chrome::FAVICON, gfx::kFaviconSize),
466 ui::SCALE_FACTOR_100P,
467 base::Bind(&BookmarkFaviconFetcher::OnFaviconDataAvailable,
468 base::Unretained(this)),
469 &cancelable_task_tracker_);
470 return true;
471 } else {
472 bookmark_urls_.pop_front();
473 }
474 } while (!bookmark_urls_.empty());
475 return false;
476 }
477
OnFaviconDataAvailable(const chrome::FaviconBitmapResult & bitmap_result)478 void BookmarkFaviconFetcher::OnFaviconDataAvailable(
479 const chrome::FaviconBitmapResult& bitmap_result) {
480 GURL url;
481 if (!bookmark_urls_.empty()) {
482 url = GURL(bookmark_urls_.front());
483 bookmark_urls_.pop_front();
484 }
485 if (bitmap_result.is_valid() && !url.is_empty()) {
486 favicons_map_->insert(
487 make_pair(url.spec(), bitmap_result.bitmap_data));
488 }
489
490 if (FetchNextFavicon()) {
491 return;
492 }
493 ExecuteWriter();
494 }
495
496 namespace bookmark_html_writer {
497
WriteBookmarks(Profile * profile,const base::FilePath & path,BookmarksExportObserver * observer)498 void WriteBookmarks(Profile* profile,
499 const base::FilePath& path,
500 BookmarksExportObserver* observer) {
501 // BookmarkModel isn't thread safe (nor would we want to lock it down
502 // for the duration of the write), as such we make a copy of the
503 // BookmarkModel using BookmarkCodec then write from that.
504 if (fetcher == NULL) {
505 fetcher = new BookmarkFaviconFetcher(profile, path, observer);
506 fetcher->ExportBookmarks();
507 }
508 }
509
510 } // namespace bookmark_html_writer
511