OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser/translate/translate_manager.h" | 5 #include "chrome/browser/translate/translate_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "chrome/common/chrome_switches.h" | 32 #include "chrome/common/chrome_switches.h" |
33 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
34 #include "chrome/common/render_messages.h" | 34 #include "chrome/common/render_messages.h" |
35 #include "chrome/common/translate_errors.h" | 35 #include "chrome/common/translate_errors.h" |
36 #include "chrome/common/url_constants.h" | 36 #include "chrome/common/url_constants.h" |
37 #include "content/browser/renderer_host/render_process_host.h" | 37 #include "content/browser/renderer_host/render_process_host.h" |
38 #include "content/browser/renderer_host/render_view_host.h" | 38 #include "content/browser/renderer_host/render_view_host.h" |
39 #include "content/browser/tab_contents/navigation_details.h" | 39 #include "content/browser/tab_contents/navigation_details.h" |
40 #include "content/browser/tab_contents/navigation_entry.h" | 40 #include "content/browser/tab_contents/navigation_entry.h" |
41 #include "content/browser/tab_contents/tab_contents.h" | 41 #include "content/browser/tab_contents/tab_contents.h" |
| 42 #include "content/common/net/url_fetcher.h" |
42 #include "content/public/browser/notification_service.h" | 43 #include "content/public/browser/notification_service.h" |
43 #include "content/public/browser/notification_details.h" | 44 #include "content/public/browser/notification_details.h" |
44 #include "content/public/browser/notification_source.h" | 45 #include "content/public/browser/notification_source.h" |
45 #include "content/public/browser/notification_types.h" | 46 #include "content/public/browser/notification_types.h" |
46 #include "grit/browser_resources.h" | 47 #include "grit/browser_resources.h" |
47 #include "net/base/escape.h" | 48 #include "net/base/escape.h" |
48 #include "net/url_request/url_request_status.h" | 49 #include "net/url_request/url_request_status.h" |
49 #include "ui/base/resource/resource_bundle.h" | 50 #include "ui/base/resource/resource_bundle.h" |
50 | 51 |
51 namespace { | 52 namespace { |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 prefs::kAcceptLanguages); | 352 prefs::kAcceptLanguages); |
352 PrefService* prefs = content::Source<PrefService>(source).ptr(); | 353 PrefService* prefs = content::Source<PrefService>(source).ptr(); |
353 InitAcceptLanguages(prefs); | 354 InitAcceptLanguages(prefs); |
354 break; | 355 break; |
355 } | 356 } |
356 default: | 357 default: |
357 NOTREACHED(); | 358 NOTREACHED(); |
358 } | 359 } |
359 } | 360 } |
360 | 361 |
361 void TranslateManager::OnURLFetchComplete(const URLFetcher* source, | 362 void TranslateManager::OnURLFetchComplete(const URLFetcher* source) { |
362 const GURL& url, | |
363 const net::URLRequestStatus& status, | |
364 int response_code, | |
365 const net::ResponseCookies& cookies, | |
366 const std::string& data) { | |
367 if (translate_script_request_pending_.get() != source && | 363 if (translate_script_request_pending_.get() != source && |
368 language_list_request_pending_.get() != source) { | 364 language_list_request_pending_.get() != source) { |
369 // Looks like crash on Mac is possibly caused with callback entering here | 365 // Looks like crash on Mac is possibly caused with callback entering here |
370 // with unknown fetcher when network is refreshed. | 366 // with unknown fetcher when network is refreshed. |
371 scoped_ptr<const URLFetcher> delete_ptr(source); | 367 scoped_ptr<const URLFetcher> delete_ptr(source); |
372 return; | 368 return; |
373 } | 369 } |
374 | 370 |
375 bool error = (status.status() != net::URLRequestStatus::SUCCESS || | 371 bool error = (source->status().status() != net::URLRequestStatus::SUCCESS || |
376 response_code != 200); | 372 source->response_code() != 200); |
377 if (translate_script_request_pending_.get() == source) { | 373 if (translate_script_request_pending_.get() == source) { |
378 scoped_ptr<const URLFetcher> delete_ptr( | 374 scoped_ptr<const URLFetcher> delete_ptr( |
379 translate_script_request_pending_.release()); | 375 translate_script_request_pending_.release()); |
380 if (!error) { | 376 if (!error) { |
381 base::StringPiece str = ResourceBundle::GetSharedInstance(). | 377 base::StringPiece str = ResourceBundle::GetSharedInstance(). |
382 GetRawDataResource(IDR_TRANSLATE_JS); | 378 GetRawDataResource(IDR_TRANSLATE_JS); |
383 DCHECK(translate_script_.empty()); | 379 DCHECK(translate_script_.empty()); |
384 str.CopyToString(&translate_script_); | 380 str.CopyToString(&translate_script_); |
| 381 std::string data; |
| 382 source->GetResponseAsString(&data); |
385 translate_script_ += "\n" + data; | 383 translate_script_ += "\n" + data; |
386 // We'll expire the cached script after some time, to make sure long | 384 // We'll expire the cached script after some time, to make sure long |
387 // running browsers still get fixes that might get pushed with newer | 385 // running browsers still get fixes that might get pushed with newer |
388 // scripts. | 386 // scripts. |
389 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 387 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
390 base::Bind(&TranslateManager::ClearTranslateScript, | 388 base::Bind(&TranslateManager::ClearTranslateScript, |
391 weak_method_factory_.GetWeakPtr()), | 389 weak_method_factory_.GetWeakPtr()), |
392 translate_script_expiration_delay_); | 390 translate_script_expiration_delay_); |
393 } | 391 } |
394 // Process any pending requests. | 392 // Process any pending requests. |
(...skipping 27 matching lines...) Expand all Loading... |
422 } else { | 420 } else { |
423 // Translate the page. | 421 // Translate the page. |
424 DoTranslatePage(tab, translate_script_, | 422 DoTranslatePage(tab, translate_script_, |
425 request.source_lang, request.target_lang); | 423 request.source_lang, request.target_lang); |
426 } | 424 } |
427 } | 425 } |
428 pending_requests_.clear(); | 426 pending_requests_.clear(); |
429 } else { // if (translate_script_request_pending_.get() == source) | 427 } else { // if (translate_script_request_pending_.get() == source) |
430 scoped_ptr<const URLFetcher> delete_ptr( | 428 scoped_ptr<const URLFetcher> delete_ptr( |
431 language_list_request_pending_.release()); | 429 language_list_request_pending_.release()); |
432 if (!error) | 430 if (!error) { |
| 431 std::string data; |
| 432 source->GetResponseAsString(&data); |
433 SetSupportedLanguages(data); | 433 SetSupportedLanguages(data); |
434 else | 434 } else { |
435 VLOG(1) << "Failed to Fetch languages from: " << kLanguageListFetchURL; | 435 VLOG(1) << "Failed to Fetch languages from: " << kLanguageListFetchURL; |
| 436 } |
436 } | 437 } |
437 } | 438 } |
438 | 439 |
439 // static | 440 // static |
440 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { | 441 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { |
441 return GetTranslateInfoBarDelegate(tab) != NULL; | 442 return GetTranslateInfoBarDelegate(tab) != NULL; |
442 } | 443 } |
443 | 444 |
444 TranslateManager::TranslateManager() | 445 TranslateManager::TranslateManager() |
445 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_method_factory_(this)), | 446 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_method_factory_(this)), |
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
834 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); | 835 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
835 | 836 |
836 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { | 837 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { |
837 TranslateInfoBarDelegate* delegate = | 838 TranslateInfoBarDelegate* delegate = |
838 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | 839 infobar_helper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); |
839 if (delegate) | 840 if (delegate) |
840 return delegate; | 841 return delegate; |
841 } | 842 } |
842 return NULL; | 843 return NULL; |
843 } | 844 } |
OLD | NEW |