1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Use of this source code is governed by a BSD-style license that can be
3ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// found in the LICENSE file.
4ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
5ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_HELPER_H_
6ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#define CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_HELPER_H_
7ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#pragma once
8ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
9ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "content/browser/tab_contents/tab_contents_observer.h"
10ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/extensions/image_loading_tracker.h"
11ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/common/web_apps.h"
12ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "third_party/skia/include/core/SkBitmap.h"
13ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
14ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass Extension;
15ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass TabContentsWrapper;
16ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenstruct WebApplicationInfo;
17ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
18ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Per-tab extension helper. Also handles non-extension apps.
19ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass ExtensionTabHelper : public TabContentsObserver,
20ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                           public ImageLoadingTracker::Observer {
21ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen public:
22ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  explicit ExtensionTabHelper(TabContentsWrapper* wrapper);
23ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  virtual ~ExtensionTabHelper();
24ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
25ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Copies the internal state from another ExtensionTabHelper.
26ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void CopyStateFrom(const ExtensionTabHelper& source);
27ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
28ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Call this after updating a page action to notify clients about the changes.
29ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void PageActionStateChanged();
30ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
31ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Requests application info for the specified page. This is an asynchronous
32ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // request. The delegate is notified by way of OnDidGetApplicationInfo when
33ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // the data is available.
34ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void GetApplicationInfo(int32 page_id);
35ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
36ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // App extensions ------------------------------------------------------------
37ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
38ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Sets the extension denoting this as an app. If |extension| is non-null this
39ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // tab becomes an app-tab. TabContents does not listen for unload events for
40ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // the extension. It's up to consumers of TabContents to do that.
41ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  //
42ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // NOTE: this should only be manipulated before the tab is added to a browser.
43ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // TODO(sky): resolve if this is the right way to identify an app tab. If it
44ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // is, than this should be passed in the constructor.
45ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void SetExtensionApp(const Extension* extension);
46ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
47ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Convenience for setting the app extension by id. This does nothing if
48ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // |extension_app_id| is empty, or an extension can't be found given the
49ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // specified id.
50ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void SetExtensionAppById(const std::string& extension_app_id);
51ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
52ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  const Extension* extension_app() const { return extension_app_; }
53ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool is_app() const { return extension_app_ != NULL; }
54ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  const WebApplicationInfo& web_app_info() const {
55ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return web_app_info_;
56ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
57ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
58ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // If an app extension has been explicitly set for this TabContents its icon
59ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // is returned.
60ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  //
61ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // NOTE: the returned icon is larger than 16x16 (its size is
62ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Extension::EXTENSION_ICON_SMALLISH).
63ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  SkBitmap* GetExtensionAppIcon();
64ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
65ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  TabContents* tab_contents() const {
66ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      return TabContentsObserver::tab_contents();
67ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
68ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
69ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Sets a non-extension app icon associated with TabContents and fires an
70ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // INVALIDATE_TITLE navigation state change to trigger repaint of title.
71ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void SetAppIcon(const SkBitmap& app_icon);
72ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
73ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen private:
74ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // TabContentsObserver overrides.
75ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  virtual void DidNavigateMainFramePostCommit(
76ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      const NavigationController::LoadCommittedDetails& details,
77ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      const ViewHostMsg_FrameNavigate_Params& params) OVERRIDE;
78ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  virtual bool OnMessageReceived(const IPC::Message& message);
79ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
80ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Message handlers.
81ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void OnDidGetApplicationInfo(int32 page_id, const WebApplicationInfo& info);
82ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void OnInstallApplication(const WebApplicationInfo& info);
83ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
84ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // App extensions related methods:
85ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
86ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Resets app_icon_ and if |extension| is non-null creates a new
87ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // ImageLoadingTracker to load the extension's image.
88ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void UpdateExtensionAppIcon(const Extension* extension);
89ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
90ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // ImageLoadingTracker::Observer.
91ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  virtual void OnImageLoaded(SkBitmap* image, const ExtensionResource& resource,
92ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                             int index);
93ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
94ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Data for app extensions ---------------------------------------------------
95ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
96ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // If non-null this tab is an app tab and this is the extension the tab was
97ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // created for.
98ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  const Extension* extension_app_;
99ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
100ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Icon for extension_app_ (if non-null) or a manually-set icon for
101ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // non-extension apps.
102ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  SkBitmap extension_app_icon_;
103ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
104ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Used for loading extension_app_icon_.
105ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  scoped_ptr<ImageLoadingTracker> extension_app_image_loader_;
106ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
107ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Cached web app info data.
108ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  WebApplicationInfo web_app_info_;
109ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
110ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  TabContentsWrapper* wrapper_;
111ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
112ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  DISALLOW_COPY_AND_ASSIGN(ExtensionTabHelper);
113ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen};
114ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
115ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_TAB_HELPER_H_
116