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 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_SCRIPT_H_
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_SCRIPT_H_
10 #include "base/callback.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h"
16 class TranslateScriptTest
;
17 class TranslateURLFetcher
;
19 class TranslateScript
{
21 typedef base::Callback
<void(bool, const std::string
&)> Callback
;
23 static const int kFetcherId
= 0;
26 virtual ~TranslateScript();
28 // Returns the feched the translate script.
29 const std::string
& data() { return data_
; }
31 // Used by unit-tests to override some defaults:
32 // Delay after which the translate script is fetched again from the
33 // translation server.
34 void set_expiration_delay(int delay_ms
) {
35 expiration_delay_
= base::TimeDelta::FromMilliseconds(delay_ms
);
38 // Clears the translate script, so it will be fetched next time we translate.
39 void Clear() { data_
.clear(); }
41 // Fetches the JS translate script (the script that is injected in the page
43 void Request(const Callback
& callback
);
45 // Returns true if this has a pending request.
46 bool HasPendingRequest() const { return fetcher_
.get() != NULL
; }
49 friend class TranslateScriptTest
;
50 FRIEND_TEST_ALL_PREFIXES(TranslateScriptTest
, CheckScriptParameters
);
51 FRIEND_TEST_ALL_PREFIXES(TranslateScriptTest
, CheckScriptURL
);
53 static const char kScriptURL
[];
54 static const char kRequestHeader
[];
56 // Used in kTranslateScriptURL to specify using always ssl to load resources.
57 static const char kAlwaysUseSslQueryName
[];
58 static const char kAlwaysUseSslQueryValue
[];
60 // Used in kTranslateScriptURL to specify a callback function name.
61 static const char kCallbackQueryName
[];
62 static const char kCallbackQueryValue
[];
64 // Used in kTranslateScriptURL to specify a CSS loader callback function name.
65 static const char kCssLoaderCallbackQueryName
[];
66 static const char kCssLoaderCallbackQueryValue
[];
68 // Used in kTranslateScriptURL to specify a JavaScript loader callback
70 static const char kJavascriptLoaderCallbackQueryName
[];
71 static const char kJavascriptLoaderCallbackQueryValue
[];
73 // The callback when the script is fetched or a server error occured.
74 void OnScriptFetchComplete(int id
, bool success
, const std::string
& data
);
76 // URL fetcher to fetch the translate script.
77 scoped_ptr
<TranslateURLFetcher
> fetcher_
;
79 // The JS injected in the page to do the translation.
82 // Delay after which the translate script is fetched again from the translate
84 base::TimeDelta expiration_delay_
;
86 // The callback called when the server sends a response.
89 base::WeakPtrFactory
<TranslateScript
> weak_method_factory_
;
91 DISALLOW_COPY_AND_ASSIGN(TranslateScript
);
94 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_SCRIPT_H_