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 #include "extensions/browser/process_map.h"
      6 
      7 namespace extensions {
      8 
      9 // Item
     10 struct ProcessMap::Item {
     11   Item() : process_id(0), site_instance_id(0) {
     12   }
     13 
     14   // Purposely implicit constructor needed on older gcc's. See:
     15   // http://codereview.chromium.org/8769022/
     16   explicit Item(const ProcessMap::Item& other)
     17       : extension_id(other.extension_id),
     18         process_id(other.process_id),
     19         site_instance_id(other.site_instance_id) {
     20   }
     21 
     22   Item(const std::string& extension_id, int process_id,
     23        int site_instance_id)
     24       : extension_id(extension_id),
     25         process_id(process_id),
     26         site_instance_id(site_instance_id) {
     27   }
     28 
     29   ~Item() {
     30   }
     31 
     32   bool operator<(const ProcessMap::Item& other) const {
     33     if (extension_id < other.extension_id)
     34       return true;
     35 
     36     if (extension_id == other.extension_id &&
     37         process_id < other.process_id) {
     38       return true;
     39     }
     40 
     41     if (extension_id == other.extension_id &&
     42         process_id == other.process_id &&
     43         site_instance_id < other.site_instance_id) {
     44       return true;
     45     }
     46 
     47     return false;
     48   }
     49 
     50   std::string extension_id;
     51   int process_id;
     52   int site_instance_id;
     53 };
     54 
     55 
     56 // ProcessMap
     57 ProcessMap::ProcessMap() {
     58 }
     59 
     60 ProcessMap::~ProcessMap() {
     61 }
     62 
     63 bool ProcessMap::Insert(const std::string& extension_id, int process_id,
     64                         int site_instance_id) {
     65   return items_.insert(Item(extension_id, process_id, site_instance_id)).second;
     66 }
     67 
     68 bool ProcessMap::Remove(const std::string& extension_id, int process_id,
     69                         int site_instance_id) {
     70   return items_.erase(Item(extension_id, process_id, site_instance_id)) > 0;
     71 }
     72 
     73 int ProcessMap::RemoveAllFromProcess(int process_id) {
     74   int result = 0;
     75   for (ItemSet::iterator iter = items_.begin(); iter != items_.end(); ) {
     76     if (iter->process_id == process_id) {
     77       items_.erase(iter++);
     78       ++result;
     79     } else {
     80       ++iter;
     81     }
     82   }
     83   return result;
     84 }
     85 
     86 bool ProcessMap::Contains(const std::string& extension_id,
     87                           int process_id) const {
     88   for (ItemSet::const_iterator iter = items_.begin(); iter != items_.end();
     89        ++iter) {
     90     if (iter->process_id == process_id && iter->extension_id == extension_id)
     91       return true;
     92   }
     93   return false;
     94 }
     95 
     96 bool ProcessMap::Contains(int process_id) const {
     97   for (ItemSet::const_iterator iter = items_.begin(); iter != items_.end();
     98        ++iter) {
     99     if (iter->process_id == process_id)
    100       return true;
    101   }
    102   return false;
    103 }
    104 
    105 std::set<std::string> ProcessMap::GetExtensionsInProcess(int process_id) const {
    106   std::set<std::string> result;
    107   for (ItemSet::const_iterator iter = items_.begin(); iter != items_.end();
    108        ++iter) {
    109     if (iter->process_id == process_id)
    110       result.insert(iter->extension_id);
    111   }
    112   return result;
    113 }
    114 
    115 }  // extensions
    116