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.
5 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
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"
18 // Set ThumbnailService now as Profile isn't thread safe.
19 ThumbnailSource::ThumbnailSource(Profile
* profile
, bool capture_thumbnails
)
20 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile
)),
22 capture_thumbnails_(capture_thumbnails
) {
25 ThumbnailSource::~ThumbnailSource() {
28 std::string
ThumbnailSource::GetSource() const {
29 return capture_thumbnails_
?
30 chrome::kChromeUIThumbnailHost2
: chrome::kChromeUIThumbnailHost
;
33 void ThumbnailSource::StartDataRequest(
34 const std::string
& path
,
35 int render_process_id
,
37 const content::URLDataSource::GotDataCallback
& callback
) {
38 scoped_refptr
<base::RefCountedMemory
> data
;
39 if (thumbnail_service_
->GetPageThumbnail(GURL(path
), capture_thumbnails_
,
41 // We have the thumbnail.
42 callback
.Run(data
.get());
44 callback
.Run(default_thumbnail_
.get());
46 if (capture_thumbnails_
)
47 thumbnail_service_
->AddForcedURL(GURL(path
));
50 std::string
ThumbnailSource::GetMimeType(const std::string
&) const {
51 // We need to explicitly return a mime type, otherwise if the user tries to
52 // drag the image they get no extension.
56 base::MessageLoop
* ThumbnailSource::MessageLoopForRequestPath(
57 const std::string
& path
) const {
58 // TopSites can be accessed from the IO thread.
59 return thumbnail_service_
.get() ?
60 NULL
: content::URLDataSource::MessageLoopForRequestPath(path
);
63 bool ThumbnailSource::ShouldServiceRequest(
64 const net::URLRequest
* request
) const {
65 if (request
->url().SchemeIs(chrome::kChromeSearchScheme
))
66 return InstantIOContext::ShouldServiceRequest(request
);
67 return URLDataSource::ShouldServiceRequest(request
);