Home | History | Annotate | Download | only in translate
      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/gtest_prod_util.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/time/time.h"
     15 
     16 class TranslateScriptTest;
     17 class TranslateURLFetcher;
     18 
     19 class TranslateScript {
     20  public:
     21   typedef base::Callback<void(bool, const std::string&)> Callback;
     22 
     23   static const int kFetcherId = 0;
     24 
     25   TranslateScript();
     26   virtual ~TranslateScript();
     27 
     28   // Returns the feched the translate script.
     29   const std::string& data() { return data_; }
     30 
     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);
     36   }
     37 
     38   // Clears the translate script, so it will be fetched next time we translate.
     39   void Clear() { data_.clear(); }
     40 
     41   // Fetches the JS translate script (the script that is injected in the page
     42   // to translate it).
     43   void Request(const Callback& callback);
     44 
     45   // Returns true if this has a pending request.
     46   bool HasPendingRequest() const { return fetcher_.get() != NULL; }
     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 callback called when the server sends a response.
     87   Callback callback_;
     88 
     89   base::WeakPtrFactory<TranslateScript> weak_method_factory_;
     90 
     91   DISALLOW_COPY_AND_ASSIGN(TranslateScript);
     92 };
     93 
     94 #endif  // CHROME_BROWSER_TRANSLATE_TRANSLATE_SCRIPT_H_
     95