| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014 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. | |
| 4 | |
| 5 #include "chrome/browser/search/suggestions/suggestions_source.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/memory/ref_counted_memory.h" | |
| 11 #include "base/strings/string16.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "base/strings/utf_string_conversions.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "chrome/browser/search/instant_io_context.h" | |
| 16 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | |
| 17 #include "chrome/browser/search/suggestions/suggestions_service.h" | |
| 18 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | |
| 19 #include "chrome/common/url_constants.h" | |
| 20 #include "net/base/escape.h" | |
| 21 #include "net/url_request/url_request.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 const char* kHtmlHeader = | |
| 26 "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n" | |
| 27 "<meta charset=\"utf-8\">\n" | |
| 28 "<style type=\"text/css\">\nli {white-space: nowrap;}\n</style>\n"; | |
| 29 const char* kHtmlBody = "</head>\n<body>\n"; | |
| 30 const char* kHtmlFooter = "</body>\n</html>\n"; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 SuggestionsSource::SuggestionsSource(Profile* profile) | |
| 35 : profile_(profile), | |
| 36 weak_ptr_factory_(this) { | |
| 37 } | |
| 38 | |
| 39 SuggestionsSource::~SuggestionsSource() { | |
| 40 } | |
| 41 | |
| 42 std::string SuggestionsSource::GetSource() const { | |
| 43 return chrome::kChromeUISuggestionsHost; | |
| 44 } | |
| 45 | |
| 46 void SuggestionsSource::StartDataRequest( | |
| 47 const std::string& path, | |
| 48 int render_process_id, | |
| 49 int render_frame_id, | |
| 50 const content::URLDataSource::GotDataCallback& callback) { | |
| 51 suggestions::SuggestionsServiceFactory* suggestions_service_factory = | |
| 52 suggestions::SuggestionsServiceFactory::GetInstance(); | |
| 53 | |
| 54 suggestions::SuggestionsService* suggestions_service( | |
| 55 suggestions_service_factory->GetForProfile(profile_)); | |
| 56 | |
| 57 // If SuggestionsService is unavailable, then SuggestionsSource should not | |
| 58 // have been instantiated in the first place. | |
| 59 DCHECK(suggestions_service != NULL); | |
| 60 | |
| 61 suggestions_service->FetchSuggestionsData( | |
| 62 base::Bind(&SuggestionsSource::OnSuggestionsAvailable, | |
| 63 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 64 } | |
| 65 | |
| 66 std::string SuggestionsSource::GetMimeType(const std::string& path) const { | |
| 67 return "text/html"; | |
| 68 } | |
| 69 | |
| 70 base::MessageLoop* SuggestionsSource::MessageLoopForRequestPath( | |
| 71 const std::string& path) const { | |
| 72 // This can be accessed from the IO thread. | |
| 73 return content::URLDataSource::MessageLoopForRequestPath(path); | |
| 74 } | |
| 75 | |
| 76 bool SuggestionsSource::ShouldServiceRequest( | |
| 77 const net::URLRequest* request) const { | |
| 78 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) | |
| 79 return InstantIOContext::ShouldServiceRequest(request); | |
| 80 return URLDataSource::ShouldServiceRequest(request); | |
| 81 } | |
| 82 | |
| 83 void SuggestionsSource::OnSuggestionsAvailable( | |
| 84 const content::URLDataSource::GotDataCallback& callback, | |
| 85 const suggestions::SuggestionsProfile& suggestions_profile) { | |
| 86 // Render HTML. | |
| 87 std::vector<std::string> out; | |
| 88 out.push_back(kHtmlHeader); | |
| 89 out.push_back(kHtmlBody); | |
| 90 out.push_back("<h1>Suggestions</h1>\n"); | |
| 91 | |
| 92 size_t size = suggestions_profile.suggestions_size(); | |
| 93 if (size == 0) { | |
| 94 out.push_back("<p>You have no suggestions.</p>\n"); | |
| 95 } else { | |
| 96 out.push_back("<ol>\n"); | |
| 97 for (size_t i = 0; i < size; ++i) { | |
| 98 const suggestions::ChromeSuggestion& suggestion = | |
| 99 suggestions_profile.suggestions(i); | |
| 100 std::string escaped_text(net::EscapeForHTML(suggestion.title())); | |
| 101 out.push_back("<li><a href=\"" + suggestion.url() + | |
| 102 "\" target=\"_blank\">" + escaped_text + "</a></li>\n"); | |
| 103 } | |
| 104 out.push_back("</ol>\n"); | |
| 105 } | |
| 106 out.push_back(kHtmlFooter); | |
| 107 | |
| 108 std::string out_html = JoinString(out, ""); | |
| 109 callback.Run(base::RefCountedString::TakeString(&out_html)); | |
| 110 } | |
| OLD | NEW |