[Thumbnails] Injecting TopSites into ThumbnailListSource.

Previously we store Profile and use it to retrieve TopSites. This caused
debug mode crash when we tried to retrieve TopSites from Profile from a
different thread. By storing TopSites directly this problem is averted.

BUG=468141

Review URL: https://codereview.chromium.org/1021803002

Cr-Commit-Position: refs/heads/master@{#321488}
diff --git a/chrome/browser/thumbnails/thumbnail_list_source.cc b/chrome/browser/thumbnails/thumbnail_list_source.cc
index 1cba5a6..604b3d3 100644
--- a/chrome/browser/thumbnails/thumbnail_list_source.cc
+++ b/chrome/browser/thumbnails/thumbnail_list_source.cc
@@ -18,9 +18,12 @@
 #include "chrome/browser/thumbnails/thumbnail_service_factory.h"
 #include "chrome/common/url_constants.h"
 #include "components/history/core/browser/top_sites.h"
+#include "content/public/browser/browser_thread.h"
 #include "net/base/escape.h"
 #include "net/url_request/url_request.h"
 
+using content::BrowserThread;
+
 namespace {
 
 const char kHtmlHeader[] =
@@ -79,7 +82,7 @@
 
 ThumbnailListSource::ThumbnailListSource(Profile* profile)
     : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)),
-      profile_(profile),
+      top_sites_(TopSitesFactory::GetForProfile(profile)),
       weak_ptr_factory_(this) {
 }
 
@@ -95,14 +98,13 @@
     int render_process_id,
     int render_frame_id,
     const content::URLDataSource::GotDataCallback& callback) {
-  scoped_refptr<history::TopSites> top_sites =
-      TopSitesFactory::GetForProfile(profile_);
-  if (!top_sites) {
+  DCHECK_CURRENTLY_ON(BrowserThread::IO);
+  if (!top_sites_) {
     callback.Run(NULL);
     return;
   }
 
-  top_sites->GetMostVisitedURLs(
+  top_sites_->GetMostVisitedURLs(
       base::Bind(&ThumbnailListSource::OnMostVisitedURLsAvailable,
                  weak_ptr_factory_.GetWeakPtr(), callback),
       true);
@@ -133,6 +135,7 @@
 void ThumbnailListSource::OnMostVisitedURLsAvailable(
     const content::URLDataSource::GotDataCallback& callback,
     const history::MostVisitedURLList& mvurl_list) {
+  DCHECK_CURRENTLY_ON(BrowserThread::IO);
   const size_t num_mv = mvurl_list.size();
   size_t num_mv_with_thumb = 0;
 
diff --git a/chrome/browser/thumbnails/thumbnail_list_source.h b/chrome/browser/thumbnails/thumbnail_list_source.h
index 9421cc6..d587bfd 100644
--- a/chrome/browser/thumbnails/thumbnail_list_source.h
+++ b/chrome/browser/thumbnails/thumbnail_list_source.h
@@ -19,6 +19,10 @@
 class RefCountedMemory;
 }
 
+namespace history {
+class TopSites;
+}
+
 namespace thumbnails {
 class ThumbnailService;
 }
@@ -33,11 +37,13 @@
 
   // content::URLDataSource implementation.
   std::string GetSource() const override;
+  // Called on the IO thread.
   void StartDataRequest(
       const std::string& path,
       int render_process_id,
       int render_frame_id,
       const content::URLDataSource::GotDataCallback& callback) override;
+
   std::string GetMimeType(const std::string& path) const override;
   base::MessageLoop* MessageLoopForRequestPath(
       const std::string& path) const override;
@@ -47,6 +53,7 @@
  private:
   ~ThumbnailListSource() override;
 
+  // Called on the IO thread.
   void OnMostVisitedURLsAvailable(
     const content::URLDataSource::GotDataCallback& callback,
     const history::MostVisitedURLList& mvurl_list);
@@ -54,8 +61,7 @@
   // ThumbnailService.
   scoped_refptr<thumbnails::ThumbnailService> thumbnail_service_;
 
-  // Only used when servicing requests on the UI thread.
-  Profile* const profile_;
+  scoped_refptr<history::TopSites> top_sites_;
 
   // For callbacks may be run after destruction.
   base::WeakPtrFactory<ThumbnailListSource> weak_ptr_factory_;