app_activity.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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
3// found in the LICENSE file.
4
5#include "athena/content/app_activity.h"
6
7#include "athena/activity/public/activity_manager.h"
8#include "content/public/browser/web_contents.h"
9#include "extensions/shell/browser/shell_app_window.h"
10#include "ui/views/controls/webview/webview.h"
11
12namespace athena {
13
14// TODO(mukai): specifies the same accelerators of WebActivity.
15AppActivity::AppActivity(extensions::ShellAppWindow* app_window)
16    : app_window_(app_window),
17      web_view_(NULL),
18      current_state_(ACTIVITY_UNLOADED) {
19  DCHECK(app_window_);
20}
21
22AppActivity::~AppActivity() {
23  if (GetCurrentState() != ACTIVITY_UNLOADED)
24    SetCurrentState(ACTIVITY_UNLOADED);
25}
26
27ActivityViewModel* AppActivity::GetActivityViewModel() {
28  return this;
29}
30
31void AppActivity::SetCurrentState(Activity::ActivityState state) {
32  switch (state) {
33    case ACTIVITY_VISIBLE:
34      // Fall through (for the moment).
35    case ACTIVITY_INVISIBLE:
36      // By clearing the overview mode image we allow the content to be shown.
37      overview_mode_image_ = gfx::ImageSkia();
38      // TODO(skuhne): Find out how to reload an app from the extension system.
39      break;
40    case ACTIVITY_BACKGROUND_LOW_PRIORITY:
41      DCHECK(ACTIVITY_VISIBLE == current_state_ ||
42             ACTIVITY_INVISIBLE == current_state_);
43      // TODO(skuhne): Do this.
44      break;
45    case ACTIVITY_PERSISTENT:
46      DCHECK_EQ(ACTIVITY_BACKGROUND_LOW_PRIORITY, current_state_);
47      // TODO(skuhne): Do this.
48      break;
49    case ACTIVITY_UNLOADED:
50      DCHECK_NE(ACTIVITY_UNLOADED, current_state_);
51      // TODO(skuhne): Find out how to evict an app from the extension system.
52      //      web_view_->EvictContent();
53      break;
54  }
55  // Remember the last requested state.
56  current_state_ = state;
57}
58
59Activity::ActivityState AppActivity::GetCurrentState() {
60  // TODO(skuhne): Check here also eviction status.
61  if (!web_view_) {
62    DCHECK_EQ(ACTIVITY_UNLOADED, current_state_);
63    return ACTIVITY_UNLOADED;
64  }
65  // TODO(skuhne): This should be controlled by an observer and should not
66  // reside here.
67  if (IsVisible() && current_state_ != ACTIVITY_VISIBLE)
68    SetCurrentState(ACTIVITY_VISIBLE);
69  // Note: If the activity is not visible it does not necessarily mean that it
70  // does not have GPU compositor resources (yet).
71  return current_state_;
72}
73
74bool AppActivity::IsVisible() {
75  return web_view_ && web_view_->IsDrawn();
76}
77
78Activity::ActivityMediaState AppActivity::GetMediaState() {
79  // TODO(skuhne): The function GetTabMediaStateForContents(WebContents),
80  // and the AudioStreamMonitor needs to be moved from Chrome into contents to
81  // make it more modular and so that we can use it from here.
82  return Activity::ACTIVITY_MEDIA_STATE_NONE;
83}
84
85void AppActivity::Init() {
86}
87
88SkColor AppActivity::GetRepresentativeColor() const {
89  // TODO(sad): Compute the color from the favicon.
90  return SK_ColorGRAY;
91}
92
93base::string16 AppActivity::GetTitle() const {
94  return web_view_->GetWebContents()->GetTitle();
95}
96
97bool AppActivity::UsesFrame() const {
98  return false;
99}
100
101views::View* AppActivity::GetContentsView() {
102  if (!web_view_) {
103    // TODO(oshima): use apps::NativeAppWindowViews
104    content::WebContents* web_contents =
105        app_window_->GetAssociatedWebContents();
106    web_view_ = new views::WebView(web_contents->GetBrowserContext());
107    web_view_->SetWebContents(web_contents);
108    SetCurrentState(ACTIVITY_INVISIBLE);
109    Observe(web_contents);
110    overview_mode_image_ = gfx::ImageSkia();
111  }
112  return web_view_;
113}
114
115void AppActivity::CreateOverviewModeImage() {
116  // TODO(skuhne): Implement this!
117}
118
119gfx::ImageSkia AppActivity::GetOverviewModeImage() {
120  return overview_mode_image_;
121}
122
123void AppActivity::TitleWasSet(content::NavigationEntry* entry,
124                              bool explicit_set) {
125  ActivityManager::Get()->UpdateActivity(this);
126}
127
128void AppActivity::DidUpdateFaviconURL(
129    const std::vector<content::FaviconURL>& candidates) {
130  ActivityManager::Get()->UpdateActivity(this);
131}
132
133}  // namespace athena
134