| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/bookmarks/bookmark_html_writer.h" | 5 #include "chrome/browser/bookmarks/bookmark_html_writer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 #include "net/base/escape.h" | 35 #include "net/base/escape.h" |
| 36 #include "ui/base/l10n/l10n_util.h" | 36 #include "ui/base/l10n/l10n_util.h" |
| 37 #include "ui/gfx/favicon_size.h" | 37 #include "ui/gfx/favicon_size.h" |
| 38 | 38 |
| 39 using bookmarks::BookmarkCodec; | 39 using bookmarks::BookmarkCodec; |
| 40 using bookmarks::BookmarkNode; | 40 using bookmarks::BookmarkNode; |
| 41 using content::BrowserThread; | 41 using content::BrowserThread; |
| 42 | 42 |
| 43 namespace { | 43 namespace { |
| 44 | 44 |
| 45 static BookmarkFaviconFetcher* fetcher = NULL; | 45 BookmarkFaviconFetcher* g_fetcher = nullptr; |
| 46 | 46 |
| 47 // File header. | 47 // File header. |
| 48 const char kHeader[] = | 48 const char kHeader[] = |
| 49 "<!DOCTYPE NETSCAPE-Bookmark-file-1>\r\n" | 49 "<!DOCTYPE NETSCAPE-Bookmark-file-1>\r\n" |
| 50 "<!-- This is an automatically generated file.\r\n" | 50 "<!-- This is an automatically generated file.\r\n" |
| 51 " It will be read and overwritten.\r\n" | 51 " It will be read and overwritten.\r\n" |
| 52 " DO NOT EDIT! -->\r\n" | 52 " DO NOT EDIT! -->\r\n" |
| 53 "<META HTTP-EQUIV=\"Content-Type\"" | 53 "<META HTTP-EQUIV=\"Content-Type\"" |
| 54 " CONTENT=\"text/html; charset=UTF-8\">\r\n" | 54 " CONTENT=\"text/html; charset=UTF-8\">\r\n" |
| 55 "<TITLE>Bookmarks</TITLE>\r\n" | 55 "<TITLE>Bookmarks</TITLE>\r\n" |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 if (!bookmark_urls_.empty()) | 416 if (!bookmark_urls_.empty()) |
| 417 FetchNextFavicon(); | 417 FetchNextFavicon(); |
| 418 else | 418 else |
| 419 ExecuteWriter(); | 419 ExecuteWriter(); |
| 420 } | 420 } |
| 421 | 421 |
| 422 void BookmarkFaviconFetcher::Observe( | 422 void BookmarkFaviconFetcher::Observe( |
| 423 int type, | 423 int type, |
| 424 const content::NotificationSource& source, | 424 const content::NotificationSource& source, |
| 425 const content::NotificationDetails& details) { | 425 const content::NotificationDetails& details) { |
| 426 if (chrome::NOTIFICATION_PROFILE_DESTROYED == type && fetcher != NULL) { | 426 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type); |
| 427 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, fetcher); | 427 if (g_fetcher) { |
| 428 fetcher = NULL; | 428 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, g_fetcher); |
| 429 g_fetcher = nullptr; |
| 429 } | 430 } |
| 430 } | 431 } |
| 431 | 432 |
| 432 void BookmarkFaviconFetcher::ExtractUrls(const BookmarkNode* node) { | 433 void BookmarkFaviconFetcher::ExtractUrls(const BookmarkNode* node) { |
| 433 if (node->is_url()) { | 434 if (node->is_url()) { |
| 434 std::string url = node->url().spec(); | 435 std::string url = node->url().spec(); |
| 435 if (!url.empty()) | 436 if (!url.empty()) |
| 436 bookmark_urls_.push_back(url); | 437 bookmark_urls_.push_back(url); |
| 437 } else { | 438 } else { |
| 438 for (int i = 0; i < node->child_count(); ++i) | 439 for (int i = 0; i < node->child_count(); ++i) |
| 439 ExtractUrls(node->GetChild(i)); | 440 ExtractUrls(node->GetChild(i)); |
| 440 } | 441 } |
| 441 } | 442 } |
| 442 | 443 |
| 443 void BookmarkFaviconFetcher::ExecuteWriter() { | 444 void BookmarkFaviconFetcher::ExecuteWriter() { |
| 444 // BookmarkModel isn't thread safe (nor would we want to lock it down | 445 // BookmarkModel isn't thread safe (nor would we want to lock it down |
| 445 // for the duration of the write), as such we make a copy of the | 446 // for the duration of the write), as such we make a copy of the |
| 446 // BookmarkModel using BookmarkCodec then write from that. | 447 // BookmarkModel using BookmarkCodec then write from that. |
| 447 BookmarkCodec codec; | 448 BookmarkCodec codec; |
| 448 BrowserThread::PostTask( | 449 BrowserThread::PostTask( |
| 449 BrowserThread::FILE, FROM_HERE, | 450 BrowserThread::FILE, FROM_HERE, |
| 450 base::Bind(&Writer::DoWrite, | 451 base::Bind(&Writer::DoWrite, |
| 451 new Writer(codec.Encode(BookmarkModelFactory::GetForProfile( | 452 new Writer(codec.Encode(BookmarkModelFactory::GetForProfile( |
| 452 profile_)), | 453 profile_)), |
| 453 path_, favicons_map_.release(), observer_))); | 454 path_, favicons_map_.release(), observer_))); |
| 454 if (fetcher != NULL) { | 455 if (g_fetcher) { |
| 455 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, fetcher); | 456 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, g_fetcher); |
| 456 fetcher = NULL; | 457 g_fetcher = nullptr; |
| 457 } | 458 } |
| 458 } | 459 } |
| 459 | 460 |
| 460 bool BookmarkFaviconFetcher::FetchNextFavicon() { | 461 bool BookmarkFaviconFetcher::FetchNextFavicon() { |
| 461 if (bookmark_urls_.empty()) { | 462 if (bookmark_urls_.empty()) { |
| 462 return false; | 463 return false; |
| 463 } | 464 } |
| 464 do { | 465 do { |
| 465 std::string url = bookmark_urls_.front(); | 466 std::string url = bookmark_urls_.front(); |
| 466 // Filter out urls that we've already got favicon for. | 467 // Filter out urls that we've already got favicon for. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 } | 504 } |
| 504 | 505 |
| 505 namespace bookmark_html_writer { | 506 namespace bookmark_html_writer { |
| 506 | 507 |
| 507 void WriteBookmarks(Profile* profile, | 508 void WriteBookmarks(Profile* profile, |
| 508 const base::FilePath& path, | 509 const base::FilePath& path, |
| 509 BookmarksExportObserver* observer) { | 510 BookmarksExportObserver* observer) { |
| 510 // BookmarkModel isn't thread safe (nor would we want to lock it down | 511 // BookmarkModel isn't thread safe (nor would we want to lock it down |
| 511 // for the duration of the write), as such we make a copy of the | 512 // for the duration of the write), as such we make a copy of the |
| 512 // BookmarkModel using BookmarkCodec then write from that. | 513 // BookmarkModel using BookmarkCodec then write from that. |
| 513 if (fetcher == NULL) { | 514 if (!g_fetcher) { |
| 514 fetcher = new BookmarkFaviconFetcher(profile, path, observer); | 515 g_fetcher = new BookmarkFaviconFetcher(profile, path, observer); |
| 515 fetcher->ExportBookmarks(); | 516 g_fetcher->ExportBookmarks(); |
| 516 } | 517 } |
| 517 } | 518 } |
| 518 | 519 |
| 519 } // namespace bookmark_html_writer | 520 } // namespace bookmark_html_writer |
| OLD | NEW |