Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(706)

Side by Side Diff: chrome/browser/translate/translate_script.cc

Issue 22645015: Translate: cleanup on how to pass api key to the script (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: (rebase) Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/resources/translate.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/translate/translate_script.h" 5 #include "chrome/browser/translate/translate_script.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_piece.h"
11 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/translate/translate_url_fetcher.h" 14 #include "chrome/browser/translate/translate_url_fetcher.h"
13 #include "chrome/browser/translate/translate_url_util.h" 15 #include "chrome/browser/translate/translate_url_util.h"
14 #include "chrome/common/chrome_switches.h" 16 #include "chrome/common/chrome_switches.h"
15 #include "google_apis/google_api_keys.h" 17 #include "google_apis/google_api_keys.h"
16 #include "grit/browser_resources.h" 18 #include "grit/browser_resources.h"
17 #include "net/base/escape.h" 19 #include "net/base/escape.h"
18 #include "net/base/url_util.h" 20 #include "net/base/url_util.h"
19 #include "ui/base/resource/resource_bundle.h" 21 #include "ui/base/resource/resource_bundle.h"
20 22
21 namespace { 23 namespace {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 } 91 }
90 92
91 93
92 void TranslateScript::OnScriptFetchComplete( 94 void TranslateScript::OnScriptFetchComplete(
93 int id, bool success, const std::string& data) { 95 int id, bool success, const std::string& data) {
94 DCHECK_EQ(kFetcherId, id); 96 DCHECK_EQ(kFetcherId, id);
95 97
96 scoped_ptr<const TranslateURLFetcher> delete_ptr(fetcher_.release()); 98 scoped_ptr<const TranslateURLFetcher> delete_ptr(fetcher_.release());
97 99
98 if (success) { 100 if (success) {
101 DCHECK(data_.empty());
102 data_ = base::StringPrintf("var translateApiKey = '%s';\n",
103 google_apis::GetAPIKey().c_str());
99 base::StringPiece str = ResourceBundle::GetSharedInstance(). 104 base::StringPiece str = ResourceBundle::GetSharedInstance().
100 GetRawDataResource(IDR_TRANSLATE_JS); 105 GetRawDataResource(IDR_TRANSLATE_JS);
101 DCHECK(data_.empty()); 106 str.AppendToString(&data_);
102 str.CopyToString(&data_); 107 data_ += data;
103 std::string argument = "('";
104 std::string api_key = google_apis::GetAPIKey();
105 argument += net::EscapeQueryParamValue(api_key, true);
106 argument += "');\n";
107 data_ += argument + data;
108 108
109 // We'll expire the cached script after some time, to make sure long 109 // We'll expire the cached script after some time, to make sure long
110 // running browsers still get fixes that might get pushed with newer 110 // running browsers still get fixes that might get pushed with newer
111 // scripts. 111 // scripts.
112 base::MessageLoop::current()->PostDelayedTask( 112 base::MessageLoop::current()->PostDelayedTask(
113 FROM_HERE, 113 FROM_HERE,
114 base::Bind(&TranslateScript::Clear, 114 base::Bind(&TranslateScript::Clear,
115 weak_method_factory_.GetWeakPtr()), 115 weak_method_factory_.GetWeakPtr()),
116 expiration_delay_); 116 expiration_delay_);
117 } 117 }
118 118
119 callback_.Run(success, data); 119 callback_.Run(success, data);
120 } 120 }
OLDNEW
« no previous file with comments | « chrome/browser/resources/translate.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698