| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/automation/automation_provider_observers.h" | 5 #include "chrome/browser/automation/automation_provider_observers.h" |
| 6 | 6 |
| 7 #include <deque> | 7 #include <deque> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 1724 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1735 reply_message_.release()).SendSuccess(NULL); | 1735 reply_message_.release()).SendSuccess(NULL); |
| 1736 } | 1736 } |
| 1737 delete this; | 1737 delete this; |
| 1738 } | 1738 } |
| 1739 | 1739 |
| 1740 void SavePackageNotificationObserver::ManagerGoingDown( | 1740 void SavePackageNotificationObserver::ManagerGoingDown( |
| 1741 content::DownloadManager* manager) { | 1741 content::DownloadManager* manager) { |
| 1742 delete this; | 1742 delete this; |
| 1743 } | 1743 } |
| 1744 | 1744 |
| 1745 PageSnapshotTaker::PageSnapshotTaker(AutomationProvider* automation, | |
| 1746 IPC::Message* reply_message, | |
| 1747 WebContents* web_contents, | |
| 1748 const base::FilePath& path) | |
| 1749 : automation_(automation->AsWeakPtr()), | |
| 1750 reply_message_(reply_message), | |
| 1751 web_contents_(web_contents), | |
| 1752 image_path_(path) { | |
| 1753 registrar_.Add(this, chrome::NOTIFICATION_APP_MODAL_DIALOG_SHOWN, | |
| 1754 content::NotificationService::AllSources()); | |
| 1755 } | |
| 1756 | |
| 1757 PageSnapshotTaker::~PageSnapshotTaker() {} | |
| 1758 | |
| 1759 void PageSnapshotTaker::Start() { | |
| 1760 AutomationTabHelper* automation_tab_helper = | |
| 1761 AutomationTabHelper::FromWebContents(web_contents_); | |
| 1762 StartObserving(automation_tab_helper); | |
| 1763 automation_tab_helper->SnapshotEntirePage(); | |
| 1764 } | |
| 1765 | |
| 1766 void PageSnapshotTaker::OnSnapshotEntirePageACK( | |
| 1767 bool success, | |
| 1768 const std::vector<unsigned char>& png_data, | |
| 1769 const std::string& error_msg) { | |
| 1770 bool overall_success = success; | |
| 1771 std::string overall_error_msg = error_msg; | |
| 1772 if (success) { | |
| 1773 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 1774 int bytes_written = file_util::WriteFile(image_path_, | |
| 1775 reinterpret_cast<const char*>(&png_data[0]), png_data.size()); | |
| 1776 overall_success = (bytes_written == static_cast<int>(png_data.size())); | |
| 1777 if (!overall_success) | |
| 1778 overall_error_msg = "could not write snapshot to disk"; | |
| 1779 } | |
| 1780 SendMessage(overall_success, overall_error_msg); | |
| 1781 } | |
| 1782 | |
| 1783 void PageSnapshotTaker::Observe(int type, | |
| 1784 const content::NotificationSource& source, | |
| 1785 const content::NotificationDetails& details) { | |
| 1786 SendMessage(false, "a modal dialog is active"); | |
| 1787 } | |
| 1788 | |
| 1789 void PageSnapshotTaker::SendMessage(bool success, | |
| 1790 const std::string& error_msg) { | |
| 1791 if (automation_) { | |
| 1792 if (success) { | |
| 1793 AutomationJSONReply(automation_, reply_message_.release()) | |
| 1794 .SendSuccess(NULL); | |
| 1795 } else { | |
| 1796 AutomationJSONReply(automation_, reply_message_.release()) | |
| 1797 .SendError("Failed to take snapshot of page: " + error_msg); | |
| 1798 } | |
| 1799 } | |
| 1800 delete this; | |
| 1801 } | |
| 1802 | |
| 1803 AutomationMouseEventProcessor::AutomationMouseEventProcessor( | |
| 1804 RenderViewHost* render_view_host, | |
| 1805 const AutomationMouseEvent& event, | |
| 1806 const CompletionCallback& completion_callback, | |
| 1807 const ErrorCallback& error_callback) | |
| 1808 : RenderViewHostObserver(render_view_host), | |
| 1809 completion_callback_(completion_callback), | |
| 1810 error_callback_(error_callback), | |
| 1811 has_point_(false) { | |
| 1812 registrar_.Add(this, chrome::NOTIFICATION_APP_MODAL_DIALOG_SHOWN, | |
| 1813 content::NotificationService::AllSources()); | |
| 1814 Send(new AutomationMsg_ProcessMouseEvent(routing_id(), event)); | |
| 1815 } | |
| 1816 | |
| 1817 AutomationMouseEventProcessor::~AutomationMouseEventProcessor() {} | |
| 1818 | |
| 1819 void AutomationMouseEventProcessor::OnWillProcessMouseEventAt( | |
| 1820 const gfx::Point& point) { | |
| 1821 has_point_ = true; | |
| 1822 point_ = point; | |
| 1823 } | |
| 1824 | |
| 1825 void AutomationMouseEventProcessor::OnProcessMouseEventACK( | |
| 1826 bool success, | |
| 1827 const std::string& error_msg) { | |
| 1828 InvokeCallback(automation::Error(error_msg)); | |
| 1829 } | |
| 1830 | |
| 1831 void AutomationMouseEventProcessor::RenderViewHostDestroyed( | |
| 1832 RenderViewHost* host) { | |
| 1833 InvokeCallback(automation::Error("The render view host was destroyed")); | |
| 1834 } | |
| 1835 | |
| 1836 bool AutomationMouseEventProcessor::OnMessageReceived( | |
| 1837 const IPC::Message& message) { | |
| 1838 bool handled = true; | |
| 1839 bool msg_is_good = true; | |
| 1840 IPC_BEGIN_MESSAGE_MAP_EX(AutomationMouseEventProcessor, message, msg_is_good) | |
| 1841 IPC_MESSAGE_HANDLER(AutomationMsg_WillProcessMouseEventAt, | |
| 1842 OnWillProcessMouseEventAt) | |
| 1843 IPC_MESSAGE_HANDLER(AutomationMsg_ProcessMouseEventACK, | |
| 1844 OnProcessMouseEventACK) | |
| 1845 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 1846 IPC_END_MESSAGE_MAP_EX() | |
| 1847 if (!msg_is_good) { | |
| 1848 LOG(ERROR) << "Failed to deserialize an IPC message"; | |
| 1849 } | |
| 1850 return handled; | |
| 1851 } | |
| 1852 | |
| 1853 void AutomationMouseEventProcessor::Observe( | |
| 1854 int type, | |
| 1855 const content::NotificationSource& source, | |
| 1856 const content::NotificationDetails& details) { | |
| 1857 InvokeCallback(automation::Error(automation::kBlockedByModalDialog)); | |
| 1858 } | |
| 1859 | |
| 1860 void AutomationMouseEventProcessor::InvokeCallback( | |
| 1861 const automation::Error& error) { | |
| 1862 if (has_point_) | |
| 1863 completion_callback_.Run(point_); | |
| 1864 else | |
| 1865 error_callback_.Run(error); | |
| 1866 delete this; | |
| 1867 } | |
| 1868 | |
| 1869 namespace { | 1745 namespace { |
| 1870 | 1746 |
| 1871 // Returns a vector of dictionaries containing information about installed apps, | 1747 // Returns a vector of dictionaries containing information about installed apps, |
| 1872 // as identified from a given list of extensions. The caller takes ownership | 1748 // as identified from a given list of extensions. The caller takes ownership |
| 1873 // of the created vector. | 1749 // of the created vector. |
| 1874 std::vector<DictionaryValue*>* GetAppInfoFromExtensions( | 1750 std::vector<DictionaryValue*>* GetAppInfoFromExtensions( |
| 1875 const ExtensionSet* extensions, | 1751 const ExtensionSet* extensions, |
| 1876 ExtensionService* ext_service) { | 1752 ExtensionService* ext_service) { |
| 1877 std::vector<DictionaryValue*>* apps_list = | 1753 std::vector<DictionaryValue*>* apps_list = |
| 1878 new std::vector<DictionaryValue*>(); | 1754 new std::vector<DictionaryValue*>(); |
| (...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2308 reply_message_.release()).SendSuccess(NULL); | 2184 reply_message_.release()).SendSuccess(NULL); |
| 2309 delete this; | 2185 delete this; |
| 2310 } | 2186 } |
| 2311 } else { | 2187 } else { |
| 2312 LOG(WARNING) << "Ignoring unexpected event type: " | 2188 LOG(WARNING) << "Ignoring unexpected event type: " |
| 2313 << *request_details.ptr() << " (expected: " << event_type_ | 2189 << *request_details.ptr() << " (expected: " << event_type_ |
| 2314 << ")"; | 2190 << ")"; |
| 2315 } | 2191 } |
| 2316 } | 2192 } |
| 2317 | 2193 |
| 2318 AllViewsStoppedLoadingObserver::AllViewsStoppedLoadingObserver( | |
| 2319 AutomationProvider* automation, | |
| 2320 IPC::Message* reply_message, | |
| 2321 ExtensionProcessManager* extension_process_manager) | |
| 2322 : automation_(automation->AsWeakPtr()), | |
| 2323 reply_message_(reply_message), | |
| 2324 extension_process_manager_(extension_process_manager) { | |
| 2325 registrar_.Add(this, | |
| 2326 chrome::NOTIFICATION_APP_MODAL_DIALOG_SHOWN, | |
| 2327 content::NotificationService::AllSources()); | |
| 2328 registrar_.Add(this, | |
| 2329 content::NOTIFICATION_LOAD_STOP, | |
| 2330 content::NotificationService::AllSources()); | |
| 2331 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | |
| 2332 Browser* browser = *it; | |
| 2333 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { | |
| 2334 WebContents* web_contents = | |
| 2335 browser->tab_strip_model()->GetWebContentsAt(i); | |
| 2336 AutomationTabHelper* automation_tab_helper = | |
| 2337 AutomationTabHelper::FromWebContents(web_contents); | |
| 2338 StartObserving(automation_tab_helper); | |
| 2339 if (automation_tab_helper->has_pending_loads()) | |
| 2340 pending_tabs_.insert(web_contents); | |
| 2341 } | |
| 2342 } | |
| 2343 CheckIfNoMorePendingLoads(); | |
| 2344 } | |
| 2345 | |
| 2346 AllViewsStoppedLoadingObserver::~AllViewsStoppedLoadingObserver() { | |
| 2347 } | |
| 2348 | |
| 2349 void AllViewsStoppedLoadingObserver::OnFirstPendingLoad( | |
| 2350 content::WebContents* web_contents) { | |
| 2351 pending_tabs_.insert(web_contents); | |
| 2352 } | |
| 2353 | |
| 2354 void AllViewsStoppedLoadingObserver::OnNoMorePendingLoads( | |
| 2355 content::WebContents* web_contents) { | |
| 2356 if (!automation_) { | |
| 2357 delete this; | |
| 2358 return; | |
| 2359 } | |
| 2360 | |
| 2361 TabSet::iterator iter = pending_tabs_.find(web_contents); | |
| 2362 if (iter == pending_tabs_.end()) { | |
| 2363 LOG(ERROR) << "Received OnNoMorePendingLoads for tab without " | |
| 2364 << "OnFirstPendingLoad."; | |
| 2365 return; | |
| 2366 } | |
| 2367 pending_tabs_.erase(iter); | |
| 2368 CheckIfNoMorePendingLoads(); | |
| 2369 } | |
| 2370 | |
| 2371 void AllViewsStoppedLoadingObserver::Observe( | |
| 2372 int type, | |
| 2373 const content::NotificationSource& source, | |
| 2374 const content::NotificationDetails& details) { | |
| 2375 if (!automation_) { | |
| 2376 delete this; | |
| 2377 return; | |
| 2378 } | |
| 2379 if (type == content::NOTIFICATION_LOAD_STOP) { | |
| 2380 CheckIfNoMorePendingLoads(); | |
| 2381 } else if (type == chrome::NOTIFICATION_APP_MODAL_DIALOG_SHOWN) { | |
| 2382 AutomationJSONReply(automation_, | |
| 2383 reply_message_.release()).SendSuccess(NULL); | |
| 2384 delete this; | |
| 2385 } | |
| 2386 } | |
| 2387 | |
| 2388 void AllViewsStoppedLoadingObserver::CheckIfNoMorePendingLoads() { | |
| 2389 if (!automation_) { | |
| 2390 delete this; | |
| 2391 return; | |
| 2392 } | |
| 2393 | |
| 2394 if (pending_tabs_.empty() && | |
| 2395 DidExtensionViewsStopLoading(extension_process_manager_)) { | |
| 2396 AutomationJSONReply(automation_, | |
| 2397 reply_message_.release()).SendSuccess(NULL); | |
| 2398 delete this; | |
| 2399 } | |
| 2400 } | |
| 2401 | |
| 2402 NewTabObserver::NewTabObserver(AutomationProvider* automation, | 2194 NewTabObserver::NewTabObserver(AutomationProvider* automation, |
| 2403 IPC::Message* reply_message, | 2195 IPC::Message* reply_message, |
| 2404 bool use_json_interface) | 2196 bool use_json_interface) |
| 2405 : automation_(automation->AsWeakPtr()), | 2197 : automation_(automation->AsWeakPtr()), |
| 2406 reply_message_(reply_message), | 2198 reply_message_(reply_message), |
| 2407 use_json_interface_(use_json_interface) { | 2199 use_json_interface_(use_json_interface) { |
| 2408 // Use TAB_PARENTED to detect the new tab. | 2200 // Use TAB_PARENTED to detect the new tab. |
| 2409 registrar_.Add(this, | 2201 registrar_.Add(this, |
| 2410 chrome::NOTIFICATION_TAB_PARENTED, | 2202 chrome::NOTIFICATION_TAB_PARENTED, |
| 2411 content::NotificationService::AllSources()); | 2203 content::NotificationService::AllSources()); |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2849 if (automation_) { | 2641 if (automation_) { |
| 2850 AutomationJSONReply(automation_, reply_message_.release()) | 2642 AutomationJSONReply(automation_, reply_message_.release()) |
| 2851 .SendSuccess(NULL); | 2643 .SendSuccess(NULL); |
| 2852 } | 2644 } |
| 2853 delete this; | 2645 delete this; |
| 2854 } | 2646 } |
| 2855 } else { | 2647 } else { |
| 2856 NOTREACHED(); | 2648 NOTREACHED(); |
| 2857 } | 2649 } |
| 2858 } | 2650 } |
| OLD | NEW |