Chromium Code Reviews
Help | Chromium Project | Sign in
(1078)

Side by Side Diff: chrome/browser/automation/automation_provider.cc

Issue 3026016: New pyauto hook for the translate feature. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Small changes Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/automation/automation_provider.h" 5 #include "chrome/browser/automation/automation_provider.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "app/l10n_util.h" 9 #include "app/l10n_util.h"
10 #include "app/message_box_flags.h" 10 #include "app/message_box_flags.h"
(...skipping 2642 matching lines...) Expand 10 before | Expand all | Expand 10 after
2653 BrowsingDataRemover* remover = new BrowsingDataRemover( 2653 BrowsingDataRemover* remover = new BrowsingDataRemover(
2654 profile(), string_to_time_period[time_period], base::Time()); 2654 profile(), string_to_time_period[time_period], base::Time());
2655 2655
2656 remover->AddObserver( 2656 remover->AddObserver(
2657 new AutomationProviderBrowsingDataObserver(this, reply_message)); 2657 new AutomationProviderBrowsingDataObserver(this, reply_message));
2658 remover->Remove(remove_mask); 2658 remover->Remove(remove_mask);
2659 // BrowsingDataRemover deletes itself using DeleteTask. 2659 // BrowsingDataRemover deletes itself using DeleteTask.
2660 // The observer also deletes itself after sending the reply. 2660 // The observer also deletes itself after sending the reply.
2661 } 2661 }
2662 2662
2663 namespace {
2664
2665 // Get the TabContents from a dictionary of arguments.
2666 TabContents* GetTabContentsFromDict(const Browser* browser,
2667 const DictionaryValue* args,
2668 std::string* error_message) {
2669 int tab_index;
2670 if (!args->GetInteger(L"tab_index", &tab_index)) {
2671 *error_message = "Must include tab_index.";
2672 return NULL;
2673 }
2674
2675 TabContents* tab_contents = browser->GetTabContentsAt(tab_index);
2676 if (!tab_contents) {
2677 *error_message = StringPrintf("No tab at index %d.", tab_index);
2678 return NULL;
2679 }
2680 return tab_contents;
2681 }
2682
2683 // Get the TranslateInfoBarDelegate from TabContents.
2684 TranslateInfoBarDelegate* GetTranslateInfoBarDelegate(
2685 TabContents* tab_contents) {
2686 for (int i = 0; i < tab_contents->infobar_delegate_count(); i++) {
2687 InfoBarDelegate* infobar = tab_contents->GetInfoBarDelegateAt(i);
2688 if (infobar->AsTranslateInfoBarDelegate())
2689 return infobar->AsTranslateInfoBarDelegate();
2690 }
2691 // No translate infobar.
2692 return NULL;
2693 }
2694
2695 } // namespace
2696
2697 // See GetTranslateInfo() in chrome/test/pyautolib/pyauto.py for sample json
2698 // input and output.
2699 void AutomationProvider::GetTranslateInfo(Browser* browser,
2700 DictionaryValue* args,
2701 IPC::Message* reply_message) {
2702 std::string error_message;
2703 TabContents* tab_contents = GetTabContentsFromDict(browser, args,
2704 &error_message);
2705 if (!tab_contents) {
2706 AutomationJSONReply(this, reply_message).SendError(error_message);
2707 return;
2708 }
2709
2710 // Get the translate bar if there is one and pass it to the observer.
2711 // The observer will check for null and populate the information accordingly.
2712 TranslateInfoBarDelegate* translate_bar =
2713 GetTranslateInfoBarDelegate(tab_contents);
2714
2715 TabLanguageDeterminedObserver* observer = new TabLanguageDeterminedObserver(
2716 this, reply_message, tab_contents, translate_bar);
2717 // If the language for the page hasn't been loaded yet, then just make
2718 // the observer, otherwise call observe directly.
2719 std::string language = tab_contents->language_state().original_language();
2720 if (!language.empty()) {
2721 observer->Observe(NotificationType::TAB_LANGUAGE_DETERMINED,
2722 Source<TabContents>(tab_contents),
2723 Details<std::string>(&language));
2724 }
2725 }
2726
2727 // See SelectTranslateOption() in chrome/test/pyautolib/pyauto.py for sample
2728 // json input.
2729 // Sample json output: {}
2730 void AutomationProvider::SelectTranslateOption(Browser* browser,
2731 DictionaryValue* args,
2732 IPC::Message* reply_message) {
2733 std::string option;
2734 std::string error_message;
2735 TabContents* tab_contents = GetTabContentsFromDict(browser, args,
2736 &error_message);
2737 if (!tab_contents) {
2738 AutomationJSONReply(this, reply_message).SendError(error_message);
2739 return;
2740 }
2741
2742 TranslateInfoBarDelegate* translate_bar =
2743 GetTranslateInfoBarDelegate(tab_contents);
2744 if (!translate_bar) {
2745 AutomationJSONReply(this, reply_message)
2746 .SendError("There is no translate bar open.");
2747 return;
2748 }
2749
2750 if (!args->GetString(L"option", &option)) {
2751 AutomationJSONReply(this, reply_message).SendError("Must include option");
2752 return;
2753 }
2754
2755 if (option == "translate_page") {
2756 // Make a new notification observer which will send the reply.
2757 new PageTranslatedObserver(this, reply_message, tab_contents);
2758 translate_bar->Translate();
2759 return;
2760 }
2761
2762 AutomationJSONReply reply(this, reply_message);
2763 if (option == "never_translate_language") {
2764 if (translate_bar->IsLanguageBlacklisted()) {
2765 reply.SendError("The language was already blacklisted.");
2766 return;
2767 }
2768 translate_bar->ToggleLanguageBlacklist();
2769 reply.SendSuccess(NULL);
2770 } else if (option == "never_translate_site") {
2771 if (translate_bar->IsSiteBlacklisted()) {
2772 reply.SendError("The site was already blacklisted.");
2773 return;
2774 }
2775 translate_bar->ToggleSiteBlacklist();
2776 reply.SendSuccess(NULL);
2777 } else if (option == "toggle_always_translate") {
2778 translate_bar->ToggleAlwaysTranslate();
2779 reply.SendSuccess(NULL);
2780 } else if (option == "revert_translation") {
2781 translate_bar->RevertTranslation();
2782 reply.SendSuccess(NULL);
2783 } else {
2784 reply.SendError("Invalid string found for option.");
2785 }
2786 }
2787
2663 // Sample json input: { "command": "GetThemeInfo" } 2788 // Sample json input: { "command": "GetThemeInfo" }
2664 // Refer GetThemeInfo() in chrome/test/pyautolib/pyauto.py for sample output. 2789 // Refer GetThemeInfo() in chrome/test/pyautolib/pyauto.py for sample output.
2665 void AutomationProvider::GetThemeInfo(Browser* browser, 2790 void AutomationProvider::GetThemeInfo(Browser* browser,
2666 DictionaryValue* args, 2791 DictionaryValue* args,
2667 IPC::Message* reply_message) { 2792 IPC::Message* reply_message) {
2668 scoped_ptr<DictionaryValue> return_value(new DictionaryValue); 2793 scoped_ptr<DictionaryValue> return_value(new DictionaryValue);
2669 Extension* theme = browser->profile()->GetTheme(); 2794 Extension* theme = browser->profile()->GetTheme();
2670 if (theme) { 2795 if (theme) {
2671 return_value->SetString(L"name", theme->name()); 2796 return_value->SetString(L"name", theme->name());
2672 return_value->Set(L"images", theme->GetThemeImages()->DeepCopy()); 2797 return_value->Set(L"images", theme->GetThemeImages()->DeepCopy());
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
2998 handler_map["ImportSettings"] = &AutomationProvider::ImportSettings; 3123 handler_map["ImportSettings"] = &AutomationProvider::ImportSettings;
2999 3124
3000 handler_map["AddSavedPassword"] = &AutomationProvider::AddSavedPassword; 3125 handler_map["AddSavedPassword"] = &AutomationProvider::AddSavedPassword;
3001 handler_map["GetSavedPasswords"] = &AutomationProvider::GetSavedPasswords; 3126 handler_map["GetSavedPasswords"] = &AutomationProvider::GetSavedPasswords;
3002 3127
3003 handler_map["ClearBrowsingData"] = &AutomationProvider::ClearBrowsingData; 3128 handler_map["ClearBrowsingData"] = &AutomationProvider::ClearBrowsingData;
3004 3129
3005 // SetTheme() implemented using InstallExtension(). 3130 // SetTheme() implemented using InstallExtension().
3006 handler_map["GetThemeInfo"] = &AutomationProvider::GetThemeInfo; 3131 handler_map["GetThemeInfo"] = &AutomationProvider::GetThemeInfo;
3007 3132
3133 handler_map["SelectTranslateOption"] =
3134 &AutomationProvider::SelectTranslateOption;
3135 handler_map["GetTranslateInfo"] = &AutomationProvider::GetTranslateInfo;
3136
3008 handler_map["GetAutoFillProfile"] = &AutomationProvider::GetAutoFillProfile; 3137 handler_map["GetAutoFillProfile"] = &AutomationProvider::GetAutoFillProfile;
3009 handler_map["FillAutoFillProfile"] = &AutomationProvider::FillAutoFillProfile; 3138 handler_map["FillAutoFillProfile"] = &AutomationProvider::FillAutoFillProfile;
3010 3139
3011 if (handler_map.find(std::string(command)) != handler_map.end()) { 3140 if (handler_map.find(std::string(command)) != handler_map.end()) {
3012 (this->*handler_map[command])(browser, dict_value, reply_message); 3141 (this->*handler_map[command])(browser, dict_value, reply_message);
3013 } else { 3142 } else {
3014 std::string error_string = "Unknown command. Options: "; 3143 std::string error_string = "Unknown command. Options: ";
3015 for (std::map<std::string, JsonHandler>::const_iterator it = 3144 for (std::map<std::string, JsonHandler>::const_iterator it =
3016 handler_map.begin(); it != handler_map.end(); ++it) { 3145 handler_map.begin(); it != handler_map.end(); ++it) {
3017 error_string += it->first + ", "; 3146 error_string += it->first + ", ";
(...skipping 1163 matching lines...) Expand 10 before | Expand all | Expand 10 after
4181 } 4310 }
4182 4311
4183 void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) { 4312 void AutomationProvider::WaitForPopupMenuToOpen(IPC::Message* reply_message) {
4184 NOTIMPLEMENTED(); 4313 NOTIMPLEMENTED();
4185 } 4314 }
4186 #endif // !defined(TOOLKIT_VIEWS) 4315 #endif // !defined(TOOLKIT_VIEWS)
4187 4316
4188 void AutomationProvider::ResetToDefaultTheme() { 4317 void AutomationProvider::ResetToDefaultTheme() {
4189 profile_->ClearTheme(); 4318 profile_->ClearTheme();
4190 } 4319 }
OLDNEW
« no previous file with comments | « chrome/browser/automation/automation_provider.h ('k') | chrome/browser/automation/automation_provider_observers.h » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld 1280:2d3e6564b7b6