Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / search / thumbnail_source.cc
blobf0d47c7f6ebefe906e019e85eeaf0d962162004f
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/search/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/search/suggestions/image_fetcher_impl.h"
13 #include "chrome/browser/thumbnails/thumbnail_service.h"
14 #include "chrome/browser/thumbnails/thumbnail_service_factory.h"
15 #include "chrome/common/url_constants.h"
16 #include "components/suggestions/image_encoder.h"
17 #include "net/url_request/url_request.h"
18 #include "ui/gfx/image/image_skia.h"
20 // The delimiter between the first url and the fallback url passed to
21 // StartDataRequest.
22 const char kUrlDelimiter[] = "?fb=";
24 // Set ThumbnailService now as Profile isn't thread safe.
25 ThumbnailSource::ThumbnailSource(Profile* profile, bool capture_thumbnails)
26 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)),
27 capture_thumbnails_(capture_thumbnails),
28 weak_ptr_factory_(this) {
29 image_fetcher_.reset(
30 new suggestions::ImageFetcherImpl(profile->GetRequestContext()));
33 ThumbnailSource::~ThumbnailSource() {
36 std::string ThumbnailSource::GetSource() const {
37 return capture_thumbnails_ ?
38 chrome::kChromeUIThumbnailHost2 : chrome::kChromeUIThumbnailHost;
41 void ThumbnailSource::StartDataRequest(
42 const std::string& path,
43 int render_process_id,
44 int render_frame_id,
45 const content::URLDataSource::GotDataCallback& callback) {
46 GURL page_url;
47 GURL fallback_thumbnail_url;
48 ExtractPageAndThumbnailUrls(path, &page_url, &fallback_thumbnail_url);
50 scoped_refptr<base::RefCountedMemory> data;
51 if (page_url.is_valid() &&
52 thumbnail_service_->GetPageThumbnail(page_url, capture_thumbnails_,
53 &data)) {
54 // If a local thumbnail is available for the page's URL, provide it.
55 callback.Run(data.get());
56 } else if (fallback_thumbnail_url.is_valid()) {
57 // Otherwise, if a fallback thumbnail URL was provided, fetch it and
58 // eventually return it.
59 image_fetcher_->StartOrQueueNetworkRequest(
60 page_url, fallback_thumbnail_url,
61 base::Bind(&ThumbnailSource::SendFetchedUrlImage,
62 weak_ptr_factory_.GetWeakPtr(), callback));
63 } else {
64 callback.Run(default_thumbnail_.get());
66 if (page_url.is_valid() && capture_thumbnails_)
67 thumbnail_service_->AddForcedURL(page_url);
70 std::string ThumbnailSource::GetMimeType(const std::string&) const {
71 // We need to explicitly return a mime type, otherwise if the user tries to
72 // drag the image they get no extension.
73 return "image/png";
76 base::MessageLoop* ThumbnailSource::MessageLoopForRequestPath(
77 const std::string& path) const {
78 // TopSites can be accessed from the IO thread. Otherwise, the URLs should be
79 // accessed on the UI thread.
80 return thumbnail_service_.get()
81 ? NULL
82 : content::URLDataSource::MessageLoopForRequestPath(path);
85 bool ThumbnailSource::ShouldServiceRequest(
86 const net::URLRequest* request) const {
87 if (request->url().SchemeIs(chrome::kChromeSearchScheme))
88 return InstantIOContext::ShouldServiceRequest(request);
89 return URLDataSource::ShouldServiceRequest(request);
92 void ThumbnailSource::ExtractPageAndThumbnailUrls(
93 const std::string& path,
94 GURL* page_url,
95 GURL* fallback_thumbnail_url) {
96 std::string page_url_str(path);
97 std::string fallback_thumbnail_url_str;
99 size_t pos = path.find(kUrlDelimiter);
100 if (pos != std::string::npos) {
101 page_url_str = path.substr(0, pos);
102 fallback_thumbnail_url_str = path.substr(pos + strlen(kUrlDelimiter));
105 *page_url = GURL(page_url_str);
106 *fallback_thumbnail_url = GURL(fallback_thumbnail_url_str);
109 void ThumbnailSource::SendFetchedUrlImage(
110 const content::URLDataSource::GotDataCallback& callback,
111 const GURL& url,
112 const SkBitmap* bitmap) {
113 if (!bitmap) {
114 callback.Run(default_thumbnail_.get());
115 return;
118 scoped_refptr<base::RefCountedBytes> encoded_data(
119 new base::RefCountedBytes());
120 if (!suggestions::EncodeSkBitmapToJPEG(*bitmap, &encoded_data->data())) {
121 callback.Run(default_thumbnail_.get());
122 return;
125 callback.Run(encoded_data.get());