| 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/automation/automation_provider.h" | 5 #include "chrome/browser/automation/automation_provider.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/callback.h" | 10 #include "base/callback.h" |
| 10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 11 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
| 12 #include "base/file_path.h" | 13 #include "base/file_path.h" |
| 13 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 14 #include "base/json/json_value_serializer.h" | 15 #include "base/json/json_value_serializer.h" |
| 15 #include "base/json/json_writer.h" | 16 #include "base/json/json_writer.h" |
| 16 #include "base/json/string_escape.h" | 17 #include "base/json/string_escape.h" |
| 17 #include "base/message_loop.h" | 18 #include "base/message_loop.h" |
| 18 #include "base/path_service.h" | 19 #include "base/path_service.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 #endif // defined(OS_WIN) | 100 #endif // defined(OS_WIN) |
| 100 | 101 |
| 101 #if defined(OS_CHROMEOS) | 102 #if defined(OS_CHROMEOS) |
| 102 #include "chrome/browser/chromeos/login/user_manager.h" | 103 #include "chrome/browser/chromeos/login/user_manager.h" |
| 103 #endif // defined(OS_CHROMEOS) | 104 #endif // defined(OS_CHROMEOS) |
| 104 | 105 |
| 105 using content::BrowserThread; | 106 using content::BrowserThread; |
| 106 using WebKit::WebFindOptions; | 107 using WebKit::WebFindOptions; |
| 107 using base::Time; | 108 using base::Time; |
| 108 | 109 |
| 110 namespace { |
| 111 |
| 112 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) { |
| 113 DCHECK(pc); |
| 114 bool no_proxy = false; |
| 115 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) { |
| 116 // Make no changes to the ProxyConfig. |
| 117 return; |
| 118 } |
| 119 bool auto_config; |
| 120 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) { |
| 121 pc->set_auto_detect(true); |
| 122 } |
| 123 std::string pac_url; |
| 124 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) { |
| 125 pc->set_pac_url(GURL(pac_url)); |
| 126 } |
| 127 bool pac_mandatory; |
| 128 if (dict.GetBoolean(automation::kJSONProxyPacMandatory, &pac_mandatory)) { |
| 129 pc->set_pac_mandatory(pac_mandatory); |
| 130 } |
| 131 std::string proxy_bypass_list; |
| 132 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) { |
| 133 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list); |
| 134 } |
| 135 std::string proxy_server; |
| 136 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) { |
| 137 pc->proxy_rules().ParseFromString(proxy_server); |
| 138 } |
| 139 } |
| 140 |
| 141 void SetProxyConfigCallback( |
| 142 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 143 const std::string& proxy_config) { |
| 144 // First, deserialize the JSON string. If this fails, log and bail. |
| 145 JSONStringValueSerializer deserializer(proxy_config); |
| 146 std::string error_msg; |
| 147 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg)); |
| 148 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) { |
| 149 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: " |
| 150 << error_msg; |
| 151 return; |
| 152 } |
| 153 |
| 154 scoped_ptr<DictionaryValue> dict( |
| 155 static_cast<DictionaryValue*>(root.release())); |
| 156 // Now put together a proxy configuration from the deserialized string. |
| 157 net::ProxyConfig pc; |
| 158 PopulateProxyConfig(*dict.get(), &pc); |
| 159 |
| 160 net::ProxyService* proxy_service = |
| 161 request_context_getter->GetURLRequestContext()->proxy_service(); |
| 162 DCHECK(proxy_service); |
| 163 scoped_ptr<net::ProxyConfigService> proxy_config_service( |
| 164 new net::ProxyConfigServiceFixed(pc)); |
| 165 proxy_service->ResetConfigService(proxy_config_service.release()); |
| 166 } |
| 167 |
| 168 } // namespace |
| 169 |
| 109 AutomationProvider::AutomationProvider(Profile* profile) | 170 AutomationProvider::AutomationProvider(Profile* profile) |
| 110 : profile_(profile), | 171 : profile_(profile), |
| 111 reply_message_(NULL), | 172 reply_message_(NULL), |
| 112 reinitialize_on_channel_error_( | 173 reinitialize_on_channel_error_( |
| 113 CommandLine::ForCurrentProcess()->HasSwitch( | 174 CommandLine::ForCurrentProcess()->HasSwitch( |
| 114 switches::kAutomationReinitializeOnChannelError)), | 175 switches::kAutomationReinitializeOnChannelError)), |
| 115 is_connected_(false), | 176 is_connected_(false), |
| 116 initial_tab_loads_complete_(false), | 177 initial_tab_loads_complete_(false), |
| 117 network_library_initialized_(true), | 178 network_library_initialized_(true), |
| 118 login_webui_ready_(true) { | 179 login_webui_ready_(true) { |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 << "(like ChromeFrame). Closing the automation channel."; | 485 << "(like ChromeFrame). Closing the automation channel."; |
| 425 channel_->Close(); | 486 channel_->Close(); |
| 426 } | 487 } |
| 427 | 488 |
| 428 void AutomationProvider::OnMessageDeserializationFailure() { | 489 void AutomationProvider::OnMessageDeserializationFailure() { |
| 429 LOG(ERROR) << "Failed to deserialize IPC message. " | 490 LOG(ERROR) << "Failed to deserialize IPC message. " |
| 430 << "Closing the automation channel."; | 491 << "Closing the automation channel."; |
| 431 channel_->Close(); | 492 channel_->Close(); |
| 432 } | 493 } |
| 433 | 494 |
| 434 // This task just adds another task to the event queue. This is useful if | |
| 435 // you want to ensure that any tasks added to the event queue after this one | |
| 436 // have already been processed by the time |task| is run. | |
| 437 class InvokeTaskLaterTask : public Task { | |
| 438 public: | |
| 439 explicit InvokeTaskLaterTask(Task* task) : task_(task) {} | |
| 440 virtual ~InvokeTaskLaterTask() {} | |
| 441 | |
| 442 virtual void Run() { | |
| 443 MessageLoop::current()->PostTask(FROM_HERE, task_); | |
| 444 } | |
| 445 | |
| 446 private: | |
| 447 Task* task_; | |
| 448 | |
| 449 DISALLOW_COPY_AND_ASSIGN(InvokeTaskLaterTask); | |
| 450 }; | |
| 451 | |
| 452 void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) { | 495 void AutomationProvider::HandleUnused(const IPC::Message& message, int handle) { |
| 453 if (window_tracker_->ContainsHandle(handle)) { | 496 if (window_tracker_->ContainsHandle(handle)) { |
| 454 window_tracker_->Remove(window_tracker_->GetResource(handle)); | 497 window_tracker_->Remove(window_tracker_->GetResource(handle)); |
| 455 } | 498 } |
| 456 } | 499 } |
| 457 | 500 |
| 458 bool AutomationProvider::ReinitializeChannel() { | 501 bool AutomationProvider::ReinitializeChannel() { |
| 459 base::ThreadRestrictions::ScopedAllowIO allow_io; | 502 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 460 | 503 |
| 461 // Make sure any old channels are cleaned up before starting up a new one. | 504 // Make sure any old channels are cleaned up before starting up a new one. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 | 578 |
| 536 WebFindOptions options; | 579 WebFindOptions options; |
| 537 options.forward = forward; | 580 options.forward = forward; |
| 538 options.matchCase = match_case; | 581 options.matchCase = match_case; |
| 539 options.findNext = find_next; | 582 options.findNext = find_next; |
| 540 tab_contents->render_view_host()->Find( | 583 tab_contents->render_view_host()->Find( |
| 541 FindInPageNotificationObserver::kFindInPageRequestId, search_string, | 584 FindInPageNotificationObserver::kFindInPageRequestId, search_string, |
| 542 options); | 585 options); |
| 543 } | 586 } |
| 544 | 587 |
| 545 class SetProxyConfigTask : public Task { | |
| 546 public: | |
| 547 SetProxyConfigTask(net::URLRequestContextGetter* request_context_getter, | |
| 548 const std::string& new_proxy_config) | |
| 549 : request_context_getter_(request_context_getter), | |
| 550 proxy_config_(new_proxy_config) {} | |
| 551 virtual void Run() { | |
| 552 // First, deserialize the JSON string. If this fails, log and bail. | |
| 553 JSONStringValueSerializer deserializer(proxy_config_); | |
| 554 std::string error_msg; | |
| 555 scoped_ptr<Value> root(deserializer.Deserialize(NULL, &error_msg)); | |
| 556 if (!root.get() || root->GetType() != Value::TYPE_DICTIONARY) { | |
| 557 DLOG(WARNING) << "Received bad JSON string for ProxyConfig: " | |
| 558 << error_msg; | |
| 559 return; | |
| 560 } | |
| 561 | |
| 562 scoped_ptr<DictionaryValue> dict( | |
| 563 static_cast<DictionaryValue*>(root.release())); | |
| 564 // Now put together a proxy configuration from the deserialized string. | |
| 565 net::ProxyConfig pc; | |
| 566 PopulateProxyConfig(*dict.get(), &pc); | |
| 567 | |
| 568 net::ProxyService* proxy_service = | |
| 569 request_context_getter_->GetURLRequestContext()->proxy_service(); | |
| 570 DCHECK(proxy_service); | |
| 571 scoped_ptr<net::ProxyConfigService> proxy_config_service( | |
| 572 new net::ProxyConfigServiceFixed(pc)); | |
| 573 proxy_service->ResetConfigService(proxy_config_service.release()); | |
| 574 } | |
| 575 | |
| 576 void PopulateProxyConfig(const DictionaryValue& dict, net::ProxyConfig* pc) { | |
| 577 DCHECK(pc); | |
| 578 bool no_proxy = false; | |
| 579 if (dict.GetBoolean(automation::kJSONProxyNoProxy, &no_proxy)) { | |
| 580 // Make no changes to the ProxyConfig. | |
| 581 return; | |
| 582 } | |
| 583 bool auto_config; | |
| 584 if (dict.GetBoolean(automation::kJSONProxyAutoconfig, &auto_config)) { | |
| 585 pc->set_auto_detect(true); | |
| 586 } | |
| 587 std::string pac_url; | |
| 588 if (dict.GetString(automation::kJSONProxyPacUrl, &pac_url)) { | |
| 589 pc->set_pac_url(GURL(pac_url)); | |
| 590 } | |
| 591 bool pac_mandatory; | |
| 592 if (dict.GetBoolean(automation::kJSONProxyPacMandatory, &pac_mandatory)) { | |
| 593 pc->set_pac_mandatory(pac_mandatory); | |
| 594 } | |
| 595 std::string proxy_bypass_list; | |
| 596 if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) { | |
| 597 pc->proxy_rules().bypass_rules.ParseFromString(proxy_bypass_list); | |
| 598 } | |
| 599 std::string proxy_server; | |
| 600 if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) { | |
| 601 pc->proxy_rules().ParseFromString(proxy_server); | |
| 602 } | |
| 603 } | |
| 604 | |
| 605 private: | |
| 606 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
| 607 std::string proxy_config_; | |
| 608 }; | |
| 609 | |
| 610 | |
| 611 void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) { | 588 void AutomationProvider::SetProxyConfig(const std::string& new_proxy_config) { |
| 612 net::URLRequestContextGetter* context_getter = | 589 net::URLRequestContextGetter* context_getter = |
| 613 profile_->GetRequestContext(); | 590 profile_->GetRequestContext(); |
| 614 DCHECK(context_getter); | 591 DCHECK(context_getter); |
| 615 | 592 |
| 616 BrowserThread::PostTask( | 593 BrowserThread::PostTask( |
| 617 BrowserThread::IO, FROM_HERE, | 594 BrowserThread::IO, FROM_HERE, |
| 618 new SetProxyConfigTask(context_getter, new_proxy_config)); | 595 base::Bind(SetProxyConfigCallback, make_scoped_refptr(context_getter), |
| 596 new_proxy_config)); |
| 619 } | 597 } |
| 620 | 598 |
| 621 TabContents* AutomationProvider::GetTabContentsForHandle( | 599 TabContents* AutomationProvider::GetTabContentsForHandle( |
| 622 int handle, NavigationController** tab) { | 600 int handle, NavigationController** tab) { |
| 623 if (tab_tracker_->ContainsHandle(handle)) { | 601 if (tab_tracker_->ContainsHandle(handle)) { |
| 624 NavigationController* nav_controller = tab_tracker_->GetResource(handle); | 602 NavigationController* nav_controller = tab_tracker_->GetResource(handle); |
| 625 if (tab) | 603 if (tab) |
| 626 *tab = nav_controller; | 604 *tab = nav_controller; |
| 627 return nav_controller->tab_contents(); | 605 return nav_controller->tab_contents(); |
| 628 } | 606 } |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1019 } | 997 } |
| 1020 } | 998 } |
| 1021 } | 999 } |
| 1022 | 1000 |
| 1023 void AutomationProvider::SaveAsAsync(int tab_handle) { | 1001 void AutomationProvider::SaveAsAsync(int tab_handle) { |
| 1024 NavigationController* tab = NULL; | 1002 NavigationController* tab = NULL; |
| 1025 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab); | 1003 TabContents* tab_contents = GetTabContentsForHandle(tab_handle, &tab); |
| 1026 if (tab_contents) | 1004 if (tab_contents) |
| 1027 tab_contents->OnSavePage(); | 1005 tab_contents->OnSavePage(); |
| 1028 } | 1006 } |
| OLD | NEW |