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.
5 #include "components/translate/core/browser/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/profiler/scoped_tracker.h"
12 #include "base/strings/string_piece.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "components/translate/core/browser/translate_url_fetcher.h"
16 #include "components/translate/core/browser/translate_url_util.h"
17 #include "components/translate/core/common/translate_switches.h"
18 #include "components/translate/core/common/translate_util.h"
19 #include "google_apis/google_api_keys.h"
20 #include "grit/components_resources.h"
21 #include "net/base/escape.h"
22 #include "net/base/url_util.h"
23 #include "ui/base/resource/resource_bundle.h"
29 const int kExpirationDelayDays
= 1;
33 const char TranslateScript::kScriptURL
[] =
34 "https://translate.google.com/translate_a/element.js";
35 const char TranslateScript::kRequestHeader
[] =
36 "Google-Translate-Element-Mode: library";
37 const char TranslateScript::kAlwaysUseSslQueryName
[] = "aus";
38 const char TranslateScript::kAlwaysUseSslQueryValue
[] = "true";
39 const char TranslateScript::kCallbackQueryName
[] = "cb";
40 const char TranslateScript::kCallbackQueryValue
[] =
41 "cr.googleTranslate.onTranslateElementLoad";
42 const char TranslateScript::kCssLoaderCallbackQueryName
[] = "clc";
43 const char TranslateScript::kCssLoaderCallbackQueryValue
[] =
44 "cr.googleTranslate.onLoadCSS";
45 const char TranslateScript::kJavascriptLoaderCallbackQueryName
[] = "jlc";
46 const char TranslateScript::kJavascriptLoaderCallbackQueryValue
[] =
47 "cr.googleTranslate.onLoadJavascript";
49 TranslateScript::TranslateScript()
50 : expiration_delay_(base::TimeDelta::FromDays(kExpirationDelayDays
)),
51 weak_method_factory_(this) {
54 TranslateScript::~TranslateScript() {
57 void TranslateScript::Request(const RequestCallback
& callback
) {
58 DCHECK(data_
.empty()) << "Do not fetch the script if it is already fetched";
59 callback_list_
.push_back(callback
);
61 if (fetcher_
.get() != NULL
) {
62 // If there is already a request in progress, do nothing. |callback| will be
67 GURL translate_script_url
;
68 // Check if command-line contains an alternative URL for translate service.
69 const base::CommandLine
& command_line
=
70 *base::CommandLine::ForCurrentProcess();
71 if (command_line
.HasSwitch(translate::switches::kTranslateScriptURL
)) {
72 translate_script_url
= GURL(command_line
.GetSwitchValueASCII(
73 translate::switches::kTranslateScriptURL
));
74 if (!translate_script_url
.is_valid() ||
75 !translate_script_url
.query().empty()) {
76 LOG(WARNING
) << "The following translate URL specified at the "
77 << "command-line is invalid: "
78 << translate_script_url
.spec();
79 translate_script_url
= GURL();
83 // Use default URL when command-line argument is not specified, or specified
85 if (translate_script_url
.is_empty())
86 translate_script_url
= GURL(kScriptURL
);
88 translate_script_url
= net::AppendQueryParameter(
92 translate_script_url
= net::AppendQueryParameter(
94 kAlwaysUseSslQueryName
,
95 kAlwaysUseSslQueryValue
);
97 // iOS doesn't need to use specific loaders for the isolated world.
98 translate_script_url
= net::AppendQueryParameter(
100 kCssLoaderCallbackQueryName
,
101 kCssLoaderCallbackQueryValue
);
102 translate_script_url
= net::AppendQueryParameter(
103 translate_script_url
,
104 kJavascriptLoaderCallbackQueryName
,
105 kJavascriptLoaderCallbackQueryValue
);
106 #endif // !defined(OS_IOS)
108 translate_script_url
= AddHostLocaleToUrl(translate_script_url
);
109 translate_script_url
= AddApiKeyToUrl(translate_script_url
);
111 fetcher_
.reset(new TranslateURLFetcher(kFetcherId
));
112 fetcher_
->set_extra_request_header(kRequestHeader
);
114 translate_script_url
,
115 base::Bind(&TranslateScript::OnScriptFetchComplete
,
116 base::Unretained(this)));
120 void TranslateScript::OnScriptFetchComplete(
121 int id
, bool success
, const std::string
& data
) {
122 DCHECK_EQ(kFetcherId
, id
);
124 scoped_ptr
<const TranslateURLFetcher
> delete_ptr(fetcher_
.release());
127 DCHECK(data_
.empty());
128 // Insert variable definitions on API Key and security origin.
129 data_
= base::StringPrintf("var translateApiKey = '%s';\n",
130 google_apis::GetAPIKey().c_str());
132 GURL security_origin
= translate::GetTranslateSecurityOrigin();
134 &data_
, "var securityOrigin = '%s';", security_origin
.spec().c_str());
136 // Append embedded translate.js and a remote element library.
137 base::StringPiece str
= ResourceBundle::GetSharedInstance().
138 GetRawDataResource(IDR_TRANSLATE_JS
);
139 str
.AppendToString(&data_
);
142 // We'll expire the cached script after some time, to make sure long
143 // running browsers still get fixes that might get pushed with newer
145 base::MessageLoop::current()->PostDelayedTask(
147 base::Bind(&TranslateScript::Clear
,
148 weak_method_factory_
.GetWeakPtr()),
152 for (RequestCallbackList::iterator it
= callback_list_
.begin();
153 it
!= callback_list_
.end();
155 it
->Run(success
, data
);
157 callback_list_
.clear();
160 } // namespace translate