app_activity.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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 "athena/content/app_activity_registry.h"
9#include "athena/content/public/app_content_control_delegate.h"
10#include "athena/content/public/app_registry.h"
11#include "content/public/browser/web_contents.h"
12#include "extensions/shell/browser/shell_app_window.h"
13#include "ui/views/controls/webview/webview.h"
14#include "ui/views/widget/widget.h"
15
16namespace athena {
17
18// TODO(mukai): specifies the same accelerators of WebActivity.
19AppActivity::AppActivity(extensions::ShellAppWindow* app_window)
20    : app_window_(app_window),
21      web_view_(NULL),
22      current_state_(ACTIVITY_UNLOADED),
23      app_activity_registry_(NULL) {
24}
25
26AppActivity::~AppActivity() {
27  // If this activity is registered, we unregister it now.
28  if (app_activity_registry_)
29    app_activity_registry_->UnregisterAppActivity(this);
30}
31
32ActivityViewModel* AppActivity::GetActivityViewModel() {
33  return this;
34}
35
36void AppActivity::SetCurrentState(Activity::ActivityState state) {
37  ActivityState current_state = state;
38  // Remember the last requested state now so that a call to GetCurrentState()
39  // returns the new state.
40  current_state_ = state;
41
42  switch (state) {
43    case ACTIVITY_VISIBLE:
44      // Fall through (for the moment).
45    case ACTIVITY_INVISIBLE:
46      // By clearing the overview mode image we allow the content to be shown.
47      overview_mode_image_ = gfx::ImageSkia();
48      // Note: A reload from the unloaded state will be performed through the
49      // |AppActivityProxy| object and no further action isn't necessary here.
50      break;
51    case ACTIVITY_BACKGROUND_LOW_PRIORITY:
52      DCHECK(ACTIVITY_VISIBLE == current_state ||
53             ACTIVITY_INVISIBLE == current_state);
54      // TODO(skuhne): Do this.
55      break;
56    case ACTIVITY_PERSISTENT:
57      DCHECK_EQ(ACTIVITY_BACKGROUND_LOW_PRIORITY, current_state);
58      // TODO(skuhne): Do this.
59      break;
60    case ACTIVITY_UNLOADED:
61      DCHECK_NE(ACTIVITY_UNLOADED, current_state);
62      // This will cause the application to shut down, close its windows and
63      // delete this object. Instead a |AppActivityProxy| will be created as
64      // place holder.
65      if (app_activity_registry_)
66        app_activity_registry_->Unload();
67      break;
68  }
69}
70
71Activity::ActivityState AppActivity::GetCurrentState() {
72  if (!web_view_) {
73    DCHECK_EQ(ACTIVITY_UNLOADED, current_state_);
74    return ACTIVITY_UNLOADED;
75  }
76  // TODO(skuhne): This should be controlled by an observer and should not
77  // reside here.
78  if (IsVisible() && current_state_ != ACTIVITY_VISIBLE)
79    SetCurrentState(ACTIVITY_VISIBLE);
80  // Note: If the activity is not visible it does not necessarily mean that it
81  // does not have GPU compositor resources (yet).
82  return current_state_;
83}
84
85bool AppActivity::IsVisible() {
86  return web_view_ && web_view_->IsDrawn();
87}
88
89Activity::ActivityMediaState AppActivity::GetMediaState() {
90  // TODO(skuhne): The function GetTabMediaStateForContents(WebContents),
91  // and the AudioStreamMonitor needs to be moved from Chrome into contents to
92  // make it more modular and so that we can use it from here.
93  return Activity::ACTIVITY_MEDIA_STATE_NONE;
94}
95
96aura::Window* AppActivity::GetWindow() {
97  return !web_view_ ? NULL : web_view_->GetWidget()->GetNativeWindow();
98}
99
100void AppActivity::Init() {
101}
102
103SkColor AppActivity::GetRepresentativeColor() const {
104  // TODO(sad): Compute the color from the favicon.
105  return SK_ColorGRAY;
106}
107
108base::string16 AppActivity::GetTitle() const {
109  return web_view_->GetWebContents()->GetTitle();
110}
111
112bool AppActivity::UsesFrame() const {
113  return false;
114}
115
116views::View* AppActivity::GetContentsView() {
117  if (!web_view_) {
118    // TODO(oshima): use apps::NativeAppWindowViews
119    content::WebContents* web_contents =
120        app_window_->GetAssociatedWebContents();
121    web_view_ = new views::WebView(web_contents->GetBrowserContext());
122    web_view_->SetWebContents(web_contents);
123    SetCurrentState(ACTIVITY_INVISIBLE);
124    Observe(web_contents);
125    overview_mode_image_ = gfx::ImageSkia();
126  }
127  return web_view_;
128}
129
130void AppActivity::CreateOverviewModeImage() {
131  // TODO(skuhne): Implement this!
132}
133
134gfx::ImageSkia AppActivity::GetOverviewModeImage() {
135  return overview_mode_image_;
136}
137
138void AppActivity::TitleWasSet(content::NavigationEntry* entry,
139                              bool explicit_set) {
140  ActivityManager::Get()->UpdateActivity(this);
141}
142
143void AppActivity::DidUpdateFaviconURL(
144    const std::vector<content::FaviconURL>& candidates) {
145  ActivityManager::Get()->UpdateActivity(this);
146}
147
148void AppActivity::DidStartNavigationToPendingEntry(
149      const GURL& url,
150      content::NavigationController::ReloadType reload_type) {
151  if (!app_activity_registry_)
152    RegisterActivity();
153}
154
155// Register an |activity| with an application.
156// Note: This should only get called once for an |app_window| of the
157// |activity|.
158void AppActivity::RegisterActivity() {
159  content::WebContents* web_contents = app_window_->GetAssociatedWebContents();
160  AppRegistry* app_registry = AppRegistry::Get();
161  // Get the application's registry.
162  app_activity_registry_ = app_registry->GetAppActivityRegistry(
163          app_registry->GetDelegate()->GetApplicationID(web_contents),
164          web_contents->GetBrowserContext());
165  DCHECK(app_activity_registry_);
166  // Register the activity.
167  app_activity_registry_->RegisterAppActivity(this);
168}
169
170}  // namespace athena
171