OLD | NEW |
(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 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_SCRIPT_H_ |
| 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_SCRIPT_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/time.h" |
| 14 |
| 15 class TranslateURLFetcher; |
| 16 |
| 17 class TranslateScript { |
| 18 public: |
| 19 typedef base::Callback<void(bool, const std::string&)> Callback; |
| 20 |
| 21 TranslateScript(); |
| 22 virtual ~TranslateScript(); |
| 23 |
| 24 // Returns the feched the translate script. |
| 25 const std::string& data() { return data_; } |
| 26 |
| 27 // Used by unit-tests to override some defaults: |
| 28 // Delay after which the translate script is fetched again from the |
| 29 // translation server. |
| 30 void set_expiration_delay(int delay_ms) { |
| 31 expiration_delay_ = base::TimeDelta::FromMilliseconds(delay_ms); |
| 32 } |
| 33 |
| 34 // Clears the translate script, so it will be fetched next time we translate. |
| 35 void Clear() { data_.clear(); } |
| 36 |
| 37 // Fetches the JS translate script (the script that is injected in the page |
| 38 // to translate it). |
| 39 void Request(const Callback& callback); |
| 40 |
| 41 // Returns true if this has a pending request. |
| 42 bool HasPendingRequest() const { return fetcher_.get() != NULL; } |
| 43 |
| 44 private: |
| 45 // The callback when the script is fetched or a server error occured. |
| 46 void OnScriptFetchComplete(int id, bool success, const std::string& data); |
| 47 |
| 48 base::WeakPtrFactory<TranslateScript> weak_method_factory_; |
| 49 |
| 50 // URL fetcher to fetch the translate script. |
| 51 scoped_ptr<TranslateURLFetcher> fetcher_; |
| 52 |
| 53 // The JS injected in the page to do the translation. |
| 54 std::string data_; |
| 55 |
| 56 // Delay after which the translate script is fetched again from the translate |
| 57 // server. |
| 58 base::TimeDelta expiration_delay_; |
| 59 |
| 60 // The callback called when the server sends a response. |
| 61 Callback callback_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(TranslateScript); |
| 64 }; |
| 65 |
| 66 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_SCRIPT_H_ |
OLD | NEW |