• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ui/webui/ntp/thumbnail_source.h"
6 
7 #include "base/callback.h"
8 #include "base/memory/ref_counted_memory.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search/instant_io_context.h"
12 #include "chrome/browser/thumbnails/thumbnail_service.h"
13 #include "chrome/browser/thumbnails/thumbnail_service_factory.h"
14 #include "chrome/common/url_constants.h"
15 #include "net/url_request/url_request.h"
16 #include "url/gurl.h"
17 
18 using content::BrowserThread;
19 
20 // Set ThumbnailService now as Profile isn't thread safe.
ThumbnailSource(Profile * profile,bool capture_thumbnails)21 ThumbnailSource::ThumbnailSource(Profile* profile, bool capture_thumbnails)
22     : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)),
23       profile_(profile),
24       capture_thumbnails_(capture_thumbnails) {
25 }
26 
~ThumbnailSource()27 ThumbnailSource::~ThumbnailSource() {
28 }
29 
GetSource() const30 std::string ThumbnailSource::GetSource() const {
31   return capture_thumbnails_ ?
32       chrome::kChromeUIThumbnailHost2 : chrome::kChromeUIThumbnailHost;
33 }
34 
StartDataRequest(const std::string & path,int render_process_id,int render_frame_id,const content::URLDataSource::GotDataCallback & callback)35 void ThumbnailSource::StartDataRequest(
36     const std::string& path,
37     int render_process_id,
38     int render_frame_id,
39     const content::URLDataSource::GotDataCallback& callback) {
40   scoped_refptr<base::RefCountedMemory> data;
41   if (thumbnail_service_->GetPageThumbnail(GURL(path), capture_thumbnails_,
42                                            &data)) {
43     // We have the thumbnail.
44     callback.Run(data.get());
45   } else {
46     callback.Run(default_thumbnail_.get());
47   }
48   if (capture_thumbnails_)
49     thumbnail_service_->AddForcedURL(GURL(path));
50 }
51 
GetMimeType(const std::string &) const52 std::string ThumbnailSource::GetMimeType(const std::string&) const {
53   // We need to explicitly return a mime type, otherwise if the user tries to
54   // drag the image they get no extension.
55   return "image/png";
56 }
57 
MessageLoopForRequestPath(const std::string & path) const58 base::MessageLoop* ThumbnailSource::MessageLoopForRequestPath(
59     const std::string& path) const {
60   // TopSites can be accessed from the IO thread.
61   return thumbnail_service_.get() ?
62       NULL : content::URLDataSource::MessageLoopForRequestPath(path);
63 }
64 
ShouldServiceRequest(const net::URLRequest * request) const65 bool ThumbnailSource::ShouldServiceRequest(
66     const net::URLRequest* request) const {
67   if (request->url().SchemeIs(chrome::kChromeSearchScheme))
68     return InstantIOContext::ShouldServiceRequest(request);
69   return URLDataSource::ShouldServiceRequest(request);
70 }
71