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

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

Issue 17390018: Refactoring: Create TranslateScript from TranslateManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactoring-translate-url-fetcher
Patch Set: (Rebasing) Created 7 years, 6 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
OLDNEW
(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 kTranslateScriptURL[] =
Takashi Toyoshima 2013/06/20 06:59:53 We can simplify these const names. ScriptURL?
hajimehoshi 2013/06/20 08:50:08 Done.
26 "https://translate.google.com/translate_a/element.js";
27 const char kTranslateScriptHeader[] = "Google-Translate-Element-Mode: library";
28
29 // Used in kTranslateScriptURL to specify a callback function name.
30 const char kCallbackQueryName[] = "cb";
31 const char kCallbackQueryValue[] =
32 "cr.googleTranslate.onTranslateElementLoad";
33
34 } // namespace
35
36 TranslateScript::TranslateScript()
37 : weak_method_factory_(this),
38 expiration_delay_(base::TimeDelta::FromDays(kExpirationDelayDays)) {
39 }
40
41 TranslateScript::~TranslateScript() {
42 weak_method_factory_.InvalidateWeakPtrs();
43 }
44
45 void TranslateScript::Request(const Callback& callback) {
46 if (fetcher_.get() != NULL)
Takashi Toyoshima 2013/06/20 06:59:53 NOTREACHED, and it's better to return false as Tra
hajimehoshi 2013/06/20 08:50:08 I expect returning boolean will be implemented an
47 return;
48
49 callback_ = callback;
50
51 GURL translate_script_url;
52 // Check if command-line contains an alternative URL for translate service.
53 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
54 if (command_line.HasSwitch(switches::kTranslateScriptURL)) {
55 translate_script_url = GURL(
56 command_line.GetSwitchValueASCII(switches::kTranslateScriptURL));
57 if (!translate_script_url.is_valid() ||
58 !translate_script_url.query().empty()) {
59 LOG(WARNING) << "The following translate URL specified at the "
60 << "command-line is invalid: "
61 << translate_script_url.spec();
62 translate_script_url = GURL();
63 }
64 }
65
66 // Use default URL when command-line argument is not specified, or specified
67 // URL is invalid.
68 if (translate_script_url.is_empty())
69 translate_script_url = GURL(kTranslateScriptURL);
70
71 translate_script_url = net::AppendQueryParameter(
72 translate_script_url,
73 kCallbackQueryName,
74 kCallbackQueryValue);
75
76 translate_script_url =
77 TranslateURLUtil::AddHostLocaleToUrl(translate_script_url);
78 translate_script_url =
79 TranslateURLUtil::AddApiKeyToUrl(translate_script_url);
80
81 fetcher_.reset(new TranslateURLFetcher(0));
Takashi Toyoshima 2013/06/20 06:59:53 Define it as an anonymous const int and use it her
hajimehoshi 2013/06/20 08:50:08 Done.
82 fetcher_->set_extra_request_header(kTranslateScriptHeader);
83 fetcher_->Request(
84 translate_script_url,
85 base::Bind(&TranslateScript::OnScriptFetchComplete,
86 base::Unretained(this)));
87 }
88
89 bool TranslateScript::IsFetching() const {
90 return fetcher_ != NULL;
Takashi Toyoshima 2013/06/20 06:59:53 .get() != NULL Also, implementing it in the heade
hajimehoshi 2013/06/20 08:50:08 Done.
91 }
92
93 void TranslateScript::OnScriptFetchComplete(
94 int id, bool success, const std::string& data) {
95 DCHECK_EQ(0, 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(translate_script_.empty());
103 str.CopyToString(&translate_script_);
104 std::string argument = "('";
105 std::string api_key = google_apis::GetAPIKey();
106 argument += net::EscapeQueryParamValue(api_key, true);
107 argument += "');\n";
108 translate_script_ += 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::ClearTranslateScript,
116 weak_method_factory_.GetWeakPtr()),
117 expiration_delay_);
118 }
119
120 callback_.Run(success, data);
121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698