Merge tools/grit from https://chromium.googlesource.com/external/grit-i18n.git at 6a46b04d35eade32b1270392317dca516c9dd56d

This commit was generated by merge_from_chromium.py.

Change-Id: I08c9767ca5fb215061b32b9b9a62d5fc5ef85126
diff --git a/grit/format/resource_map.py b/grit/format/resource_map.py
index b5a7b45..37ac54a 100644
--- a/grit/format/resource_map.py
+++ b/grit/format/resource_map.py
@@ -109,18 +109,18 @@
   tids = rc_header.GetIds(root)
   seen = set()
   active_descendants = [item for item in root.ActiveDescendants()]
+  output_all_resource_defines = root.ShouldOutputAllResourceDefines()
   for item in root:
-    if isinstance(item, (include.IncludeNode,
-                         structure.StructureNode,
-                         message.MessageNode)):
-      key = get_key(item)
-      tid = item.attrs['name']
-      if tid in tids and key not in seen:
-        seen.add(key)
-        # For messages, only include the active ones
-        if not isinstance(item, message.MessageNode) \
-            or item in active_descendants:
-          yield '  {"%s", %s},\n' % (key, tid)
+    if not item.IsResourceMapSource():
+      continue
+    key = get_key(item)
+    tid = item.attrs['name']
+    if tid not in tids or key in seen:
+      continue
+    seen.add(key)
+    if item.GeneratesResourceMapEntry(output_all_resource_defines,
+                                      item in active_descendants):
+      yield '  {"%s", %s},\n' % (key, tid)
   yield _FormatSourceFooter(root)
 
 
diff --git a/grit/format/resource_map_unittest.py b/grit/format/resource_map_unittest.py
index 8f162b5..cc6a79b 100644
--- a/grit/format/resource_map_unittest.py
+++ b/grit/format/resource_map_unittest.py
@@ -94,6 +94,65 @@
 };
 const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output)
 
+  def testFormatResourceMapOutputAllEqualsFalse(self):
+    grd = grd_reader.Parse(StringIO.StringIO(
+      '''<?xml version="1.0" encoding="UTF-8"?>
+      <grit latest_public_release="2" source_lang_id="en" current_release="3"
+            base_dir="." output_all_resource_defines="false">
+        <outputs>
+          <output type="rc_header" filename="the_rc_header.h" />
+          <output type="resource_map_header"
+                  filename="the_resource_map_header.h" />
+          <output type="resource_map_source"
+                  filename="the_resource_map_header.cc" />
+        </outputs>
+        <release seq="3">
+          <structures first_id="300">
+            <structure type="chrome_scaled_image" name="IDR_KLONKMENU"
+                       file="foo.png" />
+            <if expr="False">
+              <structure type="chrome_scaled_image" name="IDR_MISSING"
+                         file="bar.png" />
+            </if>
+         </structures>
+        </release>
+      </grit>'''), util.PathFromRoot('.'))
+    grd.SetOutputLanguage('en')
+    grd.RunGatherers()
+    output = util.StripBlankLinesAndComments(''.join(
+        resource_map.GetFormatter('resource_map_header')(grd, 'en', '.')))
+    self.assertEqual('''\
+#include <stddef.h>
+#ifndef GRIT_RESOURCE_MAP_STRUCT_
+#define GRIT_RESOURCE_MAP_STRUCT_
+struct GritResourceMap {
+  const char* const name;
+  int value;
+};
+#endif // GRIT_RESOURCE_MAP_STRUCT_
+extern const GritResourceMap kTheRcHeader[];
+extern const size_t kTheRcHeaderSize;''', output)
+    output = util.StripBlankLinesAndComments(''.join(
+        resource_map.GetFormatter('resource_map_source')(grd, 'en', '.')))
+    self.assertEqual('''\
+#include "the_resource_map_header.h"
+#include "base/basictypes.h"
+#include "the_rc_header.h"
+const GritResourceMap kTheRcHeader[] = {
+  {"IDR_KLONKMENU", IDR_KLONKMENU},
+};
+const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output)
+    output = util.StripBlankLinesAndComments(''.join(
+        resource_map.GetFormatter('resource_map_source')(grd, 'en', '.')))
+    self.assertEqual('''\
+#include "the_resource_map_header.h"
+#include "base/basictypes.h"
+#include "the_rc_header.h"
+const GritResourceMap kTheRcHeader[] = {
+  {"IDR_KLONKMENU", IDR_KLONKMENU},
+};
+const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output)
+
   def testFormatStringResourceMap(self):
     grd = grd_reader.Parse(StringIO.StringIO(
       '''<?xml version="1.0" encoding="UTF-8"?>
diff --git a/grit/node/base.py b/grit/node/base.py
index 3de51b6..a40794b 100644
--- a/grit/node/base.py
+++ b/grit/node/base.py
@@ -590,6 +590,21 @@
     '''Whether we need to expand variables on a given node.'''
     return False
 
+  def IsResourceMapSource(self):
+    '''Whether this node is a resource map source.'''
+    return False
+
+  def GeneratesResourceMapEntry(self, output_all_resource_defines,
+                                is_active_descendant):
+    '''Whether this node should output a resource map entry.
+
+    Args:
+      output_all_resource_defines: The value of output_all_resource_defines for
+                                   the root node.
+      is_active_descendant: Whether the current node is an active descendant
+                            from the root node.'''
+    return False
+
 
 class ContentNode(Node):
   '''Convenience baseclass for nodes that can have content.'''
diff --git a/grit/node/include.py b/grit/node/include.py
index 9c3685f..0f114c3 100644
--- a/grit/node/include.py
+++ b/grit/node/include.py
@@ -113,6 +113,14 @@
          self.ToRealPath(self.GetInputPath()),
          allow_external_script=allow_external_script)
 
+  def IsResourceMapSource(self):
+    return True
+
+  def GeneratesResourceMapEntry(self, output_all_resource_defines,
+                                is_active_descendant):
+    # includes always generate resource entries.
+    return True
+
   @staticmethod
   def Construct(parent, name, type, file, translateable=True,
                 filenameonly=False, mkoutput=False, relativepath=False):
diff --git a/grit/node/message.py b/grit/node/message.py
index ca80f41..48cd1c7 100644
--- a/grit/node/message.py
+++ b/grit/node/message.py
@@ -224,6 +224,13 @@
     message = self.ws_at_start + self.Translate(lang) + self.ws_at_end
     return id, util.Encode(message, encoding)
 
+  def IsResourceMapSource(self):
+    return True
+
+  def GeneratesResourceMapEntry(self, output_all_resource_defines,
+                                is_active_descendant):
+    return is_active_descendant
+
   @staticmethod
   def Construct(parent, message, name, desc='', meaning='', translateable=True):
     '''Constructs a new message node that is a child of 'parent', with the
diff --git a/grit/node/structure.py b/grit/node/structure.py
index 48968f6..7ccd2fb 100644
--- a/grit/node/structure.py
+++ b/grit/node/structure.py
@@ -332,6 +332,15 @@
 
     return filename
 
+  def IsResourceMapSource(self):
+    return True
+
+  def GeneratesResourceMapEntry(self, output_all_resource_defines,
+                                is_active_descendant):
+    if output_all_resource_defines:
+      return True
+    return is_active_descendant
+
   @staticmethod
   def Construct(parent, name, type, file, encoding='cp1252'):
     '''Creates a new node which is a child of 'parent', with attributes set