content_proxy.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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#ifndef ATHENA_CONTENT_CONTENT_PROXY_H_
6#define ATHENA_CONTENT_CONTENT_PROXY_H_
7
8#include "base/macros.h"
9#include "base/memory/ref_counted_memory.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/memory/weak_ptr.h"
12#include "ui/gfx/image/image_skia.h"
13
14namespace views {
15class WebView;
16}
17
18namespace athena {
19
20class Activity;
21class ProxyImageData;
22
23// This object creates and holds proxy content which gets shown instead of the
24// actual web content, generated from the passed |web_view|.
25// The created |proxy_content_| will be destroyed with the destruction of this
26// object.
27// Calling EvictContent() will release the rendered content.
28// When ContentGetsDestroyed() gets called, the old view will be made visible
29// and then the link to the |web_view_| will get severed.
30class ContentProxy {
31 public:
32  // Creates the object by creating a sized down |web_view| layer and making it
33  // visible inside |activity|'s window.
34  ContentProxy(views::WebView* web_view, Activity* activity);
35  // TODO(skuhne): Add a function to create this object from a passed PNG, so
36  // that we can create it from a session restore.
37
38  // With the destruction of the object, the layer should get destroyed and the
39  // content should become visible again.
40  virtual ~ContentProxy();
41
42  // Called when the content will get unloaded.
43  void ContentWillUnload();
44
45  // Can be called to save resources by creating a layer with a solid color
46  // instead of creating a content image layer.
47  // Note: Currently the GPU does create a full size texture and fills it with
48  // the given color - so there isn't really memory savings yet.
49  void EvictContent();
50
51  // Get the image of the content. If there is no image known, an empty image
52  // will be returned.
53  gfx::ImageSkia GetContentImage();
54
55  // The content is about to get destroyed by its creator.
56  // Note: This function should only be used by AppActivity.
57  void OnPreContentDestroyed();
58
59 private:
60  // Make the original (web)content visible. This call should only be paired
61  // with HideOriginalContent.
62  void ShowOriginalContent();
63
64  // Make the content invisible. This call should only be paired with
65  // MakeVisible.
66  void HideOriginalContent();
67
68  // Creates proxy content from |web_view_|.
69  void CreateProxyContent();
70
71  // Creates an image from the current content.
72  bool CreateContentImage();
73
74  // Called once the content was read back.
75  void OnContentImageRead(bool success, const SkBitmap& bitmap);
76
77  // Called once the image content has been converted to PNG.
78  void OnContentImageEncodeComplete(scoped_refptr<ProxyImageData> image);
79
80  // The web view which was passed on creation and is associated with this
81  // object. It will be shown when OnPreContentDestroyed() gets called and then
82  // set to NULL. The ownership remains with the creator.
83  views::WebView* web_view_;
84
85  // While we are doing our PNG encode, we keep the read back image to have
86  // something which we can pass back to the overview mode. (It would make no
87  // sense to the user to see that more recent windows get painted later than
88  // older ones).
89  gfx::ImageSkia raw_image_;
90
91  // True if the content is visible.
92  bool content_visible_;
93
94  // True if the content is loaded and needs a re-layout when it gets shown
95  // again.
96  bool content_loaded_;
97
98  // True if a content creation was kicked off once. This ensures that the
99  // function is never called twice.
100  bool content_creation_called_;
101
102  // The PNG image data.
103  scoped_refptr<base::RefCountedBytes> png_data_;
104
105  // Creating an encoded image from the content will be asynchronous. Use a
106  // weakptr for the callback to make sure that the read back / encoding image
107  // completion callback does not trigger on a destroyed ContentProxy.
108  base::WeakPtrFactory<ContentProxy> proxy_content_to_image_factory_;
109
110  DISALLOW_COPY_AND_ASSIGN(ContentProxy);
111};
112
113}  // namespace athena
114
115#endif  // ATHENA_CONTENT_CONTENT_PROXY_H_
116