[email protected] | 3b5f702 | 2010-03-25 20:37:40 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include "chrome/browser/automation/automation_provider.h" |
| 6 | |
[email protected] | 5ae5bed | 2009-08-21 18:52:44 | [diff] [blame] | 7 | #include <set> |
| 8 | |
[email protected] | 3712621 | 2009-05-06 02:23:31 | [diff] [blame] | 9 | #include "app/message_box_flags.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 11 | #include "base/debug/trace_event.h" |
[email protected] | 7060bb29 | 2010-06-24 00:52:49 | [diff] [blame] | 12 | #include "base/file_path.h" |
[email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 13 | #include "base/json/json_reader.h" |
[email protected] | 59a61124 | 2010-04-02 02:24:04 | [diff] [blame] | 14 | #include "base/json/json_writer.h" |
[email protected] | 93364da | 2010-06-29 18:03:44 | [diff] [blame] | 15 | #include "base/json/string_escape.h" |
[email protected] | 5fac962 | 2009-02-04 21:49:38 | [diff] [blame] | 16 | #include "base/message_loop.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | #include "base/path_service.h" |
[email protected] | 201b273 | 2009-11-13 18:57:46 | [diff] [blame] | 18 | #include "base/process_util.h" |
[email protected] | f44265b | 2009-05-19 18:52:50 | [diff] [blame] | 19 | #include "base/stl_util-inl.h" |
[email protected] | 4c4d8d2 | 2009-03-04 05:29:27 | [diff] [blame] | 20 | #include "base/string_util.h" |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 21 | #include "base/task.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame^] | 22 | #include "base/threading/thread.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 23 | #include "base/string_number_conversions.h" |
[email protected] | 6d8ffc9f | 2010-03-12 18:27:53 | [diff] [blame] | 24 | #include "base/utf_string_conversions.h" |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 25 | #include "base/values.h" |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 26 | #include "base/waitable_event.h" |
[email protected] | 1a3aba8 | 2010-11-08 23:52:54 | [diff] [blame] | 27 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | 55846ad84 | 2010-07-09 18:22:56 | [diff] [blame] | 28 | #include "chrome/browser/autofill/autofill_manager.h" |
[email protected] | 67908205 | 2010-07-21 21:30:13 | [diff] [blame] | 29 | #include "chrome/browser/automation/automation_autocomplete_edit_tracker.h" |
| 30 | #include "chrome/browser/automation/automation_browser_tracker.h" |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 31 | #include "chrome/browser/automation/automation_extension_tracker.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | #include "chrome/browser/automation/automation_provider_list.h" |
[email protected] | e12de87e | 2009-08-28 00:02:08 | [diff] [blame] | 33 | #include "chrome/browser/automation/automation_provider_observers.h" |
[email protected] | 67908205 | 2010-07-21 21:30:13 | [diff] [blame] | 34 | #include "chrome/browser/automation/automation_resource_message_filter.h" |
| 35 | #include "chrome/browser/automation/automation_tab_tracker.h" |
| 36 | #include "chrome/browser/automation/automation_window_tracker.h" |
[email protected] | f44265b | 2009-05-19 18:52:50 | [diff] [blame] | 37 | #include "chrome/browser/automation/extension_port_container.h" |
[email protected] | 1280270 | 2010-07-09 19:43:09 | [diff] [blame] | 38 | #include "chrome/browser/autocomplete/autocomplete_edit.h" |
[email protected] | c65e2f15 | 2010-10-14 15:30:40 | [diff] [blame] | 39 | #include "chrome/browser/blocked_content_container.h" |
[email protected] | 6d8ffc9f | 2010-03-12 18:27:53 | [diff] [blame] | 40 | #include "chrome/browser/bookmarks/bookmark_model.h" |
| 41 | #include "chrome/browser/bookmarks/bookmark_storage.h" |
[email protected] | ef413ca | 2010-05-25 21:09:14 | [diff] [blame] | 42 | #include "chrome/browser/browser_list.h" |
[email protected] | 5c23875 | 2009-06-13 10:29:07 | [diff] [blame] | 43 | #include "chrome/browser/browser_process.h" |
[email protected] | ed7e6dd | 2010-10-12 02:02:45 | [diff] [blame] | 44 | #include "chrome/browser/browser_thread.h" |
[email protected] | f3e99e3 | 2008-07-30 04:48:39 | [diff] [blame] | 45 | #include "chrome/browser/browser_window.h" |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 46 | #include "chrome/browser/browsing_data_remover.h" |
[email protected] | f83f910 | 2010-05-04 17:01:05 | [diff] [blame] | 47 | #include "chrome/browser/character_encoding.h" |
[email protected] | 9d01a6a | 2010-11-30 12:03:33 | [diff] [blame] | 48 | #include "chrome/browser/content_settings/host_content_settings_map.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | #include "chrome/browser/dom_operation_notification_details.h" |
[email protected] | d9f9b79 | 2009-06-24 13:17:12 | [diff] [blame] | 50 | #include "chrome/browser/debugger/devtools_manager.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame] | 51 | #include "chrome/browser/download/download_item.h" |
[email protected] | 59560e0b | 2009-06-04 03:30:22 | [diff] [blame] | 52 | #include "chrome/browser/download/download_shelf.h" |
[email protected] | f83f910 | 2010-05-04 17:01:05 | [diff] [blame] | 53 | #include "chrome/browser/download/save_package.h" |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 54 | #include "chrome/browser/extensions/crx_installer.h" |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 55 | #include "chrome/browser/extensions/extension_browser_event_router.h" |
[email protected] | ef413ca | 2010-05-25 21:09:14 | [diff] [blame] | 56 | #include "chrome/browser/extensions/extension_host.h" |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 57 | #include "chrome/browser/extensions/extension_install_ui.h" |
[email protected] | a902489 | 2009-06-16 23:13:55 | [diff] [blame] | 58 | #include "chrome/browser/extensions/extension_message_service.h" |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 59 | #include "chrome/browser/extensions/extension_tabs_module.h" |
| 60 | #include "chrome/browser/extensions/extension_toolbar_model.h" |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 61 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 8cb5d5b | 2010-02-09 11:36:16 | [diff] [blame] | 62 | #include "chrome/browser/extensions/user_script_master.h" |
[email protected] | c5aa532 | 2010-07-15 19:00:07 | [diff] [blame] | 63 | #include "chrome/browser/importer/importer.h" |
| 64 | #include "chrome/browser/importer/importer_data_types.h" |
[email protected] | 0ac8368 | 2010-01-22 17:46:27 | [diff] [blame] | 65 | #include "chrome/browser/io_thread.h" |
[email protected] | f732c1e | 2009-07-30 15:48:53 | [diff] [blame] | 66 | #include "chrome/browser/net/url_request_mock_util.h" |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 67 | #include "chrome/browser/platform_util.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 68 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | f83f910 | 2010-05-04 17:01:05 | [diff] [blame] | 69 | #include "chrome/browser/printing/print_job.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 70 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 1db6ff15 | 2009-10-12 15:32:07 | [diff] [blame] | 71 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | 6524b5f9 | 2009-01-22 17:48:25 | [diff] [blame] | 72 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 3b073b2 | 2009-01-16 03:29:03 | [diff] [blame] | 73 | #include "chrome/browser/ssl/ssl_manager.h" |
| 74 | #include "chrome/browser/ssl/ssl_blocking_page.h" |
[email protected] | b5558cf2 | 2010-07-12 17:30:06 | [diff] [blame] | 75 | #include "chrome/browser/tab_contents/infobar_delegate.h" |
[email protected] | 8982ab1 | 2010-08-31 17:39:42 | [diff] [blame] | 76 | #include "chrome/browser/tab_contents/navigation_entry.h" |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 77 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | 81af939 | 2009-04-21 02:37:45 | [diff] [blame] | 78 | #include "chrome/browser/tab_contents/tab_contents_view.h" |
[email protected] | 38b5a385 | 2010-07-21 06:49:52 | [diff] [blame] | 79 | #include "chrome/browser/translate/translate_infobar_delegate.h" |
[email protected] | 1caa152 | 2010-12-01 20:23:35 | [diff] [blame] | 80 | #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" |
| 81 | #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" |
[email protected] | 45300ad4 | 2010-12-02 15:51:14 | [diff] [blame] | 82 | #include "chrome/browser/ui/find_bar/find_bar.h" |
| 83 | #include "chrome/browser/ui/find_bar/find_bar_controller.h" |
| 84 | #include "chrome/browser/ui/find_bar/find_notification_details.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 85 | #include "chrome/browser/ui/login/login_prompt.h" |
[email protected] | 6a3ec231 | 2010-12-02 19:30:19 | [diff] [blame] | 86 | #include "chrome/browser/ui/omnibox/location_bar.h" |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 87 | #include "chrome/common/automation_constants.h" |
[email protected] | a8ba636 | 2010-11-10 20:02:25 | [diff] [blame] | 88 | #include "chrome/common/automation_messages.h" |
[email protected] | a9ff2c0 | 2010-05-13 17:33:05 | [diff] [blame] | 89 | #include "chrome/common/chrome_constants.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | #include "chrome/common/chrome_paths.h" |
[email protected] | c984d9f | 2010-07-20 20:52:20 | [diff] [blame] | 91 | #include "chrome/common/chrome_switches.h" |
[email protected] | 1eeb5e0 | 2010-07-20 23:02:11 | [diff] [blame] | 92 | #include "chrome/common/chrome_version_info.h" |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 93 | #include "chrome/common/extensions/extension.h" |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 94 | #include "chrome/common/json_value_serializer.h" |
[email protected] | 68d2a05f | 2010-05-07 21:39:55 | [diff] [blame] | 95 | #include "chrome/common/net/url_request_context_getter.h" |
[email protected] | 1bb5f89 | 2009-10-06 01:44:57 | [diff] [blame] | 96 | #include "chrome/common/pref_names.h" |
[email protected] | f5bf8ccf | 2010-02-05 18:19:25 | [diff] [blame] | 97 | #include "chrome/common/url_constants.h" |
[email protected] | 1bb5f89 | 2009-10-06 01:44:57 | [diff] [blame] | 98 | #include "chrome/test/automation/tab_proxy.h" |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 99 | #include "net/proxy/proxy_service.h" |
| 100 | #include "net/proxy/proxy_config_service_fixed.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 101 | #include "net/url_request/url_request_context.h" |
[email protected] | 1b5a48c | 2010-04-29 23:08:30 | [diff] [blame] | 102 | #include "chrome/browser/automation/ui_controls.h" |
[email protected] | 9a08bcf | 2009-08-12 19:56:28 | [diff] [blame] | 103 | #include "views/event.h" |
[email protected] | 5bcfe167 | 2010-07-16 20:51:57 | [diff] [blame] | 104 | #include "webkit/glue/password_form.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | |
[email protected] | de246f5 | 2009-02-25 18:25:45 | [diff] [blame] | 106 | #if defined(OS_WIN) |
[email protected] | 4bdde60 | 2010-06-16 03:17:35 | [diff] [blame] | 107 | #include "chrome/browser/external_tab_container_win.h" |
[email protected] | de246f5 | 2009-02-25 18:25:45 | [diff] [blame] | 108 | #endif // defined(OS_WIN) |
| 109 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 110 | using base::Time; |
| 111 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | AutomationProvider::AutomationProvider(Profile* profile) |
[email protected] | c97080f | 2010-08-20 20:42:48 | [diff] [blame] | 113 | : profile_(profile), |
[email protected] | dfe0deb | 2010-12-23 20:40:16 | [diff] [blame] | 114 | reply_message_(NULL), |
| 115 | is_connected_(false), |
| 116 | initial_loads_complete_(false) { |
[email protected] | a872ea1f | 2010-08-11 04:45:33 | [diff] [blame] | 117 | TRACE_EVENT_BEGIN("AutomationProvider::AutomationProvider", 0, ""); |
| 118 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | browser_tracker_.reset(new AutomationBrowserTracker(this)); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 120 | extension_tracker_.reset(new AutomationExtensionTracker(this)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | tab_tracker_.reset(new AutomationTabTracker(this)); |
[email protected] | 0e9f4ee | 2009-04-08 01:44:20 | [diff] [blame] | 122 | window_tracker_.reset(new AutomationWindowTracker(this)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | autocomplete_edit_tracker_.reset( |
| 124 | new AutomationAutocompleteEditTracker(this)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | new_tab_ui_load_observer_.reset(new NewTabUILoadObserver(this)); |
[email protected] | 4203e6b | 2010-11-12 21:41:50 | [diff] [blame] | 126 | dom_operation_observer_.reset(new DomOperationMessageSender(this)); |
[email protected] | 84abba6 | 2009-10-07 17:01:44 | [diff] [blame] | 127 | metric_event_duration_observer_.reset(new MetricEventDurationObserver()); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 128 | extension_test_result_observer_.reset( |
| 129 | new ExtensionTestResultNotificationObserver(this)); |
[email protected] | 528211a | 2010-01-14 15:25:13 | [diff] [blame] | 130 | g_browser_process->AddRefModule(); |
[email protected] | a872ea1f | 2010-08-11 04:45:33 | [diff] [blame] | 131 | |
| 132 | TRACE_EVENT_END("AutomationProvider::AutomationProvider", 0, ""); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | AutomationProvider::~AutomationProvider() { |
[email protected] | f44265b | 2009-05-19 18:52:50 | [diff] [blame] | 136 | STLDeleteContainerPairSecondPointers(port_containers_.begin(), |
| 137 | port_containers_.end()); |
| 138 | port_containers_.clear(); |
| 139 | |
[email protected] | 0da050b9 | 2008-08-19 19:29:47 | [diff] [blame] | 140 | // Make sure that any outstanding NotificationObservers also get destroyed. |
| 141 | ObserverList<NotificationObserver>::Iterator it(notification_observer_list_); |
[email protected] | 5a52f16 | 2008-08-27 04:15:31 | [diff] [blame] | 142 | NotificationObserver* observer; |
[email protected] | 0da050b9 | 2008-08-19 19:29:47 | [diff] [blame] | 143 | while ((observer = it.GetNext()) != NULL) |
| 144 | delete observer; |
[email protected] | 528211a | 2010-01-14 15:25:13 | [diff] [blame] | 145 | |
| 146 | if (channel_.get()) { |
| 147 | channel_->Close(); |
| 148 | } |
| 149 | g_browser_process->ReleaseModule(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | dfe0deb | 2010-12-23 20:40:16 | [diff] [blame] | 152 | bool AutomationProvider::InitializeChannel(const std::string& channel_id) { |
| 153 | TRACE_EVENT_BEGIN("AutomationProvider::InitializeChannel", 0, ""); |
| 154 | |
| 155 | std::string effective_channel_id = channel_id; |
| 156 | |
| 157 | // If the channel_id starts with kNamedInterfacePrefix, create a named IPC |
| 158 | // server and listen on it, else connect as client to an existing IPC server |
| 159 | bool use_named_interface = |
| 160 | channel_id.find(automation::kNamedInterfacePrefix) == 0; |
| 161 | if (use_named_interface) { |
| 162 | effective_channel_id = channel_id.substr( |
| 163 | strlen(automation::kNamedInterfacePrefix)); |
| 164 | if (effective_channel_id.length() <= 0) |
| 165 | return false; |
| 166 | } |
[email protected] | a872ea1f | 2010-08-11 04:45:33 | [diff] [blame] | 167 | |
[email protected] | b58dd3e | 2010-10-12 21:44:35 | [diff] [blame] | 168 | if (!automation_resource_message_filter_.get()) { |
| 169 | automation_resource_message_filter_ = new AutomationResourceMessageFilter; |
| 170 | } |
| 171 | |
[email protected] | dfe0deb | 2010-12-23 20:40:16 | [diff] [blame] | 172 | channel_.reset(new IPC::SyncChannel( |
| 173 | effective_channel_id, |
| 174 | use_named_interface ? IPC::Channel::MODE_NAMED_SERVER |
| 175 | : IPC::Channel::MODE_CLIENT, |
| 176 | this, |
| 177 | g_browser_process->io_thread()->message_loop(), |
| 178 | true, g_browser_process->shutdown_event())); |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 179 | channel_->AddFilter(automation_resource_message_filter_); |
[email protected] | c6cb199 | 2009-04-13 16:45:29 | [diff] [blame] | 180 | |
[email protected] | dfe0deb | 2010-12-23 20:40:16 | [diff] [blame] | 181 | TRACE_EVENT_END("AutomationProvider::InitializeChannel", 0, ""); |
[email protected] | a872ea1f | 2010-08-11 04:45:33 | [diff] [blame] | 182 | |
[email protected] | dfe0deb | 2010-12-23 20:40:16 | [diff] [blame] | 183 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 22a9128 | 2010-10-26 04:37:20 | [diff] [blame] | 186 | std::string AutomationProvider::GetProtocolVersion() { |
| 187 | chrome::VersionInfo version_info; |
| 188 | return version_info.Version().c_str(); |
| 189 | } |
| 190 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 191 | void AutomationProvider::SetExpectedTabCount(size_t expected_tabs) { |
[email protected] | dfe0deb | 2010-12-23 20:40:16 | [diff] [blame] | 192 | if (expected_tabs == 0) |
| 193 | OnInitialLoadsComplete(); |
| 194 | else |
[email protected] | 713a5f2 | 2010-12-11 11:37:41 | [diff] [blame] | 195 | initial_load_observer_.reset(new InitialLoadObserver(expected_tabs, this)); |
[email protected] | dfe0deb | 2010-12-23 20:40:16 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void AutomationProvider::OnInitialLoadsComplete() { |
| 199 | initial_loads_complete_ = true; |
| 200 | if (is_connected_) |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 201 | Send(new AutomationMsg_InitialLoadsComplete()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | NotificationObserver* AutomationProvider::AddNavigationStatusListener( |
[email protected] | 2e028a08 | 2009-08-19 20:32:58 | [diff] [blame] | 205 | NavigationController* tab, IPC::Message* reply_message, |
[email protected] | 7dad3d5f | 2010-03-04 00:27:01 | [diff] [blame] | 206 | int number_of_navigations, bool include_current_navigation) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | NotificationObserver* observer = |
[email protected] | 2e028a08 | 2009-08-19 20:32:58 | [diff] [blame] | 208 | new NavigationNotificationObserver(tab, this, reply_message, |
[email protected] | 7dad3d5f | 2010-03-04 00:27:01 | [diff] [blame] | 209 | number_of_navigations, |
| 210 | include_current_navigation); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 211 | |
[email protected] | 71f65dd | 2009-02-11 19:14:56 | [diff] [blame] | 212 | notification_observer_list_.AddObserver(observer); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 213 | return observer; |
| 214 | } |
| 215 | |
[email protected] | faf2ee4 | 2010-05-11 14:26:17 | [diff] [blame] | 216 | void AutomationProvider::RemoveNavigationStatusListener( |
| 217 | NotificationObserver* obs) { |
| 218 | notification_observer_list_.RemoveObserver(obs); |
| 219 | } |
| 220 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 221 | NotificationObserver* AutomationProvider::AddTabStripObserver( |
[email protected] | 1c58a5c | 2009-05-21 18:47:14 | [diff] [blame] | 222 | Browser* parent, |
| 223 | IPC::Message* reply_message) { |
[email protected] | 71f65dd | 2009-02-11 19:14:56 | [diff] [blame] | 224 | NotificationObserver* observer = |
[email protected] | 1c58a5c | 2009-05-21 18:47:14 | [diff] [blame] | 225 | new TabAppendedNotificationObserver(parent, this, reply_message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 226 | notification_observer_list_.AddObserver(observer); |
| 227 | |
| 228 | return observer; |
| 229 | } |
| 230 | |
[email protected] | faf2ee4 | 2010-05-11 14:26:17 | [diff] [blame] | 231 | void AutomationProvider::RemoveTabStripObserver(NotificationObserver* obs) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | notification_observer_list_.RemoveObserver(obs); |
| 233 | } |
| 234 | |
| 235 | void AutomationProvider::AddLoginHandler(NavigationController* tab, |
| 236 | LoginHandler* handler) { |
| 237 | login_handler_map_[tab] = handler; |
| 238 | } |
| 239 | |
| 240 | void AutomationProvider::RemoveLoginHandler(NavigationController* tab) { |
| 241 | DCHECK(login_handler_map_[tab]); |
| 242 | login_handler_map_.erase(tab); |
| 243 | } |
| 244 | |
[email protected] | f44265b | 2009-05-19 18:52:50 | [diff] [blame] | 245 | void AutomationProvider::AddPortContainer(ExtensionPortContainer* port) { |
| 246 | int port_id = port->port_id(); |
| 247 | DCHECK_NE(-1, port_id); |
| 248 | DCHECK(port_containers_.find(port_id) == port_containers_.end()); |
| 249 | |
| 250 | port_containers_[port_id] = port; |
| 251 | } |
| 252 | |
| 253 | void AutomationProvider::RemovePortContainer(ExtensionPortContainer* port) { |
| 254 | int port_id = port->port_id(); |
| 255 | DCHECK_NE(-1, port_id); |
| 256 | |
| 257 | PortContainerMap::iterator it = port_containers_.find(port_id); |
| 258 | DCHECK(it != port_containers_.end()); |
| 259 | |
| 260 | if (it != port_containers_.end()) { |
| 261 | delete it->second; |
| 262 | port_containers_.erase(it); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | ExtensionPortContainer* AutomationProvider::GetPortContainer( |
| 267 | int port_id) const { |
| 268 | PortContainerMap::const_iterator it = port_containers_.find(port_id); |
| 269 | if (it == port_containers_.end()) |
| 270 | return NULL; |
| 271 | |
| 272 | return it->second; |
| 273 | } |
| 274 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 275 | int AutomationProvider::GetIndexForNavigationController( |
| 276 | const NavigationController* controller, const Browser* parent) const { |
| 277 | DCHECK(parent); |
[email protected] | 902cdf77 | 2009-05-06 15:08:12 | [diff] [blame] | 278 | return parent->GetIndexOfController(controller); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 281 | int AutomationProvider::AddExtension(const Extension* extension) { |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 282 | DCHECK(extension); |
| 283 | return extension_tracker_->Add(extension); |
| 284 | } |
| 285 | |
[email protected] | e51cb6f | 2010-09-02 23:51:45 | [diff] [blame] | 286 | // TODO(phajdan.jr): move to TestingAutomationProvider. |
| 287 | DictionaryValue* AutomationProvider::GetDictionaryFromDownloadItem( |
| 288 | const DownloadItem* download) { |
| 289 | std::map<DownloadItem::DownloadState, std::string> state_to_string; |
| 290 | state_to_string[DownloadItem::IN_PROGRESS] = std::string("IN_PROGRESS"); |
| 291 | state_to_string[DownloadItem::CANCELLED] = std::string("CANCELLED"); |
| 292 | state_to_string[DownloadItem::REMOVING] = std::string("REMOVING"); |
| 293 | state_to_string[DownloadItem::COMPLETE] = std::string("COMPLETE"); |
| 294 | |
| 295 | std::map<DownloadItem::SafetyState, std::string> safety_state_to_string; |
| 296 | safety_state_to_string[DownloadItem::SAFE] = std::string("SAFE"); |
| 297 | safety_state_to_string[DownloadItem::DANGEROUS] = std::string("DANGEROUS"); |
| 298 | safety_state_to_string[DownloadItem::DANGEROUS_BUT_VALIDATED] = |
| 299 | std::string("DANGEROUS_BUT_VALIDATED"); |
| 300 | |
| 301 | DictionaryValue* dl_item_value = new DictionaryValue; |
| 302 | dl_item_value->SetInteger("id", static_cast<int>(download->id())); |
| 303 | dl_item_value->SetString("url", download->url().spec()); |
| 304 | dl_item_value->SetString("referrer_url", download->referrer_url().spec()); |
[email protected] | 44b94b8 | 2010-11-08 20:49:18 | [diff] [blame] | 305 | dl_item_value->SetString("file_name", |
| 306 | download->GetFileNameToReportUser().value()); |
| 307 | dl_item_value->SetString("full_path", |
| 308 | download->GetTargetFilePath().value()); |
[email protected] | e51cb6f | 2010-09-02 23:51:45 | [diff] [blame] | 309 | dl_item_value->SetBoolean("is_paused", download->is_paused()); |
| 310 | dl_item_value->SetBoolean("open_when_complete", |
| 311 | download->open_when_complete()); |
| 312 | dl_item_value->SetBoolean("is_extension_install", |
| 313 | download->is_extension_install()); |
| 314 | dl_item_value->SetBoolean("is_temporary", download->is_temporary()); |
| 315 | dl_item_value->SetBoolean("is_otr", download->is_otr()); // off-the-record |
| 316 | dl_item_value->SetString("state", state_to_string[download->state()]); |
| 317 | dl_item_value->SetString("safety_state", |
| 318 | safety_state_to_string[download->safety_state()]); |
| 319 | dl_item_value->SetInteger("PercentComplete", download->PercentComplete()); |
| 320 | |
| 321 | return dl_item_value; |
| 322 | } |
| 323 | |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 324 | const Extension* AutomationProvider::GetExtension(int extension_handle) { |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 325 | return extension_tracker_->GetResource(extension_handle); |
| 326 | } |
| 327 | |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 328 | const Extension* AutomationProvider::GetEnabledExtension(int extension_handle) { |
| 329 | const Extension* extension = |
| 330 | extension_tracker_->GetResource(extension_handle); |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 331 | ExtensionService* service = profile_->GetExtensionService(); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 332 | if (extension && service && |
| 333 | service->GetExtensionById(extension->id(), false)) |
| 334 | return extension; |
| 335 | return NULL; |
| 336 | } |
| 337 | |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 338 | const Extension* AutomationProvider::GetDisabledExtension( |
| 339 | int extension_handle) { |
| 340 | const Extension* extension = |
| 341 | extension_tracker_->GetResource(extension_handle); |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 342 | ExtensionService* service = profile_->GetExtensionService(); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 343 | if (extension && service && |
| 344 | service->GetExtensionById(extension->id(), true) && |
| 345 | !service->GetExtensionById(extension->id(), false)) |
| 346 | return extension; |
| 347 | return NULL; |
| 348 | } |
| 349 | |
[email protected] | dfe0deb | 2010-12-23 20:40:16 | [diff] [blame] | 350 | void AutomationProvider::OnChannelConnected(int pid) { |
| 351 | is_connected_ = true; |
| 352 | LOG(INFO) << "Testing channel connected, sending hello message"; |
| 353 | |
| 354 | // Send a hello message with our current automation protocol version. |
| 355 | chrome::VersionInfo version_info; |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 356 | channel_->Send(new AutomationMsg_Hello(version_info.Version())); |
[email protected] | dfe0deb | 2010-12-23 20:40:16 | [diff] [blame] | 357 | if (initial_loads_complete_) |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 358 | Send(new AutomationMsg_InitialLoadsComplete()); |
[email protected] | dfe0deb | 2010-12-23 20:40:16 | [diff] [blame] | 359 | } |
| 360 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 361 | bool AutomationProvider::OnMessageReceived(const IPC::Message& message) { |
| 362 | bool handled = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 363 | IPC_BEGIN_MESSAGE_MAP(AutomationProvider, message) |
[email protected] | 1b5a48c | 2010-04-29 23:08:30 | [diff] [blame] | 364 | #if !defined(OS_MACOSX) |
[email protected] | 71f65dd | 2009-02-11 19:14:56 | [diff] [blame] | 365 | IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WindowDrag, |
| 366 | WindowSimulateDrag) |
[email protected] | 1b5a48c | 2010-04-29 23:08:30 | [diff] [blame] | 367 | #endif // !defined(OS_MACOSX) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 368 | IPC_MESSAGE_HANDLER(AutomationMsg_HandleUnused, HandleUnused) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 369 | IPC_MESSAGE_HANDLER(AutomationMsg_SetProxyConfig, SetProxyConfig) |
[email protected] | d301c95 | 2009-07-13 15:02:41 | [diff] [blame] | 370 | IPC_MESSAGE_HANDLER(AutomationMsg_PrintAsync, PrintAsync) |
[email protected] | 1c58a5c | 2009-05-21 18:47:14 | [diff] [blame] | 371 | IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_Find, HandleFindRequest) |
[email protected] | 1c58a5c | 2009-05-21 18:47:14 | [diff] [blame] | 372 | IPC_MESSAGE_HANDLER(AutomationMsg_OverrideEncoding, OverrideEncoding) |
[email protected] | f7a6843 | 2009-07-29 23:18:19 | [diff] [blame] | 373 | IPC_MESSAGE_HANDLER(AutomationMsg_SelectAll, SelectAll) |
| 374 | IPC_MESSAGE_HANDLER(AutomationMsg_Cut, Cut) |
| 375 | IPC_MESSAGE_HANDLER(AutomationMsg_Copy, Copy) |
| 376 | IPC_MESSAGE_HANDLER(AutomationMsg_Paste, Paste) |
| 377 | IPC_MESSAGE_HANDLER(AutomationMsg_ReloadAsync, ReloadAsync) |
| 378 | IPC_MESSAGE_HANDLER(AutomationMsg_StopAsync, StopAsync) |
[email protected] | 1bb5f89 | 2009-10-06 01:44:57 | [diff] [blame] | 379 | IPC_MESSAGE_HANDLER(AutomationMsg_SetPageFontSize, OnSetPageFontSize) |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 380 | IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_InstallExtension, |
| 381 | InstallExtension) |
| 382 | IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoadExpandedExtension, |
| 383 | LoadExpandedExtension) |
[email protected] | a1e62d1 | 2010-03-16 02:18:43 | [diff] [blame] | 384 | IPC_MESSAGE_HANDLER(AutomationMsg_GetEnabledExtensions, |
| 385 | GetEnabledExtensions) |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 386 | IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_WaitForExtensionTestResult, |
| 387 | WaitForExtensionTestResult) |
| 388 | IPC_MESSAGE_HANDLER_DELAY_REPLY( |
| 389 | AutomationMsg_InstallExtensionAndGetHandle, |
| 390 | InstallExtensionAndGetHandle) |
| 391 | IPC_MESSAGE_HANDLER(AutomationMsg_UninstallExtension, |
| 392 | UninstallExtension) |
| 393 | IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_EnableExtension, |
| 394 | EnableExtension) |
| 395 | IPC_MESSAGE_HANDLER(AutomationMsg_DisableExtension, |
| 396 | DisableExtension) |
| 397 | IPC_MESSAGE_HANDLER_DELAY_REPLY( |
| 398 | AutomationMsg_ExecuteExtensionActionInActiveTabAsync, |
| 399 | ExecuteExtensionActionInActiveTabAsync) |
| 400 | IPC_MESSAGE_HANDLER(AutomationMsg_MoveExtensionBrowserAction, |
| 401 | MoveExtensionBrowserAction) |
| 402 | IPC_MESSAGE_HANDLER(AutomationMsg_GetExtensionProperty, |
| 403 | GetExtensionProperty) |
[email protected] | 673fd2c0 | 2010-02-04 23:10:00 | [diff] [blame] | 404 | IPC_MESSAGE_HANDLER(AutomationMsg_SaveAsAsync, SaveAsAsync) |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 405 | IPC_MESSAGE_HANDLER(AutomationMsg_RemoveBrowsingData, RemoveBrowsingData) |
[email protected] | 52415f84 | 2010-06-10 21:51:52 | [diff] [blame] | 406 | #if defined(OS_WIN) |
| 407 | // These are for use with external tabs. |
| 408 | IPC_MESSAGE_HANDLER(AutomationMsg_CreateExternalTab, CreateExternalTab) |
| 409 | IPC_MESSAGE_HANDLER(AutomationMsg_ProcessUnhandledAccelerator, |
| 410 | ProcessUnhandledAccelerator) |
| 411 | IPC_MESSAGE_HANDLER(AutomationMsg_SetInitialFocus, SetInitialFocus) |
| 412 | IPC_MESSAGE_HANDLER(AutomationMsg_TabReposition, OnTabReposition) |
| 413 | IPC_MESSAGE_HANDLER(AutomationMsg_ForwardContextMenuCommandToChrome, |
| 414 | OnForwardContextMenuCommandToChrome) |
| 415 | IPC_MESSAGE_HANDLER(AutomationMsg_NavigateInExternalTab, |
| 416 | NavigateInExternalTab) |
| 417 | IPC_MESSAGE_HANDLER(AutomationMsg_NavigateExternalTabAtIndex, |
| 418 | NavigateExternalTabAtIndex) |
| 419 | IPC_MESSAGE_HANDLER(AutomationMsg_ConnectExternalTab, ConnectExternalTab) |
| 420 | IPC_MESSAGE_HANDLER(AutomationMsg_SetEnableExtensionAutomation, |
| 421 | SetEnableExtensionAutomation) |
| 422 | IPC_MESSAGE_HANDLER(AutomationMsg_HandleMessageFromExternalHost, |
| 423 | OnMessageFromExternalHost) |
[email protected] | bdd5a9c9 | 2010-06-14 18:21:00 | [diff] [blame] | 424 | IPC_MESSAGE_HANDLER(AutomationMsg_BrowserMove, OnBrowserMoved) |
[email protected] | d9d8f0c | 2010-09-17 21:47:16 | [diff] [blame] | 425 | IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_RunUnloadHandlers, |
| 426 | OnRunUnloadHandlers) |
[email protected] | 7f860dd8 | 2010-08-09 23:18:05 | [diff] [blame] | 427 | IPC_MESSAGE_HANDLER(AutomationMsg_SetZoomLevel, OnSetZoomLevel) |
[email protected] | bdd5a9c9 | 2010-06-14 18:21:00 | [diff] [blame] | 428 | #endif // defined(OS_WIN) |
| 429 | #if defined(OS_CHROMEOS) |
| 430 | IPC_MESSAGE_HANDLER_DELAY_REPLY(AutomationMsg_LoginWithUserAndPass, |
| 431 | LoginWithUserAndPass) |
| 432 | #endif // defined(OS_CHROMEOS) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 433 | IPC_MESSAGE_UNHANDLED(handled = false;OnUnhandledMessage()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 434 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 435 | return handled; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 436 | } |
| 437 | |
[email protected] | 89c75df3 | 2010-09-14 16:45:34 | [diff] [blame] | 438 | void AutomationProvider::OnUnhandledMessage() { |
| 439 | // We should not hang here. Print a message to indicate what's going on, |
| 440 | // and disconnect the channel to notify the caller about the error |
| 441 | // in a way it can't ignore, and make any further attempts to send |
| 442 | // messages fail fast. |
| 443 | LOG(ERROR) << "AutomationProvider received a message it can't handle. " |
| 444 | << "Please make sure that you use switches::kTestingChannelID " |
| 445 | << "for test code (TestingAutomationProvider), and " |
| 446 | << "switches::kAutomationClientChannelID for everything else " |
| 447 | << "(like ChromeFrame). Closing the automation channel."; |
| 448 | channel_->Close(); |
| 449 | } |
| 450 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 451 | // This task just adds another task to the event queue. This is useful if |
| 452 | // you want to ensure that any tasks added to the event queue after this one |
| 453 | // have already been processed by the time |task| is run. |
| 454 | class InvokeTaskLaterTask : public Task { |
| 455 | public: |
| 456 | explicit InvokeTaskLaterTask(Task* task) : task_(task) {} |
| 457 | virtual ~InvokeTaskLaterTask() {} |
| 458 | |
| 459 | virtual void Run() { |
| 460 | MessageLoop::current()->PostTask(FROM_HERE, task_); |
| 461 | } |
| 462 | |
| 463 | private: |
| 464 | Task* task_; |
| 465 | |
| 466 | DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask); |
| 467 | }; |
| 468 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 469 | void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) { |
| 470 | if (window_tracker_->ContainsHandle(handle)) { |
| 471 | window_tracker_->Remove(window_tracker_->GetResource(handle)); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | void AutomationProvider::OnChannelError() { |
[email protected] | 5fb40dc | 2010-10-22 20:35:27 | [diff] [blame] | 476 | VLOG(1) << "AutomationProxy went away, shutting down app."; |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 477 | AutomationProviderList::GetInstance()->RemoveProvider(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 478 | } |
| 479 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 480 | bool AutomationProvider::Send(IPC::Message* msg) { |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 481 | DCHECK(channel_.get()); |
| 482 | return channel_->Send(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | Browser* AutomationProvider::FindAndActivateTab( |
| 486 | NavigationController* controller) { |
| 487 | int tab_index; |
| 488 | Browser* browser = Browser::GetBrowserForController(controller, &tab_index); |
| 489 | if (browser) |
| 490 | browser->SelectTabContentsAt(tab_index, true); |
| 491 | |
| 492 | return browser; |
| 493 | } |
| 494 | |
[email protected] | 4f99913 | 2009-03-31 18:08:40 | [diff] [blame] | 495 | void AutomationProvider::HandleFindRequest( |
| 496 | int handle, |
| 497 | const AutomationMsg_Find_Params& params, |
| 498 | IPC::Message* reply_message) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 499 | if (!tab_tracker_->ContainsHandle(handle)) { |
[email protected] | f9e3c52 | 2010-10-08 00:41:26 | [diff] [blame] | 500 | AutomationMsg_Find::WriteReplyParams(reply_message, -1, -1); |
[email protected] | 71f65dd | 2009-02-11 19:14:56 | [diff] [blame] | 501 | Send(reply_message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 502 | return; |
| 503 | } |
| 504 | |
| 505 | NavigationController* nav = tab_tracker_->GetResource(handle); |
[email protected] | 7f0005a | 2009-04-15 03:25:11 | [diff] [blame] | 506 | TabContents* tab_contents = nav->tab_contents(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 507 | |
[email protected] | f9e3c52 | 2010-10-08 00:41:26 | [diff] [blame] | 508 | SendFindRequest(tab_contents, |
| 509 | false, |
| 510 | params.search_string, |
| 511 | params.forward, |
| 512 | params.match_case, |
| 513 | params.find_next, |
| 514 | reply_message); |
| 515 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 516 | |
[email protected] | f9e3c52 | 2010-10-08 00:41:26 | [diff] [blame] | 517 | void AutomationProvider::SendFindRequest( |
| 518 | TabContents* tab_contents, |
| 519 | bool with_json, |
| 520 | const string16& search_string, |
| 521 | bool forward, |
| 522 | bool match_case, |
| 523 | bool find_next, |
| 524 | IPC::Message* reply_message) { |
| 525 | int request_id = FindInPageNotificationObserver::kFindInPageRequestId; |
[email protected] | 7694f5e4 | 2010-10-18 22:41:17 | [diff] [blame] | 526 | FindInPageNotificationObserver* observer = |
[email protected] | f9e3c52 | 2010-10-08 00:41:26 | [diff] [blame] | 527 | new FindInPageNotificationObserver(this, |
| 528 | tab_contents, |
| 529 | with_json, |
[email protected] | 7694f5e4 | 2010-10-18 22:41:17 | [diff] [blame] | 530 | reply_message); |
| 531 | if (!with_json) { |
| 532 | find_in_page_observer_.reset(observer); |
| 533 | } |
[email protected] | f9e3c52 | 2010-10-08 00:41:26 | [diff] [blame] | 534 | tab_contents->set_current_find_request_id(request_id); |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 535 | tab_contents->render_view_host()->StartFinding( |
| 536 | FindInPageNotificationObserver::kFindInPageRequestId, |
[email protected] | f9e3c52 | 2010-10-08 00:41:26 | [diff] [blame] | 537 | search_string, |
| 538 | forward, |
| 539 | match_case, |
| 540 | find_next); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 541 | } |
| 542 | |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 543 | class SetProxyConfigTask : public Task { |
| 544 | public: |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 545 | SetProxyConfigTask(URLRequestContextGetter* request_context_getter, |
| 546 | const std::string& new_proxy_config) |
[email protected] | 2aa336e | 2010-04-06 21:05:25 | [diff] [blame] | 547 | : request_context_getter_(request_context_getter), |
| 548 | proxy_config_(new_proxy_config) {} |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 549 | virtual void Run() { |
| 550 | // First, deserialize the JSON string. If this fails, log and bail. |
| 551 | JSONStringValueSerializer deserializer(proxy_config_); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 552 | std::string error_msg; |
| 553 | scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg)); |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 554 | if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) { |
| 555 | DLOG(WARNING) << "Received bad JSON string for ProxyConfig: " |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 556 | << error_msg; |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 557 | return; |
| 558 | } |
| 559 | |
| 560 | scoped_ptr<DictionaryValue> dict( |
| 561 | static_cast<DictionaryValue*>(root.release())); |
| 562 | // Now put together a proxy configuration from the deserialized string. |
| 563 | net::ProxyConfig pc; |
| 564 | PopulateProxyConfig(*dict.get(), &pc); |
| 565 | |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 566 | net::ProxyService* proxy_service = |
| 567 | request_context_getter_->GetURLRequestContext()->proxy_service(); |
| 568 | DCHECK(proxy_service); |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 569 | scoped_ptr<net::ProxyConfigService> proxy_config_service( |
| 570 | new net::ProxyConfigServiceFixed(pc)); |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 571 | proxy_service->ResetConfigService(proxy_config_service.release()); |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) { |
| 575 | DCHECK(pc); |
| 576 | bool no_proxy = false; |
| 577 | if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) { |
| 578 | // Make no changes to the ProxyConfig. |
| 579 | return; |
| 580 | } |
| 581 | bool auto_config; |
| 582 | if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) { |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 583 | pc->set_auto_detect(true); |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 584 | } |
| 585 | std::string pac_url; |
| 586 | if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) { |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 587 | pc->set_pac_url(GURL(pac_url)); |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 588 | } |
| 589 | std::string proxy_bypass_list; |
| 590 | if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) { |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 591 | pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list); |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 592 | } |
| 593 | std::string proxy_server; |
| 594 | if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) { |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 595 | pc->proxy_rules().ParseFromString(proxy_server); |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
| 599 | private: |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 600 | scoped_refptr<URLRequestContextGetter> request_context_getter_; |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 601 | std::string proxy_config_; |
| 602 | }; |
| 603 | |
| 604 | |
| 605 | void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) { |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 606 | URLRequestContextGetter* context_getter = Profile::GetDefaultRequestContext(); |
| 607 | if (!context_getter) { |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 608 | FilePath user_data_dir; |
| 609 | PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 610 | ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 611 | DCHECK(profile_manager); |
| 612 | Profile* profile = profile_manager->GetDefaultProfile(user_data_dir); |
| 613 | DCHECK(profile); |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 614 | context_getter = profile->GetRequestContext(); |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 615 | } |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 616 | DCHECK(context_getter); |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 617 | |
[email protected] | e0d586c | 2010-10-06 03:57:24 | [diff] [blame] | 618 | BrowserThread::PostTask( |
| 619 | BrowserThread::IO, FROM_HERE, |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 620 | new SetProxyConfigTask(context_getter, new_proxy_config)); |
[email protected] | a7eee32f | 2009-05-22 18:08:17 | [diff] [blame] | 621 | } |
| 622 | |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 623 | TabContents* AutomationProvider::GetTabContentsForHandle( |
[email protected] | 20e93d1 | 2008-08-28 16:31:57 | [diff] [blame] | 624 | int handle, NavigationController** tab) { |
[email protected] | 20e93d1 | 2008-08-28 16:31:57 | [diff] [blame] | 625 | if (tab_tracker_->ContainsHandle(handle)) { |
| 626 | NavigationController* nav_controller = tab_tracker_->GetResource(handle); |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 627 | if (tab) |
| 628 | *tab = nav_controller; |
| 629 | return nav_controller->tab_contents(); |
[email protected] | 20e93d1 | 2008-08-28 16:31:57 | [diff] [blame] | 630 | } |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 631 | return NULL; |
[email protected] | 20e93d1 | 2008-08-28 16:31:57 | [diff] [blame] | 632 | } |
| 633 | |
[email protected] | 97fa6ce3 | 2008-12-19 01:48:16 | [diff] [blame] | 634 | // Gets the current used encoding name of the page in the specified tab. |
[email protected] | 71f65dd | 2009-02-11 19:14:56 | [diff] [blame] | 635 | void AutomationProvider::OverrideEncoding(int tab_handle, |
[email protected] | 41fc032 | 2009-09-04 22:23:40 | [diff] [blame] | 636 | const std::string& encoding_name, |
[email protected] | 71f65dd | 2009-02-11 19:14:56 | [diff] [blame] | 637 | bool* success) { |
| 638 | *success = false; |
[email protected] | 97fa6ce3 | 2008-12-19 01:48:16 | [diff] [blame] | 639 | if (tab_tracker_->ContainsHandle(tab_handle)) { |
| 640 | NavigationController* nav = tab_tracker_->GetResource(tab_handle); |
[email protected] | 2f2afba | 2010-04-01 01:53:19 | [diff] [blame] | 641 | if (!nav) |
| 642 | return; |
[email protected] | 97fa6ce3 | 2008-12-19 01:48:16 | [diff] [blame] | 643 | Browser* browser = FindAndActivateTab(nav); |
[email protected] | 97fa6ce3 | 2008-12-19 01:48:16 | [diff] [blame] | 644 | |
[email protected] | 2f2afba | 2010-04-01 01:53:19 | [diff] [blame] | 645 | // If the browser has UI, simulate what a user would do. |
| 646 | // Activate the tab and then click the encoding menu. |
| 647 | if (browser && |
| 648 | browser->command_updater()->IsCommandEnabled(IDC_ENCODING_MENU)) { |
[email protected] | 97fa6ce3 | 2008-12-19 01:48:16 | [diff] [blame] | 649 | int selected_encoding_id = |
| 650 | CharacterEncoding::GetCommandIdByCanonicalEncodingName(encoding_name); |
| 651 | if (selected_encoding_id) { |
| 652 | browser->OverrideEncoding(selected_encoding_id); |
[email protected] | 71f65dd | 2009-02-11 19:14:56 | [diff] [blame] | 653 | *success = true; |
[email protected] | 97fa6ce3 | 2008-12-19 01:48:16 | [diff] [blame] | 654 | } |
[email protected] | 2f2afba | 2010-04-01 01:53:19 | [diff] [blame] | 655 | } else { |
| 656 | // There is no UI, Chrome probably runs as Chrome-Frame mode. |
| 657 | // Try to get TabContents and call its override_encoding method. |
| 658 | TabContents* contents = nav->tab_contents(); |
| 659 | if (!contents) |
| 660 | return; |
| 661 | const std::string selected_encoding = |
| 662 | CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding_name); |
| 663 | if (selected_encoding.empty()) |
| 664 | return; |
| 665 | contents->SetOverrideEncoding(selected_encoding); |
[email protected] | 97fa6ce3 | 2008-12-19 01:48:16 | [diff] [blame] | 666 | } |
| 667 | } |
[email protected] | 97fa6ce3 | 2008-12-19 01:48:16 | [diff] [blame] | 668 | } |
[email protected] | 5bcdb31 | 2009-01-07 21:43:20 | [diff] [blame] | 669 | |
[email protected] | f7a6843 | 2009-07-29 23:18:19 | [diff] [blame] | 670 | void AutomationProvider::SelectAll(int tab_handle) { |
| 671 | RenderViewHost* view = GetViewForTab(tab_handle); |
| 672 | if (!view) { |
| 673 | NOTREACHED(); |
| 674 | return; |
| 675 | } |
| 676 | |
| 677 | view->SelectAll(); |
| 678 | } |
| 679 | |
| 680 | void AutomationProvider::Cut(int tab_handle) { |
| 681 | RenderViewHost* view = GetViewForTab(tab_handle); |
| 682 | if (!view) { |
| 683 | NOTREACHED(); |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | view->Cut(); |
| 688 | } |
| 689 | |
| 690 | void AutomationProvider::Copy(int tab_handle) { |
| 691 | RenderViewHost* view = GetViewForTab(tab_handle); |
| 692 | if (!view) { |
| 693 | NOTREACHED(); |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | view->Copy(); |
| 698 | } |
| 699 | |
| 700 | void AutomationProvider::Paste(int tab_handle) { |
| 701 | RenderViewHost* view = GetViewForTab(tab_handle); |
| 702 | if (!view) { |
| 703 | NOTREACHED(); |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | view->Paste(); |
| 708 | } |
| 709 | |
| 710 | void AutomationProvider::ReloadAsync(int tab_handle) { |
| 711 | if (tab_tracker_->ContainsHandle(tab_handle)) { |
| 712 | NavigationController* tab = tab_tracker_->GetResource(tab_handle); |
| 713 | if (!tab) { |
| 714 | NOTREACHED(); |
| 715 | return; |
| 716 | } |
| 717 | |
[email protected] | 106a081 | 2010-03-18 00:15:12 | [diff] [blame] | 718 | const bool check_for_repost = true; |
| 719 | tab->Reload(check_for_repost); |
[email protected] | f7a6843 | 2009-07-29 23:18:19 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | void AutomationProvider::StopAsync(int tab_handle) { |
| 724 | RenderViewHost* view = GetViewForTab(tab_handle); |
| 725 | if (!view) { |
[email protected] | 8b2b331 | 2009-09-14 18:38:36 | [diff] [blame] | 726 | // We tolerate StopAsync being called even before a view has been created. |
| 727 | // So just log a warning instead of a NOTREACHED(). |
| 728 | DLOG(WARNING) << "StopAsync: no view for handle " << tab_handle; |
[email protected] | f7a6843 | 2009-07-29 23:18:19 | [diff] [blame] | 729 | return; |
| 730 | } |
| 731 | |
| 732 | view->Stop(); |
| 733 | } |
| 734 | |
[email protected] | 1bb5f89 | 2009-10-06 01:44:57 | [diff] [blame] | 735 | void AutomationProvider::OnSetPageFontSize(int tab_handle, |
| 736 | int font_size) { |
| 737 | AutomationPageFontSize automation_font_size = |
| 738 | static_cast<AutomationPageFontSize>(font_size); |
| 739 | |
| 740 | if (automation_font_size < SMALLEST_FONT || |
| 741 | automation_font_size > LARGEST_FONT) { |
| 742 | DLOG(ERROR) << "Invalid font size specified : " |
| 743 | << font_size; |
| 744 | return; |
| 745 | } |
| 746 | |
| 747 | if (tab_tracker_->ContainsHandle(tab_handle)) { |
| 748 | NavigationController* tab = tab_tracker_->GetResource(tab_handle); |
| 749 | DCHECK(tab != NULL); |
| 750 | if (tab && tab->tab_contents()) { |
| 751 | DCHECK(tab->tab_contents()->profile() != NULL); |
| 752 | tab->tab_contents()->profile()->GetPrefs()->SetInteger( |
| 753 | prefs::kWebKitDefaultFontSize, font_size); |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
[email protected] | bc73b4e5 | 2010-03-26 04:16:20 | [diff] [blame] | 758 | void AutomationProvider::RemoveBrowsingData(int remove_mask) { |
| 759 | BrowsingDataRemover* remover; |
| 760 | remover = new BrowsingDataRemover(profile(), |
| 761 | BrowsingDataRemover::EVERYTHING, // All time periods. |
| 762 | base::Time()); |
| 763 | remover->Remove(remove_mask); |
| 764 | // BrowsingDataRemover deletes itself. |
| 765 | } |
[email protected] | 1bb5f89 | 2009-10-06 01:44:57 | [diff] [blame] | 766 | |
[email protected] | f7a6843 | 2009-07-29 23:18:19 | [diff] [blame] | 767 | RenderViewHost* AutomationProvider::GetViewForTab(int tab_handle) { |
| 768 | if (tab_tracker_->ContainsHandle(tab_handle)) { |
| 769 | NavigationController* tab = tab_tracker_->GetResource(tab_handle); |
| 770 | if (!tab) { |
| 771 | NOTREACHED(); |
| 772 | return NULL; |
| 773 | } |
| 774 | |
| 775 | TabContents* tab_contents = tab->tab_contents(); |
| 776 | if (!tab_contents) { |
| 777 | NOTREACHED(); |
| 778 | return NULL; |
| 779 | } |
| 780 | |
| 781 | RenderViewHost* view_host = tab_contents->render_view_host(); |
| 782 | return view_host; |
| 783 | } |
| 784 | |
| 785 | return NULL; |
| 786 | } |
[email protected] | 675595f | 2009-08-26 22:32:04 | [diff] [blame] | 787 | |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 788 | void AutomationProvider::InstallExtension(const FilePath& crx_path, |
| 789 | IPC::Message* reply_message) { |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 790 | ExtensionService* service = profile_->GetExtensionService(); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 791 | if (service) { |
| 792 | // The observer will delete itself when done. |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 793 | new ExtensionInstallNotificationObserver(this, |
| 794 | AutomationMsg_InstallExtension::ID, |
| 795 | reply_message); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 796 | |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 797 | scoped_refptr<CrxInstaller> installer( |
[email protected] | bb46153 | 2010-11-26 21:50:23 | [diff] [blame] | 798 | new CrxInstaller(service, NULL)); // silent install, no UI |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 799 | installer->InstallCrx(crx_path); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 800 | } else { |
| 801 | AutomationMsg_InstallExtension::WriteReplyParams( |
| 802 | reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED); |
| 803 | Send(reply_message); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | void AutomationProvider::LoadExpandedExtension( |
| 808 | const FilePath& extension_dir, |
| 809 | IPC::Message* reply_message) { |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 810 | if (profile_->GetExtensionService()) { |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 811 | // The observer will delete itself when done. |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 812 | new ExtensionInstallNotificationObserver( |
| 813 | this, |
| 814 | AutomationMsg_LoadExpandedExtension::ID, |
| 815 | reply_message); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 816 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 817 | profile_->GetExtensionService()->LoadExtension(extension_dir); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 818 | } else { |
| 819 | AutomationMsg_LoadExpandedExtension::WriteReplyParams( |
| 820 | reply_message, AUTOMATION_MSG_EXTENSION_INSTALL_FAILED); |
| 821 | Send(reply_message); |
| 822 | } |
| 823 | } |
[email protected] | 673fd2c0 | 2010-02-04 23:10:00 | [diff] [blame] | 824 | |
[email protected] | a1e62d1 | 2010-03-16 02:18:43 | [diff] [blame] | 825 | void AutomationProvider::GetEnabledExtensions( |
| 826 | std::vector<FilePath>* result) { |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 827 | ExtensionService* service = profile_->GetExtensionService(); |
[email protected] | a1e62d1 | 2010-03-16 02:18:43 | [diff] [blame] | 828 | DCHECK(service); |
| 829 | if (service->extensions_enabled()) { |
| 830 | const ExtensionList* extensions = service->extensions(); |
| 831 | DCHECK(extensions); |
| 832 | for (size_t i = 0; i < extensions->size(); ++i) { |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 833 | const Extension* extension = (*extensions)[i]; |
[email protected] | a1e62d1 | 2010-03-16 02:18:43 | [diff] [blame] | 834 | DCHECK(extension); |
[email protected] | ad58842 | 2010-12-21 20:13:19 | [diff] [blame] | 835 | // AutomationProvider only exposes non app internal/loaded extensions. |
| 836 | if (!extension->is_app() && |
| 837 | (extension->location() == Extension::INTERNAL || |
| 838 | extension->location() == Extension::LOAD)) { |
[email protected] | 237f28167 | 2010-03-20 12:37:07 | [diff] [blame] | 839 | result->push_back(extension->path()); |
| 840 | } |
[email protected] | a1e62d1 | 2010-03-16 02:18:43 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 845 | void AutomationProvider::WaitForExtensionTestResult( |
| 846 | IPC::Message* reply_message) { |
| 847 | DCHECK(reply_message_ == NULL); |
| 848 | reply_message_ = reply_message; |
| 849 | // Call MaybeSendResult, because the result might have come in before |
| 850 | // we were waiting on it. |
| 851 | extension_test_result_observer_->MaybeSendResult(); |
| 852 | } |
| 853 | |
| 854 | void AutomationProvider::InstallExtensionAndGetHandle( |
[email protected] | d7e5525d | 2010-04-20 14:37:09 | [diff] [blame] | 855 | const FilePath& crx_path, bool with_ui, IPC::Message* reply_message) { |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 856 | ExtensionService* service = profile_->GetExtensionService(); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 857 | ExtensionProcessManager* manager = profile_->GetExtensionProcessManager(); |
| 858 | if (service && manager) { |
| 859 | // The observer will delete itself when done. |
| 860 | new ExtensionReadyNotificationObserver( |
| 861 | manager, |
| 862 | this, |
| 863 | AutomationMsg_InstallExtensionAndGetHandle::ID, |
| 864 | reply_message); |
| 865 | |
[email protected] | d7e5525d | 2010-04-20 14:37:09 | [diff] [blame] | 866 | ExtensionInstallUI* client = |
| 867 | (with_ui ? new ExtensionInstallUI(profile_) : NULL); |
[email protected] | bb46153 | 2010-11-26 21:50:23 | [diff] [blame] | 868 | scoped_refptr<CrxInstaller> installer(new CrxInstaller(service, client)); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 869 | installer->InstallCrx(crx_path); |
| 870 | } else { |
| 871 | AutomationMsg_InstallExtensionAndGetHandle::WriteReplyParams( |
| 872 | reply_message, 0); |
| 873 | Send(reply_message); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | void AutomationProvider::UninstallExtension(int extension_handle, |
| 878 | bool* success) { |
| 879 | *success = false; |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 880 | const Extension* extension = GetExtension(extension_handle); |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 881 | ExtensionService* service = profile_->GetExtensionService(); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 882 | if (extension && service) { |
| 883 | ExtensionUnloadNotificationObserver observer; |
| 884 | service->UninstallExtension(extension->id(), false); |
| 885 | // The extension unload notification should have been sent synchronously |
| 886 | // with the uninstall. Just to be safe, check that it was received. |
| 887 | *success = observer.did_receive_unload_notification(); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | void AutomationProvider::EnableExtension(int extension_handle, |
| 892 | IPC::Message* reply_message) { |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 893 | const Extension* extension = GetDisabledExtension(extension_handle); |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 894 | ExtensionService* service = profile_->GetExtensionService(); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 895 | ExtensionProcessManager* manager = profile_->GetExtensionProcessManager(); |
| 896 | // Only enable if this extension is disabled. |
| 897 | if (extension && service && manager) { |
| 898 | // The observer will delete itself when done. |
| 899 | new ExtensionReadyNotificationObserver( |
| 900 | manager, |
| 901 | this, |
| 902 | AutomationMsg_EnableExtension::ID, |
| 903 | reply_message); |
| 904 | service->EnableExtension(extension->id()); |
| 905 | } else { |
| 906 | AutomationMsg_EnableExtension::WriteReplyParams(reply_message, false); |
| 907 | Send(reply_message); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | void AutomationProvider::DisableExtension(int extension_handle, |
| 912 | bool* success) { |
| 913 | *success = false; |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 914 | const Extension* extension = GetEnabledExtension(extension_handle); |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 915 | ExtensionService* service = profile_->GetExtensionService(); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 916 | if (extension && service) { |
| 917 | ExtensionUnloadNotificationObserver observer; |
| 918 | service->DisableExtension(extension->id()); |
| 919 | // The extension unload notification should have been sent synchronously |
| 920 | // with the disable. Just to be safe, check that it was received. |
| 921 | *success = observer.did_receive_unload_notification(); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | void AutomationProvider::ExecuteExtensionActionInActiveTabAsync( |
| 926 | int extension_handle, int browser_handle, |
| 927 | IPC::Message* reply_message) { |
| 928 | bool success = false; |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 929 | const Extension* extension = GetEnabledExtension(extension_handle); |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 930 | ExtensionService* service = profile_->GetExtensionService(); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 931 | ExtensionMessageService* message_service = |
| 932 | profile_->GetExtensionMessageService(); |
| 933 | Browser* browser = browser_tracker_->GetResource(browser_handle); |
| 934 | if (extension && service && message_service && browser) { |
| 935 | int tab_id = ExtensionTabUtil::GetTabId(browser->GetSelectedTabContents()); |
| 936 | if (extension->page_action()) { |
| 937 | ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( |
| 938 | browser->profile(), extension->id(), "action", tab_id, "", 1); |
| 939 | success = true; |
| 940 | } else if (extension->browser_action()) { |
| 941 | ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted( |
| 942 | browser->profile(), extension->id(), browser); |
| 943 | success = true; |
| 944 | } |
| 945 | } |
| 946 | AutomationMsg_ExecuteExtensionActionInActiveTabAsync::WriteReplyParams( |
| 947 | reply_message, success); |
| 948 | Send(reply_message); |
| 949 | } |
| 950 | |
| 951 | void AutomationProvider::MoveExtensionBrowserAction( |
| 952 | int extension_handle, int index, bool* success) { |
| 953 | *success = false; |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 954 | const Extension* extension = GetEnabledExtension(extension_handle); |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 955 | ExtensionService* service = profile_->GetExtensionService(); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 956 | if (extension && service) { |
| 957 | ExtensionToolbarModel* toolbar = service->toolbar_model(); |
| 958 | if (toolbar) { |
| 959 | if (index >= 0 && index < static_cast<int>(toolbar->size())) { |
| 960 | toolbar->MoveBrowserAction(extension, index); |
| 961 | *success = true; |
| 962 | } else { |
| 963 | DLOG(WARNING) << "Attempted to move browser action to invalid index."; |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | void AutomationProvider::GetExtensionProperty( |
| 970 | int extension_handle, |
| 971 | AutomationMsg_ExtensionProperty type, |
| 972 | bool* success, |
| 973 | std::string* value) { |
| 974 | *success = false; |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 975 | const Extension* extension = GetExtension(extension_handle); |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 976 | ExtensionService* service = profile_->GetExtensionService(); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 977 | if (extension && service) { |
| 978 | ExtensionToolbarModel* toolbar = service->toolbar_model(); |
| 979 | int found_index = -1; |
| 980 | int index = 0; |
| 981 | switch (type) { |
| 982 | case AUTOMATION_MSG_EXTENSION_ID: |
| 983 | *value = extension->id(); |
| 984 | *success = true; |
| 985 | break; |
| 986 | case AUTOMATION_MSG_EXTENSION_NAME: |
| 987 | *value = extension->name(); |
| 988 | *success = true; |
| 989 | break; |
| 990 | case AUTOMATION_MSG_EXTENSION_VERSION: |
| 991 | *value = extension->VersionString(); |
| 992 | *success = true; |
| 993 | break; |
| 994 | case AUTOMATION_MSG_EXTENSION_BROWSER_ACTION_INDEX: |
| 995 | if (toolbar) { |
| 996 | for (ExtensionList::const_iterator iter = toolbar->begin(); |
| 997 | iter != toolbar->end(); iter++) { |
| 998 | // Skip this extension if we are in incognito mode |
| 999 | // and it is not incognito-enabled. |
| 1000 | if (profile_->IsOffTheRecord() && |
| 1001 | !service->IsIncognitoEnabled(*iter)) |
| 1002 | continue; |
| 1003 | if (*iter == extension) { |
| 1004 | found_index = index; |
| 1005 | break; |
| 1006 | } |
| 1007 | index++; |
| 1008 | } |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 1009 | *value = base::IntToString(found_index); |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 1010 | *success = true; |
| 1011 | } |
| 1012 | break; |
| 1013 | default: |
| 1014 | LOG(WARNING) << "Trying to get undefined extension property"; |
| 1015 | break; |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
[email protected] | 673fd2c0 | 2010-02-04 23:10:00 | [diff] [blame] | 1020 | void AutomationProvider::SaveAsAsync(int tab_handle) { |
| 1021 | NavigationController* tab = NULL; |
| 1022 | TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab); |
| 1023 | if (tab_contents) |
| 1024 | tab_contents->OnSavePage(); |
| 1025 | } |