Chromium Code Reviews| 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 "base/strings/string_number_conversions.h" | |
| 10 #include "chrome/browser/instant/instant_io_context.h" | 11 #include "chrome/browser/instant/instant_io_context.h" |
| 12 #include "chrome/browser/instant/instant_service.h" | |
| 13 #include "chrome/browser/instant/instant_service_factory.h" | |
| 11 #include "chrome/browser/thumbnails/thumbnail_service.h" | 14 #include "chrome/browser/thumbnails/thumbnail_service.h" |
| 12 #include "chrome/browser/thumbnails/thumbnail_service_factory.h" | 15 #include "chrome/browser/thumbnails/thumbnail_service_factory.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 15 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 16 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
| 17 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 19 | 22 |
| 23 using content::BrowserThread; | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 const std::string MaybeTranslateInstantPath( | |
|
palmer
2013/03/11 20:42:31
This is (or is nearly) duplicate code. Unify it by
dhollowa
2013/03/11 23:27:59
I considered that, but the two code paths are on d
| |
| 28 const net::URLRequest* request, const std::string& path) { | |
| 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 30 uint64 restricted_id = 0; | |
| 31 if (base::StringToUint64(path, &restricted_id)) { | |
| 32 GURL url; | |
| 33 if (InstantIOContext::GetURLForRestrictedId(request, restricted_id, &url)) | |
| 34 return url.spec(); | |
| 35 } | |
| 36 return path; | |
| 37 } | |
| 38 | |
| 39 } | |
| 40 | |
| 20 // Set ThumbnailService now as Profile isn't thread safe. | 41 // Set ThumbnailService now as Profile isn't thread safe. |
| 21 ThumbnailSource::ThumbnailSource(Profile* profile) | 42 ThumbnailSource::ThumbnailSource(Profile* profile) |
| 22 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)) { | 43 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)), |
| 44 current_request_(NULL), | |
| 45 profile_(profile) { | |
| 23 } | 46 } |
| 24 | 47 |
| 25 ThumbnailSource::~ThumbnailSource() { | 48 ThumbnailSource::~ThumbnailSource() { |
| 26 } | 49 } |
| 27 | 50 |
| 28 std::string ThumbnailSource::GetSource() { | 51 std::string ThumbnailSource::GetSource() { |
| 29 return chrome::kChromeUIThumbnailHost; | 52 return chrome::kChromeUIThumbnailHost; |
| 30 } | 53 } |
| 31 | 54 |
| 32 void ThumbnailSource::StartDataRequest( | 55 void ThumbnailSource::StartDataRequest( |
| 33 const std::string& path, | 56 const std::string& raw_path, |
| 34 bool is_incognito, | 57 bool is_incognito, |
| 35 const content::URLDataSource::GotDataCallback& callback) { | 58 const content::URLDataSource::GotDataCallback& callback) { |
| 59 // Translate to regular path if |raw_path| is of the form | |
| 60 // chrome-search://favicon/<rid>, where rid is a uint64. | |
| 61 std::string path = raw_path; | |
| 62 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) | |
| 63 path = MaybeTranslateInstantPath(current_request_, raw_path); | |
| 64 else if (BrowserThread::CurrentlyOn(BrowserThread::UI)) | |
| 65 path = InstantService::MaybeTranslateInstantPath(profile_, raw_path); | |
| 66 | |
| 36 scoped_refptr<base::RefCountedMemory> data; | 67 scoped_refptr<base::RefCountedMemory> data; |
| 37 if (thumbnail_service_->GetPageThumbnail(GURL(path), &data)) { | 68 if (thumbnail_service_->GetPageThumbnail(GURL(path), &data)) { |
| 38 // We have the thumbnail. | 69 // We have the thumbnail. |
| 39 callback.Run(data.get()); | 70 callback.Run(data.get()); |
| 40 } else { | 71 } else { |
| 41 callback.Run(default_thumbnail_); | 72 callback.Run(default_thumbnail_); |
| 42 } | 73 } |
| 74 current_request_ = NULL; | |
| 43 } | 75 } |
| 44 | 76 |
| 45 std::string ThumbnailSource::GetMimeType(const std::string&) const { | 77 std::string ThumbnailSource::GetMimeType(const std::string&) const { |
| 46 // We need to explicitly return a mime type, otherwise if the user tries to | 78 // We need to explicitly return a mime type, otherwise if the user tries to |
| 47 // drag the image they get no extension. | 79 // drag the image they get no extension. |
| 48 return "image/png"; | 80 return "image/png"; |
| 49 } | 81 } |
| 50 | 82 |
| 51 MessageLoop* ThumbnailSource::MessageLoopForRequestPath( | 83 MessageLoop* ThumbnailSource::MessageLoopForRequestPath( |
| 52 const std::string& path) const { | 84 const std::string& path) const { |
| 53 // TopSites can be accessed from the IO thread. | 85 // TopSites can be accessed from the IO thread. |
| 54 return thumbnail_service_.get() ? | 86 return thumbnail_service_.get() ? |
| 55 NULL : content::URLDataSource::MessageLoopForRequestPath(path); | 87 NULL : content::URLDataSource::MessageLoopForRequestPath(path); |
| 56 } | 88 } |
| 57 | 89 |
| 58 bool ThumbnailSource::ShouldServiceRequest( | 90 bool ThumbnailSource::ShouldServiceRequest( |
| 59 const net::URLRequest* request) const { | 91 const net::URLRequest* request) const { |
| 60 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) | 92 current_request_ = request; |
| 61 return InstantIOContext::ShouldServiceRequest(request); | 93 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { |
| 94 return InstantService::IsInstantPath(request->url()) && | |
| 95 InstantIOContext::ShouldServiceRequest(request); | |
| 96 } | |
| 62 return URLDataSource::ShouldServiceRequest(request); | 97 return URLDataSource::ShouldServiceRequest(request); |
| 63 } | 98 } |
| OLD | NEW |