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_factory.h" |
8 #include "athena/activity/public/activity_manager.h" | 8 #include "athena/activity/public/activity_manager.h" |
9 #include "athena/content/public/dialogs.h" | 9 #include "athena/content/public/dialogs.h" |
10 #include "athena/input/public/accelerator_manager.h" | 10 #include "athena/input/public/accelerator_manager.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/favicon_base/select_favicon_frames.h" |
14 #include "content/public/browser/native_web_keyboard_event.h" | 15 #include "content/public/browser/native_web_keyboard_event.h" |
15 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
17 #include "content/public/browser/web_contents_delegate.h" | 18 #include "content/public/browser/web_contents_delegate.h" |
18 #include "content/public/common/content_switches.h" | 19 #include "content/public/common/content_switches.h" |
| 20 #include "content/public/common/favicon_url.h" |
19 #include "ui/aura/window.h" | 21 #include "ui/aura/window.h" |
20 #include "ui/compositor/closure_animation_observer.h" | 22 #include "ui/compositor/closure_animation_observer.h" |
21 #include "ui/compositor/scoped_layer_animation_settings.h" | 23 #include "ui/compositor/scoped_layer_animation_settings.h" |
22 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" | 24 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" |
23 #include "ui/views/controls/webview/webview.h" | 25 #include "ui/views/controls/webview/webview.h" |
24 #include "ui/views/focus/focus_manager.h" | 26 #include "ui/views/focus/focus_manager.h" |
25 #include "ui/views/widget/widget.h" | 27 #include "ui/views/widget/widget.h" |
26 | 28 |
27 namespace athena { | 29 namespace athena { |
28 namespace { | 30 namespace { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 views::WebView* web_view_; | 141 views::WebView* web_view_; |
140 bool reserved_accelerator_enabled_; | 142 bool reserved_accelerator_enabled_; |
141 scoped_ptr<AcceleratorManager> accelerator_manager_; | 143 scoped_ptr<AcceleratorManager> accelerator_manager_; |
142 views::UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_; | 144 views::UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_; |
143 | 145 |
144 DISALLOW_COPY_AND_ASSIGN(WebActivityController); | 146 DISALLOW_COPY_AND_ASSIGN(WebActivityController); |
145 }; | 147 }; |
146 | 148 |
147 const SkColor kDefaultTitleColor = SkColorSetRGB(0xf2, 0xf2, 0xf2); | 149 const SkColor kDefaultTitleColor = SkColorSetRGB(0xf2, 0xf2, 0xf2); |
148 const SkColor kDefaultUnavailableColor = SkColorSetRGB(0xbb, 0x77, 0x77); | 150 const SkColor kDefaultUnavailableColor = SkColorSetRGB(0xbb, 0x77, 0x77); |
| 151 const int kIconSize = 32; |
149 | 152 |
150 } // namespace | 153 } // namespace |
151 | 154 |
152 // A web view for athena's web activity. Note that AthenaWebView will create its | 155 // A web view for athena's web activity. Note that AthenaWebView will create its |
153 // own content so that it can eject and reload it. | 156 // own content so that it can eject and reload it. |
154 class AthenaWebView : public views::WebView { | 157 class AthenaWebView : public views::WebView { |
155 public: | 158 public: |
156 AthenaWebView(content::BrowserContext* context) | 159 AthenaWebView(content::BrowserContext* context) |
157 : views::WebView(context), controller_(new WebActivityController(this)), | 160 : views::WebView(context), controller_(new WebActivityController(this)), |
158 fullscreen_(false) { | 161 fullscreen_(false) { |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 }; | 343 }; |
341 | 344 |
342 WebActivity::WebActivity(content::BrowserContext* browser_context, | 345 WebActivity::WebActivity(content::BrowserContext* browser_context, |
343 const base::string16& title, | 346 const base::string16& title, |
344 const GURL& url) | 347 const GURL& url) |
345 : browser_context_(browser_context), | 348 : browser_context_(browser_context), |
346 title_(title), | 349 title_(title), |
347 url_(url), | 350 url_(url), |
348 web_view_(NULL), | 351 web_view_(NULL), |
349 title_color_(kDefaultTitleColor), | 352 title_color_(kDefaultTitleColor), |
350 current_state_(ACTIVITY_UNLOADED) { | 353 current_state_(ACTIVITY_UNLOADED), |
| 354 weak_ptr_factory_(this) { |
351 } | 355 } |
352 | 356 |
353 WebActivity::WebActivity(AthenaWebView* web_view) | 357 WebActivity::WebActivity(AthenaWebView* web_view) |
354 : browser_context_(web_view->browser_context()), | 358 : browser_context_(web_view->browser_context()), |
355 url_(web_view->GetWebContents()->GetURL()), | 359 url_(web_view->GetWebContents()->GetURL()), |
356 web_view_(web_view), | 360 web_view_(web_view), |
357 current_state_(ACTIVITY_UNLOADED) { | 361 current_state_(ACTIVITY_UNLOADED), |
| 362 weak_ptr_factory_(this) { |
358 // Transition to state ACTIVITY_INVISIBLE to perform the same setup steps | 363 // Transition to state ACTIVITY_INVISIBLE to perform the same setup steps |
359 // as on new activities (namely adding a WebContentsObserver). | 364 // as on new activities (namely adding a WebContentsObserver). |
360 SetCurrentState(ACTIVITY_INVISIBLE); | 365 SetCurrentState(ACTIVITY_INVISIBLE); |
361 } | 366 } |
362 | 367 |
363 WebActivity::~WebActivity() { | 368 WebActivity::~WebActivity() { |
364 // It is not required to change the activity state to UNLOADED - unless we | 369 // It is not required to change the activity state to UNLOADED - unless we |
365 // would add state observers. | 370 // would add state observers. |
366 } | 371 } |
367 | 372 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 aura::Window* WebActivity::GetWindow() { | 434 aura::Window* WebActivity::GetWindow() { |
430 return !web_view_ ? NULL : web_view_->GetWidget()->GetNativeWindow(); | 435 return !web_view_ ? NULL : web_view_->GetWidget()->GetNativeWindow(); |
431 } | 436 } |
432 | 437 |
433 void WebActivity::Init() { | 438 void WebActivity::Init() { |
434 DCHECK(web_view_); | 439 DCHECK(web_view_); |
435 web_view_->InstallAccelerators(); | 440 web_view_->InstallAccelerators(); |
436 } | 441 } |
437 | 442 |
438 SkColor WebActivity::GetRepresentativeColor() const { | 443 SkColor WebActivity::GetRepresentativeColor() const { |
439 // TODO(sad): Compute the color from the favicon. | |
440 return web_view_ ? title_color_ : kDefaultUnavailableColor; | 444 return web_view_ ? title_color_ : kDefaultUnavailableColor; |
441 } | 445 } |
442 | 446 |
443 base::string16 WebActivity::GetTitle() const { | 447 base::string16 WebActivity::GetTitle() const { |
444 if (!title_.empty()) | 448 if (!title_.empty()) |
445 return title_; | 449 return title_; |
446 // TODO(oshima): Use title set by the web contents. | 450 // TODO(oshima): Use title set by the web contents. |
447 return web_view_ ? base::UTF8ToUTF16( | 451 return web_view_ ? base::UTF8ToUTF16( |
448 web_view_->GetWebContents()->GetVisibleURL().host()) | 452 web_view_->GetWebContents()->GetVisibleURL().host()) |
449 : base::string16(); | 453 : base::string16(); |
450 } | 454 } |
451 | 455 |
| 456 gfx::ImageSkia WebActivity::GetIcon() const { |
| 457 return icon_; |
| 458 } |
| 459 |
452 bool WebActivity::UsesFrame() const { | 460 bool WebActivity::UsesFrame() const { |
453 return true; | 461 return true; |
454 } | 462 } |
455 | 463 |
456 views::View* WebActivity::GetContentsView() { | 464 views::View* WebActivity::GetContentsView() { |
457 if (!web_view_) { | 465 if (!web_view_) { |
458 web_view_ = new AthenaWebView(browser_context_); | 466 web_view_ = new AthenaWebView(browser_context_); |
459 web_view_->LoadInitialURL(url_); | 467 web_view_->LoadInitialURL(url_); |
460 // Make sure the content gets properly shown. | 468 // Make sure the content gets properly shown. |
461 if (current_state_ == ACTIVITY_VISIBLE) { | 469 if (current_state_ == ACTIVITY_VISIBLE) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 void WebActivity::ResetContentsView() { | 501 void WebActivity::ResetContentsView() { |
494 web_view_->SetFastResize(false); | 502 web_view_->SetFastResize(false); |
495 web_view_->Layout(); | 503 web_view_->Layout(); |
496 } | 504 } |
497 | 505 |
498 void WebActivity::TitleWasSet(content::NavigationEntry* entry, | 506 void WebActivity::TitleWasSet(content::NavigationEntry* entry, |
499 bool explicit_set) { | 507 bool explicit_set) { |
500 ActivityManager::Get()->UpdateActivity(this); | 508 ActivityManager::Get()->UpdateActivity(this); |
501 } | 509 } |
502 | 510 |
| 511 void WebActivity::DidNavigateMainFrame( |
| 512 const content::LoadCommittedDetails& details, |
| 513 const content::FrameNavigateParams& params) { |
| 514 // Prevent old image requests from calling back to OnDidDownloadFavicon(). |
| 515 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 516 |
| 517 icon_ = gfx::ImageSkia(); |
| 518 ActivityManager::Get()->UpdateActivity(this); |
| 519 } |
| 520 |
503 void WebActivity::DidUpdateFaviconURL( | 521 void WebActivity::DidUpdateFaviconURL( |
504 const std::vector<content::FaviconURL>& candidates) { | 522 const std::vector<content::FaviconURL>& candidates) { |
| 523 // Pick an arbitrary favicon of type FAVICON to use. |
| 524 // TODO(pkotwicz): Do something better once the favicon code is componentized. |
| 525 // (crbug.com/401997) |
| 526 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 527 for (size_t i = 0; i < candidates.size(); ++i) { |
| 528 if (candidates[i].icon_type == content::FaviconURL::FAVICON) { |
| 529 web_view_->GetWebContents()->DownloadImage( |
| 530 candidates[i].icon_url, |
| 531 true, |
| 532 0, |
| 533 base::Bind(&WebActivity::OnDidDownloadFavicon, |
| 534 weak_ptr_factory_.GetWeakPtr())); |
| 535 break; |
| 536 } |
| 537 } |
| 538 } |
| 539 |
| 540 void WebActivity::OnDidDownloadFavicon( |
| 541 int id, |
| 542 int http_status_code, |
| 543 const GURL& url, |
| 544 const std::vector<SkBitmap>& bitmaps, |
| 545 const std::vector<gfx::Size>& original_bitmap_sizes) { |
| 546 icon_ = CreateFaviconImageSkia( |
| 547 bitmaps, original_bitmap_sizes, kIconSize, NULL); |
505 ActivityManager::Get()->UpdateActivity(this); | 548 ActivityManager::Get()->UpdateActivity(this); |
506 } | 549 } |
507 | 550 |
508 void WebActivity::DidChangeThemeColor(SkColor theme_color) { | 551 void WebActivity::DidChangeThemeColor(SkColor theme_color) { |
509 title_color_ = theme_color; | 552 title_color_ = theme_color; |
510 ActivityManager::Get()->UpdateActivity(this); | 553 ActivityManager::Get()->UpdateActivity(this); |
511 } | 554 } |
512 | 555 |
513 void WebActivity::MakeVisible() { | 556 void WebActivity::MakeVisible() { |
514 // TODO(skuhne): Once we know how to handle the Overview mode, this has to | 557 // TODO(skuhne): Once we know how to handle the Overview mode, this has to |
(...skipping 30 matching lines...) Expand all Loading... |
545 | 588 |
546 void WebActivity::ReloadAndObserve() { | 589 void WebActivity::ReloadAndObserve() { |
547 if (web_view_->IsContentEvicted()) { | 590 if (web_view_->IsContentEvicted()) { |
548 DCHECK_EQ(ACTIVITY_UNLOADED, current_state_); | 591 DCHECK_EQ(ACTIVITY_UNLOADED, current_state_); |
549 web_view_->ReloadContent(); | 592 web_view_->ReloadContent(); |
550 } | 593 } |
551 Observe(web_view_->GetWebContents()); | 594 Observe(web_view_->GetWebContents()); |
552 } | 595 } |
553 | 596 |
554 } // namespace athena | 597 } // namespace athena |
OLD | NEW |