| 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 "chrome/renderer/translate/translate_helper.h" | 5 #include "chrome/renderer/translate/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/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 1, | 188 1, |
| 189 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS); | 189 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS); |
| 190 } | 190 } |
| 191 | 191 |
| 192 bool TranslateHelper::ExecuteScriptAndGetBoolResult(const std::string& script, | 192 bool TranslateHelper::ExecuteScriptAndGetBoolResult(const std::string& script, |
| 193 bool fallback) { | 193 bool fallback) { |
| 194 WebFrame* main_frame = GetMainFrame(); | 194 WebFrame* main_frame = GetMainFrame(); |
| 195 if (!main_frame) | 195 if (!main_frame) |
| 196 return fallback; | 196 return fallback; |
| 197 | 197 |
| 198 v8::HandleScope handle_scope; | 198 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 199 WebVector<v8::Local<v8::Value> > results; | 199 WebVector<v8::Local<v8::Value> > results; |
| 200 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); | 200 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); |
| 201 main_frame->executeScriptInIsolatedWorld( | 201 main_frame->executeScriptInIsolatedWorld( |
| 202 chrome::ISOLATED_WORLD_ID_TRANSLATE, | 202 chrome::ISOLATED_WORLD_ID_TRANSLATE, |
| 203 &source, | 203 &source, |
| 204 1, | 204 1, |
| 205 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS, | 205 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS, |
| 206 &results); | 206 &results); |
| 207 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsBoolean()) { | 207 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsBoolean()) { |
| 208 NOTREACHED(); | 208 NOTREACHED(); |
| 209 return fallback; | 209 return fallback; |
| 210 } | 210 } |
| 211 | 211 |
| 212 return results[0]->BooleanValue(); | 212 return results[0]->BooleanValue(); |
| 213 } | 213 } |
| 214 | 214 |
| 215 std::string TranslateHelper::ExecuteScriptAndGetStringResult( | 215 std::string TranslateHelper::ExecuteScriptAndGetStringResult( |
| 216 const std::string& script) { | 216 const std::string& script) { |
| 217 WebFrame* main_frame = GetMainFrame(); | 217 WebFrame* main_frame = GetMainFrame(); |
| 218 if (!main_frame) | 218 if (!main_frame) |
| 219 return std::string(); | 219 return std::string(); |
| 220 | 220 |
| 221 v8::HandleScope handle_scope; | 221 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 222 WebVector<v8::Local<v8::Value> > results; | 222 WebVector<v8::Local<v8::Value> > results; |
| 223 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); | 223 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); |
| 224 main_frame->executeScriptInIsolatedWorld( | 224 main_frame->executeScriptInIsolatedWorld( |
| 225 chrome::ISOLATED_WORLD_ID_TRANSLATE, | 225 chrome::ISOLATED_WORLD_ID_TRANSLATE, |
| 226 &source, | 226 &source, |
| 227 1, | 227 1, |
| 228 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS, | 228 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS, |
| 229 &results); | 229 &results); |
| 230 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsString()) { | 230 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsString()) { |
| 231 NOTREACHED(); | 231 NOTREACHED(); |
| 232 return std::string(); | 232 return std::string(); |
| 233 } | 233 } |
| 234 | 234 |
| 235 v8::Local<v8::String> v8_str = results[0]->ToString(); | 235 v8::Local<v8::String> v8_str = results[0]->ToString(); |
| 236 int length = v8_str->Utf8Length() + 1; | 236 int length = v8_str->Utf8Length() + 1; |
| 237 scoped_ptr<char[]> str(new char[length]); | 237 scoped_ptr<char[]> str(new char[length]); |
| 238 v8_str->WriteUtf8(str.get(), length); | 238 v8_str->WriteUtf8(str.get(), length); |
| 239 return std::string(str.get()); | 239 return std::string(str.get()); |
| 240 } | 240 } |
| 241 | 241 |
| 242 double TranslateHelper::ExecuteScriptAndGetDoubleResult( | 242 double TranslateHelper::ExecuteScriptAndGetDoubleResult( |
| 243 const std::string& script) { | 243 const std::string& script) { |
| 244 WebFrame* main_frame = GetMainFrame(); | 244 WebFrame* main_frame = GetMainFrame(); |
| 245 if (!main_frame) | 245 if (!main_frame) |
| 246 return 0.0; | 246 return 0.0; |
| 247 | 247 |
| 248 v8::HandleScope handle_scope; | 248 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 249 WebVector<v8::Local<v8::Value> > results; | 249 WebVector<v8::Local<v8::Value> > results; |
| 250 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); | 250 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script)); |
| 251 main_frame->executeScriptInIsolatedWorld( | 251 main_frame->executeScriptInIsolatedWorld( |
| 252 chrome::ISOLATED_WORLD_ID_TRANSLATE, | 252 chrome::ISOLATED_WORLD_ID_TRANSLATE, |
| 253 &source, | 253 &source, |
| 254 1, | 254 1, |
| 255 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS, | 255 extensions::EXTENSION_GROUP_INTERNAL_TRANSLATE_SCRIPTS, |
| 256 &results); | 256 &results); |
| 257 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { | 257 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { |
| 258 NOTREACHED(); | 258 NOTREACHED(); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 | 489 |
| 490 WebFrame* TranslateHelper::GetMainFrame() { | 490 WebFrame* TranslateHelper::GetMainFrame() { |
| 491 WebView* web_view = render_view()->GetWebView(); | 491 WebView* web_view = render_view()->GetWebView(); |
| 492 | 492 |
| 493 // When the tab is going to be closed, the web_view can be NULL. | 493 // When the tab is going to be closed, the web_view can be NULL. |
| 494 if (!web_view) | 494 if (!web_view) |
| 495 return NULL; | 495 return NULL; |
| 496 | 496 |
| 497 return web_view->mainFrame(); | 497 return web_view->mainFrame(); |
| 498 } | 498 } |
| OLD | NEW |