Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / search / suggestions / suggestions_source.cc
1 // Copyright 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 #include "url/gurl.h"
23
24 namespace suggestions {
25
26 namespace {
27
28 const char kHtmlHeader[] =
29     "<!DOCTYPE html>\n<html>\n<head>\n<title>Suggestions</title>\n"
30     "<meta charset=\"utf-8\">\n"
31     "<style type=\"text/css\">\nli {white-space: nowrap;}\n</style>\n";
32 const char kHtmlBody[] = "</head>\n<body>\n";
33 const char kHtmlFooter[] = "</body>\n</html>\n";
34
35 }  // namespace
36
37 SuggestionsSource::SuggestionsSource(Profile* profile)
38     : profile_(profile),
39       weak_ptr_factory_(this) {
40 }
41
42 SuggestionsSource::~SuggestionsSource() {
43 }
44
45 std::string SuggestionsSource::GetSource() const {
46   return chrome::kChromeUISuggestionsHost;
47 }
48
49 void SuggestionsSource::StartDataRequest(
50     const std::string& path,
51     int render_process_id,
52     int render_frame_id,
53     const content::URLDataSource::GotDataCallback& callback) {
54   SuggestionsServiceFactory* suggestions_service_factory =
55       SuggestionsServiceFactory::GetInstance();
56
57   SuggestionsService* suggestions_service(
58       suggestions_service_factory->GetForProfile(profile_));
59
60   if (!suggestions_service) {
61     callback.Run(NULL);
62     return;
63   }
64
65   suggestions_service->FetchSuggestionsData(
66       base::Bind(&SuggestionsSource::OnSuggestionsAvailable,
67                  weak_ptr_factory_.GetWeakPtr(), callback));
68 }
69
70 std::string SuggestionsSource::GetMimeType(const std::string& path) const {
71   return "text/html";
72 }
73
74 base::MessageLoop* SuggestionsSource::MessageLoopForRequestPath(
75     const std::string& path) const {
76   // This can be accessed from the IO thread.
77   return content::URLDataSource::MessageLoopForRequestPath(path);
78 }
79
80 bool SuggestionsSource::ShouldServiceRequest(
81     const net::URLRequest* request) const {
82   if (request->url().SchemeIs(chrome::kChromeSearchScheme))
83     return InstantIOContext::ShouldServiceRequest(request);
84   return URLDataSource::ShouldServiceRequest(request);
85 }
86
87 void SuggestionsSource::OnSuggestionsAvailable(
88     const content::URLDataSource::GotDataCallback& callback,
89     const SuggestionsProfile& suggestions_profile) {
90   // Render HTML.
91   std::vector<std::string> out;
92   out.push_back(kHtmlHeader);
93   out.push_back(kHtmlBody);
94   out.push_back("<h1>Suggestions</h1>\n");
95
96   size_t size = suggestions_profile.suggestions_size();
97   if (size == 0) {
98     out.push_back("<p>You have no suggestions.</p>\n");
99   } else {
100     out.push_back("<ol>\n");
101     for (size_t i = 0; i < size; ++i) {
102       const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i);
103       std::string line;
104       line.append("<li><a href=\"");
105       line.append(net::EscapeForHTML(suggestion.url()));
106       line.append("\" target=\"_blank\">");
107       line.append(net::EscapeForHTML(suggestion.title()));
108       line.append("</a></li>\n");
109       out.push_back(line);
110     }
111     out.push_back("</ol>\n");
112   }
113   out.push_back(kHtmlFooter);
114
115   std::string out_html = JoinString(out, "");
116   callback.Run(base::RefCountedString::TakeString(&out_html));
117 }
118
119 }  // namespace suggestions