| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/translate/content/renderer/translate_helper.h" | 5 #include "components/translate/content/renderer/translate_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "content/public/renderer/render_frame.h" | 23 #include "content/public/renderer/render_frame.h" |
| 24 #include "content/public/renderer/render_thread.h" | 24 #include "content/public/renderer/render_thread.h" |
| 25 #include "services/service_manager/public/cpp/interface_provider.h" | 25 #include "services/service_manager/public/cpp/interface_provider.h" |
| 26 #include "third_party/WebKit/public/web/WebDocument.h" | 26 #include "third_party/WebKit/public/web/WebDocument.h" |
| 27 #include "third_party/WebKit/public/web/WebLanguageDetectionDetails.h" | 27 #include "third_party/WebKit/public/web/WebLanguageDetectionDetails.h" |
| 28 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 28 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 29 #include "third_party/WebKit/public/web/WebScriptSource.h" | 29 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 30 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 31 #include "v8/include/v8.h" | 31 #include "v8/include/v8.h" |
| 32 | 32 |
| 33 using base::ASCIIToUTF16; | |
| 34 using blink::WebDocument; | 33 using blink::WebDocument; |
| 35 using blink::WebLocalFrame; | 34 using blink::WebLocalFrame; |
| 36 using blink::WebScriptSource; | 35 using blink::WebScriptSource; |
| 37 using blink::WebSecurityOrigin; | 36 using blink::WebSecurityOrigin; |
| 38 using blink::WebString; | 37 using blink::WebString; |
| 39 using blink::WebVector; | 38 using blink::WebVector; |
| 40 using blink::WebLanguageDetectionDetails; | 39 using blink::WebLanguageDetectionDetails; |
| 41 | 40 |
| 42 namespace { | 41 namespace { |
| 43 | 42 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 // Just converts |delayInMs| without any modification in practical cases. | 181 // Just converts |delayInMs| without any modification in practical cases. |
| 183 // Tests will override this function to return modified value. | 182 // Tests will override this function to return modified value. |
| 184 return base::TimeDelta::FromMilliseconds(delayInMs); | 183 return base::TimeDelta::FromMilliseconds(delayInMs); |
| 185 } | 184 } |
| 186 | 185 |
| 187 void TranslateHelper::ExecuteScript(const std::string& script) { | 186 void TranslateHelper::ExecuteScript(const std::string& script) { |
| 188 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); | 187 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
| 189 if (!main_frame) | 188 if (!main_frame) |
| 190 return; | 189 return; |
| 191 | 190 |
| 192 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); | 191 WebScriptSource source = WebScriptSource(WebString::fromASCII(script)); |
| 193 main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1); | 192 main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1); |
| 194 } | 193 } |
| 195 | 194 |
| 196 bool TranslateHelper::ExecuteScriptAndGetBoolResult(const std::string& script, | 195 bool TranslateHelper::ExecuteScriptAndGetBoolResult(const std::string& script, |
| 197 bool fallback) { | 196 bool fallback) { |
| 198 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); | 197 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
| 199 if (!main_frame) | 198 if (!main_frame) |
| 200 return fallback; | 199 return fallback; |
| 201 | 200 |
| 202 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 201 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 203 WebVector<v8::Local<v8::Value> > results; | 202 WebVector<v8::Local<v8::Value> > results; |
| 204 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); | 203 WebScriptSource source = WebScriptSource(WebString::fromASCII(script)); |
| 205 main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1, &results); | 204 main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1, &results); |
| 206 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsBoolean()) { | 205 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsBoolean()) { |
| 207 NOTREACHED(); | 206 NOTREACHED(); |
| 208 return fallback; | 207 return fallback; |
| 209 } | 208 } |
| 210 | 209 |
| 211 return results[0]->BooleanValue(); | 210 return results[0]->BooleanValue(); |
| 212 } | 211 } |
| 213 | 212 |
| 214 std::string TranslateHelper::ExecuteScriptAndGetStringResult( | 213 std::string TranslateHelper::ExecuteScriptAndGetStringResult( |
| 215 const std::string& script) { | 214 const std::string& script) { |
| 216 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); | 215 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
| 217 if (!main_frame) | 216 if (!main_frame) |
| 218 return std::string(); | 217 return std::string(); |
| 219 | 218 |
| 220 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 219 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 221 WebVector<v8::Local<v8::Value> > results; | 220 WebVector<v8::Local<v8::Value> > results; |
| 222 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); | 221 WebScriptSource source = WebScriptSource(WebString::fromASCII(script)); |
| 223 main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1, &results); | 222 main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1, &results); |
| 224 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsString()) { | 223 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsString()) { |
| 225 NOTREACHED(); | 224 NOTREACHED(); |
| 226 return std::string(); | 225 return std::string(); |
| 227 } | 226 } |
| 228 | 227 |
| 229 v8::Local<v8::String> v8_str = results[0].As<v8::String>(); | 228 v8::Local<v8::String> v8_str = results[0].As<v8::String>(); |
| 230 int length = v8_str->Utf8Length() + 1; | 229 int length = v8_str->Utf8Length() + 1; |
| 231 std::unique_ptr<char[]> str(new char[length]); | 230 std::unique_ptr<char[]> str(new char[length]); |
| 232 v8_str->WriteUtf8(str.get(), length); | 231 v8_str->WriteUtf8(str.get(), length); |
| 233 return std::string(str.get()); | 232 return std::string(str.get()); |
| 234 } | 233 } |
| 235 | 234 |
| 236 double TranslateHelper::ExecuteScriptAndGetDoubleResult( | 235 double TranslateHelper::ExecuteScriptAndGetDoubleResult( |
| 237 const std::string& script) { | 236 const std::string& script) { |
| 238 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); | 237 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
| 239 if (!main_frame) | 238 if (!main_frame) |
| 240 return 0.0; | 239 return 0.0; |
| 241 | 240 |
| 242 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 241 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 243 WebVector<v8::Local<v8::Value> > results; | 242 WebVector<v8::Local<v8::Value> > results; |
| 244 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); | 243 WebScriptSource source = WebScriptSource(WebString::fromASCII(script)); |
| 245 main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1, &results); | 244 main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1, &results); |
| 246 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { | 245 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { |
| 247 NOTREACHED(); | 246 NOTREACHED(); |
| 248 return 0.0; | 247 return 0.0; |
| 249 } | 248 } |
| 250 | 249 |
| 251 return results[0]->NumberValue(); | 250 return results[0]->NumberValue(); |
| 252 } | 251 } |
| 253 | 252 |
| 254 // mojom::Page implementations. | 253 // mojom::Page implementations. |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 binding_.Close(); | 421 binding_.Close(); |
| 423 translate_callback_pending_.Reset(); | 422 translate_callback_pending_.Reset(); |
| 424 CancelPendingTranslation(); | 423 CancelPendingTranslation(); |
| 425 } | 424 } |
| 426 | 425 |
| 427 void TranslateHelper::OnDestruct() { | 426 void TranslateHelper::OnDestruct() { |
| 428 delete this; | 427 delete this; |
| 429 } | 428 } |
| 430 | 429 |
| 431 } // namespace translate | 430 } // namespace translate |
| OLD | NEW |