| 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/ui/webui/ntp/thumbnail_source.h" | 5 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "chrome/browser/instant/instant_io_context.h" | 10 #include "chrome/browser/instant/instant_io_context.h" |
| 11 #include "chrome/browser/instant/instant_service.h" |
| 12 #include "chrome/browser/instant/instant_service_factory.h" |
| 11 #include "chrome/browser/thumbnails/thumbnail_service.h" | 13 #include "chrome/browser/thumbnails/thumbnail_service.h" |
| 12 #include "chrome/browser/thumbnails/thumbnail_service_factory.h" | 14 #include "chrome/browser/thumbnails/thumbnail_service_factory.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 15 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 16 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
| 17 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 19 | 21 |
| 22 using content::BrowserThread; |
| 23 |
| 20 // Set ThumbnailService now as Profile isn't thread safe. | 24 // Set ThumbnailService now as Profile isn't thread safe. |
| 21 ThumbnailSource::ThumbnailSource(Profile* profile) | 25 ThumbnailSource::ThumbnailSource(Profile* profile) |
| 22 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)) { | 26 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)), |
| 27 current_request_(NULL), |
| 28 profile_(profile) { |
| 23 } | 29 } |
| 24 | 30 |
| 25 ThumbnailSource::~ThumbnailSource() { | 31 ThumbnailSource::~ThumbnailSource() { |
| 26 } | 32 } |
| 27 | 33 |
| 28 std::string ThumbnailSource::GetSource() { | 34 std::string ThumbnailSource::GetSource() { |
| 29 return chrome::kChromeUIThumbnailHost; | 35 return chrome::kChromeUIThumbnailHost; |
| 30 } | 36 } |
| 31 | 37 |
| 32 void ThumbnailSource::StartDataRequest( | 38 void ThumbnailSource::StartDataRequest( |
| 33 const std::string& path, | 39 const std::string& raw_path, |
| 34 bool is_incognito, | 40 bool is_incognito, |
| 35 const content::URLDataSource::GotDataCallback& callback) { | 41 const content::URLDataSource::GotDataCallback& callback) { |
| 42 // Translate to regular path if |raw_path| is of the form |
| 43 // chrome-search://favicon/<rid>, where rid is a uint64. |
| 44 std::string path = raw_path; |
| 45 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 46 path = InstantService::MaybeTranslateInstantPathOnIO( |
| 47 current_request_, raw_path); |
| 48 } else if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 49 path = InstantService::MaybeTranslateInstantPathOnUI(profile_, raw_path); |
| 50 } |
| 51 |
| 36 scoped_refptr<base::RefCountedMemory> data; | 52 scoped_refptr<base::RefCountedMemory> data; |
| 37 if (thumbnail_service_->GetPageThumbnail(GURL(path), &data)) { | 53 if (thumbnail_service_->GetPageThumbnail(GURL(path), &data)) { |
| 38 // We have the thumbnail. | 54 // We have the thumbnail. |
| 39 callback.Run(data.get()); | 55 callback.Run(data.get()); |
| 40 } else { | 56 } else { |
| 41 callback.Run(default_thumbnail_); | 57 callback.Run(default_thumbnail_); |
| 42 } | 58 } |
| 59 current_request_ = NULL; |
| 43 } | 60 } |
| 44 | 61 |
| 45 std::string ThumbnailSource::GetMimeType(const std::string&) const { | 62 std::string ThumbnailSource::GetMimeType(const std::string&) const { |
| 46 // We need to explicitly return a mime type, otherwise if the user tries to | 63 // We need to explicitly return a mime type, otherwise if the user tries to |
| 47 // drag the image they get no extension. | 64 // drag the image they get no extension. |
| 48 return "image/png"; | 65 return "image/png"; |
| 49 } | 66 } |
| 50 | 67 |
| 51 MessageLoop* ThumbnailSource::MessageLoopForRequestPath( | 68 MessageLoop* ThumbnailSource::MessageLoopForRequestPath( |
| 52 const std::string& path) const { | 69 const std::string& path) const { |
| 53 // TopSites can be accessed from the IO thread. | 70 // TopSites can be accessed from the IO thread. |
| 54 return thumbnail_service_.get() ? | 71 return thumbnail_service_.get() ? |
| 55 NULL : content::URLDataSource::MessageLoopForRequestPath(path); | 72 NULL : content::URLDataSource::MessageLoopForRequestPath(path); |
| 56 } | 73 } |
| 57 | 74 |
| 58 bool ThumbnailSource::ShouldServiceRequest( | 75 bool ThumbnailSource::ShouldServiceRequest( |
| 59 const net::URLRequest* request) const { | 76 const net::URLRequest* request) const { |
| 60 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) | 77 current_request_ = request; |
| 61 return InstantIOContext::ShouldServiceRequest(request); | 78 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { |
| 79 return InstantService::IsInstantPath(request->url()) && |
| 80 InstantIOContext::ShouldServiceRequest(request); |
| 81 } |
| 62 return URLDataSource::ShouldServiceRequest(request); | 82 return URLDataSource::ShouldServiceRequest(request); |
| 63 } | 83 } |
| OLD | NEW |