1 // Copyright 2013 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.
5 #include "chrome/browser/translate/translate_script.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_piece.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/browser/translate/chrome_translate_delegate.h"
15 #include "chrome/browser/translate/translate_url_util.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "components/translate/core/browser/translate_url_fetcher.h"
18 #include "components/translate/core/common/translate_util.h"
19 #include "google_apis/google_api_keys.h"
20 #include "grit/browser_resources.h"
21 #include "net/base/escape.h"
22 #include "net/base/url_util.h"
23 #include "ui/base/resource/resource_bundle.h"
27 const int kExpirationDelayDays
= 1;
31 const char TranslateScript::kScriptURL
[] =
32 "https://translate.google.com/translate_a/element.js";
33 const char TranslateScript::kRequestHeader
[] =
34 "Google-Translate-Element-Mode: library";
35 const char TranslateScript::kAlwaysUseSslQueryName
[] = "aus";
36 const char TranslateScript::kAlwaysUseSslQueryValue
[] = "true";
37 const char TranslateScript::kCallbackQueryName
[] = "cb";
38 const char TranslateScript::kCallbackQueryValue
[] =
39 "cr.googleTranslate.onTranslateElementLoad";
40 const char TranslateScript::kCssLoaderCallbackQueryName
[] = "clc";
41 const char TranslateScript::kCssLoaderCallbackQueryValue
[] =
42 "cr.googleTranslate.onLoadCSS";
43 const char TranslateScript::kJavascriptLoaderCallbackQueryName
[] = "jlc";
44 const char TranslateScript::kJavascriptLoaderCallbackQueryValue
[] =
45 "cr.googleTranslate.onLoadJavascript";
47 TranslateScript::TranslateScript()
48 : expiration_delay_(base::TimeDelta::FromDays(kExpirationDelayDays
)),
49 weak_method_factory_(this) {
52 TranslateScript::~TranslateScript() {
55 void TranslateScript::Request(const Callback
& callback
) {
56 if (fetcher_
.get() != NULL
) {
63 GURL translate_script_url
;
64 // Check if command-line contains an alternative URL for translate service.
65 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
66 if (command_line
.HasSwitch(switches::kTranslateScriptURL
)) {
67 translate_script_url
= GURL(
68 command_line
.GetSwitchValueASCII(switches::kTranslateScriptURL
));
69 if (!translate_script_url
.is_valid() ||
70 !translate_script_url
.query().empty()) {
71 LOG(WARNING
) << "The following translate URL specified at the "
72 << "command-line is invalid: "
73 << translate_script_url
.spec();
74 translate_script_url
= GURL();
78 // Use default URL when command-line argument is not specified, or specified
80 if (translate_script_url
.is_empty())
81 translate_script_url
= GURL(kScriptURL
);
83 translate_script_url
= net::AppendQueryParameter(
87 translate_script_url
= net::AppendQueryParameter(
89 kAlwaysUseSslQueryName
,
90 kAlwaysUseSslQueryValue
);
92 // iOS doesn't need to use specific loaders for the isolated world.
93 translate_script_url
= net::AppendQueryParameter(
95 kCssLoaderCallbackQueryName
,
96 kCssLoaderCallbackQueryValue
);
97 translate_script_url
= net::AppendQueryParameter(
99 kJavascriptLoaderCallbackQueryName
,
100 kJavascriptLoaderCallbackQueryValue
);
101 #endif // !defined(OS_IOS)
103 translate_script_url
=
104 TranslateURLUtil::AddHostLocaleToUrl(translate_script_url
);
105 translate_script_url
=
106 TranslateURLUtil::AddApiKeyToUrl(translate_script_url
);
108 fetcher_
.reset(new TranslateURLFetcher(
109 kFetcherId
, ChromeTranslateDelegate::GetInstance()));
110 fetcher_
->set_extra_request_header(kRequestHeader
);
112 translate_script_url
,
113 base::Bind(&TranslateScript::OnScriptFetchComplete
,
114 base::Unretained(this)));
118 void TranslateScript::OnScriptFetchComplete(
119 int id
, bool success
, const std::string
& data
) {
120 DCHECK_EQ(kFetcherId
, id
);
122 scoped_ptr
<const TranslateURLFetcher
> delete_ptr(fetcher_
.release());
125 DCHECK(data_
.empty());
126 // Insert variable definitions on API Key and security origin.
127 data_
= base::StringPrintf("var translateApiKey = '%s';\n",
128 google_apis::GetAPIKey().c_str());
130 GURL security_origin
= translate::GetTranslateSecurityOrigin();
132 &data_
, "var securityOrigin = '%s';", security_origin
.spec().c_str());
134 // Append embedded translate.js and a remote element library.
135 base::StringPiece str
= ResourceBundle::GetSharedInstance().
136 GetRawDataResource(IDR_TRANSLATE_JS
);
137 str
.AppendToString(&data_
);
140 // We'll expire the cached script after some time, to make sure long
141 // running browsers still get fixes that might get pushed with newer
143 base::MessageLoop::current()->PostDelayedTask(
145 base::Bind(&TranslateScript::Clear
,
146 weak_method_factory_
.GetWeakPtr()),
150 callback_
.Run(success
, data
);