Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / ntp / android / ntp_resource_cache_android.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
6
7 #include "base/command_line.h"
8 #include "base/memory/ref_counted_memory.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_piece.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/google/google_util.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/chrome_version_info.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/render_process_host.h"
20 #include "grit/browser_resources.h"
21 #include "grit/generated_resources.h"
22 #include "ui/base/device_form_factor.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/base/webui/jstemplate_builder.h"
26 #include "ui/base/webui/web_ui_util.h"
27
28 using chrome::VersionInfo;
29 using content::BrowserThread;
30
31 namespace {
32
33 const char kLearnMoreIncognitoUrl[] =
34     "https://www.google.com/support/chrome/bin/answer.py?answer=95464";
35
36 }  // namespace
37
38 NTPResourceCache::NTPResourceCache(Profile* profile) : profile_(profile) {}
39
40 NTPResourceCache::~NTPResourceCache() {}
41
42 NTPResourceCache::WindowType NTPResourceCache::GetWindowType(
43     Profile* profile, content::RenderProcessHost* render_host) {
44   if (render_host) {
45     // Sometimes the |profile| is the parent (non-incognito) version of the user
46     // so we check the |render_host| if it is provided.
47     if (render_host->GetBrowserContext()->IsOffTheRecord())
48       return NTPResourceCache::INCOGNITO;
49   } else if (profile->IsOffTheRecord()) {
50     return NTPResourceCache::INCOGNITO;
51   }
52   return NTPResourceCache::NORMAL;
53 }
54
55 base::RefCountedMemory* NTPResourceCache::GetNewTabHTML(WindowType win_type) {
56   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
57   // Android uses same html/css for incognito NTP and normal NTP
58   if (!new_tab_html_.get())
59     CreateNewTabHTML();
60   return new_tab_html_.get();
61 }
62
63 base::RefCountedMemory* NTPResourceCache::GetNewTabCSS(WindowType win_type) {
64   // This is used for themes, which are not currently supported on Android.
65   NOTIMPLEMENTED();
66   return NULL;
67 }
68
69 void NTPResourceCache::Observe(int type,
70                                const content::NotificationSource& source,
71                                const content::NotificationDetails& details) {
72   // No notifications necessary in Android.
73 }
74
75 void NTPResourceCache::OnPreferenceChanged() {
76   // No notifications necessary in Android.
77 }
78
79 void NTPResourceCache::CreateNewTabHTML() {
80   // TODO(estade): these strings should be defined in their relevant handlers
81   // (in GetLocalizedValues) and should have more legible names.
82   // Show the profile name in the title and most visited labels if the current
83   // profile is not the default.
84   base::DictionaryValue localized_strings;
85   localized_strings.SetBoolean("hasattribution", false);
86   localized_strings.SetString("title",
87       l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
88   localized_strings.SetString("mostvisited",
89       l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED));
90   localized_strings.SetString("recentlyclosed",
91       l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED));
92   localized_strings.SetString("opentabslastsynced",
93       l10n_util::GetStringUTF16(IDS_SYNC_NTP_OPEN_TABS_LAST_SYNCED));
94   localized_strings.SetString("elementopeninnewtab",
95       l10n_util::GetStringUTF16(IDS_NEW_TAB_CONTEXT_MENU_OPEN_IN_NEW_TAB));
96   localized_strings.SetString("elementopeninincognitotab",
97       l10n_util::GetStringUTF16(
98           IDS_NEW_TAB_CONTEXT_MENU_OPEN_IN_INCOGNITO_TAB));
99   localized_strings.SetString("elementremove",
100       l10n_util::GetStringUTF16(IDS_NEW_TAB_CONTEXT_MENU_REMOVE));
101   localized_strings.SetString("removeall",
102       l10n_util::GetStringUTF16(IDS_NEW_TAB_CONTEXT_MENU_REMOVE_ALL));
103   localized_strings.SetString("bookmarkedit",
104       l10n_util::GetStringUTF16(IDS_NEW_TAB_CONTEXT_MENU_EDIT_BOOKMARK));
105   localized_strings.SetString("bookmarkdelete",
106       l10n_util::GetStringUTF16(IDS_NEW_TAB_CONTEXT_MENU_DELETE_BOOKMARK));
107   localized_strings.SetString("bookmarkshortcut",
108         l10n_util::GetStringUTF16(IDS_NEW_TAB_CONTEXT_MENU_BOOKMARK_SHORTCUT));
109   localized_strings.SetString("editfolder",
110       l10n_util::GetStringUTF16(IDS_BOOKMARK_EDIT_FOLDER));
111   localized_strings.SetString("deletefolder",
112         l10n_util::GetStringUTF16(IDS_BOOKMARK_REMOVE_FOLDER));
113   localized_strings.SetString("receivedDocuments",
114       l10n_util::GetStringUTF16(IDS_RECEIVED_DOCUMENTS));
115   localized_strings.SetString("syncPromo",
116       l10n_util::GetStringUTF16(IDS_SYNC_PROMO_DESKTOP_INSTRUCTIONS));
117   localized_strings.SetString("syncEnableSync",
118       l10n_util::GetStringUTF16(IDS_SYNC_ENABLE_SYNC));
119   localized_strings.SetString("bookmarkstitle",
120       l10n_util::GetStringUTF16(IDS_ACCNAME_BOOKMARKS));
121   localized_strings.SetString("incognito_document_title",
122       l10n_util::GetStringUTF16(IDS_NEW_TAB_INCOGNITO_TITLE));
123   localized_strings.SetString("most_visited_document_title",
124       l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED_TITLE));
125   localized_strings.SetString("bookmarks_document_title",
126       l10n_util::GetStringUTF16(IDS_NEW_TAB_BOOKMARKS_TITLE));
127   localized_strings.SetString("open_tabs_document_title",
128       l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_TITLE));
129
130   webui::SetFontAndTextDirection(&localized_strings);
131
132   base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance().
133                                  GetRawDataResource(IDR_NEW_TAB_ANDROID_HTML));
134   localized_strings.SetString(
135       "device",
136       ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET ?
137           "tablet" : "phone");
138
139   const char* new_tab_link = kLearnMoreIncognitoUrl;
140   base::string16 learnMoreLink = base::ASCIIToUTF16(
141       google_util::AppendGoogleLocaleParam(GURL(new_tab_link)).spec());
142   localized_strings.SetString("content",
143       l10n_util::GetStringFUTF16(
144           IDS_NEW_TAB_OTR_MESSAGE_MOBILE, learnMoreLink));
145
146   // Load the new tab page appropriate for this build.
147   std::string full_html;
148
149   // Inject the template data into the HTML so that it is available before any
150   // layout is needed.
151   std::string json_html;
152   webui::AppendJsonHtml(&localized_strings, &json_html);
153
154   static const base::StringPiece template_data_placeholder(
155       "<!-- template data placeholder -->");
156   size_t pos = new_tab_html.find(template_data_placeholder);
157
158   if (pos != base::StringPiece::npos) {
159     full_html.assign(new_tab_html.data(), pos);
160     full_html.append(json_html);
161     size_t after_offset = pos + template_data_placeholder.size();
162     full_html.append(new_tab_html.data() + after_offset,
163                      new_tab_html.size() - after_offset);
164   } else {
165     NOTREACHED();
166     full_html.assign(new_tab_html.data(), new_tab_html.size());
167   }
168
169   new_tab_html_ = base::RefCountedString::TakeString(&full_html);
170 }