1 // Copyright (c) 2011 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/printing/cloud_print/cloud_print_setup_source.h"
6
7 #include <algorithm>
8
9 #include "base/message_loop.h"
10 #include "base/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "chrome/browser/google/google_util.h"
13 #include "chrome/common/jstemplate_builder.h"
14 #include "googleurl/src/gurl.h"
15 #include "grit/app_resources.h"
16 #include "grit/browser_resources.h"
17 #include "grit/chromium_strings.h"
18 #include "grit/generated_resources.h"
19 #include "grit/locale_settings.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/resource/resource_bundle.h"
22
23 // Define the values of standard URLs.
24 const char CloudPrintSetupSource::kInvalidPasswordHelpUrl[] =
25 "https://www.google.com/support/accounts/bin/answer.py?ctx=ch&answer=27444";
26 const char CloudPrintSetupSource::kCanNotAccessAccountUrl[] =
27 "https://www.google.com/support/accounts/bin/answer.py?answer=48598";
28 const char CloudPrintSetupSource::kCreateNewAccountUrl[] =
29 "https://www.google.com/accounts/NewAccount?service=chromiumsync";
30
31 namespace {
32
33 // Utility method to keep dictionary population code streamlined.
AddString(DictionaryValue * dictionary,const std::string & key,int resource_id)34 void AddString(DictionaryValue* dictionary,
35 const std::string& key,
36 int resource_id) {
37 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id));
38 }
39
40 } // namespace
41
CloudPrintSetupSource()42 CloudPrintSetupSource::CloudPrintSetupSource()
43 : DataSource(chrome::kCloudPrintSetupHost, MessageLoop::current()) {
44 }
45
StartDataRequest(const std::string & path_raw,bool is_incognito,int request_id)46 void CloudPrintSetupSource::StartDataRequest(const std::string& path_raw,
47 bool is_incognito,
48 int request_id) {
49 const char kCloudPrintSetupPath[] = "cloudprintsetup";
50 const char kCloudPrintGaiaLoginPath[] = "gaialogin";
51 const char kCloudPrintSetupFlowPath[] = "setupflow";
52 const char kCloudPrintSetupDonePath[] = "setupdone";
53
54 DictionaryValue localized_strings;
55 DictionaryValue* dict = &localized_strings;
56
57 std::string response;
58 if (path_raw == kCloudPrintSetupPath) {
59 AddString(dict, "header", IDS_CLOUD_PRINT_SETUP_HEADER);
60 AddString(dict, "explain", IDS_CLOUD_PRINT_SETUP_EXPLAIN);
61 AddString(dict, "anywhereheader", IDS_CLOUD_PRINT_SETUP_ANYWHERE_HEADER);
62 AddString(dict, "anywhereexplain", IDS_CLOUD_PRINT_SETUP_ANYWHERE_EXPLAIN);
63 AddString(dict, "printerheader", IDS_CLOUD_PRINT_SETUP_PRINTER_HEADER);
64 AddString(dict, "printerexplain", IDS_CLOUD_PRINT_SETUP_PRINTER_EXPLAIN);
65 AddString(dict, "sharingheader", IDS_CLOUD_PRINT_SETUP_SHARING_HEADER);
66 AddString(dict, "sharingexplain", IDS_CLOUD_PRINT_SETUP_SHARING_EXPLAIN);
67
68 static const base::StringPiece html(ResourceBundle::GetSharedInstance()
69 .GetRawDataResource(IDR_CLOUD_PRINT_SETUP_LOGIN_HTML));
70 SetFontAndTextDirection(dict);
71 response = jstemplate_builder::GetI18nTemplateHtml(html, dict);
72 } else if (path_raw == kCloudPrintGaiaLoginPath) {
73 // Start by setting the per-locale URLs we show on the setup wizard.
74 dict->SetString("invalidpasswordhelpurl",
75 GetLocalizedUrl(kInvalidPasswordHelpUrl));
76 dict->SetString("cannotaccessaccounturl",
77 GetLocalizedUrl(kCanNotAccessAccountUrl));
78 dict->SetString("createnewaccounturl",
79 GetLocalizedUrl(kCreateNewAccountUrl));
80
81 // None of the strings used here currently have sync-specific wording in
82 // them. There is a unit test to catch if that happens.
83 dict->SetString("introduction", "");
84 AddString(dict, "signinprefix", IDS_SYNC_LOGIN_SIGNIN_PREFIX);
85 AddString(dict, "signinsuffix", IDS_SYNC_LOGIN_SIGNIN_SUFFIX);
86 AddString(dict, "cannotbeblank", IDS_SYNC_CANNOT_BE_BLANK);
87 AddString(dict, "emaillabel", IDS_SYNC_LOGIN_EMAIL);
88 AddString(dict, "passwordlabel", IDS_SYNC_LOGIN_PASSWORD);
89 AddString(dict, "invalidcredentials", IDS_SYNC_INVALID_USER_CREDENTIALS);
90 AddString(dict, "signin", IDS_SYNC_SIGNIN);
91 AddString(dict, "couldnotconnect", IDS_SYNC_LOGIN_COULD_NOT_CONNECT);
92 AddString(dict, "cannotaccessaccount", IDS_SYNC_CANNOT_ACCESS_ACCOUNT);
93 AddString(dict, "createaccount", IDS_SYNC_CREATE_ACCOUNT);
94 AddString(dict, "cancel", IDS_CANCEL);
95 AddString(dict, "settingup", IDS_SYNC_LOGIN_SETTING_UP);
96 AddString(dict, "success", IDS_SYNC_SUCCESS);
97 AddString(dict, "errorsigningin", IDS_SYNC_ERROR_SIGNING_IN);
98 AddString(dict, "captchainstructions", IDS_SYNC_GAIA_CAPTCHA_INSTRUCTIONS);
99 AddString(dict, "invalidaccesscode", IDS_SYNC_INVALID_ACCESS_CODE_LABEL);
100 AddString(dict, "enteraccesscode", IDS_SYNC_ENTER_ACCESS_CODE_LABEL);
101 AddString(dict, "getaccesscodehelp", IDS_SYNC_ACCESS_CODE_HELP_LABEL);
102 AddString(dict, "getaccesscodeurl", IDS_SYNC_GET_ACCESS_CODE_URL);
103
104 static const base::StringPiece html(ResourceBundle::GetSharedInstance()
105 .GetRawDataResource(IDR_GAIA_LOGIN_HTML));
106 SetFontAndTextDirection(dict);
107 response = jstemplate_builder::GetI18nTemplateHtml(html, dict);
108 } else if (path_raw == kCloudPrintSetupDonePath) {
109 AddString(dict, "testpage", IDS_CLOUD_PRINT_SETUP_TEST_PAGE);
110 AddString(dict, "success", IDS_SYNC_SUCCESS);
111 AddString(dict, "okay", IDS_SYNC_SETUP_OK_BUTTON_LABEL);
112 static const base::StringPiece html(ResourceBundle::GetSharedInstance()
113 .GetRawDataResource(IDR_CLOUD_PRINT_SETUP_DONE_HTML));
114 SetFontAndTextDirection(dict);
115 response = jstemplate_builder::GetI18nTemplateHtml(html, dict);
116 } else if (path_raw == kCloudPrintSetupFlowPath) {
117 static const base::StringPiece html(
118 ResourceBundle::GetSharedInstance()
119 .GetRawDataResource(IDR_CLOUD_PRINT_SETUP_FLOW_HTML));
120 response = html.as_string();
121 }
122 // Send the response.
123 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
124 html_bytes->data.resize(response.size());
125 std::copy(response.begin(), response.end(), html_bytes->data.begin());
126 SendResponse(request_id, html_bytes);
127 }
128
GetMimeType(const std::string & path) const129 std::string CloudPrintSetupSource::GetMimeType(const std::string& path) const {
130 return "text/html";
131 }
132
GetLocalizedUrl(const std::string & url) const133 std::string CloudPrintSetupSource::GetLocalizedUrl(
134 const std::string& url) const {
135 GURL original_url(url);
136 DCHECK(original_url.is_valid());
137 GURL localized_url = google_util::AppendGoogleLocaleParam(original_url);
138 return localized_url.spec();
139 }
140