| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/dom_ui/dom_ui_thumbnail_source.h" | 5 #include "chrome/browser/dom_ui/dom_ui_thumbnail_source.h" |
| 6 | 6 |
| 7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
| 11 #include "chrome/browser/history/top_sites.h" | 11 #include "chrome/browser/history/top_sites.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/notification_service.h" | 13 #include "chrome/common/notification_service.h" |
| 14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "gfx/codec/jpeg_codec.h" | 15 #include "gfx/codec/jpeg_codec.h" |
| 16 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 17 #include "grit/theme_resources.h" | 17 #include "grit/theme_resources.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 19 | 19 |
| 20 DOMUIThumbnailSource::DOMUIThumbnailSource(Profile* profile) | 20 DOMUIThumbnailSource::DOMUIThumbnailSource(Profile* profile) |
| 21 : DataSource(chrome::kChromeUIThumbnailPath, MessageLoop::current()), | 21 : DataSource(chrome::kChromeUIThumbnailPath, MessageLoop::current()), |
| 22 profile_(profile) { | 22 profile_(profile) { |
| 23 } | 23 } |
| 24 | 24 |
| 25 DOMUIThumbnailSource::~DOMUIThumbnailSource() { |
| 26 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 27 } |
| 28 |
| 25 void DOMUIThumbnailSource::StartDataRequest(const std::string& path, | 29 void DOMUIThumbnailSource::StartDataRequest(const std::string& path, |
| 26 bool is_off_the_record, | 30 bool is_off_the_record, |
| 27 int request_id) { | 31 int request_id) { |
| 32 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 28 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTopSites)) { | 33 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTopSites)) { |
| 29 scoped_refptr<history::TopSites> top_sites = profile_->GetTopSites(); | 34 scoped_refptr<history::TopSites> top_sites = profile_->GetTopSites(); |
| 30 RefCountedBytes* data = NULL; | 35 RefCountedBytes* data = NULL; |
| 31 if (top_sites->GetPageThumbnail(GURL(path), &data)) { | 36 if (top_sites->GetPageThumbnail(GURL(path), &data)) { |
| 32 // We have the thumbnail. | 37 // We have the thumbnail. |
| 33 SendResponse(request_id, data); | 38 SendResponse(request_id, data); |
| 34 } else { | 39 } else { |
| 35 SendDefaultThumbnail(request_id); | 40 SendDefaultThumbnail(request_id); |
| 36 } | 41 } |
| 37 return; | 42 return; |
| 38 } // end --top-sites switch | 43 } // end --top-sites switch |
| 39 | 44 |
| 40 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 45 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 41 if (hs) { | 46 if (hs) { |
| 42 HistoryService::Handle handle = hs->GetPageThumbnail( | 47 HistoryService::Handle handle = hs->GetPageThumbnail( |
| 43 GURL(path), | 48 GURL(path), |
| 44 &cancelable_consumer_, | 49 &cancelable_consumer_, |
| 45 NewCallback(this, &DOMUIThumbnailSource::OnThumbnailDataAvailable)); | 50 NewCallback(this, &DOMUIThumbnailSource::OnThumbnailDataAvailable)); |
| 46 // Attach the ChromeURLDataManager request ID to the history request. | 51 // Attach the ChromeURLDataManager request ID to the history request. |
| 47 cancelable_consumer_.SetClientData(hs, handle, request_id); | 52 cancelable_consumer_.SetClientData(hs, handle, request_id); |
| 48 } else { | 53 } else { |
| 49 // Tell the caller that no thumbnail is available. | 54 // Tell the caller that no thumbnail is available. |
| 50 SendResponse(request_id, NULL); | 55 SendResponse(request_id, NULL); |
| 51 } | 56 } |
| 52 } | 57 } |
| 53 | 58 |
| 59 std::string DOMUIThumbnailSource::GetMimeType(const std::string&) const { |
| 60 // We need to explicitly return a mime type, otherwise if the user tries to |
| 61 // drag the image they get no extension. |
| 62 return "image/png"; |
| 63 } |
| 64 |
| 54 void DOMUIThumbnailSource::SendDefaultThumbnail(int request_id) { | 65 void DOMUIThumbnailSource::SendDefaultThumbnail(int request_id) { |
| 55 // Use placeholder thumbnail. | 66 // Use placeholder thumbnail. |
| 56 if (!default_thumbnail_.get()) { | 67 if (!default_thumbnail_.get()) { |
| 57 default_thumbnail_ = | 68 default_thumbnail_ = |
| 58 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 69 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 59 IDR_DEFAULT_THUMBNAIL); | 70 IDR_DEFAULT_THUMBNAIL); |
| 60 } | 71 } |
| 61 SendResponse(request_id, default_thumbnail_); | 72 SendResponse(request_id, default_thumbnail_); |
| 62 } | 73 } |
| 63 | 74 |
| 64 void DOMUIThumbnailSource::OnThumbnailDataAvailable( | 75 void DOMUIThumbnailSource::OnThumbnailDataAvailable( |
| 65 HistoryService::Handle request_handle, | 76 HistoryService::Handle request_handle, |
| 66 scoped_refptr<RefCountedBytes> data) { | 77 scoped_refptr<RefCountedBytes> data) { |
| 78 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 67 HistoryService* hs = | 79 HistoryService* hs = |
| 68 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 80 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 69 int request_id = cancelable_consumer_.GetClientData(hs, request_handle); | 81 int request_id = cancelable_consumer_.GetClientData(hs, request_handle); |
| 70 // Forward the data along to the networking system. | 82 // Forward the data along to the networking system. |
| 71 if (data.get() && !data->data.empty()) { | 83 if (data.get() && !data->data.empty()) { |
| 72 SendResponse(request_id, data); | 84 SendResponse(request_id, data); |
| 73 } else { | 85 } else { |
| 74 SendDefaultThumbnail(request_id); | 86 SendDefaultThumbnail(request_id); |
| 75 } | 87 } |
| 76 } | 88 } |
| OLD | NEW |