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 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_SCRIPT_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_SCRIPT_H_
11 #include "base/callback_forward.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
19 class TranslateScriptTest
;
20 class TranslateURLFetcher
;
22 class TranslateScript
{
24 typedef base::Callback
<void(bool, const std::string
&)> RequestCallback
;
26 static const int kFetcherId
= 0;
29 virtual ~TranslateScript();
31 // Returns the feched the translate script.
32 const std::string
& data() { return data_
; }
34 // Used by unit-tests to override some defaults:
35 // Delay after which the translate script is fetched again from the
36 // translation server.
37 void set_expiration_delay(int delay_ms
) {
38 expiration_delay_
= base::TimeDelta::FromMilliseconds(delay_ms
);
41 // Clears the translate script, so it will be fetched next time we translate.
42 void Clear() { data_
.clear(); }
44 // Fetches the JS translate script (the script that is injected in the page
46 void Request(const RequestCallback
& callback
);
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 callbacks called when the server sends a response.
87 typedef std::vector
<RequestCallback
> RequestCallbackList
;
88 RequestCallbackList callback_list_
;
90 base::WeakPtrFactory
<TranslateScript
> weak_method_factory_
;
92 DISALLOW_COPY_AND_ASSIGN(TranslateScript
);
95 } // namespace translate
97 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_SCRIPT_H_