OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/translate/translate_script.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "chrome/browser/translate/translate_url_fetcher.h" |
| 13 #include "chrome/browser/translate/translate_url_util.h" |
| 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "google_apis/google_api_keys.h" |
| 16 #include "grit/browser_resources.h" |
| 17 #include "net/base/escape.h" |
| 18 #include "net/base/url_util.h" |
| 19 #include "ui/base/resource/resource_bundle.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 const int kExpirationDelayDays = 1; |
| 24 |
| 25 const char kScriptURL[] = |
| 26 "https://translate.google.com/translate_a/element.js"; |
| 27 const char kRequestHeader[] = "Google-Translate-Element-Mode: library"; |
| 28 const int kFetcherId = 0; |
| 29 |
| 30 // Used in kTranslateScriptURL to specify a callback function name. |
| 31 const char kCallbackQueryName[] = "cb"; |
| 32 const char kCallbackQueryValue[] = |
| 33 "cr.googleTranslate.onTranslateElementLoad"; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 TranslateScript::TranslateScript() |
| 38 : weak_method_factory_(this), |
| 39 expiration_delay_(base::TimeDelta::FromDays(kExpirationDelayDays)) { |
| 40 } |
| 41 |
| 42 TranslateScript::~TranslateScript() { |
| 43 weak_method_factory_.InvalidateWeakPtrs(); |
| 44 } |
| 45 |
| 46 void TranslateScript::Request(const Callback& callback) { |
| 47 if (fetcher_.get() != NULL) { |
| 48 NOTREACHED(); |
| 49 return; |
| 50 } |
| 51 |
| 52 callback_ = callback; |
| 53 |
| 54 GURL translate_script_url; |
| 55 // Check if command-line contains an alternative URL for translate service. |
| 56 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 57 if (command_line.HasSwitch(switches::kTranslateScriptURL)) { |
| 58 translate_script_url = GURL( |
| 59 command_line.GetSwitchValueASCII(switches::kTranslateScriptURL)); |
| 60 if (!translate_script_url.is_valid() || |
| 61 !translate_script_url.query().empty()) { |
| 62 LOG(WARNING) << "The following translate URL specified at the " |
| 63 << "command-line is invalid: " |
| 64 << translate_script_url.spec(); |
| 65 translate_script_url = GURL(); |
| 66 } |
| 67 } |
| 68 |
| 69 // Use default URL when command-line argument is not specified, or specified |
| 70 // URL is invalid. |
| 71 if (translate_script_url.is_empty()) |
| 72 translate_script_url = GURL(kScriptURL); |
| 73 |
| 74 translate_script_url = net::AppendQueryParameter( |
| 75 translate_script_url, |
| 76 kCallbackQueryName, |
| 77 kCallbackQueryValue); |
| 78 |
| 79 translate_script_url = |
| 80 TranslateURLUtil::AddHostLocaleToUrl(translate_script_url); |
| 81 translate_script_url = |
| 82 TranslateURLUtil::AddApiKeyToUrl(translate_script_url); |
| 83 |
| 84 fetcher_.reset(new TranslateURLFetcher(kFetcherId)); |
| 85 fetcher_->set_extra_request_header(kRequestHeader); |
| 86 fetcher_->Request( |
| 87 translate_script_url, |
| 88 base::Bind(&TranslateScript::OnScriptFetchComplete, |
| 89 base::Unretained(this))); |
| 90 } |
| 91 |
| 92 |
| 93 void TranslateScript::OnScriptFetchComplete( |
| 94 int id, bool success, const std::string& data) { |
| 95 DCHECK_EQ(kFetcherId, id); |
| 96 |
| 97 scoped_ptr<const TranslateURLFetcher> delete_ptr(fetcher_.release()); |
| 98 |
| 99 if (success) { |
| 100 base::StringPiece str = ResourceBundle::GetSharedInstance(). |
| 101 GetRawDataResource(IDR_TRANSLATE_JS); |
| 102 DCHECK(data_.empty()); |
| 103 str.CopyToString(&data_); |
| 104 std::string argument = "('"; |
| 105 std::string api_key = google_apis::GetAPIKey(); |
| 106 argument += net::EscapeQueryParamValue(api_key, true); |
| 107 argument += "');\n"; |
| 108 data_ += argument + data; |
| 109 |
| 110 // We'll expire the cached script after some time, to make sure long |
| 111 // running browsers still get fixes that might get pushed with newer |
| 112 // scripts. |
| 113 base::MessageLoop::current()->PostDelayedTask( |
| 114 FROM_HERE, |
| 115 base::Bind(&TranslateScript::Clear, |
| 116 weak_method_factory_.GetWeakPtr()), |
| 117 expiration_delay_); |
| 118 } |
| 119 |
| 120 callback_.Run(success, data); |
| 121 } |
OLD | NEW |