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