Home | History | Annotate | Download | only in browser
      1 // Copyright 2013 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 EXTENSIONS_BROWSER_PROCESS_MAP_H_
      6 #define EXTENSIONS_BROWSER_PROCESS_MAP_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 
     13 namespace extensions {
     14 
     15 // Contains information about which extensions are assigned to which processes.
     16 //
     17 // The relationship between extensions and processes is complex:
     18 //
     19 // - Extensions can be either "split" mode or "spanning" mode.
     20 // - In spanning mode, extensions share a single process between all incognito
     21 //   and normal windows. This was the original mode for extensions.
     22 // - In split mode, extensions have separate processes in incognito windows.
     23 // - There are also hosted apps, which are a kind of extensions, and those
     24 //   usually have a process model similar to normal web sites: multiple
     25 //   processes per-profile.
     26 // - A single hosted app can have more than one SiteInstance in the same process
     27 //   if we're over the process limit and force them to share a process.
     28 //
     29 // In general, we seem to play with the process model of extensions a lot, so
     30 // it is safest to assume it is many-to-many in most places in the codebase.
     31 //
     32 // Note that because of content scripts, frames, and other edge cases in
     33 // Chrome's process isolation, extension code can still end up running outside
     34 // an assigned process.
     35 //
     36 // But we only allow high-privilege operations to be performed by an extension
     37 // when it is running in an assigned process.
     38 //
     39 // ===========================================================================
     40 // WARNINGS - PLEASE UNDERSTAND THESE BEFORE CALLING OR MODIFYING THIS CLASS
     41 // ===========================================================================
     42 //
     43 // 1. This class contains the processes for hosted apps as well as extensions
     44 //    and packaged apps. Just because a process is present here *does not* mean
     45 //    it is an "extension process" (e.g., for UI purposes). It may contain only
     46 //    hosted apps. See crbug.com/102533.
     47 //
     48 // 2. An extension can show up in multiple processes. That is why there is no
     49 //    GetExtensionProcess() method here. There are two cases: a) The extension
     50 //    is actually a hosted app, in which case this is normal, or b) there is an
     51 //    incognito window open and the extension is "split mode". It is *not safe*
     52 //    to assume that there is one process per extension. If you only care about
     53 //    extensions (not hosted apps), and you are on the UI thread, and you don't
     54 //    care about incognito version of this extension (or vice versa if you're in
     55 //    an incognito profile) then use
     56 //    extensions::ProcessManager::GetSiteInstanceForURL()->[Has|Get]Process().
     57 //
     58 // 3. The process ids contained in this class are *not limited* to the Profile
     59 //    you got this map from. They can also be associated with that profile's
     60 //    incognito/normal twin. If you care about this, use
     61 //    RenderProcessHost::FromID() and check the profile of the resulting object.
     62 //
     63 // TODO(aa): The above warnings suggest this class could use improvement :).
     64 class ProcessMap {
     65  public:
     66   ProcessMap();
     67   ~ProcessMap();
     68 
     69   size_t size() const { return items_.size(); }
     70 
     71   bool Insert(const std::string& extension_id, int process_id,
     72               int site_instance_id);
     73 
     74   bool Remove(const std::string& extension_id, int process_id,
     75               int site_instance_id);
     76   int RemoveAllFromProcess(int process_id);
     77 
     78   bool Contains(const std::string& extension_id, int process_id) const;
     79   bool Contains(int process_id) const;
     80 
     81   std::set<std::string> GetExtensionsInProcess(int process_id) const;
     82 
     83  private:
     84   struct Item;
     85 
     86   typedef std::set<Item> ItemSet;
     87   ItemSet items_;
     88 
     89   DISALLOW_COPY_AND_ASSIGN(ProcessMap);
     90 };
     91 
     92 }  // extensions
     93 
     94 #endif  // EXTENSIONS_BROWSER_PROCESS_MAP_H_
     95