Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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_RENDERER_EXTENSIONS_EXTENSION_HELPER_H_
      6 #define CHROME_RENDERER_EXTENSIONS_EXTENSION_HELPER_H_
      7 
      8 #include <map>
      9 #include <vector>
     10 
     11 #include "base/memory/linked_ptr.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "content/public/common/console_message_level.h"
     14 #include "content/public/renderer/render_view_observer.h"
     15 #include "content/public/renderer/render_view_observer_tracker.h"
     16 #include "extensions/common/view_type.h"
     17 #include "third_party/WebKit/public/platform/WebURLResponse.h"
     18 
     19 class GURL;
     20 class SkBitmap;
     21 struct ExtensionMsg_ExecuteCode_Params;
     22 struct ExtensionMsg_ExternalConnectionInfo;
     23 struct WebApplicationInfo;
     24 
     25 namespace base {
     26 class DictionaryValue;
     27 class ListValue;
     28 }
     29 
     30 namespace extensions {
     31 class Dispatcher;
     32 
     33 // RenderView-level plumbing for extension features.
     34 class ExtensionHelper
     35     : public content::RenderViewObserver,
     36       public content::RenderViewObserverTracker<ExtensionHelper> {
     37  public:
     38   // Returns a list of extension RenderViews that match the given filter
     39   // criteria. If |browser_window_id| is not extension_misc::kUnknownWindowId,
     40   // the list is restricted to views in that browser window.
     41   static std::vector<content::RenderView*> GetExtensionViews(
     42       const std::string& extension_id,
     43       int browser_window_id,
     44       ViewType view_type);
     45 
     46   // Returns the given extension's background page, or NULL if none.
     47   static content::RenderView* GetBackgroundPage(
     48       const std::string& extension_id);
     49 
     50   ExtensionHelper(content::RenderView* render_view, Dispatcher* dispatcher);
     51   virtual ~ExtensionHelper();
     52 
     53   int tab_id() const { return tab_id_; }
     54   int browser_window_id() const { return browser_window_id_; }
     55   ViewType view_type() const { return view_type_; }
     56   Dispatcher* dispatcher() const { return dispatcher_; }
     57 
     58  private:
     59   // RenderViewObserver implementation.
     60   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     61   virtual void DidFinishDocumentLoad(WebKit::WebFrame* frame) OVERRIDE;
     62   virtual void DidFinishLoad(WebKit::WebFrame* frame) OVERRIDE;
     63   virtual void DidCreateDocumentElement(WebKit::WebFrame* frame) OVERRIDE;
     64   virtual void DidStartProvisionalLoad(WebKit::WebFrame* frame) OVERRIDE;
     65   virtual void FrameDetached(WebKit::WebFrame* frame) OVERRIDE;
     66   virtual void DidCreateDataSource(WebKit::WebFrame* frame,
     67                                    WebKit::WebDataSource* ds) OVERRIDE;
     68   virtual void DraggableRegionsChanged(WebKit::WebFrame* frame) OVERRIDE;
     69 
     70   void OnExtensionResponse(int request_id, bool success,
     71                            const base::ListValue& response,
     72                            const std::string& error);
     73   void OnExtensionMessageInvoke(const std::string& extension_id,
     74                                 const std::string& module_name,
     75                                 const std::string& function_name,
     76                                 const base::ListValue& args,
     77                                 bool user_gesture);
     78   void OnExtensionDispatchOnConnect(
     79       int target_port_id,
     80       const std::string& channel_name,
     81       const base::DictionaryValue& source_tab,
     82       const ExtensionMsg_ExternalConnectionInfo& info);
     83   void OnExtensionDeliverMessage(int target_port_id,
     84                                  const std::string& message);
     85   void OnExtensionDispatchOnDisconnect(int port_id,
     86                                        const std::string& error_message);
     87   void OnExecuteCode(const ExtensionMsg_ExecuteCode_Params& params);
     88   void OnGetApplicationInfo(int page_id);
     89   void OnNotifyRendererViewType(ViewType view_type);
     90   void OnSetTabId(int tab_id);
     91   void OnUpdateBrowserWindowId(int window_id);
     92   void OnAddMessageToConsole(content::ConsoleMessageLevel level,
     93                              const std::string& message);
     94   void OnAppWindowClosed();
     95 
     96   Dispatcher* dispatcher_;
     97 
     98   // The app info that we are processing. This is used when installing an app
     99   // via application definition. The in-progress web app is stored here while
    100   // its manifest and icons are downloaded.
    101   scoped_ptr<WebApplicationInfo> pending_app_info_;
    102 
    103   // The number of app icon requests outstanding. When this reaches zero, we're
    104   // done processing an app definition file.
    105   int pending_app_icon_requests_;
    106 
    107   // Type of view attached with RenderView.
    108   ViewType view_type_;
    109 
    110   // Id of the tab which the RenderView is attached to.
    111   int tab_id_;
    112 
    113   // Id number of browser window which RenderView is attached to.
    114   int browser_window_id_;
    115 
    116   DISALLOW_COPY_AND_ASSIGN(ExtensionHelper);
    117 };
    118 
    119 }  // namespace extensions
    120 
    121 #endif  // CHROME_RENDERER_EXTENSIONS_EXTENSION_HELPER_H_
    122