Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(661)

Unified Diff: visual_studio/NativeClientVSAddIn/NativeClientVSAddIn/Utility.cs

Issue 10758009: Native Client Visual Studio Add-in (Closed) Base URL: https://nativeclient-sdk.googlecode.com/svn/trunk/src
Patch Set: Style Fixed Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: visual_studio/NativeClientVSAddIn/NativeClientVSAddIn/Utility.cs
diff --git a/visual_studio/NativeClientVSAddIn/NativeClientVSAddIn/Utility.cs b/visual_studio/NativeClientVSAddIn/NativeClientVSAddIn/Utility.cs
new file mode 100644
index 0000000000000000000000000000000000000000..0a575a8a06b78a9a1cd64a173aa9e059da1774aa
--- /dev/null
+++ b/visual_studio/NativeClientVSAddIn/NativeClientVSAddIn/Utility.cs
@@ -0,0 +1,127 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+using EnvDTE;
+using EnvDTE80;
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+
+using Microsoft.VisualStudio.VCProject;
elijahtaylor1 2012/07/11 20:56:18 ABC order
tysand 2012/07/12 23:56:15 Done.
+using Microsoft.VisualStudio.VCProjectEngine;
+
+namespace NativeClientVSAddIn
+{
+ /// <summary>
+ /// Contains helper functions for this add-in
+ /// </summary>
+ public static class Utility
+ {
+ /// <summary>
+ /// Tells us if the given project is a Visual C/C++ project
+ /// </summary>
+ /// <param name="proj">Project to check</param>
+ /// <returns>True if project is a Visual C/C++ project</returns>
+ public static bool IsVisualCProject(Project proj)
+ {
+ String projectType = proj.Properties.Item("Kind").Value as String;
+ return projectType == "VCProject";
Petr Hosek 2012/07/12 00:25:46 Possibly make this into a constant?
tysand 2012/07/12 23:56:15 I think this is essentially already handled by hav
+ }
+
+ /// <summary>
+ /// Given a generic project, checks that it is a Visual C project, and
+ /// extracts the active VCConfiguration object
+ /// </summary>
+ /// <param name="proj">Generic project object</param>
+ /// <returns>The active configuration, or null if failure</returns>
+ public static VCConfiguration GetActiveVCConfiguration(Project proj)
+ {
+ if (!IsVisualCProject(proj))
+ {
+ return null;
+ }
+
+ VCProject vcproj = (VCProject)proj.Object;
+ IVCCollection configs = vcproj.Configurations;
+ Configuration active = proj.ConfigurationManager.ActiveConfiguration;
+
+ foreach (VCConfiguration config in configs)
+ {
+ if (config.ConfigurationName == active.ConfigurationName &&
+ config.Platform.Name == active.PlatformName)
+ {
+ return config;
+ }
+ }
+ return null;
+ }
+
+ /// <summary>
+ /// Checks if the first argument is a descendant of the second, where
+ /// both arguments are process IDs of two processes.
+ /// </summary>
+ /// <param name="processSearcher">Process searcher object</param>
+ /// <param name="descendant">Process ID of the descendant</param>
+ /// <param name="ancestor">Process ID of ancestor</param>
+ /// <returns>True if descendant is a descendant of ancestor</returns>
+ public static bool IsDescendantOfProcess(IProcessSearcher processSearcher,
+ uint descendant, uint ancestor)
+ {
+ return IsDescendantOfProcessHelper(processSearcher, descendant,
+ ancestor, DateTime.UtcNow);
+ }
+
+ /// <summary>
+ /// Helper function for IsDescendantOfProcessHelper
+ /// This function prevents an edge case where a process has a parent process ID
+ /// that refers to a descendant of itself. This can occur when the parent of a process
+ /// is destroyed and the parent's pid is recycled and reused on a descendant. The
+ /// parent process ID value is never updated when the parent is destroyed. The solution
+ /// is to ensure that parents are created before children.
+ /// </summary>
noelallen1 2012/07/11 22:16:46 Lost me on the last bit. Aren't parent's always c
tysand 2012/07/12 23:56:15 Right, the word 'ensure' is key. I will rephrase f
+ /// <param name="processSearcher">Process searcher object</param>
+ /// <param name="descendant">Process ID of the descendant</param>
+ /// <param name="anscestor">Process ID of the ancestor</param>
+ /// <param name="previousCreationTime">Creation time of the previous call's descendant</param>
+ /// <returns>True if descendant is a descendant of ancestor</returns>
+ private static bool IsDescendantOfProcessHelper(IProcessSearcher processSearcher,
+ uint descendant, uint anscestor, DateTime previousCreationTime)
+ {
+ List<ProcessInfo> results = processSearcher.GetResultsByID(descendant);
+ foreach (ProcessInfo proc in results)
+ {
+ if (proc.CreationDate <= previousCreationTime) // Ensure this call is valid
+ {
+ if (descendant == anscestor)
+ {
+ return true;
+ }
+ else if (descendant == proc.ParentID) // Is its own parent
+ {
+ return false;
+ }
+
+ return IsDescendantOfProcessHelper(processSearcher, proc.ParentID,
+ anscestor, proc.CreationDate);
+ }
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Extends the String class to allow checking if a string contains another string
+ /// with case-insensitivity
+ /// </summary>
+ /// <param name="source">Base string</param>
+ /// <param name="toCheck">String to check if contained within base string</param>
+ /// <param name="comparison">Comparison type</param>
+ /// <returns>True if toCheck is contained in source</returns>
+ public static bool Contains(this String source, String toCheck, StringComparison comparison)
+ {
+ return source.IndexOf(toCheck, comparison) != -1;
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698