Home | History | Annotate | Download | only in webstore
      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 CHROME_BROWSER_EXTENSIONS_API_WEBSTORE_WEBSTORE_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_WEBSTORE_WEBSTORE_API_H_
      7 
      8 #include <list>
      9 #include <string>
     10 
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/scoped_observer.h"
     13 #include "chrome/browser/extensions/install_observer.h"
     14 #include "chrome/common/extensions/api/webstore/webstore_api_constants.h"
     15 #include "extensions/browser/browser_context_keyed_api_factory.h"
     16 #include "extensions/browser/event_router.h"
     17 
     18 namespace base {
     19 class ListValue;
     20 }
     21 
     22 namespace content {
     23 class BrowserContext;
     24 }
     25 
     26 namespace IPC {
     27 class Sender;
     28 }
     29 
     30 namespace extensions {
     31 class InstallTracker;
     32 
     33 class WebstoreAPI : public BrowserContextKeyedAPI,
     34                     public InstallObserver {
     35  public:
     36   explicit WebstoreAPI(content::BrowserContext* browser_context);
     37   virtual ~WebstoreAPI();
     38 
     39   static WebstoreAPI* Get(content::BrowserContext* browser_context);
     40 
     41   // Called whenever an inline extension install is started. Examines
     42   // |listener_mask| to determine if a download progress or install
     43   // stage listener should be added.
     44   // |routing_id| refers to the id to which we send any return messages;
     45   // |ipc_sender| is the sender through which we send them (typically this
     46   // is the TabHelper which started the inline install).
     47   void OnInlineInstallStart(int routing_id,
     48                             IPC::Sender* ipc_sender,
     49                             const std::string& extension_id,
     50                             int listener_mask);
     51 
     52   // Called when an inline extension install finishes. Removes any listeners
     53   // related to the |routing_id|-|extension_id| pair.
     54   void OnInlineInstallFinished(int routing_id, const std::string& extension_id);
     55 
     56   // BrowserContextKeyedAPI implementation.
     57   static BrowserContextKeyedAPIFactory<WebstoreAPI>* GetFactoryInstance();
     58 
     59  private:
     60   friend class BrowserContextKeyedAPIFactory<WebstoreAPI>;
     61 
     62   // A simple struct to hold our listeners' information for each observed
     63   // install.
     64   struct ObservedInstallInfo;
     65   typedef std::list<ObservedInstallInfo> ObservedInstallInfoList;
     66 
     67   // Sends an installation stage update message if we are observing
     68   // the extension's install.
     69   void SendInstallMessageIfObserved(const std::string& extension_id,
     70                                     api::webstore::InstallStage install_stage);
     71 
     72   // Removes listeners for the given |extension_id|-|routing_id| pair from
     73   // |listeners|.
     74   void RemoveListeners(int routing_id,
     75                        const std::string& extension_id,
     76                        ObservedInstallInfoList* listeners);
     77 
     78   // InstallObserver implementation.
     79   virtual void OnBeginExtensionDownload(const std::string& extension_id)
     80       OVERRIDE;
     81   virtual void OnDownloadProgress(const std::string& extension_id,
     82                                   int percent_downloaded) OVERRIDE;
     83   virtual void OnBeginCrxInstall(const std::string& extension_id) OVERRIDE;
     84   virtual void OnShutdown() OVERRIDE;
     85 
     86   // BrowserContextKeyedService implementation.
     87   virtual void Shutdown() OVERRIDE;
     88 
     89   // BrowserContextKeyedAPI implementation.
     90   static const char* service_name() { return "WebstoreAPI"; }
     91   static const bool kServiceIsNULLWhileTesting = true;
     92 
     93   ObservedInstallInfoList download_progress_listeners_;
     94   ObservedInstallInfoList install_stage_listeners_;
     95   content::BrowserContext* browser_context_;
     96   scoped_ptr<ScopedObserver<InstallTracker, InstallObserver> >
     97       install_observer_;
     98 
     99   DISALLOW_COPY_AND_ASSIGN(WebstoreAPI);
    100 };
    101 
    102 }  // namespace extensions
    103 
    104 #endif  // CHROME_BROWSER_EXTENSIONS_API_WEBSTORE_WEBSTORE_API_H_
    105