OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "athena/content/web_activity.h" | 5 #include "athena/content/web_activity.h" |
6 | 6 |
| 7 #include "athena/activity/public/activity_factory.h" |
7 #include "athena/activity/public/activity_manager.h" | 8 #include "athena/activity/public/activity_manager.h" |
8 #include "athena/input/public/accelerator_manager.h" | 9 #include "athena/input/public/accelerator_manager.h" |
9 #include "content/public/browser/native_web_keyboard_event.h" | 10 #include "content/public/browser/native_web_keyboard_event.h" |
| 11 #include "content/public/browser/navigation_controller.h" |
10 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/browser/web_contents_delegate.h" |
11 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" | 14 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" |
12 #include "ui/views/controls/webview/webview.h" | 15 #include "ui/views/controls/webview/webview.h" |
13 #include "ui/views/focus/focus_manager.h" | 16 #include "ui/views/focus/focus_manager.h" |
14 | 17 |
15 namespace athena { | 18 namespace athena { |
16 namespace { | 19 namespace { |
17 | 20 |
18 class WebActivityController : public AcceleratorHandler { | 21 class WebActivityController : public AcceleratorHandler { |
19 public: | 22 public: |
20 enum Command { | 23 enum Command { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 } // namespace | 122 } // namespace |
120 | 123 |
121 // A web view for athena's web activity. Note that AthenaWebView will create its | 124 // A web view for athena's web activity. Note that AthenaWebView will create its |
122 // own content so that it can eject and reload it. | 125 // own content so that it can eject and reload it. |
123 class AthenaWebView : public views::WebView { | 126 class AthenaWebView : public views::WebView { |
124 public: | 127 public: |
125 AthenaWebView(content::BrowserContext* context) | 128 AthenaWebView(content::BrowserContext* context) |
126 : views::WebView(context), controller_(new WebActivityController(this)) { | 129 : views::WebView(context), controller_(new WebActivityController(this)) { |
127 // We create the first web contents ourselves to allow us to replace it | 130 // We create the first web contents ourselves to allow us to replace it |
128 // later on. | 131 // later on. |
129 SetWebContents(content::WebContents::Create( | 132 content::WebContents* web_contents(content::WebContents::Create( |
130 content::WebContents::CreateParams(context))); | 133 content::WebContents::CreateParams(context))); |
| 134 SetWebContents(web_contents); |
| 135 web_contents->SetDelegate(this); |
131 // TODO(skuhne): Add content observer to detect renderer crash and set | 136 // TODO(skuhne): Add content observer to detect renderer crash and set |
132 // content status to unloaded if that happens. | 137 // content status to unloaded if that happens. |
133 } | 138 } |
| 139 |
| 140 AthenaWebView(content::WebContents* web_contents) |
| 141 : views::WebView(web_contents->GetBrowserContext()), |
| 142 controller_(new WebActivityController(this)) { |
| 143 SetWebContents(web_contents); |
| 144 web_contents->SetDelegate(this); |
| 145 } |
| 146 |
134 virtual ~AthenaWebView() { | 147 virtual ~AthenaWebView() { |
135 // |WebView| does not own the content, so we need to destroy it here. | 148 // |WebView| does not own the content, so we need to destroy it here. |
136 content::WebContents* current_contents = GetWebContents(); | 149 content::WebContents* current_contents = GetWebContents(); |
137 SetWebContents(NULL); | 150 SetWebContents(NULL); |
138 delete current_contents; | 151 delete current_contents; |
139 } | 152 } |
140 | 153 |
141 void InstallAccelerators() { controller_->InstallAccelerators(); } | 154 void InstallAccelerators() { controller_->InstallAccelerators(); } |
142 | 155 |
143 void EvictContent() { | 156 void EvictContent() { |
(...skipping 10 matching lines...) Expand all Loading... |
154 // TODO(skuhne): This breaks script connections with other activities. | 167 // TODO(skuhne): This breaks script connections with other activities. |
155 // Even though this is the same technique as used by the TabStripModel, | 168 // Even though this is the same technique as used by the TabStripModel, |
156 // we might want to address this cleaner since we are more likely to | 169 // we might want to address this cleaner since we are more likely to |
157 // run into this state. by unloading. | 170 // run into this state. by unloading. |
158 } | 171 } |
159 | 172 |
160 void ReloadContent() { | 173 void ReloadContent() { |
161 CHECK(evicted_web_contents_.get()); | 174 CHECK(evicted_web_contents_.get()); |
162 content::WebContents* null_contents = GetWebContents(); | 175 content::WebContents* null_contents = GetWebContents(); |
163 SetWebContents(evicted_web_contents_.release()); | 176 SetWebContents(evicted_web_contents_.release()); |
| 177 web_contents()->SetDelegate(this); |
164 delete null_contents; | 178 delete null_contents; |
165 } | 179 } |
166 | 180 |
167 // Check if the content got evicted. | 181 // Check if the content got evicted. |
168 const bool IsContentEvicted() { return !!evicted_web_contents_.get(); } | 182 const bool IsContentEvicted() { return !!evicted_web_contents_.get(); } |
169 | 183 |
170 private: | 184 // content::WebContentsDelegate: |
171 // WebContentsDelegate: | 185 virtual content::WebContents* OpenURLFromTab( |
| 186 content::WebContents* source, |
| 187 const content::OpenURLParams& params) OVERRIDE { |
| 188 switch(params.disposition) { |
| 189 case CURRENT_TAB: { |
| 190 DCHECK(source == web_contents()); |
| 191 content::NavigationController::LoadURLParams load_url_params( |
| 192 params.url); |
| 193 load_url_params.referrer = params.referrer; |
| 194 load_url_params.frame_tree_node_id = params.frame_tree_node_id; |
| 195 load_url_params.transition_type = params.transition; |
| 196 load_url_params.extra_headers = params.extra_headers; |
| 197 load_url_params.should_replace_current_entry = |
| 198 params.should_replace_current_entry; |
| 199 load_url_params.is_renderer_initiated = params.is_renderer_initiated; |
| 200 load_url_params.transferred_global_request_id = |
| 201 params.transferred_global_request_id; |
| 202 web_contents()->GetController().LoadURLWithParams(load_url_params); |
| 203 return web_contents(); |
| 204 } |
| 205 case NEW_FOREGROUND_TAB: |
| 206 case NEW_BACKGROUND_TAB: |
| 207 case NEW_POPUP: |
| 208 case NEW_WINDOW: { |
| 209 ActivityManager::Get()->AddActivity( |
| 210 ActivityFactory::Get()->CreateWebActivity(browser_context(), |
| 211 params.url)); |
| 212 break; |
| 213 } |
| 214 default: |
| 215 break; |
| 216 } |
| 217 // NULL is returned if the URL wasn't opened immediately. |
| 218 return NULL; |
| 219 } |
| 220 |
| 221 virtual void AddNewContents(content::WebContents* source, |
| 222 content::WebContents* new_contents, |
| 223 WindowOpenDisposition disposition, |
| 224 const gfx::Rect& initial_pos, |
| 225 bool user_gesture, |
| 226 bool* was_blocked) OVERRIDE { |
| 227 ActivityManager::Get()->AddActivity( |
| 228 new WebActivity(new AthenaWebView(new_contents))); |
| 229 } |
| 230 |
| 231 virtual void WebContentsFocused(content::WebContents* web_contents) OVERRIDE { |
| 232 OnWebContentsFocused(web_contents); |
| 233 } |
| 234 |
172 virtual bool PreHandleKeyboardEvent( | 235 virtual bool PreHandleKeyboardEvent( |
173 content::WebContents* source, | 236 content::WebContents* source, |
174 const content::NativeWebKeyboardEvent& event, | 237 const content::NativeWebKeyboardEvent& event, |
175 bool* is_keyboard_shortcut) OVERRIDE { | 238 bool* is_keyboard_shortcut) OVERRIDE { |
176 return controller_->PreHandleKeyboardEvent( | 239 return controller_->PreHandleKeyboardEvent( |
177 source, event, is_keyboard_shortcut); | 240 source, event, is_keyboard_shortcut); |
178 } | 241 } |
179 | 242 |
180 virtual void HandleKeyboardEvent( | 243 virtual void HandleKeyboardEvent( |
181 content::WebContents* source, | 244 content::WebContents* source, |
182 const content::NativeWebKeyboardEvent& event) OVERRIDE { | 245 const content::NativeWebKeyboardEvent& event) OVERRIDE { |
183 controller_->HandleKeyboardEvent(source, event); | 246 controller_->HandleKeyboardEvent(source, event); |
184 } | 247 } |
185 | 248 |
| 249 private: |
186 scoped_ptr<WebActivityController> controller_; | 250 scoped_ptr<WebActivityController> controller_; |
187 | 251 |
188 // If the activity got evicted, this is the web content which holds the known | 252 // If the activity got evicted, this is the web content which holds the known |
189 // state of the content before eviction. | 253 // state of the content before eviction. |
190 scoped_ptr<content::WebContents> evicted_web_contents_; | 254 scoped_ptr<content::WebContents> evicted_web_contents_; |
191 | 255 |
192 DISALLOW_COPY_AND_ASSIGN(AthenaWebView); | 256 DISALLOW_COPY_AND_ASSIGN(AthenaWebView); |
193 }; | 257 }; |
194 | 258 |
195 WebActivity::WebActivity(content::BrowserContext* browser_context, | 259 WebActivity::WebActivity(content::BrowserContext* browser_context, |
196 const GURL& url) | 260 const GURL& url) |
197 : browser_context_(browser_context), | 261 : browser_context_(browser_context), |
198 url_(url), | 262 url_(url), |
199 web_view_(NULL), | 263 web_view_(NULL), |
200 current_state_(ACTIVITY_UNLOADED) { | 264 current_state_(ACTIVITY_UNLOADED) { |
201 } | 265 } |
202 | 266 |
| 267 WebActivity::WebActivity(AthenaWebView* web_view) |
| 268 : browser_context_(web_view->browser_context()), |
| 269 url_(web_view->GetWebContents()->GetURL()), |
| 270 web_view_(web_view), |
| 271 current_state_(ACTIVITY_UNLOADED) { |
| 272 // Transition to state ACTIVITY_INVISIBLE to perform the same setup steps |
| 273 // as on new activities (namely adding a WebContentsObserver). |
| 274 SetCurrentState(ACTIVITY_INVISIBLE); |
| 275 } |
| 276 |
203 WebActivity::~WebActivity() { | 277 WebActivity::~WebActivity() { |
204 // It is not required to change the activity state to UNLOADED - unless we | 278 // It is not required to change the activity state to UNLOADED - unless we |
205 // would add state observers. | 279 // would add state observers. |
206 } | 280 } |
207 | 281 |
208 ActivityViewModel* WebActivity::GetActivityViewModel() { | 282 ActivityViewModel* WebActivity::GetActivityViewModel() { |
209 return this; | 283 return this; |
210 } | 284 } |
211 | 285 |
212 void WebActivity::SetCurrentState(Activity::ActivityState state) { | 286 void WebActivity::SetCurrentState(Activity::ActivityState state) { |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 bool explicit_set) { | 383 bool explicit_set) { |
310 ActivityManager::Get()->UpdateActivity(this); | 384 ActivityManager::Get()->UpdateActivity(this); |
311 } | 385 } |
312 | 386 |
313 void WebActivity::DidUpdateFaviconURL( | 387 void WebActivity::DidUpdateFaviconURL( |
314 const std::vector<content::FaviconURL>& candidates) { | 388 const std::vector<content::FaviconURL>& candidates) { |
315 ActivityManager::Get()->UpdateActivity(this); | 389 ActivityManager::Get()->UpdateActivity(this); |
316 } | 390 } |
317 | 391 |
318 } // namespace athena | 392 } // namespace athena |
OLD | NEW |