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/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/search/instant_io_context.h" | 11 #include "chrome/browser/search/instant_io_context.h" |
12 #include "chrome/browser/search/instant_service.h" | 12 #include "chrome/browser/search/instant_service.h" |
13 #include "chrome/browser/search/instant_service_factory.h" | 13 #include "chrome/browser/search/instant_service_factory.h" |
14 #include "chrome/browser/thumbnails/thumbnail_service.h" | 14 #include "chrome/browser/thumbnails/thumbnail_service.h" |
15 #include "chrome/browser/thumbnails/thumbnail_service_factory.h" | 15 #include "chrome/browser/thumbnails/thumbnail_service_factory.h" |
16 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
18 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
19 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
21 | 21 |
22 using content::BrowserThread; | 22 using content::BrowserThread; |
23 | 23 |
24 // Set ThumbnailService now as Profile isn't thread safe. | 24 // Set ThumbnailService now as Profile isn't thread safe. |
25 ThumbnailSource::ThumbnailSource(Profile* profile) | 25 ThumbnailSource::ThumbnailSource(Profile* profile) |
26 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)), | 26 : thumbnail_service_(ThumbnailServiceFactory::GetForProfile(profile)), |
27 current_request_(NULL), | |
28 profile_(profile) { | 27 profile_(profile) { |
29 } | 28 } |
30 | 29 |
31 ThumbnailSource::~ThumbnailSource() { | 30 ThumbnailSource::~ThumbnailSource() { |
32 } | 31 } |
33 | 32 |
34 std::string ThumbnailSource::GetSource() { | 33 std::string ThumbnailSource::GetSource() { |
35 return chrome::kChromeUIThumbnailHost; | 34 return chrome::kChromeUIThumbnailHost; |
36 } | 35 } |
37 | 36 |
38 void ThumbnailSource::StartDataRequest( | 37 void ThumbnailSource::StartDataRequest( |
39 const std::string& raw_path, | 38 const std::string& raw_path, |
40 bool is_incognito, | 39 bool is_incognito, |
41 const content::URLDataSource::GotDataCallback& callback) { | 40 const content::URLDataSource::GotDataCallback& callback) { |
42 // Translate to regular path if |raw_path| is of the form | 41 // Translate to regular path if |raw_path| is of the form |
43 // chrome-search://favicon/<rid>, where rid is a uint64. | 42 // chrome-search://favicon/<id> or chrome-search://thumb/<id>, where <id> is |
43 // an integer. | |
44 std::string path = raw_path; | 44 std::string path = raw_path; |
45 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 45 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
46 path = InstantService::MaybeTranslateInstantPathOnIO( | 46 std::map<std::string, std::string>::iterator it = |
47 current_request_, raw_path); | 47 id_to_url_map_.find(raw_path); |
48 if (it != id_to_url_map_.end()) { | |
49 path = id_to_url_map_[raw_path]; | |
50 id_to_url_map_.erase(it); | |
51 } | |
48 } else if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 52 } else if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
49 path = InstantService::MaybeTranslateInstantPathOnUI(profile_, raw_path); | 53 path = InstantService::MaybeTranslateInstantPathOnUI(profile_, raw_path); |
50 } | 54 } |
51 | 55 |
52 scoped_refptr<base::RefCountedMemory> data; | 56 scoped_refptr<base::RefCountedMemory> data; |
53 if (thumbnail_service_->GetPageThumbnail(GURL(path), &data)) { | 57 if (thumbnail_service_->GetPageThumbnail(GURL(path), &data)) { |
54 // We have the thumbnail. | 58 // We have the thumbnail. |
55 callback.Run(data.get()); | 59 callback.Run(data.get()); |
56 } else { | 60 } else { |
57 callback.Run(default_thumbnail_); | 61 callback.Run(default_thumbnail_); |
58 } | 62 } |
59 current_request_ = NULL; | |
60 } | 63 } |
61 | 64 |
62 std::string ThumbnailSource::GetMimeType(const std::string&) const { | 65 std::string ThumbnailSource::GetMimeType(const std::string&) const { |
63 // We need to explicitly return a mime type, otherwise if the user tries to | 66 // We need to explicitly return a mime type, otherwise if the user tries to |
64 // drag the image they get no extension. | 67 // drag the image they get no extension. |
65 return "image/png"; | 68 return "image/png"; |
66 } | 69 } |
67 | 70 |
68 MessageLoop* ThumbnailSource::MessageLoopForRequestPath( | 71 MessageLoop* ThumbnailSource::MessageLoopForRequestPath( |
69 const std::string& path) const { | 72 const std::string& path) const { |
70 // TopSites can be accessed from the IO thread. | 73 // TopSites can be accessed from the IO thread. |
71 return thumbnail_service_.get() ? | 74 return thumbnail_service_.get() ? |
72 NULL : content::URLDataSource::MessageLoopForRequestPath(path); | 75 NULL : content::URLDataSource::MessageLoopForRequestPath(path); |
73 } | 76 } |
74 | 77 |
75 bool ThumbnailSource::ShouldServiceRequest( | 78 bool ThumbnailSource::ShouldServiceRequest( |
76 const net::URLRequest* request) const { | 79 const net::URLRequest* request) const { |
77 current_request_ = request; | |
78 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { | 80 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { |
79 return InstantService::IsInstantPath(request->url()) && | 81 if (InstantService::IsInstantPath(request->url()) && |
80 InstantIOContext::ShouldServiceRequest(request); | 82 InstantIOContext::ShouldServiceRequest(request)) { |
83 // If this request will be serviced on the IO thread, then do the | |
84 // translation from raw_path to path here, where we have the |request| | |
85 // object in-hand, saving the result for later use. | |
86 std::string raw_path = request->url().path().substr(1); | |
Dan Beam
2013/03/20 19:26:13
why .substr(1)?
dhollowa
2013/03/20 19:58:54
Done. Added comment.
| |
87 if (!MessageLoopForRequestPath(raw_path)) { | |
88 std::string path = | |
89 InstantService::MaybeTranslateInstantPathOnIO(request, raw_path); | |
90 id_to_url_map_[raw_path] = path; | |
Dan Beam
2013/03/20 19:26:13
nit:
id_to_url_map_[raw_path] =
InstantSe
dhollowa
2013/03/20 19:58:54
Done.
| |
91 } | |
92 return true; | |
93 } | |
Dan Beam
2013/03/20 19:26:13
should there be a NOTREACHED() here or anything?
dhollowa
2013/03/20 19:58:54
Not really, no. It is allowable to request a non-
| |
94 return false; | |
81 } | 95 } |
82 return URLDataSource::ShouldServiceRequest(request); | 96 return URLDataSource::ShouldServiceRequest(request); |
83 } | 97 } |
OLD | NEW |