| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/automation/automation_tab_helper.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "chrome/common/automation_messages.h" | |
| 10 #include "content/public/browser/web_contents.h" | |
| 11 #include "ipc/ipc_message.h" | |
| 12 #include "ipc/ipc_message_macros.h" | |
| 13 #include "ui/gfx/size.h" | |
| 14 | |
| 15 using content::WebContents; | |
| 16 | |
| 17 DEFINE_WEB_CONTENTS_USER_DATA_KEY(AutomationTabHelper); | |
| 18 | |
| 19 TabEventObserver::TabEventObserver() { } | |
| 20 | |
| 21 TabEventObserver::~TabEventObserver() { | |
| 22 for (size_t i = 0; i < event_sources_.size(); ++i) { | |
| 23 if (event_sources_[i]) | |
| 24 event_sources_[i]->RemoveObserver(this); | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 void TabEventObserver::StartObserving(AutomationTabHelper* tab_helper) { | |
| 29 tab_helper->AddObserver(this); | |
| 30 event_sources_.push_back(tab_helper->AsWeakPtr()); | |
| 31 } | |
| 32 | |
| 33 void TabEventObserver::StopObserving(AutomationTabHelper* tab_helper) { | |
| 34 tab_helper->RemoveObserver(this); | |
| 35 EventSourceVector::iterator iter = | |
| 36 std::find(event_sources_.begin(), event_sources_.end(), tab_helper); | |
| 37 if (iter != event_sources_.end()) | |
| 38 event_sources_.erase(iter); | |
| 39 } | |
| 40 | |
| 41 AutomationTabHelper::AutomationTabHelper(WebContents* web_contents) | |
| 42 : content::WebContentsObserver(web_contents), | |
| 43 is_loading_(false) { | |
| 44 } | |
| 45 | |
| 46 AutomationTabHelper::~AutomationTabHelper() { } | |
| 47 | |
| 48 void AutomationTabHelper::AddObserver(TabEventObserver* observer) { | |
| 49 observers_.AddObserver(observer); | |
| 50 } | |
| 51 | |
| 52 void AutomationTabHelper::RemoveObserver(TabEventObserver* observer) { | |
| 53 observers_.RemoveObserver(observer); | |
| 54 } | |
| 55 | |
| 56 void AutomationTabHelper::SnapshotEntirePage() { | |
| 57 Send(new AutomationMsg_SnapshotEntirePage(routing_id())); | |
| 58 } | |
| 59 | |
| 60 #if !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS)) | |
| 61 void AutomationTabHelper::HeapProfilerDump(const std::string& reason) { | |
| 62 Send(new AutomationMsg_HeapProfilerDump(routing_id(), reason)); | |
| 63 } | |
| 64 #endif // !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS)) | |
| 65 | |
| 66 bool AutomationTabHelper::has_pending_loads() const { | |
| 67 return is_loading_ || !pending_client_redirects_.empty(); | |
| 68 } | |
| 69 | |
| 70 void AutomationTabHelper::DidStartLoading( | |
| 71 content::RenderViewHost* render_view_host) { | |
| 72 if (is_loading_) { | |
| 73 // DidStartLoading is often called twice. Once when the renderer sends a | |
| 74 // load start message, and once when the browser calls it directly as a | |
| 75 // result of some user-initiated navigation. | |
| 76 VLOG(1) << "Received DidStartLoading while loading already started."; | |
| 77 return; | |
| 78 } | |
| 79 bool had_pending_loads = has_pending_loads(); | |
| 80 is_loading_ = true; | |
| 81 if (!had_pending_loads) { | |
| 82 FOR_EACH_OBSERVER(TabEventObserver, observers_, | |
| 83 OnFirstPendingLoad(web_contents())); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 void AutomationTabHelper::DidStopLoading( | |
| 88 content::RenderViewHost* render_view_host) { | |
| 89 if (!is_loading_) { | |
| 90 LOG(WARNING) << "Received DidStopLoading while loading already stopped."; | |
| 91 return; | |
| 92 } | |
| 93 is_loading_ = false; | |
| 94 if (!has_pending_loads()) { | |
| 95 FOR_EACH_OBSERVER(TabEventObserver, observers_, | |
| 96 OnNoMorePendingLoads(web_contents())); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 void AutomationTabHelper::RenderViewGone(base::TerminationStatus status) { | |
| 101 OnTabOrRenderViewDestroyed(web_contents()); | |
| 102 } | |
| 103 | |
| 104 void AutomationTabHelper::WebContentsDestroyed(WebContents* web_contents) { | |
| 105 OnTabOrRenderViewDestroyed(web_contents); | |
| 106 } | |
| 107 | |
| 108 void AutomationTabHelper::OnTabOrRenderViewDestroyed( | |
| 109 WebContents* web_contents) { | |
| 110 if (has_pending_loads()) { | |
| 111 is_loading_ = false; | |
| 112 pending_client_redirects_.clear(); | |
| 113 FOR_EACH_OBSERVER(TabEventObserver, observers_, | |
| 114 OnNoMorePendingLoads(web_contents)); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 void AutomationTabHelper::OnSnapshotEntirePageACK( | |
| 119 bool success, | |
| 120 const std::vector<unsigned char>& png_data, | |
| 121 const std::string& error_msg) { | |
| 122 FOR_EACH_OBSERVER(TabEventObserver, observers_, | |
| 123 OnSnapshotEntirePageACK(success, png_data, error_msg)); | |
| 124 } | |
| 125 | |
| 126 bool AutomationTabHelper::OnMessageReceived(const IPC::Message& message) { | |
| 127 bool handled = true; | |
| 128 bool msg_is_good = true; | |
| 129 IPC_BEGIN_MESSAGE_MAP_EX(AutomationTabHelper, message, msg_is_good) | |
| 130 IPC_MESSAGE_HANDLER(AutomationMsg_SnapshotEntirePageACK, | |
| 131 OnSnapshotEntirePageACK) | |
| 132 IPC_MESSAGE_HANDLER(AutomationMsg_WillPerformClientRedirect, | |
| 133 OnWillPerformClientRedirect) | |
| 134 IPC_MESSAGE_HANDLER(AutomationMsg_DidCompleteOrCancelClientRedirect, | |
| 135 OnDidCompleteOrCancelClientRedirect) | |
| 136 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 137 IPC_END_MESSAGE_MAP_EX() | |
| 138 if (!msg_is_good) { | |
| 139 LOG(ERROR) << "Failed to deserialize an IPC message"; | |
| 140 } | |
| 141 return handled; | |
| 142 } | |
| 143 | |
| 144 void AutomationTabHelper::OnWillPerformClientRedirect( | |
| 145 int64 frame_id, double delay_seconds) { | |
| 146 // Ignore all non-zero delays. | |
| 147 // TODO(kkania): Handle timed redirects. | |
| 148 if (delay_seconds > 0) { | |
| 149 LOG(WARNING) << "Ignoring timed redirect scheduled for " << delay_seconds | |
| 150 << " seconds later. Will not wait for the redirect to occur"; | |
| 151 return; | |
| 152 } | |
| 153 | |
| 154 bool first_pending_load = !has_pending_loads(); | |
| 155 pending_client_redirects_.insert(frame_id); | |
| 156 if (first_pending_load) { | |
| 157 FOR_EACH_OBSERVER(TabEventObserver, observers_, | |
| 158 OnFirstPendingLoad(web_contents())); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 void AutomationTabHelper::OnDidCompleteOrCancelClientRedirect(int64 frame_id) { | |
| 163 std::set<int64>::iterator iter = | |
| 164 pending_client_redirects_.find(frame_id); | |
| 165 // It is possible that we did not track the redirect becasue it had a non-zero | |
| 166 // delay. See the comment in |OnWillPerformClientRedirect|. | |
| 167 if (iter != pending_client_redirects_.end()) { | |
| 168 pending_client_redirects_.erase(iter); | |
| 169 if (!has_pending_loads()) { | |
| 170 FOR_EACH_OBSERVER(TabEventObserver, observers_, | |
| 171 OnNoMorePendingLoads(web_contents())); | |
| 172 } | |
| 173 } | |
| 174 } | |
| OLD | NEW |