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. 4 5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_SCRIPT_H_ 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_SCRIPT_H_ 7 8 #include <string> 9 #include <vector> 10 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" 16 17 namespace translate { 18 19 class TranslateScriptTest; 20 class TranslateURLFetcher; 21 22 class TranslateScript { 23 public: 24 typedef base::Callback<void(bool, const std::string&)> RequestCallback; 25 26 static const int kFetcherId = 0; 27 28 TranslateScript(); 29 virtual ~TranslateScript(); 30 31 // Returns the feched the translate script. data()32 const std::string& data() { return data_; } 33 34 // Used by unit-tests to override some defaults: 35 // Delay after which the translate script is fetched again from the 36 // translation server. set_expiration_delay(int delay_ms)37 void set_expiration_delay(int delay_ms) { 38 expiration_delay_ = base::TimeDelta::FromMilliseconds(delay_ms); 39 } 40 41 // Clears the translate script, so it will be fetched next time we translate. Clear()42 void Clear() { data_.clear(); } 43 44 // Fetches the JS translate script (the script that is injected in the page 45 // to translate it). 46 void Request(const RequestCallback& callback); 47 48 private: 49 friend class TranslateScriptTest; 50 FRIEND_TEST_ALL_PREFIXES(TranslateScriptTest, CheckScriptParameters); 51 FRIEND_TEST_ALL_PREFIXES(TranslateScriptTest, CheckScriptURL); 52 53 static const char kScriptURL[]; 54 static const char kRequestHeader[]; 55 56 // Used in kTranslateScriptURL to specify using always ssl to load resources. 57 static const char kAlwaysUseSslQueryName[]; 58 static const char kAlwaysUseSslQueryValue[]; 59 60 // Used in kTranslateScriptURL to specify a callback function name. 61 static const char kCallbackQueryName[]; 62 static const char kCallbackQueryValue[]; 63 64 // Used in kTranslateScriptURL to specify a CSS loader callback function name. 65 static const char kCssLoaderCallbackQueryName[]; 66 static const char kCssLoaderCallbackQueryValue[]; 67 68 // Used in kTranslateScriptURL to specify a JavaScript loader callback 69 // function name. 70 static const char kJavascriptLoaderCallbackQueryName[]; 71 static const char kJavascriptLoaderCallbackQueryValue[]; 72 73 // The callback when the script is fetched or a server error occured. 74 void OnScriptFetchComplete(int id, bool success, const std::string& data); 75 76 // URL fetcher to fetch the translate script. 77 scoped_ptr<TranslateURLFetcher> fetcher_; 78 79 // The JS injected in the page to do the translation. 80 std::string data_; 81 82 // Delay after which the translate script is fetched again from the translate 83 // server. 84 base::TimeDelta expiration_delay_; 85 86 // The callbacks called when the server sends a response. 87 typedef std::vector<RequestCallback> RequestCallbackList; 88 RequestCallbackList callback_list_; 89 90 base::WeakPtrFactory<TranslateScript> weak_method_factory_; 91 92 DISALLOW_COPY_AND_ASSIGN(TranslateScript); 93 }; 94 95 } // namespace translate 96 97 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_SCRIPT_H_ 98