1// Copyright (c) 2011 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 CHROME_BROWSER_EXTENSIONS_EXTENSION_WEB_UI_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_WEB_UI_H_
7#pragma once
8
9#include <string>
10
11#include "base/memory/scoped_ptr.h"
12#include "chrome/browser/extensions/extension_bookmark_manager_api.h"
13#include "chrome/browser/extensions/extension_function_dispatcher.h"
14#include "chrome/browser/favicon_service.h"
15#include "chrome/common/extensions/extension.h"
16#include "content/browser/webui/web_ui.h"
17
18class GURL;
19class ListValue;
20class PrefService;
21class Profile;
22class RenderViewHost;
23class TabContents;
24struct ExtensionHostMsg_DomMessage_Params;
25
26// This class implements WebUI for extensions and allows extensions to put UI in
27// the main tab contents area. For example, each extension can specify an
28// "options_page", and that page is displayed in the tab contents area and is
29// hosted by this class.
30class ExtensionWebUI
31    : public WebUI,
32      public ExtensionFunctionDispatcher::Delegate {
33 public:
34  static const char kExtensionURLOverrides[];
35
36  explicit ExtensionWebUI(TabContents* tab_contents, const GURL& url);
37
38  virtual ~ExtensionWebUI();
39
40  ExtensionFunctionDispatcher* extension_function_dispatcher() const {
41    return extension_function_dispatcher_.get();
42  }
43
44  // WebUI
45  virtual void RenderViewCreated(RenderViewHost* render_view_host);
46  virtual void RenderViewReused(RenderViewHost* render_view_host);
47  virtual void ProcessWebUIMessage(
48      const ExtensionHostMsg_DomMessage_Params& params);
49
50  // ExtensionFunctionDispatcher::Delegate
51  virtual Browser* GetBrowser();
52  virtual gfx::NativeView GetNativeViewOfHost();
53  virtual gfx::NativeWindow GetCustomFrameNativeWindow();
54  virtual TabContents* associated_tab_contents() const;
55
56  virtual ExtensionBookmarkManagerEventRouter*
57      extension_bookmark_manager_event_router();
58
59  // BrowserURLHandler
60  static bool HandleChromeURLOverride(GURL* url, Profile* profile);
61
62  // Register and unregister a dictionary of one or more overrides.
63  // Page names are the keys, and chrome-extension: URLs are the values.
64  // (e.g. { "newtab": "chrome-extension://<id>/my_new_tab.html" }
65  static void RegisterChromeURLOverrides(Profile* profile,
66      const Extension::URLOverrideMap& overrides);
67  static void UnregisterChromeURLOverrides(Profile* profile,
68      const Extension::URLOverrideMap& overrides);
69  static void UnregisterChromeURLOverride(const std::string& page,
70                                          Profile* profile,
71                                          Value* override);
72
73  // Called from BrowserPrefs
74  static void RegisterUserPrefs(PrefService* prefs);
75
76  // Get the favicon for the extension by getting an icon from the manifest.
77  static void GetFaviconForURL(Profile* profile,
78                               FaviconService::GetFaviconRequest* request,
79                               const GURL& page_url);
80
81 private:
82  // Unregister the specified override, and if it's the currently active one,
83  // ensure that something takes its place.
84  static void UnregisterAndReplaceOverride(const std::string& page,
85                                           Profile* profile,
86                                           ListValue* list,
87                                           Value* override);
88
89  // When the RenderViewHost changes (RenderViewCreated and RenderViewReused),
90  // we need to reset the ExtensionFunctionDispatcher so it's talking to the
91  // right one, as well as being linked to the correct URL.
92  void ResetExtensionFunctionDispatcher(RenderViewHost* render_view_host);
93
94  void ResetExtensionBookmarkManagerEventRouter();
95
96  scoped_ptr<ExtensionFunctionDispatcher> extension_function_dispatcher_;
97
98  // TODO(aa): This seems out of place. Why is it not with the event routers for
99  // the other extension APIs?
100  scoped_ptr<ExtensionBookmarkManagerEventRouter>
101      extension_bookmark_manager_event_router_;
102
103  // The URL this WebUI was created for.
104  GURL url_;
105};
106
107#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEB_UI_H_
108